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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <thread_pool.h>
25#include <pcb_track.h>
26#include <drc/drc_engine.h>
27#include <drc/drc_item.h>
28#include <drc/drc_rule.h>
30
31
32/*
33 Track segment length test. As the name says, checks segment length of the tracks (including segments and arcs)
34 Errors generated:
35 - DRCE_TRACK_SEGMENT_LENGTH
36*/
37
39{
40public:
42 {}
43
45
46 virtual bool Run() override;
47
48 virtual const wxString GetName() const override { return wxT( "segment_length" ); };
49};
50
51
53{
55 {
56 REPORT_AUX( wxT( "Track segment length violations ignored. Tests not run." ) );
57 return true; // continue with other tests
58 }
59
61 {
62 REPORT_AUX( wxT( "No track segment length constraints found. Tests not run." ) );
63 return true; // continue with other tests
64 }
65
66 if( !reportPhase( _( "Checking track segment lengths..." ) ) )
67 return false; // DRC cancelled
68
69 auto checkTrackSegmentLength =
70 [&]( BOARD_ITEM* item ) -> bool
71 {
73 return false;
74
75 int actual;
76 VECTOR2I p0;
77
78 if( item->Type() == PCB_ARC_T )
79 {
80 PCB_ARC* arc = static_cast<PCB_ARC*>( item );
81
82 actual = arc->GetLength();
83 p0 = arc->GetStart();
84 }
85 else if( item->Type() == PCB_TRACE_T )
86 {
87 PCB_TRACK* track = static_cast<PCB_TRACK*>( item );
88
89 actual = track->GetLength();
90 p0 = ( track->GetStart() + track->GetEnd() ) / 2;
91 }
92 else
93 {
94 return true;
95 }
96
97 auto constraint = m_drcEngine->EvalRules( TRACK_SEGMENT_LENGTH_CONSTRAINT, item, nullptr,
98 item->GetLayer() );
99 bool fail_min = false;
100 bool fail_max = false;
101 int constraintLength = 0;
102
103 if( constraint.GetSeverity() != RPT_SEVERITY_IGNORE )
104 {
105 if( constraint.Value().HasMin() && actual < constraint.Value().Min() )
106 {
107 fail_min = true;
108 constraintLength = constraint.Value().Min();
109 }
110
111 if( constraint.Value().HasMax() && actual > constraint.Value().Max() )
112 {
113 fail_max = true;
114 constraintLength = constraint.Value().Max();
115 }
116 }
117
118 if( fail_min || fail_max )
119 {
120 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_TRACK_SEGMENT_LENGTH );
121 wxString constraintName = constraint.GetName();
122 wxString msg;
123
124 if( fail_min )
125 {
126 if( constraint.m_ImplicitMin )
127 constraintName = _( "board setup constraints" );
128
129 msg = formatMsg( _( "(%s min length %s; actual %s)" ),
130 constraintName,
131 constraintLength,
132 actual );
133 }
134 else
135 {
136 msg = formatMsg( _( "(%s max length %s; actual %s)" ),
137 constraintName,
138 constraintLength,
139 actual );
140 }
141
142 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
143 drcItem->SetItems( item );
144 drcItem->SetViolatingRule( constraint.GetParentRule() );
145
146 reportViolation( drcItem, p0, item->GetLayer() );
147 }
148
149 return true;
150 };
151
152 const int progressDelta = 250;
153 int ii = 0;
154
156 std::vector<std::future<bool>> returns;
157
158 returns.reserve( m_drcEngine->GetBoard()->Tracks().size() );
159
160 for( PCB_TRACK* item : m_drcEngine->GetBoard()->Tracks() )
161 {
162 returns.emplace_back( tp.submit( checkTrackSegmentLength, item ) );
163 }
164
165 for( std::future<bool>& ret : returns )
166 {
167 std::future_status status = ret.wait_for( std::chrono::milliseconds( 250 ) );
168
169 while( status != std::future_status::ready )
170 {
171 reportProgress( ii++, m_drcEngine->GetBoard()->Tracks().size(), progressDelta );
172 status = ret.wait_for( std::chrono::milliseconds( 250 ) );
173 }
174 }
175
176 return !m_drcEngine->IsCancelled();
177}
178
179
180namespace detail
181{
183}
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:79
const TRACKS & Tracks() const
Definition: board.h:356
MINOPTMAX< int > & Value()
Definition: drc_rule.h:161
BOARD * GetBoard() const
Definition: drc_engine.h:100
bool HasRulesForConstraintType(DRC_CONSTRAINT_T constraintID)
bool IsErrorLimitExceeded(int error_code)
DRC_CONSTRAINT EvalRules(DRC_CONSTRAINT_T aConstraintType, const BOARD_ITEM *a, const BOARD_ITEM *b, PCB_LAYER_ID aLayer, REPORTER *aReporter=nullptr)
Definition: drc_engine.cpp:706
bool IsCancelled() const
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Definition: drc_item.cpp:393
virtual ~DRC_TEST_PROVIDER_TRACK_SEGMENT_LENGTH()=default
virtual bool Run() override
Run this provider against the given PCB with configured options (if any).
Represent a DRC "provider" which runs some DRC functions over a BOARD and spits out DRC_ITEM and posi...
virtual bool reportPhase(const wxString &aStageName)
virtual void reportViolation(std::shared_ptr< DRC_ITEM > &item, const VECTOR2I &aMarkerPos, int aMarkerLayer, DRC_CUSTOM_MARKER_HANDLER *aCustomHandler=nullptr)
DRC_ENGINE * m_drcEngine
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)
T Min() const
Definition: minoptmax.h:33
virtual double GetLength() const override
Return the length of the arc track.
Definition: pcb_track.h:379
virtual double GetLength() const
Get the length of the track using the hypotenuse calculation.
Definition: pcb_track.cpp:764
const VECTOR2I & GetStart() const
Definition: pcb_track.h:152
const VECTOR2I & GetEnd() const
Definition: pcb_track.h:149
@ DRCE_TRACK_SEGMENT_LENGTH
Definition: drc_item.h:57
@ TRACK_SEGMENT_LENGTH_CONSTRAINT
Definition: drc_rule.h:60
#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.
Definition: thread_pool.cpp:30
static thread_pool * tp
Definition: thread_pool.cpp:28
BS::thread_pool thread_pool
Definition: thread_pool.h:31
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition: typeinfo.h:98
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:96