KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_test_provider_track_segment_length.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <thread_pool.h>
21#include <pcb_track.h>
22#include <drc/drc_engine.h>
23#include <drc/drc_item.h>
24#include <drc/drc_rule.h>
26
27
28/*
29 Track segment length test. As the name says, checks segment length of the tracks (including segments and arcs)
30 Errors generated:
31 - DRCE_TRACK_SEGMENT_LENGTH
32*/
33
35{
36public:
39
41
42 virtual bool Run() override;
43
44 virtual const wxString GetName() const override { return wxT( "segment_length" ); };
45};
46
47
49{
50 if( m_drcEngine->IsErrorLimitExceeded( DRCE_TRACK_SEGMENT_LENGTH ) )
51 {
52 REPORT_AUX( wxT( "Track segment length violations ignored. Tests not run." ) );
53 return true; // continue with other tests
54 }
55
56 if( !m_drcEngine->HasRulesForConstraintType( TRACK_SEGMENT_LENGTH_CONSTRAINT ) )
57 {
58 REPORT_AUX( wxT( "No track segment length constraints found. Tests not run." ) );
59 return true; // continue with other tests
60 }
61
62 if( !reportPhase( _( "Checking track segment lengths..." ) ) )
63 return false; // DRC cancelled
64
65 auto checkTrackSegmentLength =
66 [&]( const int idx ) -> bool
67 {
68 BOARD_ITEM* item = m_drcEngine->GetBoard()->Tracks()[idx];
69 if( m_drcEngine->IsErrorLimitExceeded( DRCE_TRACK_SEGMENT_LENGTH ) )
70 return false;
71
72 int actual;
73 VECTOR2I p0;
74
75 if( item->Type() == PCB_ARC_T )
76 {
77 PCB_ARC* arc = static_cast<PCB_ARC*>( item );
78
79 actual = arc->GetLength();
80 p0 = arc->GetStart();
81 }
82 else if( item->Type() == PCB_TRACE_T )
83 {
84 PCB_TRACK* track = static_cast<PCB_TRACK*>( item );
85
86 actual = track->GetLength();
87 p0 = ( track->GetStart() + track->GetEnd() ) / 2;
88 }
89 else
90 {
91 return true;
92 }
93
94 auto constraint = m_drcEngine->EvalRules( TRACK_SEGMENT_LENGTH_CONSTRAINT, item, nullptr,
95 item->GetLayer() );
96 bool fail_min = false;
97 bool fail_max = false;
98 int constraintLength = 0;
99
100 if( constraint.GetSeverity() != RPT_SEVERITY_IGNORE )
101 {
102 if( constraint.Value().HasMin() && actual < constraint.Value().Min() )
103 {
104 fail_min = true;
105 constraintLength = constraint.Value().Min();
106 }
107
108 if( constraint.Value().HasMax() && actual > constraint.Value().Max() )
109 {
110 fail_max = true;
111 constraintLength = constraint.Value().Max();
112 }
113 }
114
115 if( fail_min || fail_max )
116 {
117 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_TRACK_SEGMENT_LENGTH );
118 wxString constraintName = constraint.GetName();
119
120 if( fail_min )
121 {
122 if( constraint.m_ImplicitMin )
123 constraintName = _( "board setup constraints" );
124
125 drcItem->SetErrorDetail( formatMsg( _( "(%s min length %s; actual %s)" ),
126 constraintName,
127 constraintLength,
128 actual ) );
129 }
130 else
131 {
132 drcItem->SetErrorDetail( formatMsg( _( "(%s max length %s; actual %s)" ),
133 constraintName,
134 constraintLength,
135 actual ) );
136 }
137
138 drcItem->SetItems( item );
139 drcItem->SetViolatingRule( constraint.GetParentRule() );
140
141 reportViolation( drcItem, p0, item->GetLayer() );
142 }
143
144 return true;
145 };
146
147 const int progressDelta = 250;
148 int ii = 0;
149
151 auto futures = tp.submit_loop( 0, m_drcEngine->GetBoard()->Tracks().size(), checkTrackSegmentLength );
152
153 for( auto& ret : futures )
154 {
155 std::future_status status = ret.wait_for( std::chrono::milliseconds( 250 ) );
156
157 while( status != std::future_status::ready )
158 {
159 reportProgress( ii++, m_drcEngine->GetBoard()->Tracks().size(), progressDelta );
160 status = ret.wait_for( std::chrono::milliseconds( 250 ) );
161 }
162 }
163
164 return !m_drcEngine->IsCancelled();
165}
166
167
168namespace detail
169{
171}
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:81
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition board_item.h:265
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Definition drc_item.cpp:417
virtual ~DRC_TEST_PROVIDER_TRACK_SEGMENT_LENGTH()=default
virtual bool Run() override
Run this provider against the given PCB with configured options (if any).
virtual bool reportPhase(const wxString &aStageName)
void reportViolation(std::shared_ptr< DRC_ITEM > &item, const VECTOR2I &aMarkerPos, int aMarkerLayer, const std::function< void(PCB_MARKER *)> &aPathGenerator=[](PCB_MARKER *){})
wxString formatMsg(const wxString &aFormatString, const wxString &aSource, double aConstraint, double aActual, EDA_DATA_TYPE aDataType=EDA_DATA_TYPE::DISTANCE)
virtual bool reportProgress(size_t aCount, size_t aSize, size_t aDelta=1)
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:108
virtual double GetLength() const override
Return the length of the arc track.
Definition pcb_track.h:320
virtual double GetLength() const
Get the length of the track using the hypotenuse calculation.
const VECTOR2I & GetStart() const
Definition pcb_track.h:93
const VECTOR2I & GetEnd() const
Definition pcb_track.h:90
@ DRCE_TRACK_SEGMENT_LENGTH
Definition drc_item.h:54
@ TRACK_SEGMENT_LENGTH_CONSTRAINT
Definition drc_rule.h:62
#define REPORT_AUX(s)
#define _(s)
static DRC_REGISTER_TEST_PROVIDER< DRC_TEST_PROVIDER_ANNULAR_WIDTH > dummy
@ RPT_SEVERITY_IGNORE
int actual
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
static thread_pool * tp
BS::priority_thread_pool thread_pool
Definition thread_pool.h:27
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition typeinfo.h:91
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:89
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683