KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_engine.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 (C) 2004-2019 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 2014 Dick Hollenbeck, [email protected]
6 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#include <atomic>
23#include <set>
24#include <wx/log.h>
25#include <reporter.h>
26#include <common.h>
27#include <progress_reporter.h>
28#include <string_utils.h>
33#include <drc/drc_engine.h>
34#include <drc/drc_rtree.h>
35#include <drc/drc_rule_parser.h>
36#include <drc/drc_rule.h>
40#include <drc/drc_item.h>
42#include <board.h>
43#include <pcb_marker.h>
44#include <footprint.h>
45#include <pad.h>
46#include <pcb_track.h>
47#include <pcb_shape.h>
48#include <core/profile.h>
49#include <thread_pool.h>
50#include <zone.h>
55
56
57// wxListBox's performance degrades horrifically with very large datasets. It's not clear
58// they're useful to the user anyway.
59#define ERROR_LIMIT 199
60#define EXTENDED_ERROR_LIMIT 499
61
62
70static const wxChar* traceDrcProfile = wxT( "KICAD_DRC_PROFILE" );
71
72
73void drcPrintDebugMessage( int level, const wxString& msg, const char *function, int line )
74{
75 wxString valueStr;
76
77 if( wxGetEnv( wxT( "DRC_DEBUG" ), &valueStr ) )
78 {
79 int setLevel = wxAtoi( valueStr );
80
81 if( level <= setLevel )
82 printf( "%-30s:%d | %s\n", function, line, (const char *) msg.c_str() );
83 }
84}
85
86
89 m_designSettings ( aSettings ),
90 m_board( aBoard ),
91 m_drawingSheet( nullptr ),
92 m_schematicNetlist( nullptr ),
93 m_rulesValid( false ),
95 m_testFootprints( false ),
96 m_logReporter( nullptr ),
97 m_progressReporter( nullptr )
98{
99 m_errorLimits.resize( DRCE_LAST + 1 );
100
101 for( int ii = DRCE_FIRST; ii <= DRCE_LAST; ++ii )
103}
104
105
107{
108 m_rules.clear();
109
110 for( std::pair<DRC_CONSTRAINT_T, std::vector<DRC_ENGINE_CONSTRAINT*>*> pair : m_constraintMap )
111 {
112 for( DRC_ENGINE_CONSTRAINT* constraint : *pair.second )
113 delete constraint;
114
115 delete pair.second;
116 }
117}
118
119
120static bool isKeepoutZone( const BOARD_ITEM* aItem, bool aCheckFlags )
121{
122 if( !aItem || aItem->Type() != PCB_ZONE_T )
123 return false;
124
125 const ZONE* zone = static_cast<const ZONE*>( aItem );
126
127 if( !zone->GetIsRuleArea() )
128 return false;
129
130 if( !zone->HasKeepoutParametersSet() )
131 return false;
132
133 if( aCheckFlags )
134 {
135 if( !zone->GetDoNotAllowTracks()
136 && !zone->GetDoNotAllowVias()
137 && !zone->GetDoNotAllowPads()
138 && !zone->GetDoNotAllowZoneFills()
139 && !zone->GetDoNotAllowFootprints() )
140 {
141 return false;
142 }
143 }
144
145 return true;
146}
147
148
149std::shared_ptr<DRC_RULE> DRC_ENGINE::createImplicitRule( const wxString& name,
150 const DRC_IMPLICIT_SOURCE aImplicitSource )
151{
152 std::shared_ptr<DRC_RULE> rule = std::make_shared<DRC_RULE>();
153
154 rule->m_Name = name;
155 rule->SetImplicitSource( aImplicitSource );
156
157 addRule( rule );
158
159 return rule;
160}
161
162
164{
165 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
166 wxString expr, expr2, ncName;
167
168 // 1) global defaults
169
170 std::shared_ptr<DRC_RULE> rule = createImplicitRule( _( "board setup constraints" ),
172
173 DRC_CONSTRAINT widthConstraint( TRACK_WIDTH_CONSTRAINT );
174 widthConstraint.Value().SetMin( bds.m_TrackMinWidth );
175 rule->AddConstraint( widthConstraint );
176
177 DRC_CONSTRAINT connectionConstraint( CONNECTION_WIDTH_CONSTRAINT );
178 connectionConstraint.Value().SetMin( bds.m_MinConn );
179 rule->AddConstraint( connectionConstraint );
180
181 DRC_CONSTRAINT drillConstraint( HOLE_SIZE_CONSTRAINT );
182 drillConstraint.Value().SetMin( bds.m_MinThroughDrill );
183 rule->AddConstraint( drillConstraint );
184
185 DRC_CONSTRAINT annulusConstraint( ANNULAR_WIDTH_CONSTRAINT );
186 annulusConstraint.Value().SetMin( bds.m_ViasMinAnnularWidth );
187 rule->AddConstraint( annulusConstraint );
188
189 DRC_CONSTRAINT diameterConstraint( VIA_DIAMETER_CONSTRAINT );
190 diameterConstraint.Value().SetMin( bds.m_ViasMinSize );
191 rule->AddConstraint( diameterConstraint );
192
193 DRC_CONSTRAINT holeToHoleConstraint( HOLE_TO_HOLE_CONSTRAINT );
194 holeToHoleConstraint.Value().SetMin( bds.m_HoleToHoleMin );
195 rule->AddConstraint( holeToHoleConstraint );
196
197 rule = createImplicitRule( _( "board setup constraints zone fill strategy" ),
199 DRC_CONSTRAINT thermalSpokeCountConstraint( MIN_RESOLVED_SPOKES_CONSTRAINT );
200 thermalSpokeCountConstraint.Value().SetMin( bds.m_MinResolvedSpokes );
201 rule->AddConstraint( thermalSpokeCountConstraint );
202
203 rule = createImplicitRule( _( "board setup constraints silk" ), DRC_IMPLICIT_SOURCE::BOARD_SETUP_CONSTRAINT );
204 rule->m_LayerCondition = LSET( { F_SilkS, B_SilkS } );
205 DRC_CONSTRAINT silkClearanceConstraint( SILK_CLEARANCE_CONSTRAINT );
206 silkClearanceConstraint.Value().SetMin( bds.m_SilkClearance );
207 rule->AddConstraint( silkClearanceConstraint );
208
209 rule = createImplicitRule( _( "board setup constraints silk text height" ),
211 rule->m_LayerCondition = LSET( { F_SilkS, B_SilkS } );
212 DRC_CONSTRAINT silkTextHeightConstraint( TEXT_HEIGHT_CONSTRAINT );
213 silkTextHeightConstraint.Value().SetMin( bds.m_MinSilkTextHeight );
214 rule->AddConstraint( silkTextHeightConstraint );
215
216 rule = createImplicitRule( _( "board setup constraints silk line width" ),
218 rule->m_LayerCondition = LSET( { F_SilkS, B_SilkS } );
219 DRC_CONSTRAINT lineWidthConstraint( LINE_WIDTH_CONSTRAINT );
220 lineWidthConstraint.Value().SetMin( bds.m_MinSilkLineWidth );
221 rule->AddConstraint( lineWidthConstraint );
222
223 rule = createImplicitRule( _( "board setup constraints hole" ), DRC_IMPLICIT_SOURCE::BOARD_SETUP_CONSTRAINT );
224 DRC_CONSTRAINT holeClearanceConstraint( HOLE_CLEARANCE_CONSTRAINT );
225 holeClearanceConstraint.Value().SetMin( bds.m_HoleClearance );
226 rule->AddConstraint( holeClearanceConstraint );
227
228 rule = createImplicitRule( _( "board setup constraints edge" ), DRC_IMPLICIT_SOURCE::BOARD_SETUP_CONSTRAINT );
229 DRC_CONSTRAINT edgeClearanceConstraint( EDGE_CLEARANCE_CONSTRAINT );
230 edgeClearanceConstraint.Value().SetMin( bds.m_CopperEdgeClearance );
231 rule->AddConstraint( edgeClearanceConstraint );
232
233 rule = createImplicitRule( _( "board setup constraints courtyard" ), DRC_IMPLICIT_SOURCE::BOARD_SETUP_CONSTRAINT );
234 DRC_CONSTRAINT courtyardClearanceConstraint( COURTYARD_CLEARANCE_CONSTRAINT );
235 holeToHoleConstraint.Value().SetMin( 0 );
236 rule->AddConstraint( courtyardClearanceConstraint );
237
238 // 2a) micro-via specific defaults (new DRC doesn't treat microvias in any special way)
239
240 std::shared_ptr<DRC_RULE> uViaRule = createImplicitRule( _( "board setup constraints micro-via" ),
242
243 uViaRule->m_Condition = new DRC_RULE_CONDITION( wxT( "A.Via_Type == 'Micro'" ) );
244
245 DRC_CONSTRAINT uViaDrillConstraint( HOLE_SIZE_CONSTRAINT );
246 uViaDrillConstraint.Value().SetMin( bds.m_MicroViasMinDrill );
247 uViaRule->AddConstraint( uViaDrillConstraint );
248
249 DRC_CONSTRAINT uViaDiameterConstraint( VIA_DIAMETER_CONSTRAINT );
250 uViaDiameterConstraint.Value().SetMin( bds.m_MicroViasMinSize );
251 uViaRule->AddConstraint( uViaDiameterConstraint );
252
253 // 2b) barcode-specific defaults
254
255 std::shared_ptr<DRC_RULE> barcodeRule = createImplicitRule( _( "barcode visual separation default" ),
257 DRC_CONSTRAINT barcodeSeparationConstraint( PHYSICAL_CLEARANCE_CONSTRAINT );
258 barcodeSeparationConstraint.Value().SetMin( GetIuScale().mmToIU( 1.0 ) );
259 barcodeRule->AddConstraint( barcodeSeparationConstraint );
260 barcodeRule->m_Condition = new DRC_RULE_CONDITION( wxT( "A.Type == 'Barcode'" ) );
261
262 // 3) per-netclass rules
263
264 std::vector<std::shared_ptr<DRC_RULE>> netclassClearanceRules;
265 std::vector<std::shared_ptr<DRC_RULE>> netclassItemSpecificRules;
266
267 auto makeNetclassRules =
268 [&]( const std::shared_ptr<NETCLASS>& nc, bool isDefault )
269 {
270 std::optional<wxString> literal = LIBEVAL::EscapeStringLiteral( nc->GetName() );
271
272 // DRC_TEST_PROVIDER_MISC reports the class, since a rule here cannot be compiled
273 if( !literal )
274 return;
275
276 ncName = *literal;
277
278 if( nc->HasClearance() )
279 {
280 std::shared_ptr<DRC_RULE> netclassRule = std::make_shared<DRC_RULE>();
281 netclassRule->m_Name = wxString::Format( _( "netclass '%s'" ),
282 nc->GetClearanceParent()->GetHumanReadableName() );
283 netclassRule->SetImplicitSource( DRC_IMPLICIT_SOURCE::NET_CLASS );
284
285 expr = wxString::Format( wxT( "A.hasExactNetclass('%s')" ), ncName );
286 netclassRule->m_Condition = new DRC_RULE_CONDITION( expr );
287 netclassRule->m_ImplicitNetclass = nc->GetName();
288 netclassClearanceRules.push_back( netclassRule );
289
291 constraint.Value().SetMin( nc->GetClearance() );
292 netclassRule->AddConstraint( constraint );
293
294 {
295 std::unique_lock<std::shared_mutex> writeLock( m_clearanceCacheMutex );
296 m_netclassClearances[nc->GetName()] = netclassRule.get();
297 }
298 }
299
300 if( nc->HasTrackWidth() )
301 {
302 std::shared_ptr<DRC_RULE> netclassRule = std::make_shared<DRC_RULE>();
303 netclassRule->m_Name = wxString::Format( _( "netclass '%s'" ),
304 nc->GetTrackWidthParent()->GetHumanReadableName() );
305 netclassRule->SetImplicitSource( DRC_IMPLICIT_SOURCE::NET_CLASS );
306
307 expr = wxString::Format( wxT( "A.hasExactNetclass('%s')" ), ncName );
308 netclassRule->m_Condition = new DRC_RULE_CONDITION( expr );
309 netclassRule->m_ImplicitNetclass = nc->GetName();
310 netclassClearanceRules.push_back( netclassRule );
311
313 constraint.Value().SetMin( bds.m_TrackMinWidth );
314 constraint.Value().SetOpt( nc->GetTrackWidth() );
315 netclassRule->AddConstraint( constraint );
316 }
317
318 if( nc->HasDiffPairWidth() )
319 {
320 std::shared_ptr<DRC_RULE> netclassRule = std::make_shared<DRC_RULE>();
321 netclassRule->m_Name = wxString::Format( _( "netclass '%s' diff pair" ),
322 nc->GetDiffPairWidthParent()->GetHumanReadableName() );
323 netclassRule->SetImplicitSource( DRC_IMPLICIT_SOURCE::NET_CLASS );
324
325 expr = wxString::Format( wxT( "A.hasExactNetclass('%s') && A.inDiffPair('*')" ), ncName );
326 netclassRule->m_Condition = new DRC_RULE_CONDITION( expr );
327 netclassRule->m_ImplicitNetclass = nc->GetName();
328 netclassItemSpecificRules.push_back( netclassRule );
329
331 constraint.Value().SetMin( bds.m_TrackMinWidth );
332 constraint.Value().SetOpt( nc->GetDiffPairWidth() );
333 netclassRule->AddConstraint( constraint );
334 }
335
336 if( nc->HasDiffPairGap() )
337 {
338 std::shared_ptr<DRC_RULE> netclassRule = std::make_shared<DRC_RULE>();
339 netclassRule->m_Name = wxString::Format( _( "netclass '%s'" ),
340 nc->GetDiffPairGapParent()->GetHumanReadableName() );
341 netclassRule->SetImplicitSource( DRC_IMPLICIT_SOURCE::NET_CLASS );
342
343 expr = wxString::Format( wxT( "A.hasExactNetclass('%s')" ), ncName );
344 netclassRule->m_Condition = new DRC_RULE_CONDITION( expr );
345 netclassRule->m_ImplicitNetclass = nc->GetName();
346 netclassItemSpecificRules.push_back( netclassRule );
347
349 constraint.Value().SetMin( bds.m_MinClearance );
350 constraint.Value().SetOpt( nc->GetDiffPairGap() );
351 netclassRule->AddConstraint( constraint );
352
353 // A narrower diffpair gap overrides the netclass min clearance
354 if( nc->HasClearance() && nc->GetDiffPairGap() < nc->GetClearance() )
355 {
356 netclassRule = std::make_shared<DRC_RULE>();
357 netclassRule->m_Name = wxString::Format( _( "netclass '%s' diff pair" ),
358 nc->GetDiffPairGapParent()->GetHumanReadableName() );
359 netclassRule->SetImplicitSource( DRC_IMPLICIT_SOURCE::NET_CLASS );
360
361 expr = wxString::Format( wxT( "A.hasExactNetclass('%s') && AB.isCoupledDiffPair()" ), ncName );
362 netclassRule->m_Condition = new DRC_RULE_CONDITION( expr );
363 netclassRule->m_ImplicitNetclass = nc->GetName();
364 netclassItemSpecificRules.push_back( netclassRule );
365
366 DRC_CONSTRAINT min_clearanceConstraint( CLEARANCE_CONSTRAINT );
367 min_clearanceConstraint.Value().SetMin( nc->GetDiffPairGap() );
368 netclassRule->AddConstraint( min_clearanceConstraint );
369
371 }
372 }
373
374 if( nc->HasViaDiameter() )
375 {
376 std::shared_ptr<DRC_RULE> netclassRule = std::make_shared<DRC_RULE>();
377 netclassRule->m_Name = wxString::Format( _( "netclass '%s'" ),
378 nc->GetViaDiameterParent()->GetHumanReadableName() );
379 netclassRule->SetImplicitSource( DRC_IMPLICIT_SOURCE::NET_CLASS );
380
381 expr = wxString::Format( wxT( "A.hasExactNetclass('%s') && A.Via_Type != 'Micro'" ), ncName );
382 netclassRule->m_Condition = new DRC_RULE_CONDITION( expr );
383 netclassRule->m_ImplicitNetclass = nc->GetName();
384 netclassItemSpecificRules.push_back( netclassRule );
385
387 constraint.Value().SetMin( bds.m_ViasMinSize );
388 constraint.Value().SetOpt( nc->GetViaDiameter() );
389 netclassRule->AddConstraint( constraint );
390 }
391
392 if( nc->HasViaDrill() )
393 {
394 std::shared_ptr<DRC_RULE> netclassRule = std::make_shared<DRC_RULE>();
395 netclassRule->m_Name = wxString::Format( _( "netclass '%s'" ),
396 nc->GetViaDrillParent()->GetHumanReadableName() );
397 netclassRule->SetImplicitSource( DRC_IMPLICIT_SOURCE::NET_CLASS );
398
399 expr = wxString::Format( wxT( "A.hasExactNetclass('%s') && A.Via_Type != 'Micro'" ), ncName );
400 netclassRule->m_Condition = new DRC_RULE_CONDITION( expr );
401 netclassRule->m_ImplicitNetclass = nc->GetName();
402 netclassItemSpecificRules.push_back( netclassRule );
403
405 constraint.Value().SetMin( bds.m_MinThroughDrill );
406 constraint.Value().SetOpt( nc->GetViaDrill() );
407 netclassRule->AddConstraint( constraint );
408 }
409
410 if( nc->HasuViaDiameter() )
411 {
412 std::shared_ptr<DRC_RULE> netclassRule = std::make_shared<DRC_RULE>();
413 netclassRule->m_Name = wxString::Format( _( "netclass '%s' uvia" ),
414 nc->GetuViaDiameterParent()->GetHumanReadableName() );
415 netclassRule->SetImplicitSource( DRC_IMPLICIT_SOURCE::NET_CLASS );
416
417 expr = wxString::Format( wxT( "A.hasExactNetclass('%s') && A.Via_Type == 'Micro'" ), ncName );
418 netclassRule->m_Condition = new DRC_RULE_CONDITION( expr );
419 netclassRule->m_ImplicitNetclass = nc->GetName();
420 netclassItemSpecificRules.push_back( netclassRule );
421
423 constraint.Value().SetMin( bds.m_MicroViasMinSize );
424 constraint.Value().SetOpt( nc->GetuViaDiameter() );
425 netclassRule->AddConstraint( constraint );
426 }
427
428 if( nc->HasuViaDrill() )
429 {
430 std::shared_ptr<DRC_RULE> netclassRule = std::make_shared<DRC_RULE>();
431 netclassRule->m_Name = wxString::Format( _( "netclass '%s' uvia" ),
432 nc->GetuViaDrillParent()->GetHumanReadableName() );
433 netclassRule->SetImplicitSource( DRC_IMPLICIT_SOURCE::NET_CLASS );
434
435 expr = wxString::Format( wxT( "A.hasExactNetclass('%s') && A.Via_Type == 'Micro'" ), ncName );
436 netclassRule->m_Condition = new DRC_RULE_CONDITION( expr );
437 netclassRule->m_ImplicitNetclass = nc->GetName();
438 netclassItemSpecificRules.push_back( netclassRule );
439
441 constraint.Value().SetMin( bds.m_MicroViasMinDrill );
442 constraint.Value().SetOpt( nc->GetuViaDrill() );
443 netclassRule->AddConstraint( constraint );
444 }
445 };
446
447 m_board->SynchronizeNetsAndNetClasses( false );
448 makeNetclassRules( bds.m_NetSettings->GetDefaultNetclass(), true );
449
450 for( const auto& [name, netclass] : bds.m_NetSettings->GetNetclasses() )
451 makeNetclassRules( netclass, false );
452
453 for( const auto& [name, netclass] : bds.m_NetSettings->GetCompositeNetclasses() )
454 makeNetclassRules( netclass, false );
455
456 // The netclass clearance rules have to be sorted by min clearance so the right one fires
457 // if 'A' and 'B' belong to two different netclasses.
458 //
459 // The item-specific netclass rules are all unary, so there's no 'A' vs 'B' issue.
460
461 std::sort( netclassClearanceRules.begin(), netclassClearanceRules.end(),
462 []( const std::shared_ptr<DRC_RULE>& lhs, const std::shared_ptr<DRC_RULE>& rhs )
463 {
464 return lhs->m_Constraints[0].m_Value.Min() < rhs->m_Constraints[0].m_Value.Min();
465 } );
466
467 for( std::shared_ptr<DRC_RULE>& ncRule : netclassClearanceRules )
468 addRule( ncRule );
469
470 for( std::shared_ptr<DRC_RULE>& ncRule : netclassItemSpecificRules )
471 addRule( ncRule );
472
473 // 4) tuning profile rules
474 auto addTuningSingleRule =
475 [&]( const DELAY_PROFILE_TRACK_PROPAGATION_ENTRY& aLayerEntry, const wxString& aProfileName,
476 const wxString& aNetclassName )
477 {
478 if( aLayerEntry.GetWidth() <= 0 )
479 return;
480
481 std::shared_ptr<DRC_RULE> tuningRule = std::make_shared<DRC_RULE>();
482 tuningRule->m_Severity = bds.m_DRCSeverities[DRCE_TUNING_PROFILE_IMPLICIT_RULES];
483 tuningRule->m_Name = wxString::Format( _( "tuning profile '%s'" ), aProfileName );
484 tuningRule->SetImplicitSource( DRC_IMPLICIT_SOURCE::TUNING_PROFILE );
485
486 expr = wxString::Format( wxT( "A.hasExactNetclass('%s') && A.Layer == '%s'" ),
487 aNetclassName,
488 LSET::Name( aLayerEntry.GetSignalLayer() ) );
489 tuningRule->m_Condition = new DRC_RULE_CONDITION( expr );
490
492 constraint.Value().SetMin( std::max( bds.m_TrackMinWidth, aLayerEntry.GetWidth() ) );
493 constraint.Value().SetOpt( aLayerEntry.GetWidth() );
494 constraint.Value().SetMax( aLayerEntry.GetWidth() );
495 tuningRule->AddConstraint( constraint );
496
497 addRule( tuningRule );
498 };
499
500 auto addTuningDifferentialRules =
501 [&]( const DELAY_PROFILE_TRACK_PROPAGATION_ENTRY& aLayerEntry, const wxString& aProfileName,
502 const NETCLASS* aNetclass, const wxString& aNetclassName )
503 {
504 if( aLayerEntry.GetWidth() <= 0 || aLayerEntry.GetDiffPairGap() <= 0 )
505 return;
506
507 std::shared_ptr<DRC_RULE> tuningRule = std::make_shared<DRC_RULE>();
508 tuningRule->m_Severity = bds.m_DRCSeverities[DRCE_TUNING_PROFILE_IMPLICIT_RULES];
509 tuningRule->m_Name = wxString::Format( _( "tuning profile '%s'" ), aProfileName );
510 tuningRule->SetImplicitSource( DRC_IMPLICIT_SOURCE::TUNING_PROFILE );
511
512 expr = wxString::Format( wxT( "A.hasExactNetclass('%s') && A.Layer == '%s' && A.inDiffPair('*')" ),
513 aNetclassName,
514 LSET::Name( aLayerEntry.GetSignalLayer() ) );
515 tuningRule->m_Condition = new DRC_RULE_CONDITION( expr );
516
518 constraint.Value().SetMin( std::max( bds.m_TrackMinWidth, aLayerEntry.GetWidth() ) );
519 constraint.Value().SetOpt( aLayerEntry.GetWidth() );
520 constraint.Value().SetMax( aLayerEntry.GetWidth() );
521 tuningRule->AddConstraint( constraint );
522
523 addRule( tuningRule );
524
525 std::shared_ptr<DRC_RULE> tuningRule2 = std::make_shared<DRC_RULE>();
526 tuningRule2->m_Severity = bds.m_DRCSeverities[DRCE_TUNING_PROFILE_IMPLICIT_RULES];
527 tuningRule2->m_Name = wxString::Format( _( "tuning profile '%s'" ), aProfileName );
528 tuningRule2->SetImplicitSource( DRC_IMPLICIT_SOURCE::TUNING_PROFILE );
529
530 expr2 = wxString::Format( wxT( "A.hasExactNetclass('%s') && A.Layer == '%s' && A.inDiffPair('*')" ),
531 aNetclassName,
532 LSET::Name( aLayerEntry.GetSignalLayer() ) );
533 tuningRule2->m_Condition = new DRC_RULE_CONDITION( expr2 );
534
536 constraint2.Value().SetMin( std::max( bds.m_MinClearance, aLayerEntry.GetDiffPairGap() ) );
537 constraint2.Value().SetOpt( aLayerEntry.GetDiffPairGap() );
538 constraint2.Value().SetMax( aLayerEntry.GetDiffPairGap() );
539 tuningRule2->AddConstraint( constraint2 );
540
541 addRule( tuningRule2 );
542
543 // A narrower diffpair gap overrides the netclass min clearance
544 if( aNetclass->HasClearance() && aLayerEntry.GetDiffPairGap() < aNetclass->GetClearance() )
545 {
546 std::shared_ptr<DRC_RULE> diffPairClearanceRule = std::make_shared<DRC_RULE>();
547 diffPairClearanceRule->m_Severity = bds.m_DRCSeverities[DRCE_TUNING_PROFILE_IMPLICIT_RULES];
548 diffPairClearanceRule->m_Name = wxString::Format( _( "tuning profile '%s'" ), aProfileName );
549 diffPairClearanceRule->SetImplicitSource( DRC_IMPLICIT_SOURCE::TUNING_PROFILE );
550
551 expr = wxString::Format(
552 wxT( "A.hasExactNetclass('%s') && A.Layer == '%s' && AB.isCoupledDiffPair()" ),
553 aNetclassName, LSET::Name( aLayerEntry.GetSignalLayer() ) );
554 diffPairClearanceRule->m_Condition = new DRC_RULE_CONDITION( expr );
555
556 DRC_CONSTRAINT min_clearanceConstraint( CLEARANCE_CONSTRAINT );
557 min_clearanceConstraint.Value().SetMin( aLayerEntry.GetDiffPairGap() );
558 diffPairClearanceRule->AddConstraint( min_clearanceConstraint );
559
560 addRule( diffPairClearanceRule );
561 }
562 };
563
564 if( PROJECT* project = m_board->GetProject() )
565 {
566 std::shared_ptr<TUNING_PROFILES> tuningParams = project->GetProjectFile().TuningProfileParameters();
567
568 auto addNetclassTuningProfileRules =
569 [&tuningParams, &addTuningSingleRule, &addTuningDifferentialRules]( NETCLASS* aNetclass )
570 {
571 std::optional<wxString> literal = LIBEVAL::EscapeStringLiteral( aNetclass->GetName() );
572
573 if( aNetclass->HasTuningProfile() && literal )
574 {
575 const wxString delayProfileName = aNetclass->GetTuningProfile();
576 const TUNING_PROFILE& profile = tuningParams->GetTuningProfile( delayProfileName );
577
579 {
580 if( entry.GetWidth() <= 0 )
581 continue;
582
584 addTuningSingleRule( entry, delayProfileName, *literal );
585 else
586 addTuningDifferentialRules( entry, delayProfileName, aNetclass, *literal );
587 }
588 }
589 };
590
591 addNetclassTuningProfileRules( bds.m_NetSettings->GetDefaultNetclass().get() );
592
593 for( const auto& [netclassName, netclass] : bds.m_NetSettings->GetNetclasses() )
594 addNetclassTuningProfileRules( netclass.get() );
595
596 for( const auto& [netclassName, netclass] : bds.m_NetSettings->GetCompositeNetclasses() )
597 addNetclassTuningProfileRules( netclass.get() );
598 }
599
600 // 5) keepout area rules
601 auto addKeepoutZoneRule =
602 [&]( ZONE* zone, FOOTPRINT* parentFP )
603 {
604 const wxString& name = zone->GetZoneName();
605
606 if( name.IsEmpty() )
607 {
608 if( parentFP )
609 {
610 rule = createImplicitRule(
611 wxString::Format( _( "keepout area of %s" ), DescribeRef( parentFP->GetReference() ) ),
613 }
614 else
615 {
616 rule = createImplicitRule( _( "keepout area" ), DRC_IMPLICIT_SOURCE::KEEPOUT );
617 }
618
619 }
620 else
621 {
622 if( parentFP )
623 {
624 rule = createImplicitRule( wxString::Format( _( "keepout area '%s' of %s" ), name,
625 DescribeRef( parentFP->GetReference() ) ),
627 }
628 else
629 {
630 rule = createImplicitRule( wxString::Format( _( "keepout area '%s'" ), name ),
632 }
633 }
634
635 rule->m_ImplicitItemId = zone->m_Uuid;
636 rule->m_ImplicitItem = zone;
637
638 rule->m_Condition = new DRC_RULE_CONDITION( wxString::Format( wxT( "A.intersectsKeepout('%s')" ),
639 zone->m_Uuid.AsString() ) );
640
641 rule->m_LayerCondition = zone->GetLayerSet();
642
643 int disallowFlags = 0;
644
645 if( zone->GetDoNotAllowTracks() )
646 disallowFlags |= DRC_DISALLOW_TRACKS;
647
648 if( zone->GetDoNotAllowVias() )
649 disallowFlags |= DRC_DISALLOW_VIAS;
650
651 if( zone->GetDoNotAllowPads() )
652 disallowFlags |= DRC_DISALLOW_PADS;
653
654 if( zone->GetDoNotAllowZoneFills() )
655 disallowFlags |= DRC_DISALLOW_ZONES;
656
657 if( zone->GetDoNotAllowFootprints() )
658 disallowFlags |= DRC_DISALLOW_FOOTPRINTS;
659
660 DRC_CONSTRAINT disallowConstraint( DISALLOW_CONSTRAINT );
661 disallowConstraint.m_DisallowFlags = disallowFlags;
662 rule->AddConstraint( disallowConstraint );
663 };
664
665 for( ZONE* zone : m_board->Zones() )
666 {
667 if( isKeepoutZone( zone, true ) )
668 addKeepoutZoneRule( zone, nullptr );
669 }
670
671 for( FOOTPRINT* footprint : m_board->Footprints() )
672 {
673 for( ZONE* zone : footprint->Zones() )
674 {
675 if( isKeepoutZone( zone, true ) )
676 addKeepoutZoneRule( zone, footprint );
677 }
678 }
679}
680
681
682void DRC_ENGINE::loadRules( const wxFileName& aPath )
683{
684 if( m_board && aPath.FileExists() )
685 {
686 std::vector<std::shared_ptr<DRC_RULE>> rules;
687
688 if( FILE* fp = wxFopen( aPath.GetFullPath(), wxT( "rt" ) ) )
689 {
690 FILE_LINE_READER lineReader( fp, aPath.GetFullPath() ); // Will close rules file
691 wxString rulesText;
692
693 std::function<bool( wxString* )> resolver =
694 [&]( wxString* token ) -> bool
695 {
696 if( IsComponentClassSelector( *token ) )
697 return false;
698
699 return m_board->ResolveTextVar( token, 0 );
700 };
701
702 while( char* line = lineReader.ReadLine() )
703 {
704 wxString str( line );
705 str = m_board->ConvertCrossReferencesToKIIDs( str );
706 str = ExpandTextVars( str, &resolver, INTERNAL );
707
708 rulesText << str << '\n';
709 }
710
711 DRC_RULES_PARSER parser( rulesText, aPath.GetFullPath() );
712 parser.Parse( rules, m_logReporter );
713 }
714
715 // Copy the rules into the member variable afterwards so that if Parse() throws then
716 // the possibly malformed rules won't contaminate the current ruleset.
717
718 for( std::shared_ptr<DRC_RULE>& rule : rules )
719 m_rules.push_back( rule );
720 }
721}
722
723
725{
726 if( m_logReporter )
727 m_logReporter->Report( wxT( "Compiling Rules" ) );
728
729 REPORTER error_semaphore;
730
731 for( std::shared_ptr<DRC_RULE>& rule : m_rules )
732 {
733 DRC_RULE_CONDITION* condition = nullptr;
734
735 if( rule->m_Condition && !rule->m_Condition->GetExpression().IsEmpty() )
736 {
737 condition = rule->m_Condition;
738 condition->Compile( &error_semaphore );
739 }
740
741 if( error_semaphore.HasMessageOfSeverity( RPT_SEVERITY_ERROR ) )
742 THROW_PARSE_ERROR( wxT( "Parse error" ), rule->m_Name, TO_UTF8( rule->m_Condition->GetExpression() ), 0, 0 );
743
744 for( const DRC_CONSTRAINT& constraint : rule->m_Constraints )
745 {
746 auto& ruleVec = m_constraintMap[ constraint.m_Type ];
747
748 if( !ruleVec )
749 ruleVec = new std::vector<DRC_ENGINE_CONSTRAINT*>();
750
751 DRC_ENGINE_CONSTRAINT* engineConstraint = new DRC_ENGINE_CONSTRAINT;
752
753 engineConstraint->layerTest = rule->m_LayerCondition;
754 engineConstraint->condition = condition;
755 engineConstraint->constraint = constraint;
756 engineConstraint->parentRule = rule;
757
758 if( rule->IsImplicit() && constraint.m_Type == DISALLOW_CONSTRAINT
759 && rule->m_ImplicitItem && rule->m_ImplicitItem->Type() == PCB_ZONE_T )
760 {
761 // Duplicate zone UUIDs make an item-by-id lookup return the wrong same-UUID
762 // zone and defeat self-exclusion, so use the rule's own zone pointer
763 engineConstraint->implicitKeepoutZone = static_cast<ZONE*>( rule->m_ImplicitItem );
764 }
765
766 ruleVec->push_back( engineConstraint );
767 }
768 }
769
772 m_explicitConstraints.clear();
773
774 for( auto& [constraintType, ruleList] : m_constraintMap )
775 {
776 for( DRC_ENGINE_CONSTRAINT* c : *ruleList )
777 {
778 if( c->parentRule && !c->parentRule->IsImplicit() )
779 {
780 m_explicitConstraints[constraintType].push_back( c );
781
782 if( constraintType == CLEARANCE_CONSTRAINT )
784
786 && c->condition
787 && c->condition->HasGeometryDependentFunctions() )
788 {
790 }
791 }
792 }
793 }
794}
795
796
797void DRC_ENGINE::InitEngine( const std::shared_ptr<DRC_RULE>& rule )
798{
800
801 for( DRC_TEST_PROVIDER* provider : m_testProviders )
802 {
803 if( m_logReporter )
804 m_logReporter->Report( wxString::Format( wxT( "Create DRC provider: '%s'" ), provider->GetName() ) );
805
806 provider->SetDRCEngine( this );
807 }
808
809 // Existing markers may hold raw pointers to DRC_RULEs we're about to destroy.
810 // Null them out so GetSeverity() falls back to the board design settings.
811 if( m_board )
812 {
813 for( PCB_MARKER* marker : m_board->Markers() )
814 {
815 if( DRC_ITEM* drcItem = static_cast<DRC_ITEM*>( marker->GetRCItem().get() ) )
816 drcItem->SetViolatingRule( nullptr );
817 }
818 }
819
820 m_rules.clear();
821 m_rulesValid = false;
822
823 for( std::pair<DRC_CONSTRAINT_T, std::vector<DRC_ENGINE_CONSTRAINT*>*> pair : m_constraintMap )
824 {
825 for( DRC_ENGINE_CONSTRAINT* constraint : *pair.second )
826 delete constraint;
827
828 delete pair.second;
829 }
830
831 m_constraintMap.clear();
832
833 // The netclass clearance cache points at the rules just destroyed
834 {
835 std::unique_lock<std::shared_mutex> writeLock( m_clearanceCacheMutex );
836 m_netclassClearances.clear();
837 }
838
839 m_board->IncrementTimeStamp(); // Clear board-level caches
840
841 try
842 {
843 m_rules.push_back( rule );
844 compileRules();
845 }
846 catch( PARSE_ERROR& original_parse_error )
847 {
848 throw original_parse_error;
849 }
850
851 for( int ii = DRCE_FIRST; ii <= DRCE_LAST; ++ii )
853
854 m_rulesValid = true;
855}
856
857
858void DRC_ENGINE::InitEngine( const wxFileName& aRulePath )
859{
861
862 for( DRC_TEST_PROVIDER* provider : m_testProviders )
863 {
864 if( m_logReporter )
865 m_logReporter->Report( wxString::Format( wxT( "Create DRC provider: '%s'" ), provider->GetName() ) );
866
867 provider->SetDRCEngine( this );
868 }
869
870 // Existing markers may hold raw pointers to DRC_RULEs we're about to destroy.
871 // Null them out so GetSeverity() falls back to the board design settings.
872 if( m_board )
873 {
874 for( PCB_MARKER* marker : m_board->Markers() )
875 {
876 if( DRC_ITEM* drcItem = static_cast<DRC_ITEM*>( marker->GetRCItem().get() ) )
877 drcItem->SetViolatingRule( nullptr );
878 }
879 }
880
881 m_rules.clear();
882 m_rulesValid = false;
883
884 for( std::pair<DRC_CONSTRAINT_T, std::vector<DRC_ENGINE_CONSTRAINT*>*> pair : m_constraintMap )
885 {
886 for( DRC_ENGINE_CONSTRAINT* constraint : *pair.second )
887 delete constraint;
888
889 delete pair.second;
890 }
891
892 m_constraintMap.clear();
893
894 {
895 std::unique_lock<std::shared_mutex> writeLock( m_clearanceCacheMutex );
896 m_ownClearanceCache.clear();
897 m_netclassClearances.clear();
898 }
899
902 m_explicitConstraints.clear();
903
904 m_board->IncrementTimeStamp(); // Clear board-level caches
905
906 try // attempt to load full set of rules (implicit + user rules)
907 {
909 loadRules( aRulePath );
910 compileRules();
911 }
912 catch( PARSE_ERROR& original_parse_error )
913 {
914 m_rules.clear();
915
916 try // try again with just our implicit rules
917 {
919 compileRules();
920 }
921 catch( PARSE_ERROR& )
922 {
923 wxFAIL_MSG( wxT( "Compiling implicit rules failed." ) );
924 }
925
926 throw original_parse_error;
927 }
928
929 for( int ii = DRCE_FIRST; ii <= DRCE_LAST; ++ii )
931
932 m_rulesValid = true;
933}
934
935
936void DRC_ENGINE::RunTests( EDA_UNITS aUnits, bool aReportAllTrackErrors, bool aTestFootprints, BOARD_COMMIT* aCommit )
937{
938 PROF_TIMER timer;
939
940 SetUserUnits( aUnits );
941
942 m_reportAllTrackErrors = aReportAllTrackErrors;
943 m_testFootprints = aTestFootprints;
944
945 for( int ii = DRCE_FIRST; ii <= DRCE_LAST; ++ii )
946 {
947 if( m_designSettings->Ignore( ii ) )
948 m_errorLimits[ ii ] = 0;
949 else if( ii == DRCE_CLEARANCE || ii == DRCE_UNCONNECTED_ITEMS )
951 else
953 }
954
956
957 m_board->IncrementTimeStamp(); // Invalidate all caches...
958
959 DRC_CACHE_GENERATOR cacheGenerator;
960 cacheGenerator.SetDRCEngine( this );
961
962 if( !cacheGenerator.Run() ) // ... and regenerate them.
963 return;
964
965 // Recompute component classes
966 m_board->GetComponentClassManager().ForceComponentClassRecalculation();
967
968 // fromTo() conditions are evaluated by every provider, not only the length ones
969 if( std::shared_ptr<CONNECTIVITY_DATA> connectivity = m_board->GetConnectivity() )
970 connectivity->GetFromToCache()->Rebuild( m_board );
971
972 int timestamp = m_board->GetTimeStamp();
973
974 for( DRC_TEST_PROVIDER* provider : m_testProviders )
975 {
976 if( m_logReporter )
977 m_logReporter->Report( wxString::Format( wxT( "Run DRC provider: '%s'" ), provider->GetName() ) );
978
979 PROF_TIMER providerTimer;
980
981 if( !provider->RunTests( aUnits ) )
982 break;
983
984 providerTimer.Stop();
985 wxLogTrace( traceDrcProfile, "DRC provider '%s' took %0.3f ms", provider->GetName(), providerTimer.msecs() );
986 }
987
988 timer.Stop();
989 wxLogTrace( traceDrcProfile, "DRC took %0.3f ms", timer.msecs() );
990
991 // DRC tests are multi-threaded; anything that causes us to attempt to re-generate the
992 // caches while DRC is running is problematic.
993 wxASSERT( timestamp == m_board->GetTimeStamp() );
994}
995
996
997#define REPORT( s ) { if( aReporter ) { aReporter->Report( s ); } }
998
1000 REPORTER* aReporter )
1001{
1002 DRC_CONSTRAINT constraint = EvalRules( ZONE_CONNECTION_CONSTRAINT, a, b, aLayer, aReporter );
1003
1004 REPORT( "" )
1005 REPORT( wxString::Format( _( "Resolved zone connection type: %s." ),
1006 PrintZoneConnection( constraint.m_ZoneConnection ) ) )
1007
1009 {
1010 const PAD* pad = nullptr;
1011
1012 if( a->Type() == PCB_PAD_T )
1013 pad = static_cast<const PAD*>( a );
1014 else if( b->Type() == PCB_PAD_T )
1015 pad = static_cast<const PAD*>( b );
1016
1017 if( pad && pad->GetAttribute() == PAD_ATTRIB::PTH )
1018 {
1020 }
1021 else
1022 {
1023 REPORT( wxString::Format( _( "Pad is not a through hole pad; connection will be: %s." ),
1026 }
1027 }
1028
1029 return constraint;
1030}
1031
1032
1034 PCB_LAYER_ID aLayer, REPORTER* aReporter )
1035{
1036 /*
1037 * NOTE: all string manipulation MUST BE KEPT INSIDE the REPORT macro. It absolutely
1038 * kills performance when running bulk DRC tests (where aReporter is nullptr).
1039 */
1040
1041 const BOARD_CONNECTED_ITEM* ac = a && a->IsConnected() ? static_cast<const BOARD_CONNECTED_ITEM*>( a ) : nullptr;
1042 const BOARD_CONNECTED_ITEM* bc = b && b->IsConnected() ? static_cast<const BOARD_CONNECTED_ITEM*>( b ) : nullptr;
1043 const NETCLASS* acNetclass = ac ? ac->GetEffectiveNetClass() : nullptr;
1044 const NETCLASS* bcNetclass = bc ? bc->GetEffectiveNetClass() : nullptr;
1045
1046 bool a_is_non_copper = a && ( !a->IsOnCopperLayer() || isKeepoutZone( a, false ) );
1047 bool b_is_non_copper = b && ( !b->IsOnCopperLayer() || isKeepoutZone( b, false ) );
1048
1049 const PAD* pad = nullptr;
1050 const ZONE* zone = nullptr;
1051 const FOOTPRINT* parentFootprint = nullptr;
1052
1053 if( aConstraintType == ZONE_CONNECTION_CONSTRAINT
1054 || aConstraintType == THERMAL_RELIEF_GAP_CONSTRAINT
1055 || aConstraintType == THERMAL_SPOKE_WIDTH_CONSTRAINT
1056 || aConstraintType == SOLDER_MASK_EXPANSION_CONSTRAINT
1057 || aConstraintType == SOLDER_PASTE_ABS_MARGIN_CONSTRAINT
1058 || aConstraintType == SOLDER_PASTE_REL_MARGIN_CONSTRAINT )
1059 {
1060 if( a && a->Type() == PCB_PAD_T )
1061 pad = static_cast<const PAD*>( a );
1062 else if( a && a->Type() == PCB_ZONE_T )
1063 zone = static_cast<const ZONE*>( a );
1064
1065 if( b && b->Type() == PCB_PAD_T )
1066 pad = static_cast<const PAD*>( b );
1067 else if( b && b->Type() == PCB_ZONE_T )
1068 zone = static_cast<const ZONE*>( b );
1069
1070 if( pad )
1071 parentFootprint = pad->GetParentFootprint();
1072 }
1073
1074 DRC_CONSTRAINT constraint;
1075 constraint.m_Type = aConstraintType;
1076
1077 auto applyConstraint =
1078 [&]( const DRC_ENGINE_CONSTRAINT* c )
1079 {
1080 DRC_RULE* rule = c->constraint.GetParentRule();
1081 DRC_RULE* minRule = constraint.GetMinRule();
1082 DRC_RULE* optRule = constraint.GetOptRule();
1083 DRC_RULE* maxRule = constraint.GetMaxRule();
1084
1085 if( c->constraint.m_Value.HasMin() )
1086 {
1087 if( c->parentRule && c->parentRule->IsImplicit() )
1088 constraint.m_ImplicitMin = true;
1089 else
1090 constraint.m_ImplicitMin = false;
1091
1092 constraint.m_Value.SetMin( c->constraint.m_Value.Min() );
1093 minRule = rule;
1094 }
1095
1096 if( c->constraint.m_Value.HasOpt() )
1097 {
1098 constraint.m_Value.SetOpt( c->constraint.m_Value.Opt() );
1099 optRule = rule;
1100 }
1101
1102 if( c->constraint.m_Value.HasMax() )
1103 {
1104 constraint.m_Value.SetMax( c->constraint.m_Value.Max() );
1105 maxRule = rule;
1106 }
1107
1108 switch( c->constraint.m_Type )
1109 {
1117 if( constraint.m_Value.Min() > MAXIMUM_CLEARANCE )
1118 constraint.m_Value.SetMin( MAXIMUM_CLEARANCE );
1119
1120 break;
1121
1122 default:
1123 break;
1124 }
1125
1126 // While the expectation would be to OR the disallow flags, we've already
1127 // masked them down to aItem's type -- so we're really only looking for a
1128 // boolean here.
1129 constraint.m_DisallowFlags = c->constraint.m_DisallowFlags;
1130
1131 constraint.m_ZoneConnection = c->constraint.m_ZoneConnection;
1132
1133 constraint.SetParentRule( rule );
1134 constraint.SetValueRules( minRule, optRule, maxRule );
1135
1136 constraint.SetOptionsFromOther( c->constraint );
1137 };
1138
1139 const FOOTPRINT* footprints[2] = { a ? a->GetParentFootprint() : nullptr,
1140 b ? b->GetParentFootprint() : nullptr };
1141
1142 // Handle Footprint net ties, which will zero out the clearance for footprint objects
1143 if( aConstraintType == CLEARANCE_CONSTRAINT // Only zero clearance, other constraints still apply
1144 && ( ( ( !ac ) ^ ( !bc ) )// Only apply to cases where we are comparing a connected item to a non-connected item
1145 // and not both connected. Connected items of different nets still need to be checked
1146 // for their standard clearance value
1147 || ( ( footprints[0] == footprints[1] ) // Unless both items are in the same footprint
1148 && footprints[0] ) ) // And that footprint exists
1149 && !a_is_non_copper // Also, both elements need to be on copper layers
1150 && !b_is_non_copper )
1151 {
1152 const BOARD_ITEM* child_items[2] = {a, b};
1153
1154 // These are the items being compared against, so the order is reversed
1155 const BOARD_CONNECTED_ITEM* alt_items[2] = {bc, ac};
1156
1157 for( int ii = 0; ii < 2; ++ii )
1158 {
1159 // We need both a footprint item and a connected item to check for a net tie
1160 if( !footprints[ii] || !alt_items[ii] )
1161 continue;
1162
1163 const std::set<int>& netcodes = footprints[ii]->GetNetTieCache( child_items[ii] );
1164
1165 auto it = netcodes.find( alt_items[ii]->GetNetCode() );
1166
1167 if( it != netcodes.end() )
1168 {
1169 REPORT( "" )
1170 REPORT( wxString::Format( _( "Net tie on %s; clearance: 0." ),
1171 EscapeHTML( footprints[ii]->GetItemDescription( this, true ) ) ) )
1172
1173 constraint.SetName( _( "net tie" ) );
1174 constraint.m_Value.SetMin( 0 );
1175 return constraint;
1176 }
1177 }
1178 }
1179
1180 // Local overrides take precedence over everything *except* board min clearance
1181 if( aConstraintType == CLEARANCE_CONSTRAINT || aConstraintType == HOLE_CLEARANCE_CONSTRAINT )
1182 {
1183 int override_val = 0;
1184 std::optional<int> overrideA;
1185 std::optional<int> overrideB;
1186
1187 if( ac && !b_is_non_copper )
1188 overrideA = ac->GetClearanceOverrides( nullptr );
1189
1190 if( bc && !a_is_non_copper )
1191 overrideB = bc->GetClearanceOverrides( nullptr );
1192
1193 if( overrideA.has_value() || overrideB.has_value() )
1194 {
1195 wxString msg;
1196
1197 if( overrideA.has_value() )
1198 {
1199 REPORT( "" )
1200 REPORT( wxString::Format( _( "Local override on %s; clearance: %s." ),
1201 EscapeHTML( a->GetItemDescription( this, true ) ),
1202 MessageTextFromValue( overrideA.value() ) ) )
1203
1204 override_val = ac->GetClearanceOverrides( &msg ).value();
1205 }
1206
1207 if( overrideB.has_value() )
1208 {
1209 REPORT( "" )
1210 REPORT( wxString::Format( _( "Local override on %s; clearance: %s." ),
1211 EscapeHTML( b->GetItemDescription( this, true ) ),
1212 MessageTextFromValue( overrideB.value() ) ) )
1213
1214 if( overrideB > override_val )
1215 override_val = bc->GetClearanceOverrides( &msg ).value();
1216 }
1217
1218 if( override_val )
1219 {
1220 if( aConstraintType == CLEARANCE_CONSTRAINT )
1221 {
1222 if( override_val < m_designSettings->m_MinClearance )
1223 {
1224 override_val = m_designSettings->m_MinClearance;
1225 msg = _( "board minimum" );
1226
1227 REPORT( "" )
1228 REPORT( wxString::Format( _( "Board minimum clearance: %s." ),
1229 MessageTextFromValue( override_val ) ) )
1230 }
1231 }
1232 else
1233 {
1234 if( override_val < m_designSettings->m_HoleClearance )
1235 {
1236 override_val = m_designSettings->m_HoleClearance;
1237 msg = _( "board minimum hole" );
1238
1239 REPORT( "" )
1240 REPORT( wxString::Format( _( "Board minimum hole clearance: %s." ),
1241 MessageTextFromValue( override_val ) ) )
1242 }
1243 }
1244
1245 constraint.SetName( msg );
1246 constraint.m_Value.SetMin( override_val );
1247 return constraint;
1248 }
1249 }
1250 }
1251 else if( aConstraintType == ZONE_CONNECTION_CONSTRAINT )
1252 {
1253 if( pad && pad->GetLocalZoneConnection() != ZONE_CONNECTION::INHERITED )
1254 {
1255 wxString msg;
1256 ZONE_CONNECTION override = pad->GetZoneConnectionOverrides( &msg );
1257
1258 REPORT( "" )
1259 REPORT( wxString::Format( _( "Local override on %s; zone connection: %s." ),
1260 EscapeHTML( pad->GetItemDescription( this, true ) ),
1261 PrintZoneConnection( override ) ) )
1262
1263 constraint.SetName( msg );
1264 constraint.m_ZoneConnection = override;
1265 return constraint;
1266 }
1267 }
1268 else if( aConstraintType == THERMAL_RELIEF_GAP_CONSTRAINT )
1269 {
1270 if( pad && pad->GetLocalThermalGapOverride( nullptr ) > 0 )
1271 {
1272 wxString msg;
1273 int gap_override = pad->GetLocalThermalGapOverride( &msg );
1274
1275 REPORT( "" )
1276 REPORT( wxString::Format( _( "Local override on %s; thermal relief gap: %s." ),
1277 EscapeHTML( pad->GetItemDescription( this, true ) ),
1278 MessageTextFromValue( gap_override ) ) )
1279
1280 constraint.SetName( msg );
1281 constraint.m_Value.SetMin( gap_override );
1282 return constraint;
1283 }
1284 }
1285 else if( aConstraintType == THERMAL_SPOKE_WIDTH_CONSTRAINT )
1286 {
1287 if( pad && pad->GetLocalSpokeWidthOverride( nullptr ) > 0 )
1288 {
1289 wxString msg;
1290 int spoke_override = pad->GetLocalSpokeWidthOverride( &msg );
1291
1292 REPORT( "" )
1293 REPORT( wxString::Format( _( "Local override on %s; thermal spoke width: %s." ),
1294 EscapeHTML( pad->GetItemDescription( this, true ) ),
1295 MessageTextFromValue( spoke_override ) ) )
1296
1297 if( zone && zone->GetMinThickness() > spoke_override )
1298 {
1299 spoke_override = zone->GetMinThickness();
1300
1301 REPORT( "" )
1302 REPORT( wxString::Format( _( "%s min thickness: %s." ),
1303 EscapeHTML( zone->GetItemDescription( this, true ) ),
1304 MessageTextFromValue( spoke_override ) ) )
1305 }
1306
1307 constraint.SetName( msg );
1308 constraint.m_Value.SetMin( spoke_override );
1309 return constraint;
1310 }
1311 }
1312 else if( aConstraintType == SOLDER_MASK_EXPANSION_CONSTRAINT )
1313 {
1314 std::optional<int> override;
1315 const BOARD_ITEM* overrideItem = a;
1316
1317 if( pad )
1318 override = pad->GetLocalSolderMaskMargin();
1319 else if( a->Type() == PCB_SHAPE_T )
1320 override = static_cast<const PCB_SHAPE*>( a )->GetLocalSolderMaskMargin();
1321 else if( const PCB_TRACK* track = dynamic_cast<const PCB_TRACK*>( a ) )
1322 override = track->GetLocalSolderMaskMargin();
1323
1324 if( !override.has_value() && pad )
1325 {
1326 if( FOOTPRINT* overrideFootprint = pad->GetParentFootprint() )
1327 {
1328 override = overrideFootprint->GetLocalSolderMaskMargin();
1329 overrideItem = overrideFootprint;
1330 }
1331 }
1332
1333 if( override )
1334 {
1335 REPORT( "" )
1336 REPORT( wxString::Format( _( "Local override on %s; solder mask expansion: %s." ),
1337 EscapeHTML( overrideItem->GetItemDescription( this, true ) ),
1338 MessageTextFromValue( override.value() ) ) )
1339
1340 constraint.m_Value.SetOpt( override.value() );
1341 return constraint;
1342 }
1343 }
1344 else if( aConstraintType == SOLDER_PASTE_ABS_MARGIN_CONSTRAINT )
1345 {
1346 std::optional<int> override;
1347 const BOARD_ITEM* overrideItem = a;
1348
1349 if( pad )
1350 override = pad->GetLocalSolderPasteMargin();
1351
1352 if( !override.has_value() && pad )
1353 {
1354 if( FOOTPRINT* overrideFootprint = pad->GetParentFootprint() )
1355 {
1356 override = overrideFootprint->GetLocalSolderPasteMargin();
1357 overrideItem = overrideFootprint;
1358 }
1359 }
1360
1361 if( override )
1362 {
1363 REPORT( "" )
1364 REPORT( wxString::Format( _( "Local override on %s; solder paste absolute clearance: %s." ),
1365 EscapeHTML( overrideItem->GetItemDescription( this, true ) ),
1366 MessageTextFromValue( override.value() ) ) )
1367
1368 constraint.m_Value.SetOpt( override.value_or( 0 ) );
1369 return constraint;
1370 }
1371 }
1372 else if( aConstraintType == SOLDER_PASTE_REL_MARGIN_CONSTRAINT )
1373 {
1374 std::optional<double> overrideRatio;
1375 const BOARD_ITEM* overrideItem = a;
1376
1377 if( pad )
1378 overrideRatio = pad->GetLocalSolderPasteMarginRatio();
1379
1380 if( !overrideRatio.has_value() && pad )
1381 {
1382 if( FOOTPRINT* overrideFootprint = pad->GetParentFootprint() )
1383 {
1384 overrideRatio = overrideFootprint->GetLocalSolderPasteMarginRatio();
1385 overrideItem = overrideFootprint;
1386 }
1387 }
1388
1389 if( overrideRatio )
1390 {
1391 REPORT( "" )
1392 REPORT( wxString::Format( _( "Local override on %s; solder paste relative clearance: %s." ),
1393 EscapeHTML( overrideItem->GetItemDescription( this, true ) ),
1394 MessageTextFromValue( overrideRatio.value() * 100.0 ) ) )
1395
1396 constraint.m_Value.SetOpt( KiROUND( overrideRatio.value_or( 0 ) * 1000 ) );
1397 return constraint;
1398 }
1399 }
1400
1401 auto testAssertion =
1402 [&]( const DRC_ENGINE_CONSTRAINT* c )
1403 {
1404 REPORT( wxString::Format( _( "Checking assertion '%s'." ),
1405 EscapeHTML( c->constraint.m_Test->GetExpression() ) ) )
1406
1407 if( c->constraint.m_Test->EvaluateFor( a, b, c->constraint.m_Type, aLayer, aReporter ) )
1408 REPORT( _( "Assertion passed." ) )
1409 else
1410 REPORT( EscapeHTML( _( "--> Assertion failed. <--" ) ) )
1411 };
1412
1413 // Within a single EvalRules call a, b, aLayer and the constraint type are fixed, so a rule
1414 // condition's result depends only on its expression. Large rule sets contain many rules with
1415 // identical conditions; caching by expression collapses those duplicates into one evaluation
1416 // each and avoids re-running the (allocation-heavy) expression VM. Only used on the bulk path
1417 // with no reporter, since the reporter path has reporting side effects.
1418 std::unordered_map<wxString, bool> conditionCache;
1419
1420 auto reportConstraintHeader =
1421 [&]( const DRC_ENGINE_CONSTRAINT* c )
1422 {
1423 REPORT( "" )
1424
1425 bool implicit = c->parentRule && c->parentRule->IsImplicit();
1426
1427 switch( c->constraint.m_Type )
1428 {
1436 REPORT( wxString::Format( _( "Checking %s clearance: %s." ),
1437 EscapeHTML( c->constraint.GetName() ),
1438 MessageTextFromValue( c->constraint.m_Value.Min() ) ) )
1439 break;
1441 REPORT( wxString::Format( _( "Checking %s creepage: %s." ),
1442 EscapeHTML( c->constraint.GetName() ),
1443 MessageTextFromValue( c->constraint.m_Value.Min() ) ) )
1444 break;
1446 REPORT( wxString::Format( _( "Checking %s max uncoupled length: %s." ),
1447 EscapeHTML( c->constraint.GetName() ),
1448 MessageTextFromValue( c->constraint.m_Value.Max() ) ) )
1449 break;
1450
1451 case SKEW_CONSTRAINT:
1452 REPORT( wxString::Format( _( "Checking %s max skew: %s." ),
1453 EscapeHTML( c->constraint.GetName() ),
1454 MessageTextFromValue( c->constraint.m_Value.Max() ) ) )
1455 break;
1456
1458 REPORT( wxString::Format( _( "Checking %s gap: %s." ),
1459 EscapeHTML( c->constraint.GetName() ),
1460 MessageTextFromValue( c->constraint.m_Value.Min() ) ) )
1461 break;
1462
1464 REPORT( wxString::Format( _( "Checking %s thermal spoke width: %s." ),
1465 EscapeHTML( c->constraint.GetName() ),
1466 MessageTextFromValue( c->constraint.m_Value.Opt() ) ) )
1467 break;
1468
1470 REPORT( wxString::Format( _( "Checking %s solder mask expansion: %s." ),
1471 EscapeHTML( c->constraint.GetName() ),
1472 MessageTextFromValue( c->constraint.m_Value.Opt() ) ) )
1473 break;
1474
1476 REPORT( wxString::Format( _( "Checking %s solder paste absolute clearance: %s." ),
1477 EscapeHTML( c->constraint.GetName() ),
1478 MessageTextFromValue( c->constraint.m_Value.Opt() ) ) )
1479 break;
1480
1482 REPORT( wxString::Format( _( "Checking %s solder paste relative clearance: %s." ),
1483 EscapeHTML( c->constraint.GetName() ),
1484 MessageTextFromValue( c->constraint.m_Value.Opt() ) ) )
1485 break;
1486
1488 REPORT( wxString::Format( _( "Checking %s min spoke count: %s." ),
1489 EscapeHTML( c->constraint.GetName() ),
1490 MessageTextFromUnscaledValue( c->constraint.m_Value.Min() ) ) )
1491 break;
1492
1494 REPORT( wxString::Format( _( "Checking %s max microvia stack depth: %s." ),
1495 EscapeHTML( c->constraint.GetName() ),
1496 MessageTextFromUnscaledValue( c->constraint.m_Value.Max() ) ) )
1497 break;
1498
1500 REPORT( wxString::Format( _( "Checking %s max hole aspect ratio: %.3f." ),
1501 EscapeHTML( c->constraint.GetName() ),
1502 c->constraint.m_Value.Max() / 1000.0 ) )
1503 break;
1504
1506 REPORT( wxString::Format( _( "Checking %s zone connection: %s." ),
1507 EscapeHTML( c->constraint.GetName() ),
1508 PrintZoneConnection( c->constraint.m_ZoneConnection ) ) )
1509 break;
1510
1517 case LENGTH_CONSTRAINT:
1524 {
1525 if( implicit )
1526 {
1527 switch( c->constraint.m_Type )
1528 {
1530 if( c->constraint.m_Value.HasOpt() )
1531 {
1532 REPORT( wxString::Format( _( "Checking %s track width: opt %s." ),
1533 EscapeHTML( c->constraint.GetName() ),
1534 MessageTextFromValue( c->constraint.m_Value.Opt() ) ) )
1535 }
1536 else if( c->constraint.m_Value.HasMin() )
1537 {
1538 REPORT( wxString::Format( _( "Checking %s track width: min %s." ),
1539 EscapeHTML( c->constraint.GetName() ),
1540 MessageTextFromValue( c->constraint.m_Value.Min() ) ) )
1541 }
1542
1543 break;
1544
1546 REPORT( wxString::Format( _( "Checking %s annular width: min %s." ),
1547 EscapeHTML( c->constraint.GetName() ),
1548 MessageTextFromValue( c->constraint.m_Value.Opt() ) ) )
1549 break;
1550
1552 if( c->constraint.m_Value.HasOpt() )
1553 {
1554 REPORT( wxString::Format( _( "Checking %s via diameter: opt %s." ),
1555 EscapeHTML( c->constraint.GetName() ),
1556 MessageTextFromValue( c->constraint.m_Value.Opt() ) ) )
1557 }
1558 else if( c->constraint.m_Value.HasMin() )
1559 {
1560 REPORT( wxString::Format( _( "Checking %s via diameter: min %s." ),
1561 EscapeHTML( c->constraint.GetName() ),
1562 MessageTextFromValue( c->constraint.m_Value.Min() ) ) )
1563 }
1564 break;
1565
1567 if( c->constraint.m_Value.HasOpt() )
1568 {
1569 REPORT( wxString::Format( _( "Checking %s hole size: opt %s." ),
1570 EscapeHTML( c->constraint.GetName() ),
1571 MessageTextFromValue( c->constraint.m_Value.Opt() ) ) )
1572 }
1573 else if( c->constraint.m_Value.HasMin() )
1574 {
1575 REPORT( wxString::Format( _( "Checking %s hole size: min %s." ),
1576 EscapeHTML( c->constraint.GetName() ),
1577 MessageTextFromValue( c->constraint.m_Value.Min() ) ) )
1578 }
1579
1580 break;
1581
1585 REPORT( wxString::Format( _( "Checking %s: min %s." ),
1586 EscapeHTML( c->constraint.GetName() ),
1587 MessageTextFromValue( c->constraint.m_Value.Min() ) ) )
1588 break;
1589
1591 if( c->constraint.m_Value.HasOpt() )
1592 {
1593 REPORT( wxString::Format( _( "Checking %s diff pair gap: opt %s." ),
1594 EscapeHTML( c->constraint.GetName() ),
1595 MessageTextFromValue( c->constraint.m_Value.Opt() ) ) )
1596 }
1597 else if( c->constraint.m_Value.HasMin() )
1598 {
1599 REPORT( wxString::Format( _( "Checking %s clearance: min %s." ),
1600 EscapeHTML( c->constraint.GetName() ),
1601 MessageTextFromValue( c->constraint.m_Value.Min() ) ) )
1602 }
1603
1604 break;
1605
1607 REPORT( wxString::Format( _( "Checking %s hole to hole: min %s." ),
1608 EscapeHTML( c->constraint.GetName() ),
1609 MessageTextFromValue( c->constraint.m_Value.Min() ) ) )
1610 break;
1611
1612 default:
1613 REPORT( wxString::Format( _( "Checking %s." ),
1614 EscapeHTML( c->constraint.GetName() ) ) )
1615 }
1616 }
1617 else
1618 {
1619 REPORT( wxString::Format( _( "Checking %s: min %s; opt %s; max %s." ),
1620 EscapeHTML( c->constraint.GetName() ),
1621 c->constraint.m_Value.HasMin()
1622 ? MessageTextFromValue( c->constraint.m_Value.Min() )
1623 : wxT( "<i>" ) + _( "undefined" ) + wxT( "</i>" ),
1624 c->constraint.m_Value.HasOpt()
1625 ? MessageTextFromValue( c->constraint.m_Value.Opt() )
1626 : wxT( "<i>" ) + _( "undefined" ) + wxT( "</i>" ),
1627 c->constraint.m_Value.HasMax()
1628 ? MessageTextFromValue( c->constraint.m_Value.Max() )
1629 : wxT( "<i>" ) + _( "undefined" ) + wxT( "</i>" ) ) )
1630 }
1631 break;
1632 }
1633
1634 default:
1635 REPORT( wxString::Format( _( "Checking %s." ), EscapeHTML( c->constraint.GetName() ) ) )
1636 }
1637 };
1638
1639 auto checkCondition =
1640 [&]( const DRC_ENGINE_CONSTRAINT* c, REPORTER* r )
1641 {
1642 bool condMatched = false;
1643
1644 if( r )
1645 {
1646 condMatched = c->condition->EvaluateFor( a, b, c->constraint.m_Type, aLayer, r );
1647 }
1648 else
1649 {
1650 const wxString& expr = c->condition->GetExpression();
1651 auto it = conditionCache.find( expr );
1652
1653 if( it != conditionCache.end() )
1654 {
1655 condMatched = it->second;
1656 }
1657 else
1658 {
1659 condMatched = c->condition->EvaluateFor( a, b, c->constraint.m_Type, aLayer, r );
1660 conditionCache[expr] = condMatched;
1661 }
1662 }
1663
1664 return condMatched;
1665 };
1666
1667 auto processConstraint =
1668 [&]( const DRC_ENGINE_CONSTRAINT* c )
1669 {
1670 bool implicit = c->parentRule && c->parentRule->IsImplicit();
1671
1673 {
1675 {
1676 if( a_is_non_copper || b_is_non_copper )
1677 {
1678 reportConstraintHeader( c );
1679 REPORT( _( "Netclass clearances apply only between copper items." ) )
1680 return;
1681 }
1682 }
1683
1684 // The condition starts with A.hasExactNetclass( tag ) and is tried both ways round, so
1685 // it cannot match unless one of the items is in that netclass
1686 const wxString& netclassName = c->parentRule->m_ImplicitNetclass;
1687
1688 if( !netclassName.empty()
1689 && !( acNetclass && acNetclass->NameEquals( netclassName ) )
1690 && !( bcNetclass && bcNetclass->NameEquals( netclassName ) ) )
1691 {
1692 return;
1693 }
1694
1695 if( !checkCondition( c, nullptr ) )
1696 return;
1697 }
1698
1699 reportConstraintHeader( c );
1700
1702 {
1703 if( a_is_non_copper )
1704 {
1705 REPORT( wxString::Format( _( "%s contains no copper. Constraint ignored." ),
1706 EscapeHTML( a->GetItemDescription( this, true ) ) ) )
1707 return;
1708 }
1709 else if( b_is_non_copper )
1710 {
1711 REPORT( wxString::Format( _( "%s contains no copper. Constraint ignored." ),
1712 EscapeHTML( b->GetItemDescription( this, true ) ) ) )
1713 return;
1714 }
1715 }
1716 else if( c->constraint.m_Type == DISALLOW_CONSTRAINT )
1717 {
1718 // A footprint's own keepout never applies to that footprint; decide
1719 // ownership from the rule's zone so a hijacked UUID cache can't defeat it
1721 {
1722 REPORT( _( "Keepout belongs to the footprint under test; constraint ignored." ) )
1723 return;
1724 }
1725
1726 int mask;
1727
1728 if( a->GetFlags() & HOLE_PROXY )
1729 {
1730 mask = DRC_DISALLOW_HOLES;
1731 }
1732 else if( a->Type() == PCB_VIA_T )
1733 {
1734 const PCB_VIA* via = static_cast<const PCB_VIA*>( a );
1735
1736 if( via->IsMicroVia() )
1738 else if( via->IsBlindVia() )
1740 else if( via->IsBuriedVia() )
1742 else
1744 }
1745 else
1746 {
1747 switch( a->Type() )
1748 {
1749 case PCB_TRACE_T: mask = DRC_DISALLOW_TRACKS; break;
1750 case PCB_ARC_T: mask = DRC_DISALLOW_TRACKS; break;
1751 case PCB_PAD_T: mask = DRC_DISALLOW_PADS; break;
1752 case PCB_FOOTPRINT_T: mask = DRC_DISALLOW_FOOTPRINTS; break;
1753 case PCB_SHAPE_T: mask = DRC_DISALLOW_GRAPHICS; break;
1754 case PCB_BARCODE_T: mask = DRC_DISALLOW_GRAPHICS; break;
1755 case PCB_FIELD_T: mask = DRC_DISALLOW_TEXTS; break;
1756 case PCB_TEXT_T: mask = DRC_DISALLOW_TEXTS; break;
1757 case PCB_TEXTBOX_T: mask = DRC_DISALLOW_TEXTS; break;
1758 case PCB_TABLE_T:
1759 case PCB_DRILL_CHART_T:
1760 case PCB_STACKUP_TABLE_T: mask = DRC_DISALLOW_TEXTS; break;
1761
1762 case PCB_ZONE_T:
1763 // Treat teardrop areas as tracks for DRC purposes
1764 if( static_cast<const ZONE*>( a )->IsTeardropArea() )
1765 mask = DRC_DISALLOW_TRACKS;
1766 else
1767 mask = DRC_DISALLOW_ZONES;
1768
1769 break;
1770
1771 case PCB_LOCATE_HOLE_T: mask = DRC_DISALLOW_HOLES; break;
1772 default: mask = 0; break;
1773 }
1774 }
1775
1776 if( ( c->constraint.m_DisallowFlags & mask ) == 0 )
1777 {
1778 if( implicit )
1779 REPORT( _( "Keepout constraint not met." ) )
1780 else
1781 REPORT( _( "Disallow constraint not met." ) )
1782
1783 return;
1784 }
1785
1786 LSET itemLayers = a->GetLayerSet();
1787
1788 if( a->Type() == PCB_FOOTPRINT_T )
1789 {
1790 const FOOTPRINT* footprint = static_cast<const FOOTPRINT*>( a );
1791
1792 if( !footprint->GetCourtyard( F_CrtYd ).IsEmpty() )
1793 itemLayers |= LSET::FrontMask();
1794
1795 if( !footprint->GetCourtyard( B_CrtYd ).IsEmpty() )
1796 itemLayers |= LSET::BackMask();
1797 }
1798
1799 if( !( c->layerTest & itemLayers ).any() )
1800 {
1801 if( implicit )
1802 {
1803 REPORT( _( "Keepout layer(s) not matched." ) )
1804 }
1805 else if( c->parentRule )
1806 {
1807 REPORT( wxString::Format( _( "Rule layer '%s' not matched; rule ignored." ),
1809 }
1810 else
1811 {
1812 REPORT( _( "Rule layer not matched; rule ignored." ) )
1813 }
1814
1815 return;
1816 }
1817 }
1818
1819 if( ( IsPcbLayer( aLayer ) && !c->layerTest.test( aLayer ) )
1820 || ( m_board->GetEnabledLayers() & c->layerTest ).count() == 0 )
1821 {
1822 if( implicit )
1823 {
1824 REPORT( _( "Constraint layer not matched." ) )
1825 }
1826 else if( c->parentRule )
1827 {
1828 REPORT( wxString::Format( _( "Rule layer '%s' not matched; rule ignored." ),
1830 }
1831 else
1832 {
1833 REPORT( _( "Rule layer not matched; rule ignored." ) )
1834 }
1835 }
1837 && ( !a->HasHole() || !b->HasHole() ) )
1838 {
1839 // Hole-to-hole only applies between two mechanical holes; this covers both
1840 // round drilled holes and milled (oval) slots, but not a hole paired with
1841 // a non-hole item.
1842 const BOARD_ITEM* nonHole = a->HasHole() ? b : a;
1843
1844 REPORT( wxString::Format( _( "%s does not have a hole; rule ignored." ),
1845 nonHole->GetItemDescription( this, true ) ) )
1846 }
1847 else if( !c->condition || c->condition->GetExpression().IsEmpty() )
1848 {
1849 if( aReporter )
1850 {
1851 if( implicit )
1852 {
1853 REPORT( _( "Unconditional constraint applied." ) )
1854 }
1855 else if( constraint.m_Type == ASSERTION_CONSTRAINT )
1856 {
1857 REPORT( _( "Unconditional rule applied." ) )
1858 testAssertion( c );
1859 }
1860 else
1861 {
1862 REPORT( _( "Unconditional rule applied; overrides previous constraints." ) )
1863 }
1864 }
1865
1866 applyConstraint( c );
1867 }
1868 else
1869 {
1870 // For implicit keepout rules with a pre-resolved zone pointer, skip the
1871 // expensive expression evaluation when the item doesn't overlap the
1872 // zone's bounding box. This avoids UUID string parsing and cache lock
1873 // contention for the vast majority of item-keepout pairs.
1874 if( c->implicitKeepoutZone && !aReporter )
1875 {
1876 BOX2I itemBBox = a->GetBoundingBox();
1877 BOX2I zoneBBox = c->implicitKeepoutZone->GetBoundingBox();
1878
1879 if( !itemBBox.Intersects( zoneBBox ) )
1880 return;
1881 }
1882
1883 if( implicit )
1884 {
1885 // Don't report on implicit rule conditions; they're synthetic.
1886 }
1887 else
1888 {
1889 REPORT( wxString::Format( _( "Checking rule condition '%s'." ),
1891 }
1892
1893 bool condMatched;
1894
1896 condMatched = true;
1897 else
1898 condMatched = checkCondition( c, aReporter );
1899
1900 if( condMatched )
1901 {
1902 if( aReporter )
1903 {
1904 if( implicit )
1905 {
1906 REPORT( _( "Constraint applied." ) )
1907 }
1908 else if( constraint.m_Type == ASSERTION_CONSTRAINT )
1909 {
1910 REPORT( _( "Rule applied." ) )
1911 testAssertion( c );
1912 }
1913 else
1914 {
1915 REPORT( _( "Rule applied; overrides previous constraints." ) )
1916 }
1917 }
1918
1919 applyConstraint( c );
1920 }
1921 else
1922 {
1923 REPORT( implicit ? _( "Membership not satisfied; constraint ignored." )
1924 : _( "Condition not satisfied; rule ignored." ) )
1925 }
1926 }
1927 };
1928
1929 // Fast-path for netclass clearance when no explicit or diff pair override rules exist
1930 if( aConstraintType == CLEARANCE_CONSTRAINT
1933 && !aReporter
1934 && !a_is_non_copper
1935 && ( !b || !b_is_non_copper ) )
1936 {
1937 DRC_RULE* netclassRule = nullptr;
1938 int clearance = 0;
1939
1940 // Get netclass names outside of the lock to minimize critical section
1941 wxString ncNameA;
1942 wxString ncNameB;
1943
1944 if( ac )
1945 {
1946 NETCLASS* ncA = ac->GetEffectiveNetClass();
1947
1948 if( ncA )
1949 ncNameA = ncA->GetName();
1950 }
1951
1952 if( bc )
1953 {
1954 NETCLASS* ncB = bc->GetEffectiveNetClass();
1955
1956 if( ncB )
1957 ncNameB = ncB->GetName();
1958 }
1959
1960 // Look up clearances with shared lock protection
1961 if( !ncNameA.empty() || !ncNameB.empty() )
1962 {
1963 std::shared_lock<std::shared_mutex> readLock( m_clearanceCacheMutex );
1964
1965 if( !ncNameA.empty() )
1966 {
1967 auto it = m_netclassClearances.find( ncNameA );
1968
1969 if( it != m_netclassClearances.end() )
1970 {
1971 netclassRule = it->second;
1972 clearance = netclassRule->m_Constraints[0].m_Value.Min();
1973 }
1974 }
1975
1976 if( !ncNameB.empty() )
1977 {
1978 auto it = m_netclassClearances.find( ncNameB );
1979
1980 if( it != m_netclassClearances.end() && it->second->m_Constraints[0].m_Value.Min() > clearance )
1981 {
1982 netclassRule = it->second;
1983 clearance = netclassRule->m_Constraints[0].m_Value.Min();
1984 }
1985 }
1986 }
1987
1988 if( clearance > 0 )
1989 {
1990 constraint.m_Value.SetMin( clearance );
1991 constraint.m_ImplicitMin = true;
1992 constraint.SetParentRule( netclassRule );
1993 }
1994 }
1995 else
1996 {
1997 auto it = m_constraintMap.find( aConstraintType );
1998
1999 if( it != m_constraintMap.end() )
2000 {
2001 for( DRC_ENGINE_CONSTRAINT* rule : *it->second )
2002 processConstraint( rule );
2003 }
2004
2005 // DIFF_PAIR_GAP_CONSTRAINT must also respect CLEARANCE_CONSTRAINTs when called with two items
2006 if( aConstraintType == DIFF_PAIR_GAP_CONSTRAINT && b != nullptr )
2007 {
2008 DRC_CONSTRAINT clearanceConstraint = EvalRules( CLEARANCE_CONSTRAINT, a, b, aLayer, nullptr );
2009
2010 REPORT( "" )
2011 REPORT( wxString::Format( _( "Resolved minimum clearance: %s." ),
2012 MessageTextFromValue( clearanceConstraint.m_Value.Min() ) ) )
2013
2014 if( constraint.m_Value.Min() < clearanceConstraint.m_Value.Min() )
2015 {
2016 constraint.m_Value.SetMin( clearanceConstraint.m_Value.Min() );
2017 constraint.SetValueRules( clearanceConstraint.GetMinRule(), constraint.GetOptRule(),
2018 constraint.GetMaxRule() );
2019 }
2020
2021 return constraint;
2022 }
2023 }
2024
2025 if( constraint.GetParentRule() && !constraint.GetParentRule()->IsImplicit() )
2026 return constraint;
2027
2028 // Special case for properties which can be inherited from parent footprints. We've already
2029 // checked for local overrides, and there were no rules targetting the item itself, so we know
2030 // we're inheriting and need to see if there are any rules targetting the parent footprint.
2031 if( pad && parentFootprint && ( aConstraintType == ZONE_CONNECTION_CONSTRAINT
2032 || aConstraintType == THERMAL_RELIEF_GAP_CONSTRAINT
2033 || aConstraintType == THERMAL_SPOKE_WIDTH_CONSTRAINT
2034 || aConstraintType == SOLDER_MASK_EXPANSION_CONSTRAINT
2035 || aConstraintType == SOLDER_PASTE_ABS_MARGIN_CONSTRAINT
2036 || aConstraintType == SOLDER_PASTE_REL_MARGIN_CONSTRAINT ) )
2037 {
2038 REPORT( "" )
2039 REPORT( wxString::Format( _( "Inheriting from parent: %s." ),
2040 EscapeHTML( parentFootprint->GetItemDescription( this, true ) ) ) )
2041
2042 if( a == pad )
2043 a = parentFootprint;
2044 else
2045 b = parentFootprint;
2046
2047 // a/b just changed, so conditions evaluated against the pad must not be reused for the
2048 // parent footprint.
2049 conditionCache.clear();
2050
2051 auto it = m_constraintMap.find( aConstraintType );
2052
2053 if( it != m_constraintMap.end() )
2054 {
2055 for( DRC_ENGINE_CONSTRAINT* rule : *it->second )
2056 processConstraint( rule );
2057
2058 if( constraint.GetParentRule() && !constraint.GetParentRule()->IsImplicit() )
2059 return constraint;
2060 }
2061
2062 // Found nothing again? Return the defaults.
2063 if( aConstraintType == SOLDER_MASK_EXPANSION_CONSTRAINT )
2064 {
2065 constraint.SetParentRule( nullptr );
2066 constraint.SetName( _( "board setup" ) );
2067 constraint.m_Value.SetOpt( m_designSettings->m_SolderMaskExpansion );
2068 return constraint;
2069 }
2070 else if( aConstraintType == SOLDER_PASTE_ABS_MARGIN_CONSTRAINT )
2071 {
2072 constraint.SetParentRule( nullptr );
2073 constraint.SetName( _( "board setup" ) );
2074 constraint.m_Value.SetOpt( m_designSettings->m_SolderPasteMargin );
2075 return constraint;
2076 }
2077 else if( aConstraintType == SOLDER_PASTE_REL_MARGIN_CONSTRAINT )
2078 {
2079 constraint.SetParentRule( nullptr );
2080 constraint.SetName( _( "board setup" ) );
2081 constraint.m_Value.SetOpt( KiROUND( m_designSettings->m_SolderPasteMarginRatio * 1000 ) );
2082 return constraint;
2083 }
2084 }
2085
2086 // Unfortunately implicit rules don't work for local clearances (such as zones) because
2087 // they have to be max'ed with netclass values (which are already implicit rules), and our
2088 // rule selection paradigm is "winner takes all".
2089 if( aConstraintType == CLEARANCE_CONSTRAINT )
2090 {
2091 int global = constraint.m_Value.Min();
2092 int clearance = global;
2093 bool needBlankLine = true;
2094
2095 if( ac && ac->GetLocalClearance().has_value() )
2096 {
2097 int localA = ac->GetLocalClearance().value();
2098
2099 if( needBlankLine )
2100 {
2101 REPORT( "" )
2102 needBlankLine = false;
2103 }
2104
2105 REPORT( wxString::Format( _( "Local clearance on %s: %s." ),
2106 EscapeHTML( a->GetItemDescription( this, true ) ),
2107 MessageTextFromValue( localA ) ) )
2108
2109 if( localA > clearance )
2110 {
2111 wxString msg;
2112 clearance = ac->GetLocalClearance( &msg ).value();
2113 constraint.SetParentRule( nullptr );
2114 constraint.SetName( msg );
2115 constraint.m_Value.SetMin( clearance );
2116 }
2117 }
2118
2119 if( bc && bc->GetLocalClearance().has_value() )
2120 {
2121 int localB = bc->GetLocalClearance().value();
2122
2123 if( needBlankLine )
2124 {
2125 REPORT( "" )
2126 needBlankLine = false;
2127 }
2128
2129 REPORT( wxString::Format( _( "Local clearance on %s: %s." ),
2130 EscapeHTML( b->GetItemDescription( this, true ) ),
2131 MessageTextFromValue( localB ) ) )
2132
2133 if( localB > clearance )
2134 {
2135 wxString msg;
2136 clearance = bc->GetLocalClearance( &msg ).value();
2137 constraint.SetParentRule( nullptr );
2138 constraint.SetName( msg );
2139 constraint.m_Value.SetMin( clearance );
2140 }
2141 }
2142
2143 if( !a_is_non_copper && !b_is_non_copper )
2144 {
2145 if( needBlankLine )
2146 {
2147 REPORT( "" )
2148 needBlankLine = false;
2149 }
2150
2151 REPORT( wxString::Format( _( "Board minimum clearance: %s." ),
2152 MessageTextFromValue( m_designSettings->m_MinClearance ) ) )
2153
2154 if( clearance < m_designSettings->m_MinClearance )
2155 {
2156 constraint.SetParentRule( nullptr );
2157 constraint.SetName( _( "board minimum" ) );
2158 constraint.m_Value.SetMin( m_designSettings->m_MinClearance );
2159 }
2160 }
2161
2162 return constraint;
2163 }
2164 else if( aConstraintType == ZONE_CONNECTION_CONSTRAINT )
2165 {
2166 if( pad && parentFootprint )
2167 {
2168 ZONE_CONNECTION local = parentFootprint->GetLocalZoneConnection();
2169
2170 if( local != ZONE_CONNECTION::INHERITED )
2171 {
2172 REPORT( "" )
2173 REPORT( wxString::Format( _( "%s zone connection: %s." ),
2174 EscapeHTML( parentFootprint->GetItemDescription( this, true ) ),
2175 PrintZoneConnection( local ) ) )
2176
2177 constraint.SetParentRule( nullptr );
2178 constraint.SetName( _( "footprint" ) );
2179 constraint.m_ZoneConnection = local;
2180 return constraint;
2181 }
2182 }
2183
2184 if( zone )
2185 {
2186 ZONE_CONNECTION local = zone->GetPadConnection();
2187
2188 REPORT( "" )
2189 REPORT( wxString::Format( _( "%s pad connection: %s." ),
2190 EscapeHTML( zone->GetItemDescription( this, true ) ),
2191 PrintZoneConnection( local ) ) )
2192
2193 constraint.SetParentRule( nullptr );
2194 constraint.SetName( _( "zone" ) );
2195 constraint.m_ZoneConnection = local;
2196 return constraint;
2197 }
2198 }
2199 else if( aConstraintType == THERMAL_RELIEF_GAP_CONSTRAINT )
2200 {
2201 if( zone )
2202 {
2203 int local = zone->GetThermalReliefGap();
2204
2205 REPORT( "" )
2206 REPORT( wxString::Format( _( "%s thermal relief gap: %s." ),
2207 EscapeHTML( zone->GetItemDescription( this, true ) ),
2208 MessageTextFromValue( local ) ) )
2209
2210 constraint.SetParentRule( nullptr );
2211 constraint.SetName( _( "zone" ) );
2212 constraint.m_Value.SetMin( local );
2213 return constraint;
2214 }
2215 }
2216 else if( aConstraintType == THERMAL_SPOKE_WIDTH_CONSTRAINT )
2217 {
2218 if( zone )
2219 {
2220 int local = zone->GetThermalReliefSpokeWidth();
2221
2222 REPORT( "" )
2223 REPORT( wxString::Format( _( "%s thermal spoke width: %s." ),
2224 EscapeHTML( zone->GetItemDescription( this, true ) ),
2225 MessageTextFromValue( local ) ) )
2226
2227 constraint.SetParentRule( nullptr );
2228 constraint.SetName( _( "zone" ) );
2229 constraint.m_Value.SetMin( local );
2230 return constraint;
2231 }
2232 }
2233
2234 if( !constraint.GetParentRule() )
2235 {
2236 constraint.m_Type = NULL_CONSTRAINT;
2237 constraint.m_DisallowFlags = 0;
2238 }
2239
2240 return constraint;
2241}
2242
2243
2245{
2248
2249 c = EvalRules( CLEARANCE_CONSTRAINT, a, b, aLayer );
2250
2251 if( c.m_Value.HasMin() )
2252 result.clearance = c.m_Value.Min();
2253
2254 c = EvalRules( HOLE_CLEARANCE_CONSTRAINT, a, b, aLayer );
2255
2256 if( c.m_Value.HasMin() )
2257 result.holeClearance = c.m_Value.Min();
2258
2259 c = EvalRules( HOLE_TO_HOLE_CONSTRAINT, a, b, aLayer );
2260
2261 if( c.m_Value.HasMin() )
2262 result.holeToHole = c.m_Value.Min();
2263
2264 c = EvalRules( EDGE_CLEARANCE_CONSTRAINT, a, b, aLayer );
2265
2266 if( c.m_Value.HasMin() )
2267 result.edgeClearance = c.m_Value.Min();
2268
2269 c = EvalRules( PHYSICAL_CLEARANCE_CONSTRAINT, a, b, aLayer );
2270
2271 if( c.m_Value.HasMin() )
2272 result.physicalClearance = c.m_Value.Min();
2273
2274 return result;
2275}
2276
2277
2278void DRC_ENGINE::ProcessAssertions( const BOARD_ITEM* a, std::function<void( const DRC_CONSTRAINT* )> aFailureHandler,
2279 REPORTER* aReporter )
2280{
2281 /*
2282 * NOTE: all string manipulation MUST BE KEPT INSIDE the REPORT macro. It absolutely
2283 * kills performance when running bulk DRC tests (where aReporter is nullptr).
2284 */
2285
2286 auto testAssertion =
2287 [&]( const DRC_ENGINE_CONSTRAINT* c )
2288 {
2289 REPORT( wxString::Format( _( "Checking rule assertion '%s'." ),
2290 EscapeHTML( c->constraint.m_Test->GetExpression() ) ) )
2291
2292 if( c->constraint.m_Test->EvaluateFor( a, nullptr, c->constraint.m_Type, a->GetLayer(), aReporter ) )
2293 {
2294 REPORT( _( "Assertion passed." ) )
2295 }
2296 else
2297 {
2298 REPORT( EscapeHTML( _( "--> Assertion failed. <--" ) ) )
2299 aFailureHandler( &c->constraint );
2300 }
2301 };
2302
2303 auto processConstraint =
2304 [&]( const DRC_ENGINE_CONSTRAINT* c )
2305 {
2306 REPORT( "" )
2307 REPORT( wxString::Format( _( "Checking %s." ), c->constraint.GetName() ) )
2308
2309 if( !( a->GetLayerSet() & c->layerTest ).any() )
2310 {
2311 REPORT( wxString::Format( _( "Rule layer '%s' not matched; rule ignored." ),
2312 EscapeHTML( c->parentRule->m_LayerSource ) ) )
2313 }
2314
2315 if( !c->condition || c->condition->GetExpression().IsEmpty() )
2316 {
2317 REPORT( _( "Unconditional rule applied." ) )
2318 testAssertion( c );
2319 }
2320 else
2321 {
2322 REPORT( wxString::Format( _( "Checking rule condition '%s'." ),
2323 EscapeHTML( c->condition->GetExpression() ) ) )
2324
2325 if( c->condition->EvaluateFor( a, nullptr, c->constraint.m_Type, a->GetLayer(), aReporter ) )
2326 {
2327 REPORT( _( "Rule applied." ) )
2328 testAssertion( c );
2329 }
2330 else
2331 {
2332 REPORT( _( "Condition not satisfied; rule ignored." ) )
2333 }
2334 }
2335 };
2336
2337 auto it = m_constraintMap.find( ASSERTION_CONSTRAINT );
2338
2339 if( it != m_constraintMap.end() )
2340 {
2341 for( int ii = 0; ii < (int) it->second->size(); ++ii )
2342 processConstraint( it->second->at( ii ) );
2343 }
2344}
2345
2346
2347#undef REPORT
2348
2349
2351{
2352 assert( error_code >= 0 && error_code <= DRCE_LAST );
2353 std::lock_guard<std::mutex> lock( m_errorLimitsMutex );
2354 return m_errorLimits[ error_code ] <= 0;
2355}
2356
2357
2358int DRC_ENGINE::GetErrorLimit( int error_code )
2359{
2360 assert( error_code >= 0 && error_code <= DRCE_LAST );
2361 std::lock_guard<std::mutex> lock( m_errorLimitsMutex );
2362 return std::max( 0, m_errorLimits[ error_code ] );
2363}
2364
2365
2366void DRC_ENGINE::ReportViolation( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos,
2367 int aMarkerLayer, const std::function<void( PCB_MARKER* )>& aPathGenerator )
2368{
2369 {
2370 std::lock_guard<std::mutex> lock( m_errorLimitsMutex );
2371
2372 // Providers pre-check IsErrorLimitExceeded(), but that check is racy when items
2373 // are processed in parallel, letting many threads slip past the cap and overshoot
2374 // it. Enforce the per-code limit atomically here so the reported count is exact
2375 // and reproducible regardless of worker scheduling.
2376 if( m_errorLimits[ aItem->GetErrorCode() ] <= 0 )
2377 return;
2378
2379 m_errorLimits[ aItem->GetErrorCode() ] -= 1;
2380 }
2381
2382 if( m_violationHandler )
2383 {
2384 static std::mutex handlerLock;
2385 std::lock_guard<std::mutex> guard( handlerLock );
2386 m_violationHandler( aItem, aPos, aMarkerLayer, aPathGenerator );
2387 }
2388
2389 if( m_logReporter )
2390 {
2391 wxString msg = wxString::Format( wxT( "Test '%s': %s (code %d)" ),
2392 aItem->GetViolatingTest()->GetName(),
2393 aItem->GetErrorMessage( false ),
2394 aItem->GetErrorCode() );
2395
2396 DRC_RULE* rule = aItem->GetViolatingRule();
2397
2398 if( rule )
2399 msg += wxString::Format( wxT( ", violating rule: '%s'" ), rule->m_Name );
2400
2401 m_logReporter->Report( msg );
2402
2403 wxString violatingItemsStr = wxT( "Violating items: " );
2404
2405 m_logReporter->Report( wxString::Format( wxT( " |- violating position (%d, %d)" ),
2406 aPos.x,
2407 aPos.y ) );
2408 }
2409}
2410
2411
2413{
2414 if( !m_progressReporter )
2415 return true;
2416
2417 return m_progressReporter->KeepRefreshing( aWait );
2418}
2419
2420
2422{
2423 if( m_progressReporter )
2424 m_progressReporter->AdvanceProgress();
2425}
2426
2427
2429{
2430 if( m_progressReporter )
2431 m_progressReporter->SetMaxProgress( aSize );
2432}
2433
2434
2435bool DRC_ENGINE::ReportProgress( double aProgress )
2436{
2437 if( !m_progressReporter )
2438 return true;
2439
2440 m_progressReporter->SetCurrentProgress( aProgress );
2441 return m_progressReporter->KeepRefreshing( false );
2442}
2443
2444
2445bool DRC_ENGINE::ReportPhase( const wxString& aMessage )
2446{
2447 if( !m_progressReporter )
2448 return true;
2449
2450 m_progressReporter->AdvancePhase( aMessage );
2451 return m_progressReporter->KeepRefreshing( false );
2452}
2453
2454
2456{
2457 return m_progressReporter && m_progressReporter->IsCancelled();
2458}
2459
2460
2462{
2463 auto it = m_constraintMap.find( constraintID );
2464 return it != m_constraintMap.end() && !it->second->empty();
2465}
2466
2467
2469 bool aUnconditionalOnly )
2470{
2471 int worst = 0;
2472 auto it = m_constraintMap.find( aConstraintId );
2473
2474 if( it != m_constraintMap.end() )
2475 {
2476 for( DRC_ENGINE_CONSTRAINT* c : *it->second )
2477 {
2478 if( aUnconditionalOnly && c->condition )
2479 continue;
2480
2481 int current = c->constraint.GetValue().Min();
2482
2483 if( current > worst )
2484 {
2485 worst = current;
2486 aConstraint = c->constraint;
2487 }
2488 }
2489 }
2490
2491 return worst > 0;
2492}
2493
2494
2496{
2497 auto it = m_constraintMap.find( aConstraintId );
2498
2499 if( it != m_constraintMap.end() )
2500 {
2501 for( DRC_ENGINE_CONSTRAINT* c : *it->second )
2502 {
2503 if( c->condition && c->parentRule && !c->parentRule->IsImplicit() )
2504 return true;
2505 }
2506 }
2507
2508 return false;
2509}
2510
2511
2513{
2514 auto it = m_constraintMap.find( aConstraintId );
2515
2516 if( it != m_constraintMap.end() )
2517 {
2518 for( DRC_ENGINE_CONSTRAINT* c : *it->second )
2519 {
2520 if( c->parentRule && !c->parentRule->IsImplicit() )
2521 return true;
2522 }
2523 }
2524
2525 return false;
2526}
2527
2528
2534
2535
2537{
2538 std::set<int> distinctMinimums;
2539 auto it = m_constraintMap.find( aConstraintId );
2540
2541 if( it != m_constraintMap.end() )
2542 {
2543 for( DRC_ENGINE_CONSTRAINT* c : *it->second )
2544 distinctMinimums.emplace( c->constraint.GetValue().Min() );
2545 }
2546
2547 return distinctMinimums;
2548}
2549
2550
2551// fixme: move two functions below to pcbcommon?
2552int DRC_ENGINE::MatchDpSuffix( const wxString& aNetName, wxString& aComplementNet, wxString& aBaseDpName )
2553{
2554 int rv = 0;
2555 int count = 0;
2556
2557 for( auto it = aNetName.rbegin(); it != aNetName.rend() && rv == 0; ++it, ++count )
2558 {
2559 int ch = *it;
2560
2561 if( ( ch >= '0' && ch <= '9' ) || ch == '_' )
2562 {
2563 continue;
2564 }
2565 else if( ch == '+' )
2566 {
2567 aComplementNet = wxT( "-" );
2568 rv = 1;
2569 }
2570 else if( ch == '-' )
2571 {
2572 aComplementNet = wxT( "+" );
2573 rv = -1;
2574 }
2575 else if( ch == 'N' )
2576 {
2577 aComplementNet = wxT( "P" );
2578 rv = -1;
2579 }
2580 else if ( ch == 'P' )
2581 {
2582 aComplementNet = wxT( "N" );
2583 rv = 1;
2584 }
2585 else
2586 {
2587 break;
2588 }
2589 }
2590
2591 if( rv != 0 && count >= 1 )
2592 {
2593 aBaseDpName = aNetName.Left( aNetName.Length() - count );
2594 aComplementNet = wxString( aBaseDpName ) << aComplementNet << aNetName.Right( count - 1 );
2595 }
2596
2597 return rv;
2598}
2599
2600
2601bool DRC_ENGINE::IsNetADiffPair( BOARD* aBoard, const NETINFO_ITEM* aNet, int& aNetP, int& aNetN )
2602{
2603 const wxString& refName = aNet->GetNetname();
2604 wxString dummy, coupledNetName;
2605
2606 if( int polarity = MatchDpSuffix( refName, coupledNetName, dummy ) )
2607 {
2608 NETINFO_ITEM* net = aBoard->FindNet( coupledNetName );
2609
2610 if( !net )
2611 return false;
2612
2613 if( polarity > 0 )
2614 {
2615 aNetP = aNet->GetNetCode();
2616 aNetN = net->GetNetCode();
2617 }
2618 else
2619 {
2620 aNetP = net->GetNetCode();
2621 aNetN = aNet->GetNetCode();
2622 }
2623
2624 return true;
2625 }
2626
2627 return false;
2628}
2629
2630
2635bool DRC_ENGINE::IsNetTieExclusion( int aTrackNetCode, PCB_LAYER_ID aTrackLayer,
2636 const VECTOR2I& aCollisionPos, BOARD_ITEM* aCollidingItem )
2637{
2638 FOOTPRINT* parentFootprint = aCollidingItem->GetParentFootprint();
2639
2640 if( parentFootprint && parentFootprint->IsNetTie() )
2641 {
2643 std::map<wxString, int> padToNetTieGroupMap = parentFootprint->MapPadNumbersToNetTieGroups();
2644
2645 for( PAD* pad : parentFootprint->Pads() )
2646 {
2647 if( padToNetTieGroupMap[ pad->GetNumber() ] >= 0 && aTrackNetCode == pad->GetNetCode() )
2648 {
2649 if( pad->GetEffectiveShape( aTrackLayer )->Collide( aCollisionPos, epsilon ) )
2650 return true;
2651 }
2652 }
2653 }
2654
2655 return false;
2656}
2657
2658
2659namespace
2660{
2661enum class SHOWMATCH_DOMAIN
2662{
2663 ALL_ITEMS,
2664 COPPER_ITEMS,
2665 EDGE_ITEMS,
2666 FOOTPRINTS,
2667 HOLE_ITEMS,
2668 LINE_WIDTH_ITEMS,
2669 MASK_EXPANSION_ITEMS,
2670 MASK_ITEMS,
2671 PADS,
2672 PADS_AND_VIAS,
2673 PASTE_ITEMS,
2674 ROUTING_ITEMS,
2675 SILK_ITEMS,
2676 SILK_TARGET_ITEMS,
2677 TEXT_ITEMS,
2678 VIAS
2679};
2680
2681
2682struct SHOWMATCH_DOMAIN_SPEC
2683{
2684 SHOWMATCH_DOMAIN primary;
2685 SHOWMATCH_DOMAIN secondary = SHOWMATCH_DOMAIN::ALL_ITEMS;
2686 bool hasSecondary = false;
2687 bool secondaryUnary = false;
2688};
2689
2690
2691bool isShowMatchSkippable( const BOARD_ITEM* aItem )
2692{
2693 switch( aItem->Type() )
2694 {
2695 case PCB_NETINFO_T:
2696 case PCB_GENERATOR_T:
2697 case PCB_GROUP_T:
2698 return true;
2699
2700 default:
2701 return false;
2702 }
2703}
2704
2705
2706bool matchesShowMatchDomain( const BOARD_ITEM* aItem, SHOWMATCH_DOMAIN aDomain )
2707{
2708 if( !aItem || isShowMatchSkippable( aItem ) )
2709 return false;
2710
2711 switch( aDomain )
2712 {
2713 case SHOWMATCH_DOMAIN::ALL_ITEMS:
2714 return true;
2715
2716 case SHOWMATCH_DOMAIN::COPPER_ITEMS:
2717 return aItem->IsOnCopperLayer();
2718
2719 case SHOWMATCH_DOMAIN::EDGE_ITEMS:
2720 if( aItem->IsOnLayer( Edge_Cuts ) || aItem->IsOnLayer( Margin ) )
2721 return true;
2722
2723 if( aItem->Type() == PCB_PAD_T )
2724 {
2725 const PAD* pad = static_cast<const PAD*>( aItem );
2726 return pad->GetAttribute() == PAD_ATTRIB::NPTH && pad->HasHole();
2727 }
2728
2729 return false;
2730
2731 case SHOWMATCH_DOMAIN::FOOTPRINTS:
2732 return aItem->Type() == PCB_FOOTPRINT_T;
2733
2734 case SHOWMATCH_DOMAIN::HOLE_ITEMS:
2735 return aItem->HasHole();
2736
2737 case SHOWMATCH_DOMAIN::LINE_WIDTH_ITEMS:
2738 if( aItem->Type() == PCB_SHAPE_T )
2739 return !static_cast<const PCB_SHAPE*>( aItem )->IsSolidFill();
2740
2741 return matchesShowMatchDomain( aItem, SHOWMATCH_DOMAIN::TEXT_ITEMS );
2742
2743 case SHOWMATCH_DOMAIN::MASK_EXPANSION_ITEMS:
2744 switch( aItem->Type() )
2745 {
2746 case PCB_PAD_T:
2747 case PCB_TRACE_T:
2748 case PCB_ARC_T:
2749 case PCB_VIA_T:
2750 case PCB_SHAPE_T:
2751 case PCB_ZONE_T:
2752 return true;
2753
2754 default:
2755 return false;
2756 }
2757
2758 case SHOWMATCH_DOMAIN::MASK_ITEMS:
2759 return aItem->IsOnLayer( F_Mask ) || aItem->IsOnLayer( B_Mask );
2760
2761 case SHOWMATCH_DOMAIN::PADS:
2762 return aItem->Type() == PCB_PAD_T;
2763
2764 case SHOWMATCH_DOMAIN::PADS_AND_VIAS:
2765 return aItem->Type() == PCB_PAD_T || aItem->Type() == PCB_VIA_T;
2766
2767 case SHOWMATCH_DOMAIN::PASTE_ITEMS:
2768 return aItem->IsOnLayer( F_Paste ) || aItem->IsOnLayer( B_Paste );
2769
2770 case SHOWMATCH_DOMAIN::ROUTING_ITEMS:
2771 switch( aItem->Type() )
2772 {
2773 case PCB_TRACE_T:
2774 case PCB_ARC_T:
2775 case PCB_VIA_T:
2776 case PCB_PAD_T:
2777 return true;
2778
2779 default:
2780 return false;
2781 }
2782
2783 case SHOWMATCH_DOMAIN::SILK_ITEMS:
2784 return aItem->IsOnLayer( F_SilkS ) || aItem->IsOnLayer( B_SilkS );
2785
2786 case SHOWMATCH_DOMAIN::SILK_TARGET_ITEMS:
2787 return aItem->IsOnLayer( F_SilkS ) || aItem->IsOnLayer( B_SilkS ) || aItem->IsOnLayer( F_Mask )
2788 || aItem->IsOnLayer( B_Mask ) || aItem->IsOnLayer( F_Adhes ) || aItem->IsOnLayer( B_Adhes )
2789 || aItem->IsOnLayer( F_Paste ) || aItem->IsOnLayer( B_Paste ) || aItem->IsOnLayer( F_CrtYd )
2790 || aItem->IsOnLayer( B_CrtYd ) || aItem->IsOnLayer( F_Fab ) || aItem->IsOnLayer( B_Fab )
2791 || aItem->IsOnCopperLayer() || aItem->IsOnLayer( Edge_Cuts ) || aItem->IsOnLayer( Margin );
2792
2793 case SHOWMATCH_DOMAIN::TEXT_ITEMS:
2794 return aItem->Type() == PCB_FIELD_T || aItem->Type() == PCB_TEXT_T || aItem->Type() == PCB_TEXTBOX_T
2795 || aItem->Type() == PCB_TABLECELL_T || BaseType( aItem->Type() ) == PCB_DIMENSION_T;
2796
2797 case SHOWMATCH_DOMAIN::VIAS:
2798 return aItem->Type() == PCB_VIA_T;
2799 }
2800
2801 return false;
2802}
2803
2804
2805SHOWMATCH_DOMAIN_SPEC getShowMatchDomainSpec( DRC_CONSTRAINT_T aConstraint )
2806{
2807 switch( aConstraint )
2808 {
2810 return { SHOWMATCH_DOMAIN::COPPER_ITEMS };
2811
2813 return { SHOWMATCH_DOMAIN::COPPER_ITEMS, SHOWMATCH_DOMAIN::EDGE_ITEMS, true, true };
2814
2816 return { SHOWMATCH_DOMAIN::HOLE_ITEMS, SHOWMATCH_DOMAIN::ALL_ITEMS, true, false };
2817
2819 return { SHOWMATCH_DOMAIN::HOLE_ITEMS };
2820
2822 return { SHOWMATCH_DOMAIN::FOOTPRINTS };
2823
2827 return { SHOWMATCH_DOMAIN::ALL_ITEMS };
2828
2830 return { SHOWMATCH_DOMAIN::SILK_ITEMS, SHOWMATCH_DOMAIN::SILK_TARGET_ITEMS, true, false };
2831
2833 return { SHOWMATCH_DOMAIN::MASK_ITEMS };
2834
2841 case LENGTH_CONSTRAINT:
2842 case SKEW_CONSTRAINT:
2843 return { SHOWMATCH_DOMAIN::ROUTING_ITEMS };
2844
2849 return { SHOWMATCH_DOMAIN::HOLE_ITEMS };
2850
2852 return { SHOWMATCH_DOMAIN::HOLE_ITEMS };
2853
2855 return { SHOWMATCH_DOMAIN::PADS_AND_VIAS };
2856
2858 return { SHOWMATCH_DOMAIN::PADS };
2859
2861 return { SHOWMATCH_DOMAIN::TEXT_ITEMS };
2862
2864 return { SHOWMATCH_DOMAIN::LINE_WIDTH_ITEMS };
2865
2867 return { SHOWMATCH_DOMAIN::MASK_EXPANSION_ITEMS };
2868
2871 return { SHOWMATCH_DOMAIN::PASTE_ITEMS };
2872
2875 default:
2876 return { SHOWMATCH_DOMAIN::ALL_ITEMS };
2877 }
2878}
2879
2880
2881std::vector<BOARD_ITEM*> collectShowMatchCandidates( BOARD* aBoard, SHOWMATCH_DOMAIN aDomain )
2882{
2883 std::vector<BOARD_ITEM*> items;
2884
2885 if( !aBoard )
2886 return items;
2887
2888 aBoard->RunOnChildren(
2889 [&]( BOARD_ITEM* aItem )
2890 {
2891 if( matchesShowMatchDomain( aItem, aDomain ) )
2892 items.push_back( aItem );
2893 },
2895
2896 return items;
2897}
2898
2899
2900std::vector<PCB_LAYER_ID> getShowMatchLayers( const BOARD_ITEM* aItem )
2901{
2902 std::vector<PCB_LAYER_ID> layers;
2903
2904 switch( aItem->Type() )
2905 {
2906 case PCB_PAD_T:
2907 layers = static_cast<const PAD*>( aItem )->Padstack().UniqueLayers(); break;
2908
2909 case PCB_VIA_T:
2910 layers = static_cast<const PCB_VIA*>( aItem )->Padstack().UniqueLayers(); break;
2911
2912 default:
2913 for( PCB_LAYER_ID layer : aItem->GetLayerSet() )
2914 layers.push_back( layer );
2915
2916 break;
2917 }
2918
2919 if( layers.empty() )
2920 layers.push_back( UNDEFINED_LAYER );
2921
2922 return layers;
2923}
2924
2925
2926bool ruleMatchesUnary( const DRC_RULE& aRule, const BOARD_ITEM* aItem, DRC_CONSTRAINT_T aConstraint,
2927 REPORTER* aReporter )
2928{
2929 bool testedLayer = false;
2930
2931 for( PCB_LAYER_ID layer : getShowMatchLayers( aItem ) )
2932 {
2933 if( layer != UNDEFINED_LAYER && !aRule.m_LayerCondition.test( layer ) )
2934 continue;
2935
2936 testedLayer = true;
2937
2938 if( !aRule.m_Condition
2939 || aRule.m_Condition->EvaluateFor( aItem, nullptr, static_cast<int>( aConstraint ), layer, aReporter ) )
2940 {
2941 return true;
2942 }
2943 }
2944
2945 if( !testedLayer && aItem->GetLayerSet().none() )
2946 {
2947 return !aRule.m_Condition
2948 || aRule.m_Condition->EvaluateFor( aItem, nullptr, static_cast<int>( aConstraint ), UNDEFINED_LAYER,
2949 aReporter );
2950 }
2951
2952 return false;
2953}
2954
2955
2956std::vector<PCB_LAYER_ID> getShowMatchPairLayers( const DRC_RULE& aRule, const BOARD_ITEM* aItemA,
2957 const BOARD_ITEM* aItemB, DRC_CONSTRAINT_T aConstraint )
2958{
2959 std::vector<PCB_LAYER_ID> layers;
2960 std::set<int> seenLayers;
2961
2962 auto addLayer =
2963 [&]( PCB_LAYER_ID aLayer )
2964 {
2965 if( aLayer != UNDEFINED_LAYER && !aRule.m_LayerCondition.test( aLayer ) )
2966 return;
2967
2968 if( seenLayers.insert( static_cast<int>( aLayer ) ).second )
2969 layers.push_back( aLayer );
2970 };
2971
2972 switch( aConstraint )
2973 {
2975 for( PCB_LAYER_ID layer : getShowMatchLayers( aItemA ) )
2976 addLayer( layer );
2977
2978 break;
2979
2981 addLayer( UNDEFINED_LAYER );
2982 break;
2983
2984 default:
2985 for( PCB_LAYER_ID layer : getShowMatchLayers( aItemA ) )
2986 addLayer( layer );
2987
2988 for( PCB_LAYER_ID layer : getShowMatchLayers( aItemB ) )
2989 addLayer( layer );
2990
2991 break;
2992 }
2993
2994 if( layers.empty() )
2995 layers.push_back( UNDEFINED_LAYER );
2996
2997 return layers;
2998}
2999
3000
3001bool ruleMatchesPair( const DRC_RULE& aRule, const BOARD_ITEM* aItemA, const BOARD_ITEM* aItemB,
3002 DRC_CONSTRAINT_T aConstraint, REPORTER* aReporter )
3003{
3004 for( PCB_LAYER_ID layer : getShowMatchPairLayers( aRule, aItemA, aItemB, aConstraint ) )
3005 {
3006 if( !aRule.m_Condition
3007 || aRule.m_Condition->EvaluateFor( aItemA, aItemB, static_cast<int>( aConstraint ), layer, aReporter ) )
3008 {
3009 return true;
3010 }
3011 }
3012
3013 return false;
3014}
3015} // namespace
3016
3017
3019{
3020 for( DRC_TEST_PROVIDER* prov : m_testProviders )
3021 {
3022 if( name == prov->GetName() )
3023 return prov;
3024 }
3025
3026 return nullptr;
3027}
3028
3029
3030std::vector<BOARD_ITEM*> DRC_ENGINE::GetItemsMatchingCondition( const wxString& aExpression,
3031 DRC_CONSTRAINT_T aConstraint,
3032 REPORTER* aReporter )
3033{
3034 wxLogTrace( wxS( "KI_TRACE_DRC_RULE_EDITOR" ), wxS( "[ShowMatches] engine enter: expr='%s', constraint=%d" ),
3035 aExpression, (int) aConstraint );
3036
3037 std::vector<BOARD_ITEM*> matches;
3038
3039 if( !m_board )
3040 return matches;
3041
3042 DRC_RULE_CONDITION condition( aExpression );
3043
3044 if( !condition.Compile( aReporter ? aReporter : m_logReporter ) )
3045 {
3046 wxLogTrace( wxS( "KI_TRACE_DRC_RULE_EDITOR" ), wxS( "[ShowMatches] engine: compile failed" ) );
3047 return matches;
3048 }
3049
3050 // Rebuild the from-to cache so that fromTo() expressions can be evaluated.
3051 // This cache requires explicit rebuilding before use since it depends on the full
3052 // connectivity graph being available.
3053 if( std::shared_ptr<CONNECTIVITY_DATA> connectivity = m_board->GetConnectivity() )
3054 {
3055 if( std::shared_ptr<FROM_TO_CACHE> ftCache = connectivity->GetFromToCache() )
3056 ftCache->Rebuild( m_board );
3057 }
3058
3059 size_t totalItems = 0;
3060 size_t skippedItems = 0;
3061 size_t noLayerItems = 0;
3062 size_t checkedItems = 0;
3063
3064 const auto applyConditionToItemFunc =
3065 [&]( BOARD_ITEM* aItem )
3066 {
3067 totalItems++;
3068
3069 // Skip items that don't have visible geometry or can't be meaningfully matched
3070 switch( aItem->Type() )
3071 {
3072 case PCB_NETINFO_T:
3073 case PCB_GENERATOR_T:
3074 case PCB_GROUP_T:
3075 case PCB_CONSTRAINT_T:
3076 skippedItems++;
3077 return;
3078
3079 default:
3080 break;
3081 }
3082
3083 LSET itemLayers = aItem->GetLayerSet();
3084
3085 if( itemLayers.none() )
3086 {
3087 noLayerItems++;
3088 return;
3089 }
3090
3091 checkedItems++;
3092 bool matched = false;
3093
3094 for( PCB_LAYER_ID layer : itemLayers )
3095 {
3096 if( condition.EvaluateFor( aItem, nullptr, static_cast<int>( aConstraint ), layer,
3097 aReporter ? aReporter : m_logReporter ) )
3098 {
3099 matches.push_back( aItem );
3100 wxLogTrace( wxS( "KI_TRACE_DRC_RULE_EDITOR" ),
3101 wxS( "[ShowMatches] engine: match type=%d kiid=%s layer=%d" ),
3102 (int) aItem->Type(), aItem->m_Uuid.AsString(), (int) layer );
3103 matched = true;
3104 break; // No need to check other layers
3105 }
3106 }
3107
3108 // Log a few non-matching items to help debug condition issues
3109 if( !matched && matches.size() == 0 && checkedItems <= 5 )
3110 {
3111 wxLogTrace( wxS( "KI_TRACE_DRC_RULE_EDITOR" ),
3112 wxS( "[ShowMatches] engine: no-match sample type=%d kiid=%s layers=%s" ),
3113 (int) aItem->Type(), aItem->m_Uuid.AsString(), itemLayers.FmtHex() );
3114 }
3115 };
3116
3117 m_board->RunOnChildren( applyConditionToItemFunc, RECURSE_MODE::RECURSE );
3118
3119 wxLogTrace( wxS( "KI_TRACE_DRC_RULE_EDITOR" ),
3120 wxS( "[ShowMatches] engine stats: total=%zu skipped=%zu noLayer=%zu checked=%zu" ),
3121 totalItems, skippedItems, noLayerItems, checkedItems );
3122
3123 wxLogTrace( wxS( "KI_TRACE_DRC_RULE_EDITOR" ), wxS( "[ShowMatches] engine exit: total=%zu" ), matches.size() );
3124 return matches;
3125}
3126
3127
3128std::vector<BOARD_ITEM*> DRC_ENGINE::GetItemsMatchingRule( const std::shared_ptr<DRC_RULE>& aRule, REPORTER* aReporter )
3129{
3130 std::vector<BOARD_ITEM*> matches;
3131
3132 if( !m_board || !aRule )
3133 return matches;
3134
3135 const bool requiresPairwise = aRule->m_Condition && aRule->m_Condition->RequiresPairItems();
3136 std::set<BOARD_ITEM*> matchedItems;
3137
3138 if( std::shared_ptr<CONNECTIVITY_DATA> connectivity = m_board->GetConnectivity() )
3139 {
3140 if( std::shared_ptr<FROM_TO_CACHE> ftCache = connectivity->GetFromToCache() )
3141 ftCache->Rebuild( m_board );
3142 }
3143
3144 for( const DRC_CONSTRAINT& constraint : aRule->m_Constraints )
3145 {
3146 if( constraint.m_Type == NULL_CONSTRAINT )
3147 continue;
3148
3149 SHOWMATCH_DOMAIN_SPEC domainSpec = getShowMatchDomainSpec( constraint.m_Type );
3150 std::vector<BOARD_ITEM*> primaryItems = collectShowMatchCandidates( m_board, domainSpec.primary );
3151 std::vector<BOARD_ITEM*> secondaryItems;
3152
3153 if( domainSpec.hasSecondary )
3154 secondaryItems = collectShowMatchCandidates( m_board, domainSpec.secondary );
3155
3156 if( requiresPairwise )
3157 {
3158 if( secondaryItems.empty() )
3159 {
3160 for( size_t ii = 0; ii < primaryItems.size(); ++ii )
3161 {
3162 BOARD_ITEM* itemA = primaryItems[ii];
3163
3164 for( size_t jj = ii + 1; jj < primaryItems.size(); ++jj )
3165 {
3166 BOARD_ITEM* itemB = primaryItems[jj];
3167
3168 if( ruleMatchesPair( *aRule, itemA, itemB, constraint.m_Type,
3169 aReporter ? aReporter : m_logReporter ) )
3170 {
3171 matchedItems.insert( itemA );
3172 matchedItems.insert( itemB );
3173 }
3174 }
3175 }
3176 }
3177 else
3178 {
3179 for( BOARD_ITEM* itemA : primaryItems )
3180 {
3181 for( BOARD_ITEM* itemB : secondaryItems )
3182 {
3183 if( itemA == itemB )
3184 continue;
3185
3186 if( ruleMatchesPair( *aRule, itemA, itemB, constraint.m_Type,
3187 aReporter ? aReporter : m_logReporter ) )
3188 {
3189 matchedItems.insert( itemA );
3190 matchedItems.insert( itemB );
3191 }
3192 }
3193 }
3194 }
3195 }
3196 else
3197 {
3198 for( BOARD_ITEM* item : primaryItems )
3199 {
3200 if( ruleMatchesUnary( *aRule, item, constraint.m_Type, aReporter ? aReporter : m_logReporter ) )
3201 matchedItems.insert( item );
3202 }
3203
3204 if( domainSpec.hasSecondary && domainSpec.secondaryUnary )
3205 {
3206 for( BOARD_ITEM* item : secondaryItems )
3207 {
3208 if( ruleMatchesUnary( *aRule, item, constraint.m_Type, aReporter ? aReporter : m_logReporter ) )
3209 matchedItems.insert( item );
3210 }
3211 }
3212 }
3213 }
3214
3215 matches.assign( matchedItems.begin(), matchedItems.end() );
3216
3217 return matches;
3218}
3219
3220
3221int DRC_ENGINE::GetCachedOwnClearance( const BOARD_ITEM* aItem, PCB_LAYER_ID aLayer, wxString* aSource )
3222{
3223 DRC_OWN_CLEARANCE_CACHE_KEY key{ aItem->m_Uuid, aLayer };
3224
3225 // Fast path: check cache with shared (read) lock
3226 {
3227 std::shared_lock<std::shared_mutex> readLock( m_clearanceCacheMutex );
3228
3229 auto it = m_ownClearanceCache.find( key );
3230
3231 if( it != m_ownClearanceCache.end() )
3232 {
3233 // Cache hit. We don't cache the source string since it's rarely requested
3234 // and caching it would add complexity.
3235 return it->second;
3236 }
3237 }
3238
3239 // Cache miss - evaluate the constraint (outside lock to avoid blocking other threads)
3240 DRC_CONSTRAINT_T constraintType = CLEARANCE_CONSTRAINT;
3241
3242 if( aItem->Type() == PCB_PAD_T )
3243 {
3244 const PAD* pad = static_cast<const PAD*>( aItem );
3245
3246 if( pad->GetAttribute() == PAD_ATTRIB::NPTH )
3247 constraintType = HOLE_CLEARANCE_CONSTRAINT;
3248 }
3249
3250 DRC_CONSTRAINT constraint = EvalRules( constraintType, aItem, nullptr, aLayer );
3251
3252 int clearance = 0;
3253
3254 if( constraint.Value().HasMin() )
3255 {
3256 clearance = constraint.Value().Min();
3257
3258 if( aSource )
3259 *aSource = constraint.GetName();
3260 }
3261
3262 // Store in cache with exclusive (write) lock using double-checked locking
3263 {
3264 std::unique_lock<std::shared_mutex> writeLock( m_clearanceCacheMutex );
3265
3266 auto it = m_ownClearanceCache.find( key );
3267
3268 if( it == m_ownClearanceCache.end() )
3270 }
3271
3272 return clearance;
3273}
3274
3275
3277{
3278 std::unique_lock<std::shared_mutex> writeLock( m_clearanceCacheMutex );
3279
3280 if( m_board )
3281 {
3282 LSET copperLayers = m_board->GetEnabledLayers() & LSET::AllCuMask();
3283
3284 for( PCB_LAYER_ID layer : copperLayers.Seq() )
3285 m_ownClearanceCache.erase( DRC_OWN_CLEARANCE_CACHE_KEY{ aUuid, layer } );
3286 }
3287 else
3288 {
3289 std::erase_if( m_ownClearanceCache,
3290 [&aUuid]( const auto& entry )
3291 {
3292 return entry.first.m_uuid == aUuid;
3293 } );
3294 }
3295}
3296
3297
3299{
3300 std::unique_lock<std::shared_mutex> writeLock( m_clearanceCacheMutex );
3301 m_ownClearanceCache.clear();
3302}
3303
3304
3306{
3307 if( !m_board )
3308 return;
3309
3310 // Pre-populate the cache for all connected items to avoid delays during first render.
3311 // We only need to cache copper layers since clearance outlines are only drawn on copper.
3312
3313 LSET copperLayers = m_board->GetEnabledLayers() & LSET::AllCuMask();
3314
3315 using CLEARANCE_MAP = std::unordered_map<DRC_OWN_CLEARANCE_CACHE_KEY, int>;
3316
3317 // Build flat list of (item, layer) pairs to process.
3318 // Estimate size based on tracks + pads * average layers (2 for typical TH pads).
3319 std::vector<std::pair<const BOARD_ITEM*, PCB_LAYER_ID>> itemsToProcess;
3320 size_t estimatedPads = 0;
3321
3322 for( FOOTPRINT* footprint : m_board->Footprints() )
3323 estimatedPads += footprint->Pads().size();
3324
3325 itemsToProcess.reserve( m_board->Tracks().size() + estimatedPads * 2 );
3326
3327 for( PCB_TRACK* track : m_board->Tracks() )
3328 {
3329 if( track->Type() == PCB_VIA_T )
3330 {
3331 for( PCB_LAYER_ID layer : LSET( track->GetLayerSet() & copperLayers ).Seq() )
3332 itemsToProcess.emplace_back( track, layer );
3333 }
3334 else
3335 {
3336 itemsToProcess.emplace_back( track, track->GetLayer() );
3337 }
3338 }
3339
3340 for( FOOTPRINT* footprint : m_board->Footprints() )
3341 {
3342 for( PAD* pad : footprint->Pads() )
3343 {
3344 for( PCB_LAYER_ID layer : LSET( pad->GetLayerSet() & copperLayers ).Seq() )
3345 itemsToProcess.emplace_back( pad, layer );
3346 }
3347 }
3348
3349 if( itemsToProcess.empty() )
3350 return;
3351
3352 {
3353 std::unique_lock<std::shared_mutex> writeLock( m_clearanceCacheMutex );
3354 m_ownClearanceCache.reserve( itemsToProcess.size() );
3355 }
3356
3358
3359 auto processItems =
3360 [this]( size_t aStart, size_t aEnd, const std::vector<std::pair<const BOARD_ITEM*, PCB_LAYER_ID>>& aItems )
3361 -> CLEARANCE_MAP
3362 {
3363 CLEARANCE_MAP localCache;
3364
3365 for( size_t i = aStart; i < aEnd; ++i )
3366 {
3367 const BOARD_ITEM* item = aItems[i].first;
3368 PCB_LAYER_ID layer = aItems[i].second;
3369
3370 DRC_CONSTRAINT_T constraintType = CLEARANCE_CONSTRAINT;
3371
3372 if( item->Type() == PCB_PAD_T )
3373 {
3374 const PAD* pad = static_cast<const PAD*>( item );
3375
3376 if( pad->GetAttribute() == PAD_ATTRIB::NPTH )
3377 constraintType = HOLE_CLEARANCE_CONSTRAINT;
3378 }
3379
3380 DRC_CONSTRAINT constraint = EvalRules( constraintType, item, nullptr, layer );
3381
3382 int clearance = 0;
3383
3384 if( constraint.Value().HasMin() )
3385 clearance = constraint.Value().Min();
3386
3387 localCache[{ item->m_Uuid, layer }] = clearance;
3388 }
3389
3390 return localCache;
3391 };
3392
3393 auto results = tp.submit_blocks( 0, itemsToProcess.size(),
3394 [&]( size_t aStart, size_t aEnd ) -> CLEARANCE_MAP
3395 {
3396 return processItems( aStart, aEnd, itemsToProcess );
3397 } );
3398
3399 // Collect all results first WITHOUT holding the lock to avoid deadlock.
3400 // Worker threads call EvalRules() which needs a read lock on m_clearanceCacheMutex.
3401 // If we held a write lock while calling .get(), workers would block on the read lock
3402 // while we block waiting for them to complete.
3403 std::vector<CLEARANCE_MAP> collectedResults;
3404 collectedResults.reserve( results.size() );
3405
3406 for( size_t i = 0; i < results.size(); ++i )
3407 {
3408 if( results[i].valid() )
3409 collectedResults.push_back( results[i].get() );
3410 }
3411
3412 // Now merge with write lock held, but no blocking on futures
3413 {
3414 std::unique_lock<std::shared_mutex> writeLock( m_clearanceCacheMutex );
3415
3416 for( const auto& localCache : collectedResults )
3417 m_ownClearanceCache.insert( localCache.begin(), localCache.end() );
3418 }
3419}
const char * name
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:128
#define MAXIMUM_CLEARANCE
BOX2< VECTOR2I > BOX2I
Definition box2.h:917
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:985
std::string FmtHex() const
Return a hex string showing contents of this set.
Definition base_set.h:312
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
virtual std::optional< int > GetClearanceOverrides(wxString *aSource) const
Return any clearance overrides set in the "classic" (ie: pre-rule) system.
virtual NETCLASS * GetEffectiveNetClass() const
Return the NETCLASS for this item.
virtual std::optional< int > GetLocalClearance() const
Return any local clearances set in the "classic" (ie: pre-rule) system.
Container for design settings for a BOARD object.
std::shared_ptr< NET_SETTINGS > m_NetSettings
std::map< int, SEVERITY > m_DRCSeverities
int GetDRCEpsilon() const
Return an epsilon which accounts for rounding errors, etc.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:99
virtual bool IsConnected() const
Returns information if the object is derived from BOARD_CONNECTED_ITEM.
Definition board_item.h:187
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
virtual bool IsOnLayer(PCB_LAYER_ID aLayer) const
Test to see if this object is on the given layer.
Definition board_item.h:432
FOOTPRINT * GetParentFootprint() const
virtual LSET GetLayerSet() const
Return a std::bitset of all layers on which the item physically resides.
Definition board_item.h:369
virtual bool IsOnCopperLayer() const
Definition board_item.h:204
virtual bool HasHole() const
Definition board_item.h:222
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
NETINFO_ITEM * FindNet(int aNetcode) const
Search for a net with the given netcode.
Definition board.cpp:2833
void RunOnChildren(const std::function< void(BOARD_ITEM *)> &aFunction, RECURSE_MODE aMode) const override
Invoke a function on all children.
Definition board.cpp:865
constexpr bool Intersects(const BOX2< Vec > &aRect) const
Definition box2.h:314
Represents a single line in a time domain profile track propagation setup.
int GetDiffPairGap() const
int GetWidth() const
PCB_LAYER_ID GetSignalLayer() const
virtual bool Run() override
Discard the board's run-time DRC caches and regenerate them from scratch.
DRC_RULE * GetOptRule() const
Definition drc_rule.h:157
wxString GetName() const
Definition drc_rule.h:162
int m_DisallowFlags
Definition drc_rule.h:207
DRC_RULE * GetMinRule() const
Definition drc_rule.h:156
void SetParentRule(DRC_RULE *aParentRule)
Attribute the constraint, including each min, opt and max value it has, to aParentRule.
Definition drc_rule.h:135
MINOPTMAX< int > & Value()
Definition drc_rule.h:130
const MINOPTMAX< int > & GetValue() const
Definition drc_rule.h:129
ZONE_CONNECTION m_ZoneConnection
Definition drc_rule.h:208
void SetName(const wxString &aName)
Definition drc_rule.h:160
MINOPTMAX< int > m_Value
Definition drc_rule.h:206
DRC_CONSTRAINT_T m_Type
Definition drc_rule.h:205
void SetOptionsFromOther(const DRC_CONSTRAINT &aOther)
Definition drc_rule.h:198
DRC_RULE * GetMaxRule() const
Definition drc_rule.h:158
void SetValueRules(DRC_RULE *aMinRule, DRC_RULE *aOptRule, DRC_RULE *aMaxRule)
Definition drc_rule.h:149
DRC_RULE * GetParentRule() const
Definition drc_rule.h:147
bool m_ImplicitMin
Definition drc_rule.h:210
std::map< DRC_CONSTRAINT_T, std::vector< DRC_ENGINE_CONSTRAINT * > * > m_constraintMap
Definition drc_engine.h:386
DRC_CLEARANCE_BATCH EvalClearanceBatch(const BOARD_ITEM *a, const BOARD_ITEM *b, PCB_LAYER_ID aLayer)
Evaluate all clearance-related constraints in a single batch call.
void AdvanceProgress()
std::shared_ptr< DRC_RULE > createImplicitRule(const wxString &name, DRC_IMPLICIT_SOURCE aImplicitSource)
void RunTests(EDA_UNITS aUnits, bool aReportAllTrackErrors, bool aTestFootprints, BOARD_COMMIT *aCommit=nullptr)
Run the DRC tests.
std::unordered_map< DRC_OWN_CLEARANCE_CACHE_KEY, int > m_ownClearanceCache
Definition drc_engine.h:397
bool m_hasDiffPairClearanceOverrides
Definition drc_engine.h:409
bool m_testFootprints
Definition drc_engine.h:383
void addRule(std::shared_ptr< DRC_RULE > &rule)
Definition drc_engine.h:340
PROGRESS_REPORTER * m_progressReporter
Definition drc_engine.h:390
void ClearClearanceCache()
Clear the entire clearance cache.
void loadRules(const wxFileName &aPath)
Load and parse a rule set from an sexpr text file.
std::vector< DRC_TEST_PROVIDER * > m_testProviders
Definition drc_engine.h:378
REPORTER * m_logReporter
Definition drc_engine.h:389
std::map< DRC_CONSTRAINT_T, std::vector< DRC_ENGINE_CONSTRAINT * > > m_explicitConstraints
Definition drc_engine.h:410
int GetErrorLimit(int error_code)
Return the number of violations of error_code that may still be reported.
void compileRules()
std::unordered_map< wxString, DRC_RULE * > m_netclassClearances
Definition drc_engine.h:402
std::set< int > QueryDistinctConstraints(DRC_CONSTRAINT_T aConstraintId)
bool m_hasGeometryDependentRules
Definition drc_engine.h:408
DS_PROXY_VIEW_ITEM * m_drawingSheet
Definition drc_engine.h:373
NETLIST * m_schematicNetlist
Definition drc_engine.h:374
std::shared_mutex m_clearanceCacheMutex
Definition drc_engine.h:406
bool KeepRefreshing(bool aWait=false)
std::vector< BOARD_ITEM * > GetItemsMatchingRule(const std::shared_ptr< DRC_RULE > &aRule, REPORTER *aReporter=nullptr)
bool HasUserDefinedConstraint(DRC_CONSTRAINT_T aConstraintId)
BOARD * m_board
Definition drc_engine.h:372
bool HasConditionalConstraint(DRC_CONSTRAINT_T aConstraintId)
int GetCachedOwnClearance(const BOARD_ITEM *aItem, PCB_LAYER_ID aLayer, wxString *aSource=nullptr)
Get the cached own clearance for an item on a specific layer.
bool m_reportAllTrackErrors
Definition drc_engine.h:382
bool ReportProgress(double aProgress)
DRC_TEST_PROVIDER * GetTestProvider(const wxString &name) const
bool HasRulesForConstraintType(DRC_CONSTRAINT_T constraintID)
BOARD_DESIGN_SETTINGS * GetDesignSettings() const
Definition drc_engine.h:146
void SetMaxProgress(int aSize)
DRC_ENGINE(BOARD *aBoard=nullptr, BOARD_DESIGN_SETTINGS *aSettings=nullptr)
bool m_hasExplicitClearanceRules
Definition drc_engine.h:407
std::vector< int > m_errorLimits
Definition drc_engine.h:380
bool IsErrorLimitExceeded(int error_code)
void ProcessAssertions(const BOARD_ITEM *a, std::function< void(const DRC_CONSTRAINT *)> aFailureHandler, REPORTER *aReporter=nullptr)
void loadImplicitRules()
DRC_VIOLATION_HANDLER m_violationHandler
Definition drc_engine.h:388
DRC_CONSTRAINT EvalRules(DRC_CONSTRAINT_T aConstraintType, const BOARD_ITEM *a, const BOARD_ITEM *b, PCB_LAYER_ID aLayer, REPORTER *aReporter=nullptr)
std::vector< std::shared_ptr< DRC_RULE > > m_rules
Definition drc_engine.h:376
bool QueryWorstConstraint(DRC_CONSTRAINT_T aRuleId, DRC_CONSTRAINT &aConstraint, bool aUnconditionalOnly=false)
bool IsCancelled() const
bool IsNetTieExclusion(int aTrackNetCode, PCB_LAYER_ID aTrackLayer, const VECTOR2I &aCollisionPos, BOARD_ITEM *aCollidingItem)
Check if the given collision between a track and another item occurs during the track's entry into a ...
virtual ~DRC_ENGINE()
std::vector< BOARD_ITEM * > GetItemsMatchingCondition(const wxString &aExpression, DRC_CONSTRAINT_T aConstraint=ASSERTION_CONSTRAINT, REPORTER *aReporter=nullptr)
Evaluate a DRC condition against all board items and return matches.
void InitEngine(const wxFileName &aRulePath)
Initialize the DRC engine.
DRC_CONSTRAINT EvalZoneConnection(const BOARD_ITEM *a, const BOARD_ITEM *b, PCB_LAYER_ID aLayer, REPORTER *aReporter=nullptr)
static int MatchDpSuffix(const wxString &aNetName, wxString &aComplementNet, wxString &aBaseDpName)
Check if the given net is a diff pair, returning its polarity and complement if so.
bool ReportPhase(const wxString &aMessage)
static bool IsNetADiffPair(BOARD *aBoard, const NETINFO_ITEM *aNet, int &aNetP, int &aNetN)
void InitializeClearanceCache()
Initialize the clearance cache for all items on the board.
bool m_rulesValid
Definition drc_engine.h:377
void InvalidateClearanceCache(const KIID &aUuid)
Invalidate the clearance cache for a specific item.
BOARD_DESIGN_SETTINGS * m_designSettings
Definition drc_engine.h:371
bool HasUserDefinedPhysicalConstraint()
void ReportViolation(const std::shared_ptr< DRC_ITEM > &aItem, const VECTOR2I &aPos, int aMarkerLayer, const std::function< void(PCB_MARKER *)> &aPathGenerator={})
std::mutex m_errorLimitsMutex
Definition drc_engine.h:381
void Parse(std::vector< std::shared_ptr< DRC_RULE > > &aRules, REPORTER *aReporter)
bool Compile(REPORTER *aReporter, int aSourceLine=0, int aSourceOffset=0)
wxString GetExpression() const
bool EvaluateFor(const BOARD_ITEM *aItemA, const BOARD_ITEM *aItemB, int aConstraint, PCB_LAYER_ID aLayer, REPORTER *aReporter=nullptr)
DRC_RULE_CONDITION * m_Condition
Definition drc_rule.h:84
bool IsImplicit() const
Definition drc_rule.h:70
LSET m_LayerCondition
Definition drc_rule.h:83
std::vector< DRC_CONSTRAINT > m_Constraints
Definition drc_rule.h:85
wxString m_Name
Definition drc_rule.h:80
wxString m_LayerSource
Definition drc_rule.h:82
wxString m_ImplicitNetclass
Netclass named by an implicit netclass rule's condition.
Definition drc_rule.h:81
DRC_IMPLICIT_SOURCE GetImplicitSource() const
Definition drc_rule.h:74
std::vector< DRC_TEST_PROVIDER * > GetShowMatchesProviders() const
static DRC_SHOWMATCHES_PROVIDER_REGISTRY & Instance()
std::vector< DRC_TEST_PROVIDER * > GetTestProviders() const
static DRC_TEST_PROVIDER_REGISTRY & Instance()
Represent a DRC "provider" which runs some DRC functions over a BOARD and spits out DRC_ITEM and posi...
void SetDRCEngine(DRC_ENGINE *engine)
virtual const BOX2I GetBoundingBox() const
Return the orthogonal bounding box of this object for display purposes.
Definition eda_item.cpp:280
virtual wxString GetItemDescription(UNITS_PROVIDER *aUnitsProvider, bool aFull) const
Return a user-visible description string of this item.
Definition eda_item.cpp:314
const KIID m_Uuid
Definition eda_item.h:599
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
EDA_ITEM_FLAGS GetFlags() const
Definition eda_item.h:167
A LINE_READER that reads from an open file.
Definition richio.h:157
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition richio.cpp:202
ZONE_CONNECTION GetLocalZoneConnection() const
Definition footprint.h:491
std::map< wxString, int > MapPadNumbersToNetTieGroups() const
std::deque< PAD * > & Pads()
Definition footprint.h:355
const std::set< int > & GetNetTieCache(const BOARD_ITEM *aItem) const
Get the set of net codes that are allowed to connect to a footprint item.
Definition footprint.h:682
bool IsNetTie() const
Definition footprint.h:525
wxString GetItemDescription(UNITS_PROVIDER *aUnitsProvider, bool aFull) const override
Return a user-visible description string of this item.
const SHAPE_POLY_SET & GetCourtyard(PCB_LAYER_ID aLayer) const
Used in DRC to test the courtyard area (a complex polygon).
Definition kiid.h:46
wxString AsString() const
Definition kiid.cpp:264
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
static const LSET & FrontMask()
Return a mask holding all technical layers and the external CU layer on front side.
Definition lset.cpp:718
static const LSET & AllCuMask()
return AllCuMask( MAX_CU_LAYERS );
Definition lset.cpp:604
static const LSET & BackMask()
Return a mask holding all technical layers and the external CU layer on back side.
Definition lset.cpp:725
LSEQ Seq(const LSEQ &aSequence) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition lset.cpp:309
static wxString Name(PCB_LAYER_ID aLayerId)
Return the fixed name association with aLayerId.
Definition lset.cpp:184
T Min() const
Definition minoptmax.h:29
void SetMin(T v)
Definition minoptmax.h:41
void SetOpt(T v)
Definition minoptmax.h:43
bool HasMin() const
Definition minoptmax.h:37
void SetMax(T v)
Definition minoptmax.h:42
A collection of nets and the parameters used to route or test these nets.
Definition netclass.h:44
const wxString GetName() const
Gets the name of this (maybe aggregate) netclass in a format for internal usage or for export to exte...
Definition netclass.cpp:382
bool NameEquals(const wxString &aName) const
Compares a name with GetName() without constructing the aggregate name.
Definition netclass.cpp:409
Handle the data for a net.
Definition netinfo.h:50
const wxString & GetNetname() const
Definition netinfo.h:110
int GetNetCode() const
Definition netinfo.h:104
const std::map< wxString, std::shared_ptr< NETCLASS > > & GetCompositeNetclasses() const
Gets all composite (multiple assignment / missing defaults) netclasses.
const std::map< wxString, std::shared_ptr< NETCLASS > > & GetNetclasses() const
Gets all netclasses.
std::shared_ptr< NETCLASS > GetDefaultNetclass() const
Gets the default netclass for the project.
Definition pad.h:61
std::optional< int > GetLocalSolderMaskMargin() const
Definition pcb_shape.h:352
A small class to help profiling.
Definition profile.h:46
void Stop()
Save the time when this function was called, and set the counter stane to stop.
Definition profile.h:86
double msecs(bool aSinceLast=false)
Definition profile.h:148
Container for project specific data.
Definition project.h:63
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
virtual bool HasMessageOfSeverity(int aSeverityMask) const
Returns true if the reporter has one or more messages matching the specified severity mask.
Definition reporter.h:152
bool IsEmpty() const
Return true if the set is empty (no polygons at all)
const EDA_IU_SCALE & GetIuScale() const
wxString MessageTextFromUnscaledValue(double aValue, bool aAddUnitLabel=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
UNITS_PROVIDER(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits)
wxString MessageTextFromValue(double aValue, bool aAddUnitLabel=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
A lower-precision version of StringFromValue().
void SetUserUnits(EDA_UNITS aUnits)
Handle a list of polygons defining a copper zone.
Definition zone.h:70
wxString GetItemDescription(UNITS_PROVIDER *aUnitsProvider, bool aFull) const override
Return a user-visible description string of this item.
Definition zone.cpp:1539
bool GetIsRuleArea() const
Accessors to parameters used in Rule Area zones:
Definition zone.h:834
bool GetDoNotAllowVias() const
Definition zone.h:852
bool GetDoNotAllowPads() const
Definition zone.h:854
const BOX2I GetBoundingBox() const override
Definition zone.cpp:824
bool GetDoNotAllowTracks() const
Definition zone.h:853
const wxString & GetZoneName() const
Definition zone.h:160
int GetMinThickness() const
Definition zone.h:315
ZONE_CONNECTION GetPadConnection() const
Definition zone.h:312
int GetThermalReliefSpokeWidth() const
Definition zone.h:259
bool GetDoNotAllowFootprints() const
Definition zone.h:855
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition zone.h:133
bool HasKeepoutParametersSet() const
Accessor to determine if any keepout parameters are set.
Definition zone.h:825
bool GetDoNotAllowZoneFills() const
Definition zone.h:851
int GetThermalReliefGap() const
Definition zone.h:248
wxString ExpandTextVars(const wxString &aSource, const PROJECT *aProject, RESOLUTION_CONTEXT aContext)
Definition common.cpp:60
wxString DescribeRef(const wxString &aRef)
Returns a user-visible HTML string describing a footprint reference designator.
Definition common.cpp:508
@ INTERNAL
Definition common.h:96
DRC_IMPLICIT_SOURCE
@ DRC_DISALLOW_PADS
@ DRC_DISALLOW_BURIED_VIAS
@ DRC_DISALLOW_BLIND_VIAS
@ DRC_DISALLOW_TEXTS
@ DRC_DISALLOW_ZONES
@ DRC_DISALLOW_HOLES
@ DRC_DISALLOW_GRAPHICS
@ DRC_DISALLOW_THROUGH_VIAS
@ DRC_DISALLOW_FOOTPRINTS
@ DRC_DISALLOW_TRACKS
@ DRC_DISALLOW_MICRO_VIAS
@ ANNULAR_WIDTH_CONSTRAINT
@ COURTYARD_CLEARANCE_CONSTRAINT
@ VIA_DIAMETER_CONSTRAINT
@ ZONE_CONNECTION_CONSTRAINT
@ DIFF_PAIR_GAP_CONSTRAINT
@ NET_CHAIN_LENGTH_CONSTRAINT
@ SOLDER_MASK_SLIVER_CONSTRAINT
@ NET_CHAIN_STUB_LENGTH_CONSTRAINT
@ DISALLOW_CONSTRAINT
@ TRACK_WIDTH_CONSTRAINT
@ SILK_CLEARANCE_CONSTRAINT
@ EDGE_CLEARANCE_CONSTRAINT
@ MIN_RESOLVED_SPOKES_CONSTRAINT
@ TRACK_SEGMENT_LENGTH_CONSTRAINT
@ HOLE_ASPECT_RATIO_CONSTRAINT
@ LENGTH_CONSTRAINT
@ VIA_COUNT_CONSTRAINT
@ NET_CHAIN_RETURN_PATH_CONSTRAINT
@ PHYSICAL_HOLE_CLEARANCE_CONSTRAINT
@ CLEARANCE_CONSTRAINT
@ NULL_CONSTRAINT
@ THERMAL_SPOKE_WIDTH_CONSTRAINT
@ CONNECTION_WIDTH_CONSTRAINT
@ THERMAL_RELIEF_GAP_CONSTRAINT
@ MAX_UNCOUPLED_CONSTRAINT
@ ASSERTION_CONSTRAINT
@ SKEW_CONSTRAINT
@ LINE_WIDTH_CONSTRAINT
@ MICROVIA_STACK_DEPTH_CONSTRAINT
@ HOLE_CLEARANCE_CONSTRAINT
@ SOLDER_PASTE_ABS_MARGIN_CONSTRAINT
@ SOLDER_MASK_EXPANSION_CONSTRAINT
@ TRACK_ANGLE_CONSTRAINT
@ HOLE_SIZE_CONSTRAINT
@ TEXT_HEIGHT_CONSTRAINT
@ CREEPAGE_CONSTRAINT
@ PHYSICAL_CLEARANCE_CONSTRAINT
@ SOLDER_PASTE_REL_MARGIN_CONSTRAINT
@ HOLE_TO_HOLE_CONSTRAINT
constexpr int DRC_DISALLOW_VIAS
void drcPrintDebugMessage(int level, const wxString &msg, const char *function, int line)
#define EXTENDED_ERROR_LIMIT
static bool isKeepoutZone(const BOARD_ITEM *aItem, bool aCheckFlags)
#define ERROR_LIMIT
@ DRCE_TUNING_PROFILE_IMPLICIT_RULES
Definition drc_item.h:132
@ DRCE_UNCONNECTED_ITEMS
Definition drc_item.h:48
@ DRCE_CLEARANCE
Definition drc_item.h:52
@ DRCE_FIRST
Definition drc_item.h:47
@ DRCE_LAST
Definition drc_item.h:145
bool IsComponentClassSelector(const wxString &aToken)
${Class:X} inside a DRC rule is a component-class selector consumed by testFootprintSelector(),...
Definition drc_rule.cpp:32
#define _(s)
@ RECURSE
Definition eda_item.h:51
#define HOLE_PROXY
Indicates the BOARD_ITEM is a proxy for its hole.
EDA_UNITS
Definition eda_units.h:45
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
static const wxChar * traceDrcProfile
Flag to enable DRC profile timing logging.
bool IsPcbLayer(int aLayer)
Test whether a layer is a valid layer for Pcbnew.
Definition layer_ids.h:733
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ F_CrtYd
Definition layer_ids.h:112
@ B_Adhes
Definition layer_ids.h:99
@ Edge_Cuts
Definition layer_ids.h:108
@ F_Paste
Definition layer_ids.h:100
@ F_Adhes
Definition layer_ids.h:98
@ B_Mask
Definition layer_ids.h:94
@ F_Mask
Definition layer_ids.h:93
@ B_Paste
Definition layer_ids.h:101
@ F_Fab
Definition layer_ids.h:115
@ Margin
Definition layer_ids.h:109
@ F_SilkS
Definition layer_ids.h:96
@ B_CrtYd
Definition layer_ids.h:111
@ UNDEFINED_LAYER
Definition layer_ids.h:57
@ B_SilkS
Definition layer_ids.h:97
@ B_Fab
Definition layer_ids.h:114
#define REPORT(msg)
std::optional< wxString > EscapeStringLiteral(const wxString &aText)
Escape text for use between the quotes of an expression string.
@ NPTH
like PAD_PTH, but not plated mechanical use only, no connection allowed
Definition padstack.h:104
@ PTH
Plated through hole pad.
Definition padstack.h:99
std::deque< PAD * > PADS
std::deque< FOOTPRINT * > FOOTPRINTS
@ RPT_SEVERITY_ERROR
const double epsilon
std::vector< FAB_LAYER_COLOR > dummy
wxString EscapeHTML(const wxString &aString)
Return a new wxString escaped for embedding in HTML.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Batch result for clearance-related constraints to reduce per-query overhead during PNS routing.
Definition drc_engine.h:109
std::shared_ptr< DRC_RULE > parentRule
Definition drc_engine.h:358
DRC_RULE_CONDITION * condition
Definition drc_engine.h:357
Cache key for own clearance lookups, combining item UUID and layer.
Definition drc_engine.h:39
A filename or source description, a problem input line, a line number, a byte offset,...
Represents a single line in the tuning profile configuration grid.
PROFILE_TYPE m_Type
std::vector< DELAY_PROFILE_TRACK_PROPAGATION_ENTRY > m_TrackPropagationEntries
static const long long MM
int clearance
wxString result
Test unit parsing edge cases and error handling.
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
constexpr KICAD_T BaseType(const KICAD_T aType)
Return the underlying type of the given type.
Definition typeinfo.h:261
@ PCB_CONSTRAINT_T
a geometric constraint between board items
Definition typeinfo.h:237
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition typeinfo.h:80
@ PCB_GENERATOR_T
class PCB_GENERATOR, generator on a layer
Definition typeinfo.h:83
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:89
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition typeinfo.h:103
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition typeinfo.h:85
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition typeinfo.h:100
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition typeinfo.h:84
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition typeinfo.h:82
@ PCB_BARCODE_T
class PCB_BARCODE, a barcode (graphic item)
Definition typeinfo.h:93
@ PCB_TABLECELL_T
class PCB_TABLECELL, PCB_TEXTBOX for use in tables
Definition typeinfo.h:87
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition typeinfo.h:78
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition typeinfo.h:79
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition typeinfo.h:90
@ PCB_DIMENSION_T
class PCB_DIMENSION_BASE: abstract dimension meta-type
Definition typeinfo.h:92
@ PCB_TABLE_T
class PCB_TABLE, table of PCB_TABLECELLs
Definition typeinfo.h:86
@ PCB_NETINFO_T
class NETINFO_ITEM, a description of a net
Definition typeinfo.h:102
@ PCB_STACKUP_TABLE_T
class PCB_STACKUP_TABLE, a live stackup table derived from PCB_TABLE
Definition typeinfo.h:242
@ PCB_LOCATE_HOLE_T
Definition typeinfo.h:123
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:88
@ PCB_DRILL_CHART_T
class PCB_DRILL_CHART, a live drill chart derived from PCB_TABLE
Definition typeinfo.h:239
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708
wxString PrintZoneConnection(ZONE_CONNECTION aConnection)
Definition zones.h:52
ZONE_CONNECTION
How pads are covered by copper in zone.
Definition zones.h:43
@ THERMAL
Use thermal relief for pads.
Definition zones.h:46
@ THT_THERMAL
Thermal relief only for THT pads.
Definition zones.h:48
@ FULL
pads are covered by copper
Definition zones.h:47