KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_test_provider_copper_clearance.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <common.h>
22#include <footprint.h>
23#include <layer_range.h>
24#include <pcb_shape.h>
25#include <pad.h>
26#include <pcb_track.h>
27#include <thread_pool.h>
28#include <zone.h>
29
30#include <geometry/seg.h>
34
35#include <drc/drc_engine.h>
36#include <drc/drc_rtree.h>
37#include <drc/drc_item.h>
38#include <drc/drc_rule.h>
41#include <drc/drc_zone_index.h>
42#include <pcb_dimension.h>
43
44#include <future>
45#include <optional>
46#include <set>
47
48/*
49 Copper clearance test. Checks all copper items (pads, vias, tracks, drawings, zones) for their
50 electrical clearance.
51
52 Errors generated:
53 - DRCE_CLEARANCE
54 - DRCE_HOLE_CLEARANCE
55 - DRCE_TRACKS_CROSSING
56 - DRCE_SHORTING_ITEMS
57*/
58
60{
61public:
66
68
69 virtual bool Run() override;
70
71 virtual const wxString GetName() const override { return wxT( "clearance" ); };
72
73private:
84 bool testSingleLayerItemAgainstItem( BOARD_ITEM* item, SHAPE* itemShape, PCB_LAYER_ID layer,
85 BOARD_ITEM* other, SHAPE* aPreparedOther );
86
88
89 bool testPadAgainstItem( PAD* pad, const std::shared_ptr<SHAPE>& padShape, PCB_LAYER_ID layer,
90 BOARD_ITEM* other, SHAPE* aPreparedOther );
91
92 void testPadClearances();
93
95 void collectZonesNear( BOARD_ITEM* aItem, PCB_LAYER_ID aLayer, std::vector<ZONE*>& aZones );
96
98
99 void testZonesToZones();
100
102
103 void testItemAgainstZone( BOARD_ITEM* aItem, ZONE* aZone, PCB_LAYER_ID aLayer );
104
105 void testKnockoutTextAgainstZone( BOARD_ITEM* aText, NETINFO_ITEM** aInheritedNet, ZONE* aZone );
106
107 int sub_e( int aClearance )
108 {
109 return std::max( 0, aClearance - m_drcEpsilon );
110 };
111
112private:
115};
116
117
119{
120 m_board = m_drcEngine->GetBoard();
121
122 if( m_board->m_DRCMaxClearance <= 0 )
123 {
124 REPORT_AUX( wxT( "No Clearance constraints found. Tests not run." ) );
125 return true; // continue with other tests
126 }
127
128 m_drcEpsilon = m_board->GetDesignSettings().GetDRCEpsilon();
129
130 struct ZONE_INDEX_GUARD
131 {
132 ~ZONE_INDEX_GUARD() { index.Clear(); }
134 } zoneIndexGuard{ m_zoneIndex };
135
136 m_zoneIndex.Build( m_board->m_DRCCopperZonesByLayer );
137
138 if( !m_drcEngine->IsErrorLimitExceeded( DRCE_CLEARANCE ) )
139 {
140 if( !reportPhase( _( "Checking track & via clearances..." ) ) )
141 return false; // DRC cancelled
142
144 }
145 else if( !m_drcEngine->IsErrorLimitExceeded( DRCE_HOLE_CLEARANCE ) )
146 {
147 if( !reportPhase( _( "Checking hole clearances..." ) ) )
148 return false; // DRC cancelled
149
151 }
152
153 if( !m_drcEngine->IsErrorLimitExceeded( DRCE_CLEARANCE ) )
154 {
155 if( !reportPhase( _( "Checking pad clearances..." ) ) )
156 return false; // DRC cancelled
157
159 }
160 else if( !m_drcEngine->IsErrorLimitExceeded( DRCE_SHORTING_ITEMS )
161 || !m_drcEngine->IsErrorLimitExceeded( DRCE_HOLE_CLEARANCE ) )
162 {
163 if( !reportPhase( _( "Checking pads..." ) ) )
164 return false; // DRC cancelled
165
167 }
168
169 if( !m_drcEngine->IsErrorLimitExceeded( DRCE_CLEARANCE ) )
170 {
171 if( !reportPhase( _( "Checking copper graphic clearances..." ) ) )
172 return false; // DRC cancelled
173
175 }
176 else if( !m_drcEngine->IsErrorLimitExceeded( DRCE_HOLE_CLEARANCE ) )
177 {
178 if( !reportPhase( _( "Checking copper graphic hole clearances..." ) ) )
179 return false; // DRC cancelled
180
182 }
183
184 if( !m_drcEngine->IsErrorLimitExceeded( DRCE_CLEARANCE ) )
185 {
186 if( !reportPhase( _( "Checking copper zone clearances..." ) ) )
187 return false; // DRC cancelled
188
190
191 if( !reportPhase( _( "Checking teardrop clearances..." ) ) )
192 return false; // DRC cancelled
193
195 }
196
197 return !m_drcEngine->IsCancelled();
198}
199
200
202 std::vector<ZONE*>& aZones )
203{
204 aZones.clear();
205
206 // Tested before the bounding box, which costs an effective-shape build for arcs
207 if( !m_zoneIndex.HasLayer( aLayer ) )
208 return;
209
210 BOX2I query = aItem->GetBoundingBox();
211 query.Inflate( m_board->m_DRCMaxClearance );
212
213 m_zoneIndex.Query( aLayer, query, aZones );
214}
215
216
218 PCB_LAYER_ID layer, BOARD_ITEM* other,
219 SHAPE* aPreparedOther )
220{
221 bool testClearance = !m_drcEngine->IsErrorLimitExceeded( DRCE_CLEARANCE );
222 bool testShorting = !m_drcEngine->IsErrorLimitExceeded( DRCE_SHORTING_ITEMS );
223 bool testHoles = !m_drcEngine->IsErrorLimitExceeded( DRCE_HOLE_CLEARANCE );
224 DRC_CONSTRAINT constraint;
225 int clearance = -1;
226 int actual;
227 VECTOR2I pos;
228 bool has_error = false;
229 NETINFO_ITEM* itemNet = nullptr;
230 NETINFO_ITEM* otherNet = nullptr;
231
232 if( item->IsConnected() )
233 itemNet = static_cast<BOARD_CONNECTED_ITEM*>( item )->GetNet();
234
235 if( other->IsConnected() )
236 otherNet = static_cast<BOARD_CONNECTED_ITEM*>( other )->GetNet();
237
238 if( itemNet == otherNet )
239 testClearance = testShorting = false;
240
241 std::shared_ptr<SHAPE> otherShape_shared_ptr;
242
243 if( other->Type() == PCB_PAD_T )
244 {
245 PAD* pad = static_cast<PAD*>( other );
246
247 if( !pad->FlashLayer( layer ) )
248 {
249 if( pad->GetAttribute() == PAD_ATTRIB::NPTH )
250 testClearance = testShorting = false;
251
252 otherShape_shared_ptr = pad->GetEffectiveHoleShape( layer, HOLE_CLEARANCE_CONSTRAINT );
253 }
254 }
255 else if( other->Type() == PCB_VIA_T )
256 {
257 PCB_VIA* via = static_cast<PCB_VIA*>( other );
258
259 if( !via->FlashLayer( layer ) )
260 otherShape_shared_ptr = via->GetEffectiveHoleShape( layer, HOLE_CLEARANCE_CONSTRAINT );
261 }
262
263 // The explicit hole shape, where one was selected above, still outranks the prepared parent
264 SHAPE* otherShape = otherShape_shared_ptr ? otherShape_shared_ptr.get() : aPreparedOther;
265
266 // Collide (and generate violations) based on a well-defined order so that exclusion checking
267 // against previously-generated violations will work.
268 if( item->m_Uuid > other->m_Uuid )
269 {
270 std::swap( item, other );
271 std::swap( itemShape, otherShape );
272 std::swap( itemNet, otherNet );
273 }
274
275 if( testClearance || testShorting )
276 {
277 constraint = m_drcEngine->EvalRules( CLEARANCE_CONSTRAINT, item, other, layer );
278 clearance = constraint.GetValue().Min();
279 }
280
281 if( constraint.GetSeverity() != RPT_SEVERITY_IGNORE && clearance > 0 )
282 {
283 // Special processing for track:track intersections
284 if( item->Type() == PCB_TRACE_T && other->Type() == PCB_TRACE_T )
285 {
286 PCB_TRACK* track = static_cast<PCB_TRACK*>( item );
287 PCB_TRACK* otherTrack = static_cast<PCB_TRACK*>( other );
288
289 SEG trackSeg( track->GetStart(), track->GetEnd() );
290 SEG otherSeg( otherTrack->GetStart(), otherTrack->GetEnd() );
291
292 if( OPT_VECTOR2I intersection = trackSeg.Intersect( otherSeg ) )
293 {
294 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_TRACKS_CROSSING );
295 drcItem->SetItems( item, other );
296 drcItem->SetViolatingRule( constraint.GetParentRule() );
297 reportTwoPointGeometry( drcItem, *intersection, *intersection, *intersection, layer );
298 return false;
299 }
300 }
301
302 if( itemShape->Collide( otherShape, sub_e( clearance ), &actual, &pos ) )
303 {
304 if( ( itemNet && m_drcEngine->IsNetTieExclusion( itemNet->GetNetCode(), layer, pos, other ) )
305 || ( otherNet && m_drcEngine->IsNetTieExclusion( otherNet->GetNetCode(), layer, pos, item ) ) )
306 {
307 // Collision occurred as a copper item entered a pad marked as a net-tie. We allow
308 // these regardless of which side DRC happened to test first.
309 }
310 else if( actual == 0 && otherNet && testShorting )
311 {
312 int itemNetCode = itemNet ? itemNet->GetNetCode() : 0;
313 int otherNetCode = otherNet ? otherNet->GetNetCode() : 0;
314
315 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_SHORTING_ITEMS );
316 drcItem->SetErrorDetail( wxString::Format( _( "(nets %s and %s)" ),
317 itemNetCode ? itemNet->GetNetname() : _( "<no net>" ),
318 otherNetCode ? otherNet->GetNetname() : _( "<no net>" ) ) );
319 drcItem->SetItems( item, other );
320 reportTwoPointGeometry( drcItem, pos, pos, pos, layer );
321 has_error = true;
322
323 if( !m_drcEngine->GetReportAllTrackErrors() )
324 return false;
325 }
326 else if( testClearance )
327 {
328 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_CLEARANCE );
329 drcItem->SetErrorDetail( formatMsg( _( "(%s clearance %s; actual %s)" ),
330 constraint.GetName(),
331 clearance,
332 actual ) );
333 drcItem->SetItems( item, other );
334 drcItem->SetViolatingRule( constraint.GetParentRule() );
335 reportTwoShapeGeometry( drcItem, pos, itemShape, otherShape, layer, actual );
336 has_error = true;
337
338 if( !m_drcEngine->GetReportAllTrackErrors() )
339 return false;
340 }
341 }
342 }
343
344 if( testHoles && ( item->HasHole() || other->HasHole() ) )
345 {
346 std::array<BOARD_ITEM*, 2> a{ item, other };
347 std::array<BOARD_ITEM*, 2> b{ other, item };
348 std::array<NETINFO_ITEM*, 2> b_net{ otherNet, itemNet };
349 std::array<SHAPE*, 2> a_shape{ itemShape, otherShape };
350
351 for( size_t ii = 0; ii < 2; ++ii )
352 {
353 std::shared_ptr<SHAPE_SEGMENT> holeShape;
354
355 if( b[ii]->Type() == PCB_VIA_T )
356 {
357 if( b[ii]->GetLayerSet().Contains( layer ) )
358 holeShape = b[ii]->GetEffectiveHoleShape( layer, HOLE_CLEARANCE_CONSTRAINT );
359 else
360 continue;
361 }
362 else
363 {
364 if( b[ii]->HasHole() )
365 holeShape = b[ii]->GetEffectiveHoleShape( layer, HOLE_CLEARANCE_CONSTRAINT );
366 else
367 continue;
368 }
369
370 const int netcode = b_net[ii] ? b_net[ii]->GetNetCode() : 0;
371
372 // Footprint-internal net-tie pads can be left unassigned, so net code 0 must be
373 // tested too. For example, a net tie that connects across two layers by a PTH
374 // pad that isn't connected to the external nets directly.
375 if( m_drcEngine->IsNetTieExclusion( netcode, layer, holeShape->Centre(), a[ii] ) )
376 continue;
377
378 constraint = m_drcEngine->EvalRules( HOLE_CLEARANCE_CONSTRAINT, b[ii], a[ii], layer );
379 clearance = constraint.GetValue().Min();
380
381 // Test for hole to item clearance even if clearance is 0, because the item cannot be
382 // inside (or intersect) the hole.
383 if( constraint.GetSeverity() != RPT_SEVERITY_IGNORE )
384 {
385 if( a_shape[ii]->Collide( holeShape.get(), sub_e( clearance ), &actual, &pos ) )
386 {
387 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_HOLE_CLEARANCE );
388 drcItem->SetErrorDetail( formatMsg( clearance ? _( "(%s clearance %s; actual %s)" )
389 : _( "(%s clearance %s; actual < 0)" ),
390 constraint.GetName(),
391 clearance,
392 actual ) );
393 drcItem->SetItems( a[ii], b[ii] );
394 drcItem->SetViolatingRule( constraint.GetParentRule() );
395 reportTwoShapeGeometry( drcItem, pos, a_shape[ii], holeShape.get(), layer, actual );
396 return false;
397 }
398 }
399 }
400 }
401
402 return !has_error;
403}
404
405
407 PCB_LAYER_ID aLayer )
408{
409 if( !aZone->GetLayerSet().test( aLayer ) )
410 return;
411
412 if( aZone->GetNetCode() && aItem->IsConnected() )
413 {
414 if( aZone->GetNetCode() == static_cast<BOARD_CONNECTED_ITEM*>( aItem )->GetNetCode() )
415 return;
416 }
417
418 BOX2I itemBBox = aItem->GetBoundingBox();
419 BOX2I worstCaseBBox = itemBBox;
420
421 worstCaseBBox.Inflate( m_board->m_DRCMaxClearance );
422
423 if( !worstCaseBBox.Intersects( aZone->GetBoundingBox() ) )
424 return;
425
426 FOOTPRINT* parentFP = aItem->GetParentFootprint();
427
428 // Ignore graphic items which implement a net-tie to the zone's net on the layer being tested.
429 if( parentFP && parentFP->IsNetTie() && dynamic_cast<PCB_SHAPE*>( aItem ) )
430 {
431 std::set<PAD*> allowedNetTiePads;
432
433 for( PAD* pad : parentFP->Pads() )
434 {
435 if( pad->GetNetCode() == aZone->GetNetCode() && aZone->GetNetCode() != 0 )
436 {
437 if( pad->IsOnLayer( aLayer ) )
438 allowedNetTiePads.insert( pad );
439
440 for( PAD* other : parentFP->GetNetTiePads( pad ) )
441 {
442 if( other->IsOnLayer( aLayer ) )
443 allowedNetTiePads.insert( other );
444 }
445 }
446 }
447
448 if( !allowedNetTiePads.empty() )
449 {
450 std::shared_ptr<SHAPE> itemShape = aItem->GetEffectiveShape();
451
452 for( PAD* pad : allowedNetTiePads )
453 {
454 if( pad->GetBoundingBox( aLayer ).Intersects( itemBBox )
455 && pad->GetEffectiveShape( aLayer )->Collide( itemShape.get() ) )
456 {
457 return;
458 }
459 }
460 }
461 }
462
463 bool testClearance = !m_drcEngine->IsErrorLimitExceeded( DRCE_CLEARANCE );
464 bool testHoles = !m_drcEngine->IsErrorLimitExceeded( DRCE_HOLE_CLEARANCE );
465
466 if( !testClearance && !testHoles )
467 return;
468
469 DRC_RTREE* zoneTree = m_board->GetCopperZoneRTree( aZone );
470
471 if( !zoneTree )
472 return;
473
474 DRC_CONSTRAINT constraint;
475 int clearance = -1;
476 int actual;
477 VECTOR2I pos;
478
479 if( aItem->Type() == PCB_PAD_T )
480 {
481 PAD* pad = static_cast<PAD*>( aItem );
482 bool flashedPad = pad->FlashLayer( aLayer );
483 bool platedHole = pad->HasHole() && pad->GetAttribute() == PAD_ATTRIB::PTH;
484
485 if( !flashedPad && !platedHole )
486 testClearance = false;
487 }
488
489 if( testClearance )
490 {
491 constraint = m_drcEngine->EvalRules( CLEARANCE_CONSTRAINT, aItem, aZone, aLayer );
492 clearance = constraint.GetValue().Min();
493 }
494
495 if( constraint.GetSeverity() != RPT_SEVERITY_IGNORE && clearance > 0 )
496 {
497 std::shared_ptr<SHAPE> itemShape = aItem->GetEffectiveShape( aLayer, FLASHING::DEFAULT );
498
499 if( zoneTree->QueryColliding( itemBBox, itemShape.get(), aLayer, sub_e( clearance ), &actual, &pos ) )
500 {
501 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_CLEARANCE );
502 drcItem->SetErrorDetail( formatMsg( _( "(%s clearance %s; actual %s)" ),
503 constraint.GetName(),
504 clearance,
505 actual ) );
506 drcItem->SetItems( aItem, aZone );
507 drcItem->SetViolatingRule( constraint.GetParentRule() );
508 reportTwoItemGeometry( drcItem, pos, aItem, aZone, aLayer, actual );
509 }
510 }
511
512 if( testHoles && aItem->HasHole() )
513 {
514 std::shared_ptr<SHAPE_SEGMENT> holeShape;
515
516 if( aItem->Type() == PCB_VIA_T )
517 {
518 if( aItem->GetLayerSet().Contains( aLayer ) )
519 holeShape = aItem->GetEffectiveHoleShape( aLayer, HOLE_CLEARANCE_CONSTRAINT );
520 }
521 else
522 {
523 holeShape = aItem->GetEffectiveHoleShape( aLayer, HOLE_CLEARANCE_CONSTRAINT );
524 }
525
526 if( holeShape )
527 {
528 constraint = m_drcEngine->EvalRules( HOLE_CLEARANCE_CONSTRAINT, aItem, aZone, aLayer );
529 clearance = constraint.GetValue().Min();
530
531 if( constraint.GetSeverity() != RPT_SEVERITY_IGNORE && clearance > 0 )
532 {
533 if( zoneTree->QueryColliding( itemBBox, holeShape.get(), aLayer, sub_e( clearance ), &actual, &pos ) )
534 {
535 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_HOLE_CLEARANCE );
536 drcItem->SetErrorDetail( formatMsg( _( "(%s clearance %s; actual %s)" ),
537 constraint.GetName(),
538 clearance,
539 actual ) );
540 drcItem->SetItems( aItem, aZone );
541 drcItem->SetViolatingRule( constraint.GetParentRule() );
542
543 std::shared_ptr<SHAPE> zoneShape = aZone->GetEffectiveShape( aLayer );
544 reportTwoShapeGeometry( drcItem, pos, holeShape.get(), zoneShape.get(), aLayer, actual );
545 }
546 }
547 }
548 }
549}
550
551
552/*
553 * We have to special-case knockout text as it's most often knocked-out of a zone, so it's
554 * presumed to collide with one. However, if it collides with more than one, and they have
555 * different nets, then we have a short.
556 */
558 NETINFO_ITEM** aInheritedNet,
559 ZONE* aZone )
560{
561 bool testClearance = !m_drcEngine->IsErrorLimitExceeded( DRCE_CLEARANCE );
562 bool testShorts = !m_drcEngine->IsErrorLimitExceeded( DRCE_SHORTING_ITEMS );
563
564 if( !testClearance && !testShorts )
565 return;
566
567 PCB_LAYER_ID layer = aText->GetLayer();
568
569 if( !aZone->GetLayerSet().test( layer ) )
570 return;
571
572 BOX2I itemBBox = aText->GetBoundingBox();
573 BOX2I worstCaseBBox = itemBBox;
574
575 worstCaseBBox.Inflate( m_board->m_DRCMaxClearance );
576
577 if( !worstCaseBBox.Intersects( aZone->GetBoundingBox() ) )
578 return;
579
580 DRC_RTREE* zoneTree = m_board->GetCopperZoneRTree( aZone );
581
582 if( !zoneTree )
583 return;
584
585 std::shared_ptr<SHAPE> itemShape = aText->GetEffectiveShape( layer, FLASHING::DEFAULT );
586
587 if( *aInheritedNet == nullptr )
588 {
589 if( zoneTree->QueryColliding( itemBBox, itemShape.get(), layer ) )
590 *aInheritedNet = aZone->GetNet();
591 }
592
593 if( *aInheritedNet == aZone->GetNet() )
594 return;
595
596 DRC_CONSTRAINT constraint = m_drcEngine->EvalRules( CLEARANCE_CONSTRAINT, aText, aZone, layer );
597 int clearance = constraint.GetValue().Min();
598 int actual;
599 VECTOR2I pos;
600
601 if( constraint.GetSeverity() != RPT_SEVERITY_IGNORE && clearance >= 0 )
602 {
603 if( zoneTree->QueryColliding( itemBBox, itemShape.get(), layer, sub_e( clearance ), &actual, &pos ) )
604 {
605 std::shared_ptr<DRC_ITEM> drcItem;
606
607 if( testShorts && actual == 0 && *aInheritedNet )
608 {
609 int inheritedNetCode = ( *aInheritedNet )->GetNetCode();
610 const wxString& inheritedNetname = ( *aInheritedNet )->GetNetname();
611 int zoneNetCode = aZone->GetNetCode();
612
614 drcItem->SetErrorDetail( wxString::Format( _( "(nets %s and %s)" ),
615 inheritedNetCode ? inheritedNetname : _( "<no net>" ),
616 zoneNetCode ? aZone->GetNetname() : _( "<no net>" ) ) );
617 }
618 else
619 {
620 drcItem = DRC_ITEM::Create( DRCE_CLEARANCE );
621 drcItem->SetErrorDetail( formatMsg( _( "(%s clearance %s; actual %s)" ),
622 constraint.GetName(),
623 clearance,
624 actual ) );
625 }
626
627 drcItem->SetItems( aText, aZone );
628 drcItem->SetViolatingRule( constraint.GetParentRule() );
629 reportTwoItemGeometry( drcItem, pos, aText, aZone, layer, actual );
630 }
631 }
632}
633
634
636{
637 std::map<BOARD_ITEM*, int> freePadsUsageMap;
638 std::mutex freePadsUsageMapMutex;
639 std::atomic<size_t> done( 0 );
640 size_t count = m_board->Tracks().size();
641
642 REPORT_AUX( wxString::Format( wxT( "Testing %d tracks & vias..." ), count ) );
643
644 LSET boardCopperLayers = LSET::AllCuMask( m_board->GetCopperLayerCount() );
645
646 auto testTrack =
647 [&]( const int trackIdx )
648 {
649 PCB_TRACK* track = m_board->Tracks()[trackIdx];
650 std::vector<ZONE*> zones;
651
652 for( PCB_LAYER_ID layer : LSET( track->GetLayerSet() & boardCopperLayers ) )
653 {
654 std::shared_ptr<SHAPE> trackShape = track->GetEffectiveShape( layer );
655
656 m_board->m_CopperItemRTreeCache->QueryCollidingPreparedCopper( track, trackShape, layer,
657 // Filter:
658 [&]( BOARD_ITEM* other ) -> bool
659 {
660 if( other->IsConnected()
661 && static_cast<BOARD_CONNECTED_ITEM*>( other )->GetNetCode()
662 == track->GetNetCode() )
663 {
664 return false;
665 }
666
667 // Dedup on UUID, not heap address, so that exclusion checking
668 // against previously-generated violations will work
669 KICAD_T otherType = other->Type();
670
671 if( ( otherType == PCB_TRACE_T || otherType == PCB_ARC_T || otherType == PCB_VIA_T )
672 && track->m_Uuid > other->m_Uuid )
673 {
674 return false;
675 }
676
677 return true;
678 },
679 // Visitor:
680 [&]( BOARD_ITEM* other, const std::shared_ptr<SHAPE>& aPreparedOther ) -> bool
681 {
682 if( m_drcEngine->IsCancelled() )
683 return false;
684
685 if( other->Type() == PCB_PAD_T && static_cast<PAD*>( other )->IsFreePad() )
686 {
687 if( aPreparedOther->Collide( trackShape.get() ) )
688 {
689 std::lock_guard<std::mutex> lock( freePadsUsageMapMutex );
690 auto it = freePadsUsageMap.find( other );
691
692 if( it == freePadsUsageMap.end() )
693 {
694 freePadsUsageMap[ other ] = track->GetNetCode();
695 return true; // Continue colliding tests
696 }
697 else if( it->second == track->GetNetCode() )
698 {
699 return true; // Continue colliding tests
700 }
701 }
702 }
703
704 if( !testSingleLayerItemAgainstItem( track, trackShape.get(), layer, other,
705 aPreparedOther.get() ) )
706 {
707 if( !m_drcEngine->GetReportAllTrackErrors() )
708 return false;
709 }
710
711 return !m_drcEngine->IsCancelled();
712 },
713 m_board->m_DRCMaxClearance, true );
714
715 collectZonesNear( track, layer, zones );
716
717 for( ZONE* zone : zones )
718 {
719 testItemAgainstZone( track, zone, layer );
720
721 if( m_drcEngine->IsCancelled() )
722 break;
723 }
724 }
725
726 done.fetch_add( 1 );
727 };
728
730
731 auto track_futures = tp.submit_loop( 0, m_board->Tracks().size(), testTrack, m_board->Tracks().size() );
732
733 while( done < count )
734 {
735 reportProgress( done, count );
736
737 if( m_drcEngine->IsCancelled() )
738 {
739 // Wait for the submitted loop tasks to finish
740 track_futures.wait();
741 break;
742 }
743
744 track_futures.wait_for( std::chrono::milliseconds( 250 ) );
745 }
746}
747
748
749bool DRC_TEST_PROVIDER_COPPER_CLEARANCE::testPadAgainstItem( PAD* pad, const std::shared_ptr<SHAPE>& padShape,
750 PCB_LAYER_ID aLayer, BOARD_ITEM* other,
751 SHAPE* aPreparedOther )
752{
753 bool testClearance = !m_drcEngine->IsErrorLimitExceeded( DRCE_CLEARANCE );
754 bool testShorting = !m_drcEngine->IsErrorLimitExceeded( DRCE_SHORTING_ITEMS );
755 bool testHoles = !m_drcEngine->IsErrorLimitExceeded( DRCE_HOLE_CLEARANCE );
756
757 // Disable some tests for net-tie objects in a footprint
758 if( other->GetParent() == pad->GetParent() )
759 {
760 FOOTPRINT* fp = pad->GetParentFootprint();
761 std::map<wxString, int> padToNetTieGroupMap = fp->MapPadNumbersToNetTieGroups();
762 int padGroupIdx = padToNetTieGroupMap[ pad->GetNumber() ];
763
764 if( other->Type() == PCB_PAD_T )
765 {
766 PAD* otherPad = static_cast<PAD*>( other );
767
768 if( padGroupIdx >= 0 && padGroupIdx == padToNetTieGroupMap[ otherPad->GetNumber() ] )
769 testClearance = testShorting = false;
770
771 if( pad->SameLogicalPadAs( otherPad ) )
772 testHoles = false;
773 }
774
775 if( other->Type() == PCB_SHAPE_T && padGroupIdx >= 0 )
776 testClearance = testShorting = false;
777 }
778
779 BOARD_CONNECTED_ITEM* otherCItem = other->IsConnected() ? static_cast<BOARD_CONNECTED_ITEM*>( other )
780 : nullptr;
781 PAD* otherPad = nullptr;
782 PCB_VIA* otherVia = nullptr;
783
784 if( other->Type() == PCB_PAD_T )
785 otherPad = static_cast<PAD*>( other );
786
787 if( other->Type() == PCB_VIA_T )
788 otherVia = static_cast<PCB_VIA*>( other );
789
790 if( !IsCopperLayer( aLayer ) )
791 testClearance = testShorting = false;
792
793 // A NPTH has no cylinder, but it may still have pads on some layers
794 if( pad->GetAttribute() == PAD_ATTRIB::NPTH && !pad->FlashLayer( aLayer ) )
795 testClearance = testShorting = false;
796
797 if( otherPad && otherPad->GetAttribute() == PAD_ATTRIB::NPTH && !otherPad->FlashLayer( aLayer ) )
798 testClearance = testShorting = false;
799
800 // Track clearances are tested in testTrackClearances()
801 if( other->Type() == PCB_TRACE_T || other->Type() == PCB_ARC_T || other->Type() == PCB_VIA_T )
802 testClearance = testShorting = false;
803
804 // Graphic clearances are tested in testGraphicClearances()
805 if( other->Type() == PCB_SHAPE_T || other->Type() == PCB_TEXTBOX_T )
806 testClearance = testShorting = false;
807
808 int padNet = pad->GetNetCode();
809 int otherNet = otherCItem ? otherCItem->GetNetCode() : 0;
810
811 // Other objects of the same (defined) net get a waiver on clearance and hole tests
812 if( otherNet && otherNet == padNet )
813 {
814 testClearance = testShorting = false;
815 testHoles = false;
816 }
817
818 if( !( pad->GetDrillSize().x > 0 )
819 && !( otherPad && otherPad->GetDrillSize().x > 0 )
820 && !( otherVia && otherVia->GetDrill() > 0 ) )
821 {
822 testHoles = false;
823 }
824
825 if( !testClearance && !testShorting && !testHoles )
826 return true;
827
828 SHAPE* otherShape = aPreparedOther;
829 DRC_CONSTRAINT constraint;
830 int clearance = 0;
831 int actual = 0;
832 VECTOR2I pos;
833 bool has_error = false;
834
835 if( otherPad && pad->SameLogicalPadAs( otherPad ) )
836 {
837 // If pads are equivalent (ie: from the same footprint with the same pad number)...
838 // ... and have "real" nets...
839 // then they must be the same net
840 if( testShorting )
841 {
842 if( pad->GetNetCode() == 0 || pad->GetNetCode() == otherPad->GetNetCode() )
843 return true;
844
845 if( pad->GetShortNetname().StartsWith( wxS( "unconnected-(" ) )
846 && otherPad->GetShortNetname().StartsWith( wxS( "unconnected-(" ) ) )
847 {
848 return true;
849 }
850
851 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_SHORTING_ITEMS );
852 drcItem->SetErrorDetail( wxString::Format( _( "(nets %s and %s)" ),
853 padNet ? pad->GetNetname() : _( "<no net>" ),
854 otherNet ? otherPad->GetNetname() : _( "<no net>" ) ) );
855
856 drcItem->SetItems( pad, otherPad );
857 reportViolation( drcItem, otherPad->GetPosition(), aLayer );
858 has_error = true;
859 }
860
861 return !has_error;
862 }
863
864 if( testClearance || testShorting )
865 {
866 constraint = m_drcEngine->EvalRules( CLEARANCE_CONSTRAINT, pad, other, aLayer );
867 clearance = constraint.GetValue().Min();
868
869 if( constraint.GetSeverity() != RPT_SEVERITY_IGNORE && clearance > 0 )
870 {
871 if( padShape->Collide( otherShape, sub_e( clearance ), &actual, &pos ) )
872 {
873 if( m_drcEngine->IsNetTieExclusion( pad->GetNetCode(), aLayer, pos, other ) )
874 {
875 // Pads connected to pads of a net-tie footprint are allowed to collide
876 // with the net-tie footprint's graphics.
877 }
878 else if( actual == 0 && padNet && otherNet && testShorting )
879 {
880 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_SHORTING_ITEMS );
881 drcItem->SetErrorDetail( wxString::Format( _( "(nets %s and %s)" ),
882 padNet ? pad->GetNetname() : _( "<no net>" ),
883 otherNet ? otherPad->GetNetname() : _( "<no net>" ) ) );
884 drcItem->SetItems( pad, other );
885 reportTwoPointGeometry( drcItem, pos, pos, pos, aLayer );
886 has_error = true;
887 testHoles = false; // No need for multiple violations
888 }
889 else if( testClearance )
890 {
891 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_CLEARANCE );
892 drcItem->SetErrorDetail( formatMsg( _( "(%s clearance %s; actual %s)" ),
893 constraint.GetName(),
894 clearance,
895 actual ) );
896 drcItem->SetItems( pad, other );
897 drcItem->SetViolatingRule( constraint.GetParentRule() );
898 reportTwoItemGeometry( drcItem, pos, pad, other, aLayer, actual );
899 has_error = true;
900 testHoles = false; // No need for multiple violations
901 }
902 }
903 }
904 }
905
906 auto doTestHole =
907 [&]( BOARD_ITEM* item, const SHAPE* shape, BOARD_ITEM* otherItem,
908 const std::shared_ptr<SHAPE_SEGMENT>& aOtherShape, int aClearance )
909 {
910 if( shape->Collide( aOtherShape.get(), sub_e( aClearance ), &actual, &pos ) )
911 {
912 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_HOLE_CLEARANCE );
913 drcItem->SetErrorDetail( formatMsg( _( "(%s clearance %s; actual %s)" ),
914 constraint.GetName(),
915 aClearance, actual ) );
916 drcItem->SetItems( item, otherItem );
917 drcItem->SetViolatingRule( constraint.GetParentRule() );
918 reportTwoShapeGeometry( drcItem, pos, shape, aOtherShape.get(), aLayer, actual );
919 has_error = true;
920 testHoles = false; // No need for multiple violations
921 }
922 };
923
924 if( testHoles )
925 {
926 constraint = m_drcEngine->EvalRules( HOLE_CLEARANCE_CONSTRAINT, pad, other, aLayer );
927
928 if( constraint.GetSeverity() == RPT_SEVERITY_IGNORE )
929 testHoles = false;
930 }
931
932 if( testHoles && otherPad && otherPad->HasHole() && pad->FlashLayer( aLayer ) )
933 {
934 clearance = constraint.GetValue().Min();
935
936 if( clearance > 0 )
937 {
938 doTestHole( pad, padShape.get(), otherPad,
940 }
941 }
942
943 // Pad pairs are deduplicated by UUID order in testPadClearances.
944 // Run the swapped direction so we don't miss any violations.
945 if( testHoles && pad->HasHole() && otherPad && otherPad->FlashLayer( aLayer ) )
946 {
947 clearance = constraint.GetValue().Min();
948
949 if( clearance > 0 )
950 {
951 doTestHole( otherPad, otherShape, pad, pad->GetEffectiveHoleShape( aLayer, HOLE_CLEARANCE_CONSTRAINT ),
952 clearance );
953 }
954 }
955
956 if( testHoles && otherVia && otherVia->HasHole() )
957 {
958 clearance = constraint.GetValue().Min();
959
960 if( !otherVia->IsOnLayer( aLayer ) )
961 clearance = 0;
962
963 if( clearance > 0 )
964 {
965 doTestHole( pad, padShape.get(), otherVia,
967 }
968 }
969
970 return !has_error;
971}
972
973
975{
977 std::atomic<size_t> done( 1 );
978
979 LSET boardCopperLayers = LSET::AllCuMask( m_board->GetCopperLayerCount() );
980
981 const auto fp_check =
982 [&]( size_t ii )
983 {
984 FOOTPRINT* footprint = m_board->Footprints()[ ii ];
985 std::vector<ZONE*> zones;
986
987 for( PAD* pad : footprint->Pads() )
988 {
989 // Through-hole pads are tested per-layer, so overlapping TH pads
990 // may produce one violation per shared copper layer. This is
991 // intentional for parallelized DRC to avoid cross-layer shared state.
992 for( PCB_LAYER_ID layer : LSET( pad->GetLayerSet() & boardCopperLayers ) )
993 {
994 if( m_drcEngine->IsCancelled() )
995 return;
996
997 std::shared_ptr<SHAPE> padShape = pad->GetEffectiveShape( layer );
998
999 m_board->m_CopperItemRTreeCache->QueryCollidingPreparedCopper(
1000 pad, padShape, layer,
1001 // Filter:
1002 [&]( BOARD_ITEM* other ) -> bool
1003 {
1004 // Dedup on UUID, not heap address, so that exclusion
1005 // checking against previously-generated violations will work
1006 if( other->Type() == PCB_PAD_T && pad->m_Uuid > other->m_Uuid )
1007 {
1008 return false;
1009 }
1010
1011 return true;
1012 },
1013 // Visitor
1014 [&]( BOARD_ITEM* other, const std::shared_ptr<SHAPE>& aPreparedOther ) -> bool
1015 {
1016 testPadAgainstItem( pad, padShape, layer, other, aPreparedOther.get() );
1017 return !m_drcEngine->IsCancelled();
1018 },
1019 m_board->m_DRCMaxClearance, true );
1020
1021 collectZonesNear( pad, layer, zones );
1022
1023 for( ZONE* zone : zones )
1024 {
1025 testItemAgainstZone( pad, zone, layer );
1026
1027 if( m_drcEngine->IsCancelled() )
1028 return;
1029 }
1030 }
1031 }
1032
1033 done.fetch_add( 1 );
1034 };
1035
1036 size_t numFootprints = m_board->Footprints().size();
1037 auto returns = tp.submit_loop( 0, numFootprints, fp_check, numFootprints );
1038
1039 // Wait for all threads to finish
1040 for( const std::future<void>& ret : returns )
1041 {
1042 while( ret.wait_for( std::chrono::milliseconds( 250 ) ) != std::future_status::ready )
1043 reportProgress( done, numFootprints );
1044 }
1045}
1046
1047
1049{
1051 size_t count = m_board->Drawings().size();
1052 std::atomic<size_t> done( 1 );
1053
1054 for( FOOTPRINT* footprint : m_board->Footprints() )
1055 count += footprint->GraphicalItems().size() + footprint->GetFields().size();
1056
1057 REPORT_AUX( wxString::Format( wxT( "Testing %d graphics..." ), count ) );
1058
1059 auto isKnockoutText =
1060 []( BOARD_ITEM* item )
1061 {
1062 return ( item->Type() == PCB_TEXT_T || item->Type() == PCB_FIELD_T ) && item->IsKnockout();
1063 };
1064
1065 auto testGraphicAgainstZone =
1066 [this, isKnockoutText]( BOARD_ITEM* item )
1067 {
1068 if( item->Type() == PCB_REFERENCE_IMAGE_T || isInvisibleText( item ) )
1069 return;
1070
1071 if( !IsCopperLayer( item->GetLayer() ) )
1072 return;
1073
1074 // Knockout text is most often knocked-out of a zone, so it's presumed to
1075 // collide with one. However, if it collides with more than one, and they
1076 // have different nets, then we have a short.
1077 NETINFO_ITEM* inheritedNet = nullptr;
1078
1079 PCB_LAYER_ID layer = item->GetLayer();
1080 std::vector<ZONE*> zones;
1081
1082 collectZonesNear( item, layer, zones );
1083
1084 for( ZONE* zone : zones )
1085 {
1086 if( isKnockoutText( item ) )
1087 testKnockoutTextAgainstZone( item, &inheritedNet, zone );
1088 else
1089 testItemAgainstZone( item, zone, layer );
1090
1091 if( m_drcEngine->IsCancelled() )
1092 return;
1093 }
1094 };
1095
1096 auto testCopperGraphic =
1097 [this]( BOARD_ITEM* graphic )
1098 {
1099 PCB_LAYER_ID layer = graphic->GetLayer();
1100 std::shared_ptr<SHAPE> queryShape = graphic->GetEffectiveShape( layer );
1101 std::shared_ptr<SHAPE> semanticShape;
1102
1103 m_board->m_CopperItemRTreeCache->QueryCollidingPreparedCopper( graphic, queryShape, layer,
1104 // Filter:
1105 [&]( BOARD_ITEM* other ) -> bool
1106 {
1107 // Graphics are often compound shapes so ignore collisions between shapes
1108 // in a single footprint.
1109 if( graphic->Type() == PCB_SHAPE_T
1110 && other->Type() == PCB_SHAPE_T
1111 && graphic->GetParentFootprint()
1112 && graphic->GetParentFootprint() == other->GetParentFootprint() )
1113 {
1114 return false;
1115 }
1116
1117 // Track clearances are tested in testTrackClearances()
1118 if( other->Type() == PCB_TRACE_T
1119 || other->Type() == PCB_ARC_T
1120 || other->Type() == PCB_VIA_T )
1121 {
1122 return false;
1123 }
1124
1125 int graphicNet = graphic->IsConnected()
1126 ? static_cast<BOARD_CONNECTED_ITEM*>( graphic )->GetNetCode()
1127 : 0;
1128 int otherNet = other->IsConnected()
1129 ? static_cast<BOARD_CONNECTED_ITEM*>( other )->GetNetCode()
1130 : 0;
1131
1132 if( graphicNet && graphicNet == otherNet )
1133 return false;
1134
1135 // Dedup on UUID, not heap address, so that exclusion checking
1136 // against previously-generated violations will work
1137 if( ( other->Type() == PCB_SHAPE_T
1138 || other->Type() == PCB_TEXTBOX_T
1139 || other->Type() == PCB_BARCODE_T )
1140 && graphic->m_Uuid > other->m_Uuid )
1141 {
1142 return false;
1143 }
1144
1145 return true;
1146 },
1147 // Visitor:
1148 [&]( BOARD_ITEM* other, const std::shared_ptr<SHAPE>& aPreparedOther ) -> bool
1149 {
1150 if( !semanticShape )
1151 semanticShape = graphic->GetEffectiveShape();
1152
1153 testSingleLayerItemAgainstItem( graphic, semanticShape.get(), layer, other,
1154 aPreparedOther.get() );
1155
1156 return !m_drcEngine->IsCancelled();
1157 },
1158 // A graphic's bbox inflates by width/2 where its shape inflates by
1159 // (width + 1)/2, and arc bounds differ again, so the credit is unsafe here
1160 m_board->m_DRCMaxClearance, false );
1161 };
1162
1163 BS::multi_future<void> graphic_futures;
1164
1165 for( BOARD_ITEM* item : m_board->Drawings() )
1166 {
1167 graphic_futures.push_back( tp.submit_task(
1168 [this, item, &done, testGraphicAgainstZone, testCopperGraphic]()
1169 {
1170 if( !m_drcEngine->IsCancelled() )
1171 {
1172 testGraphicAgainstZone( item );
1173
1174 if( ( item->Type() == PCB_SHAPE_T || item->Type() == PCB_BARCODE_T )
1175 && item->IsOnCopperLayer() )
1176 {
1177 testCopperGraphic( item );
1178 }
1179
1180 done.fetch_add( 1 );
1181 }
1182 } ) );
1183 }
1184
1185 for( FOOTPRINT* footprint : m_board->Footprints() )
1186 {
1187 graphic_futures.push_back( tp.submit_task(
1188 [this, footprint, &done, testGraphicAgainstZone, testCopperGraphic]()
1189 {
1190 for( BOARD_ITEM* item : footprint->GraphicalItems() )
1191 {
1192 if( !m_drcEngine->IsCancelled() )
1193 {
1194 testGraphicAgainstZone( item );
1195
1196 if( ( item->Type() == PCB_SHAPE_T || item->Type() == PCB_BARCODE_T )
1197 && item->IsOnCopperLayer() )
1198 {
1199 testCopperGraphic( item );
1200 }
1201
1202 done.fetch_add( 1 );
1203 }
1204 }
1205
1206 // Fields (reference, value, etc.) live in their own list but render as real
1207 // copper when placed on a copper layer, so they must be tested too.
1208 for( PCB_FIELD* field : footprint->GetFields() )
1209 {
1210 if( !m_drcEngine->IsCancelled() )
1211 {
1212 testGraphicAgainstZone( field );
1213 done.fetch_add( 1 );
1214 }
1215 }
1216 } ) );
1217 }
1218
1219 // Wait on our own futures, not the pool; the tasks hold done by reference until they return.
1220 // reportProgress() pumps the dialog and latches a Cancel click, so run it even when ready.
1221 for( std::future<void>& future : graphic_futures )
1222 {
1223 do
1224 {
1225 reportProgress( done, count );
1226 } while( future.wait_for( std::chrono::milliseconds( 250 ) ) != std::future_status::ready );
1227 }
1228}
1229
1230
1232{
1233 LSET boardCopperLayers = LSET::AllCuMask( m_board->GetCopperLayerCount() );
1234 std::vector<ZONE*> zones;
1235
1236 for( ZONE* teardrop : m_board->m_DRCCopperZones )
1237 {
1238 if( !teardrop->IsTeardropArea() )
1239 continue;
1240
1241 for( PCB_LAYER_ID layer : LSET( teardrop->GetLayerSet() & boardCopperLayers ) )
1242 {
1243 if( m_drcEngine->IsCancelled() )
1244 return;
1245
1246 collectZonesNear( teardrop, layer, zones );
1247
1248 for( ZONE* zone : zones )
1249 {
1250 if( zone == teardrop )
1251 continue;
1252
1253 // For teardrop-vs-teardrop pairs, use pointer ordering so each
1254 // pair is tested only once.
1255 if( zone->IsTeardropArea() && zone < teardrop )
1256 continue;
1257
1258 testItemAgainstZone( teardrop, zone, layer );
1259
1260 if( m_drcEngine->IsCancelled() )
1261 return;
1262 }
1263 }
1264 }
1265}
1266
1267
1269{
1270 using SEG_RTREE = KIRTREE::PACKED_RTREE<size_t, int, 2>;
1271
1272 std::vector<std::map<PCB_LAYER_ID, std::vector<SEG>>> poly_segments( m_board->m_DRCCopperZones.size() );
1273 std::vector<std::map<PCB_LAYER_ID, SEG_RTREE>> seg_rtrees( m_board->m_DRCCopperZones.size() );
1274
1276 std::atomic<size_t> done( 0 );
1277 size_t count = 0;
1278 BS::multi_future<void> zone_futures;
1279
1280 auto reportZoneZoneViolation =
1281 [this]( ZONE* zoneA, ZONE* zoneB, const VECTOR2I& pt, const VECTOR2I& otherPt, int actual,
1282 const DRC_CONSTRAINT& constraint, PCB_LAYER_ID layer ) -> void
1283 {
1284 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_CLEARANCE );
1285 drcItem->SetErrorDetail( formatMsg( _( "(%s clearance %s; actual %s)" ),
1286 constraint.GetName(),
1287 constraint.GetValue().Min(),
1288 std::max( actual, 0 ) ) );
1289 drcItem->SetItems( zoneA, zoneB );
1290 drcItem->SetViolatingRule( constraint.GetParentRule() );
1291 reportTwoPointGeometry( drcItem, pt, pt, actual > 0 ? otherPt : pt, layer );
1292 };
1293
1294 auto checkZones =
1295 [this, reportZoneZoneViolation, &poly_segments, &seg_rtrees, &done]
1296 ( size_t zoneA_idx, size_t zoneB_idx, PCB_LAYER_ID layer ) -> void
1297 {
1298 if( m_drcEngine->IsCancelled() )
1299 {
1300 done.fetch_add( 1 );
1301 return;
1302 }
1303
1304 ZONE* zoneA = m_board->m_DRCCopperZones[zoneA_idx];
1305 ZONE* zoneB = m_board->m_DRCCopperZones[zoneB_idx];
1306 int actual = 0;
1307 VECTOR2I pt;
1308 VECTOR2I otherPt;
1309 DRC_CONSTRAINT constraint = m_drcEngine->EvalRules( CLEARANCE_CONSTRAINT, zoneA, zoneB, layer );
1310 int clearance = constraint.GetValue().Min();
1311
1312 if( constraint.GetSeverity() != RPT_SEVERITY_IGNORE && clearance > 0 )
1313 {
1314 const auto refIt = poly_segments[zoneA_idx].find( layer );
1315 const auto targetIt = poly_segments[zoneB_idx].find( layer );
1316 const auto testIt = seg_rtrees[zoneB_idx].find( layer );
1317
1318 if( refIt == poly_segments[zoneA_idx].end()
1319 || targetIt == poly_segments[zoneB_idx].end()
1320 || testIt == seg_rtrees[zoneB_idx].end() )
1321 {
1322 done.fetch_add( 1 );
1323 return;
1324 }
1325
1326 const std::vector<SEG>& refSegments = refIt->second;
1327 const std::vector<SEG>& testSegments = targetIt->second;
1328
1329 if( !testIt->second.empty() )
1330 {
1331 const SEG_RTREE& testTree = testIt->second;
1332
1333 for( const SEG& refSegment : refSegments )
1334 {
1335 // A single zone pair can be a long scan on a dense board.
1336 if( m_drcEngine->IsCancelled() )
1337 {
1338 done.fetch_add( 1 );
1339 return;
1340 }
1341
1342 int minX = std::min( refSegment.A.x, refSegment.B.x ) - clearance;
1343 int minY = std::min( refSegment.A.y, refSegment.B.y ) - clearance;
1344 int maxX = std::max( refSegment.A.x, refSegment.B.x ) + clearance;
1345 int maxY = std::max( refSegment.A.y, refSegment.B.y ) + clearance;
1346 int qmin[2] = { minX, minY };
1347 int qmax[2] = { maxX, maxY };
1348 bool found = false;
1349
1350 auto visitor =
1351 [&]( size_t segIdx ) -> bool
1352 {
1353 const SEG& testSegment = testSegments[segIdx];
1354 int64_t dist_sq = 0;
1355
1356 refSegment.NearestPoints( testSegment, pt, otherPt, dist_sq );
1357 actual = std::floor( std::sqrt( dist_sq ) + 0.5 );
1358
1359 if( actual < clearance )
1360 {
1361 found = true;
1362 return false;
1363 }
1364
1365 return true;
1366 };
1367
1368 testTree.Search( qmin, qmax, visitor );
1369
1370 if( found )
1371 {
1372 done.fetch_add( 1 );
1373 reportZoneZoneViolation( zoneA, zoneB, pt, otherPt, actual, constraint, layer );
1374 return;
1375 }
1376 }
1377 }
1378 }
1379
1380 done.fetch_add( 1 );
1381 };
1382
1383 // Pre-sort zones into layers
1384 std::map<PCB_LAYER_ID, std::vector<size_t>> zone_idx_by_layer;
1385
1386 for ( size_t ii = 0; ii < m_board->m_DRCCopperZones.size(); ii++ )
1387 {
1388 ZONE* zone = m_board->m_DRCCopperZones[ii];
1389
1390 // Teardrop areas are tested as tracks, not zones
1391 if( zone->IsTeardropArea() )
1392 continue;
1393
1394 for( PCB_LAYER_ID layer : zone->GetLayerSet() )
1395 {
1396 if( !IsCopperLayer( layer ) )
1397 continue;
1398
1399 zone_idx_by_layer[layer].push_back( ii );
1400 }
1401 }
1402
1403 struct ZONE_PAIR
1404 {
1405 size_t first;
1406 size_t second;
1407 PCB_LAYER_ID layer;
1408 };
1409
1410 std::vector<ZONE_PAIR> candidates;
1411 std::set<std::pair<size_t, PCB_LAYER_ID>> participating;
1412
1413 for( PCB_LAYER_ID layer : LAYER_RANGE( F_Cu, B_Cu, m_board->GetCopperLayerCount() ) )
1414 {
1415 if( !m_board->IsLayerEnabled( layer ) )
1416 continue;
1417
1418 auto fillBox =
1419 [&]( size_t aIndex ) -> std::optional<BOX2I>
1420 {
1421 if( SHAPE_POLY_SET* poly = m_board->m_DRCCopperZones[aIndex]->GetFill( layer ) )
1422 return poly->BBoxFromCaches();
1423
1424 return std::nullopt;
1425 };
1426
1427 for( const auto& [ia, ia2] : CollectOverlappingPairs( zone_idx_by_layer[layer], fillBox ) )
1428 {
1429 ZONE* zoneA = m_board->m_DRCCopperZones[ia];
1430 ZONE* zoneB = m_board->m_DRCCopperZones[ia2];
1431
1432 bool sameNet = zoneA->GetNetCode() == zoneB->GetNetCode() && zoneA->GetNetCode() >= 0;
1433
1434 if( sameNet )
1435 continue;
1436
1437 // rule areas may overlap at will
1438 if( zoneA->GetIsRuleArea() || zoneB->GetIsRuleArea() )
1439 continue;
1440
1441 SHAPE_POLY_SET* polyA = zoneA->GetFill( layer );
1442 SHAPE_POLY_SET* polyB = zoneB->GetFill( layer );
1443
1444 // Redundant with the enumeration above; retained as the parity boundary because a
1445 // future widening of this gate has to widen the enumeration by the same amount
1446 if( !polyA || !polyB || !polyA->BBoxFromCaches().Intersects( polyB->BBoxFromCaches() ) )
1447 continue;
1448
1449 candidates.push_back( { ia, ia2, layer } );
1450 participating.emplace( ia, layer );
1451 participating.emplace( ia2, layer );
1452 }
1453 }
1454
1455 for( const auto& [zoneIndex, layer] : participating )
1456 {
1457 SHAPE_POLY_SET* poly = m_board->m_DRCCopperZones[zoneIndex]->GetFill( layer );
1458
1459 if( !poly )
1460 continue;
1461
1462 std::vector<SEG>& segments = poly_segments[zoneIndex][layer];
1463 segments.reserve( poly->FullPointCount() );
1464
1465 for( auto it = poly->IterateSegmentsWithHoles(); it; it++ )
1466 {
1467 SEG seg = *it;
1468
1469 if( seg.A.x > seg.B.x )
1470 seg.Reverse();
1471
1472 segments.push_back( seg );
1473 }
1474
1475 SEG_RTREE::Builder builder;
1476 builder.Reserve( segments.size() );
1477
1478 for( size_t si = 0; si < segments.size(); ++si )
1479 {
1480 const SEG& seg = segments[si];
1481 int min[2] = { std::min( seg.A.x, seg.B.x ), std::min( seg.A.y, seg.B.y ) };
1482 int max[2] = { std::max( seg.A.x, seg.B.x ), std::max( seg.A.y, seg.B.y ) };
1483 builder.Add( min, max, si );
1484 }
1485
1486 seg_rtrees[zoneIndex].emplace( layer, builder.Build() );
1487 }
1488
1489 count = candidates.size();
1490
1491 for( const ZONE_PAIR& pair : candidates )
1492 {
1493 zone_futures.push_back( tp.submit_task(
1494 [checkZones, pair]()
1495 {
1496 checkZones( pair.first, pair.second, pair.layer );
1497 } ) );
1498 }
1499
1500 // Wait on our own futures, not the pool; checkZones holds done and poly_segments by reference.
1501 // reportProgress() pumps the dialog and latches a Cancel click, so run it even when ready.
1502 for( std::future<void>& future : zone_futures )
1503 {
1504 do
1505 {
1506 reportProgress( done, count );
1507 } while( future.wait_for( std::chrono::milliseconds( 250 ) ) != std::future_status::ready );
1508 }
1509}
1510
1511namespace detail
1512{
1514}
int index
BOX2< VECTOR2I > BOX2I
Definition box2.h:917
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
NETINFO_ITEM * GetNet() const
Return NETINFO_ITEM object for a given item.
const wxString & GetShortNetname() const
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 std::shared_ptr< SHAPE_SEGMENT > GetEffectiveHoleShape(PCB_LAYER_ID aLayer=UNDEFINED_LAYER, DRC_CONSTRAINT_T aUsage=NULL_CONSTRAINT) const
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
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
BOARD_ITEM_CONTAINER * GetParent() const
Definition board_item.h:281
virtual std::shared_ptr< SHAPE > GetEffectiveShape(PCB_LAYER_ID aLayer=UNDEFINED_LAYER, FLASHING aFlash=FLASHING::DEFAULT, DRC_CONSTRAINT_T aUsage=NULL_CONSTRAINT) const
Some pad shapes can be complex (rounded/chamfered rectangle), even without considering custom shapes.
virtual bool HasHole() const
Definition board_item.h:222
constexpr BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition box2.h:556
constexpr bool Intersects(const BOX2< Vec > &aRect) const
Definition box2.h:314
wxString GetName() const
Definition drc_rule.h:162
SEVERITY GetSeverity() const
Definition drc_rule.h:181
const MINOPTMAX< int > & GetValue() const
Definition drc_rule.h:129
DRC_RULE * GetParentRule() const
Definition drc_rule.h:147
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Definition drc_item.cpp:505
Implement an R-tree for fast spatial and layer indexing of connectable items.
Definition drc_rtree.h:46
int QueryColliding(BOARD_ITEM *aRefItem, PCB_LAYER_ID aRefLayer, PCB_LAYER_ID aTargetLayer, std::function< bool(BOARD_ITEM *)> aFilter=nullptr, std::function< bool(BOARD_ITEM *)> aVisitor=nullptr, int aClearance=0) const
This is a fast test which essentially does bounding-box overlap given a worst-case clearance.
Definition drc_rtree.h:231
bool testSingleLayerItemAgainstItem(BOARD_ITEM *item, SHAPE *itemShape, PCB_LAYER_ID layer, BOARD_ITEM *other, SHAPE *aPreparedOther)
Checks for track/via/hole <-> clearance.
virtual bool Run() override
Run this provider against the given PCB with configured options (if any).
bool testPadAgainstItem(PAD *pad, const std::shared_ptr< SHAPE > &padShape, PCB_LAYER_ID layer, BOARD_ITEM *other, SHAPE *aPreparedOther)
void collectZonesNear(BOARD_ITEM *aItem, PCB_LAYER_ID aLayer, std::vector< ZONE * > &aZones)
Copper zones on aLayer reachable from aItem at the worst clearance; aZones is reused.
void testItemAgainstZone(BOARD_ITEM *aItem, ZONE *aZone, PCB_LAYER_ID aLayer)
virtual ~DRC_TEST_PROVIDER_COPPER_CLEARANCE()=default
virtual const wxString GetName() const override
void testKnockoutTextAgainstZone(BOARD_ITEM *aText, NETINFO_ITEM **aInheritedNet, ZONE *aZone)
virtual bool reportPhase(const wxString &aStageName)
void reportTwoShapeGeometry(std::shared_ptr< DRC_ITEM > &aDrcItem, const VECTOR2I &aMarkerPos, const SHAPE *aShape1, const SHAPE *aShape2, PCB_LAYER_ID aLayer, int aDistance)
void reportTwoItemGeometry(std::shared_ptr< DRC_ITEM > &aDrcItem, const VECTOR2I &aMarkerPos, const BOARD_ITEM *aItem1, const BOARD_ITEM *aItem2, PCB_LAYER_ID aLayer, int aDistance)
void reportViolation(std::shared_ptr< DRC_ITEM > &item, const VECTOR2I &aMarkerPos, int aMarkerLayer, const std::function< void(PCB_MARKER *)> &aPathGenerator=[](PCB_MARKER *){})
void reportTwoPointGeometry(std::shared_ptr< DRC_ITEM > &aDrcItem, const VECTOR2I &aMarkerPos, const VECTOR2I &ptA, const VECTOR2I &ptB, PCB_LAYER_ID aLayer)
bool isInvisibleText(const BOARD_ITEM *aItem) const
wxString formatMsg(const wxString &aFormatString, const wxString &aSource, double aConstraint, double aActual, EDA_DATA_TYPE aDataType=EDA_DATA_TYPE::DISTANCE)
virtual bool reportProgress(size_t aCount, size_t aSize, size_t aDelta=1)
virtual const BOX2I GetBoundingBox() const
Return the orthogonal bounding box of this object for display purposes.
Definition eda_item.cpp:280
const KIID m_Uuid
Definition eda_item.h:599
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
std::vector< PAD * > GetNetTiePads(PAD *aPad) const
std::map< wxString, int > MapPadNumbersToNetTieGroups() const
std::deque< PAD * > & Pads()
Definition footprint.h:355
bool IsNetTie() const
Definition footprint.h:525
Static (immutable) packed R-tree built via Hilbert-curve bulk loading.
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
static const LSET & AllCuMask()
return AllCuMask( MAX_CU_LAYERS );
Definition lset.cpp:604
bool Contains(PCB_LAYER_ID aLayer) const
See if the layer set contains a PCB layer.
Definition lset.h:63
T Min() const
Definition minoptmax.h:29
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
Definition pad.h:61
bool FlashLayer(int aLayer, bool aOnlyCheckIfPermitted=false) const
Check to see whether the pad should be flashed on the specific layer.
Definition pad.cpp:697
std::shared_ptr< SHAPE_SEGMENT > GetEffectiveHoleShape(PCB_LAYER_ID aLayer=UNDEFINED_LAYER, DRC_CONSTRAINT_T aUsage=NULL_CONSTRAINT) const override
Return a SHAPE_SEGMENT object representing the pad's hole.
Definition pad.cpp:1233
PAD_ATTRIB GetAttribute() const
Definition pad.h:582
const wxString & GetNumber() const
Definition pad.h:143
VECTOR2I GetPosition() const override
Definition pad.cpp:255
VECTOR2I GetDrillSize() const
Definition pad.h:320
bool IsFreePad() const
Definition pad.cpp:614
bool HasHole() const override
Definition pad.h:113
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
const VECTOR2I & GetStart() const
Definition pcb_track.h:100
const VECTOR2I & GetEnd() const
Definition pcb_track.h:97
std::shared_ptr< SHAPE > GetEffectiveShape(PCB_LAYER_ID aLayer=UNDEFINED_LAYER, FLASHING aFlash=FLASHING::DEFAULT, DRC_CONSTRAINT_T aUsage=NULL_CONSTRAINT) const override
Some pad shapes can be complex (rounded/chamfered rectangle), even without considering custom shapes.
int GetDrill() const
Return the local drill setting for this PCB_VIA.
Definition pcb_track.h:838
std::shared_ptr< SHAPE_SEGMENT > GetEffectiveHoleShape(PCB_LAYER_ID aLayer=UNDEFINED_LAYER, DRC_CONSTRAINT_T aUsage=NULL_CONSTRAINT) const override
bool HasHole() const override
Definition pcb_track.h:511
bool IsOnLayer(PCB_LAYER_ID aLayer) const override
Test to see if this object is on the given layer.
Definition seg.h:38
VECTOR2I A
Definition seg.h:45
VECTOR2I B
Definition seg.h:46
OPT_VECTOR2I Intersect(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
Compute intersection point of segment (this) with segment aSeg.
Definition seg.cpp:401
void Reverse()
Definition seg.h:386
Represent a set of closed polygons.
int FullPointCount() const
Return the number of points in the shape poly set.
SEGMENT_ITERATOR IterateSegmentsWithHoles()
Returns an iterator object, for all outlines in the set (with holes)
const BOX2I BBoxFromCaches() const
An abstract shape on 2D plane.
Definition shape.h:124
virtual bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const
Check if the boundary of shape (this) lies closer to the point aP than aClearance,...
Definition shape.h:181
Handle a list of polygons defining a copper zone.
Definition zone.h:70
bool GetIsRuleArea() const
Accessors to parameters used in Rule Area zones:
Definition zone.h:834
const BOX2I GetBoundingBox() const override
Definition zone.cpp:824
SHAPE_POLY_SET * GetFill(PCB_LAYER_ID aLayer)
Definition zone.h:708
bool IsTeardropArea() const
Definition zone.h:809
std::shared_ptr< SHAPE > GetEffectiveShape(PCB_LAYER_ID aLayer=UNDEFINED_LAYER, FLASHING aFlash=FLASHING::DEFAULT, DRC_CONSTRAINT_T aUsage=NULL_CONSTRAINT) const override
Some pad shapes can be complex (rounded/chamfered rectangle), even without considering custom shapes.
Definition zone.cpp:2036
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition zone.h:133
@ CLEARANCE_CONSTRAINT
@ HOLE_CLEARANCE_CONSTRAINT
@ DRCE_HOLE_CLEARANCE
Definition drc_item.h:63
@ DRCE_CLEARANCE
Definition drc_item.h:52
@ DRCE_SHORTING_ITEMS
Definition drc_item.h:49
@ DRCE_TRACKS_CROSSING
Definition drc_item.h:54
#define REPORT_AUX(s)
std::vector< std::pair< size_t, size_t > > CollectOverlappingPairs(const std::vector< size_t > &aOrderedIndices, BOX_FN aBox)
Enumerate the pairs of aOrderedIndices whose boxes overlap, in the order a nested loop over the vecto...
#define _(s)
@ DEFAULT
Flashing follows connectivity.
Definition layer_ids.h:181
bool IsCopperLayer(int aLayerId)
Test whether a layer is a copper layer.
Definition layer_ids.h:744
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ B_Cu
Definition layer_ids.h:61
@ F_Cu
Definition layer_ids.h:60
static DRC_REGISTER_TEST_PROVIDER< DRC_TEST_PROVIDER_ANNULAR_WIDTH > dummy
@ 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
@ RPT_SEVERITY_IGNORE
std::optional< VECTOR2I > OPT_VECTOR2I
Definition seg.h:35
static bool Collide(const SHAPE_CIRCLE &aA, const SHAPE_CIRCLE &aB, int aClearance, int *aActual, VECTOR2I *aLocation, VECTOR2I *aMTV)
static std::vector< int > candidates(const SEGMENT_INDEX &aIndex, const SEG &aQuery, int aPadding)
VECTOR2I end
int clearance
int actual
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
static thread_pool * tp
BS::priority_thread_pool thread_pool
Definition thread_pool.h:27
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition typeinfo.h:70
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition typeinfo.h:80
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:89
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition typeinfo.h:85
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition typeinfo.h:84
@ PCB_REFERENCE_IMAGE_T
class PCB_REFERENCE_IMAGE, bitmap on a layer
Definition typeinfo.h:81
@ 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_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_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:88
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708