KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_test_provider_physical_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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <common.h>
25#include <macros.h>
27#include <footprint.h>
28#include <pad.h>
29#include <pcb_track.h>
30#include <pcb_shape.h>
31#include <zone.h>
32#include <advanced_config.h>
34#include <geometry/seg.h>
36#include <drc/drc_rtree.h>
37#include <drc/drc_item.h>
39
40/*
41 Physical clearance tests.
42
43 Errors generated:
44 - DRCE_PHYSICAL_CLEARANCE
45 - DRCE_PHYSICAL_HOLE_CLEARANCE
46*/
47
49{
50public:
54
56
57 virtual bool Run() override;
58
59 virtual const wxString GetName() const override { return wxT( "physical_clearance" ); };
60
61private:
62 int testItemAgainstItem( BOARD_ITEM* aItem, SHAPE* aItemShape, PCB_LAYER_ID aLayer,
63 BOARD_ITEM* aOther );
64
65 void testItemAgainstZones( BOARD_ITEM* aItem, PCB_LAYER_ID aLayer );
66
67 void testShapeLineChain( const SHAPE_LINE_CHAIN& aOutline, int aLineWidth, PCB_LAYER_ID aLayer,
68 BOARD_ITEM* aParentItem, DRC_CONSTRAINT& aConstraint );
69
70 void testZoneLayer( ZONE* aZone, PCB_LAYER_ID aLayer, DRC_CONSTRAINT& aConstraint );
71
72private:
74};
75
76
78{
79 m_board = m_drcEngine->GetBoard();
80 m_itemTree.clear();
81
82 int errorMax = m_board->GetDesignSettings().m_MaxError;
83 LSET boardCopperLayers = LSET::AllCuMask( m_board->GetCopperLayerCount() );
84
85 if( m_board->m_DRCMaxPhysicalClearance <= 0 )
86 {
87 REPORT_AUX( wxT( "No physical clearance constraints found. Tests not run." ) );
88 return true; // continue with other tests
89 }
90
91 size_t progressDelta = 250;
92 size_t count = 0;
93 size_t ii = 0;
94
95 if( !reportPhase( _( "Gathering physical items..." ) ) )
96 return false; // DRC cancelled
97
98 static const std::vector<KICAD_T> itemTypes = {
101 PCB_PAD_T,
107 };
108
109 static const LSET courtyards( { F_CrtYd, B_CrtYd } );
110
111 //
112 // Generate a count for use in progress reporting.
113 //
114
116 [&]( BOARD_ITEM* item ) -> bool
117 {
118 if( isInvisibleText( item ) )
119 return true;
120
121 ++count;
122 return true;
123 } );
124
125 //
126 // Generate a BOARD_ITEM RTree.
127 //
128
130 [&]( BOARD_ITEM* item ) -> bool
131 {
132 if( isInvisibleText( item ) )
133 return true;
134
135 if( !reportProgress( ii++, count, progressDelta ) )
136 return false;
137
138 LSET layers = item->GetLayerSet();
139
140 // Special-case holes and edge-cuts which pierce all physical layers
141 if( item->HasHole() )
142 {
143 if( layers.Contains( F_Cu ) )
144 layers |= LSET( LSET::FrontBoardTechMask() ).set( F_CrtYd );
145
146 if( layers.Contains( B_Cu ) )
147 layers |= LSET( LSET::BackBoardTechMask() ).set( B_CrtYd );
148
149 if( layers.Contains( F_Cu ) && layers.Contains( B_Cu ) )
150 layers |= boardCopperLayers;
151 }
152 else if( item->Type() == PCB_FOOTPRINT_T )
153 {
154 layers = courtyards;
155 }
156 else if( item->IsOnLayer( Edge_Cuts ) )
157 {
158 layers |= LSET::PhysicalLayersMask() | courtyards;
159 }
160
161 for( PCB_LAYER_ID layer : layers )
162 m_itemTree.Insert( item, layer, m_board->m_DRCMaxPhysicalClearance, ATOMIC_TABLES );
163
164 return true;
165 } );
166
167 m_itemTree.Build();
168
169 std::unordered_map<PTR_PTR_CACHE_KEY, LSET> checkedPairs;
170 progressDelta = 100;
171 ii = 0;
172
173 //
174 // Run clearance checks -between- items.
175 //
176
177 if( !m_drcEngine->IsErrorLimitExceeded( DRCE_CLEARANCE )
178 || !m_drcEngine->IsErrorLimitExceeded( DRCE_HOLE_CLEARANCE ) )
179 {
180 if( !reportPhase( _( "Checking physical clearances..." ) ) )
181 return false; // DRC cancelled
182
184 [&]( BOARD_ITEM* item ) -> bool
185 {
186 if( isInvisibleText( item ) )
187 return true;
188
189 if( !reportProgress( ii++, count, progressDelta ) )
190 return false;
191
192 LSET layers = item->GetLayerSet();
193
194 if( item->Type() == PCB_FOOTPRINT_T )
195 layers = courtyards;
196
197 for( PCB_LAYER_ID layer : layers )
198 {
199 std::shared_ptr<SHAPE> itemShape = item->GetEffectiveShape( layer );
200
201 m_itemTree.QueryColliding( item, layer, layer,
202 // Filter:
203 [&]( BOARD_ITEM* other ) -> bool
204 {
205 if( item->Type() == PCB_TABLECELL_T && item->GetParent() == other )
206 return false;
207
208 BOARD_ITEM* a = item;
209 BOARD_ITEM* b = other;
210
211 // store canonical order so we don't collide in both
212 // directions (a:b and b:a)
213 if( static_cast<void*>( a ) > static_cast<void*>( b ) )
214 std::swap( a, b );
215
216 auto it = checkedPairs.find( { a, b } );
217
218 if( it != checkedPairs.end() && it->second.test( layer ) )
219 {
220 return false;
221 }
222 else
223 {
224 checkedPairs[ { a, b } ].set( layer );
225 return true;
226 }
227 },
228 // Visitor:
229 [&]( BOARD_ITEM* other ) -> bool
230 {
231 if( testItemAgainstItem( item, itemShape.get(), layer,
232 other ) > 0 )
233 {
234 BOARD_ITEM* a = item;
235 BOARD_ITEM* b = other;
236
237 // store canonical order
238 if( static_cast<void*>( a ) > static_cast<void*>( b ) )
239 std::swap( a, b );
240
241 // Once we record one DRC for error for physical clearance
242 // we don't need to record more
243 checkedPairs[ { a, b } ].set();
244 }
245
246 return !m_drcEngine->IsCancelled();
247 },
248 m_board->m_DRCMaxPhysicalClearance );
249
250 testItemAgainstZones( item, layer );
251 }
252
253 return true;
254 } );
255 }
256
257 progressDelta = 100;
258 count = 0;
259 ii = 0;
260
261 //
262 // Generate a count for progress reporting.
263 //
264
265 forEachGeometryItem( { PCB_ZONE_T, PCB_SHAPE_T }, boardCopperLayers,
266 [&]( BOARD_ITEM* item ) -> bool
267 {
268 ZONE* zone = dynamic_cast<ZONE*>( item );
269
270 if( zone && zone->GetIsRuleArea() )
271 return true; // Continue with other items
272
273 count += ( item->GetLayerSet() & boardCopperLayers ).count();
274
275 return true;
276 } );
277
278 //
279 // Run clearance checks -within- polygonal items.
280 //
281
282 forEachGeometryItem( { PCB_ZONE_T, PCB_SHAPE_T }, boardCopperLayers,
283 [&]( BOARD_ITEM* item ) -> bool
284 {
285 PCB_SHAPE* shape = dynamic_cast<PCB_SHAPE*>( item );
286 ZONE* zone = dynamic_cast<ZONE*>( item );
287
288 if( zone && zone->GetIsRuleArea() )
289 return true; // Continue with other items
290
291 for( PCB_LAYER_ID layer : item->GetLayerSet() )
292 {
293 if( IsCopperLayer( layer ) )
294 {
295 if( !reportProgress( ii++, count, progressDelta ) )
296 return false;
297
298 DRC_CONSTRAINT c = m_drcEngine->EvalRules( PHYSICAL_CLEARANCE_CONSTRAINT, item, nullptr,
299 layer );
300
301 if( shape )
302 {
303 switch( shape->GetShape() )
304 {
305 case SHAPE_T::POLY:
306 testShapeLineChain( shape->GetPolyShape().Outline( 0 ), shape->GetWidth(), layer,
307 item, c );
308 break;
309
310 case SHAPE_T::BEZIER:
311 {
312 SHAPE_LINE_CHAIN asPoly;
313
314 shape->RebuildBezierToSegmentsPointsList( errorMax );
315
316 for( const VECTOR2I& pt : shape->GetBezierPoints() )
317 asPoly.Append( pt );
318
319 testShapeLineChain( asPoly, shape->GetWidth(), layer, item, c );
320 break;
321 }
322
323 case SHAPE_T::ARC:
324 {
325 SHAPE_LINE_CHAIN asPoly;
326
327 VECTOR2I center = shape->GetCenter();
328 EDA_ANGLE angle = -shape->GetArcAngle();
329 double r = shape->GetRadius();
330 int steps = GetArcToSegmentCount( r, errorMax, angle );
331
332 asPoly.Append( shape->GetStart() );
333
334 for( int step = 1; step <= steps; ++step )
335 {
336 EDA_ANGLE rotation = ( angle * step ) / steps;
337 VECTOR2I pt = shape->GetStart();
338
339 RotatePoint( pt, center, rotation );
340 asPoly.Append( pt );
341 }
342
343 testShapeLineChain( asPoly, shape->GetWidth(), layer, item, c );
344 break;
345 }
346
347 // Simple shapes can't create self-intersections, and I'm not sure a user
348 // would want a report that one side of their rectangle was too close to
349 // the other side.
351 case SHAPE_T::SEGMENT:
352 case SHAPE_T::CIRCLE:
353 break;
354
355 default:
357 }
358 }
359
360 if( zone )
361 testZoneLayer( static_cast<ZONE*>( item ), layer, c );
362 }
363
364 if( m_drcEngine->IsCancelled() )
365 return false;
366 }
367
368 return !m_drcEngine->IsCancelled();
369 } );
370
371 m_itemTree.clear();
372
373 return !m_drcEngine->IsCancelled();
374}
375
376
378 int aLineWidth, PCB_LAYER_ID aLayer,
379 BOARD_ITEM* aParentItem,
380 DRC_CONSTRAINT& aConstraint )
381{
382 // We don't want to collide with neighboring segments forming a curve until the concavity
383 // approaches 180 degrees.
384 double angleTolerance = DEG2RAD( 180.0 - ADVANCED_CFG::GetCfg().m_SliverAngleTolerance );
385 int epsilon = m_board->GetDesignSettings().GetDRCEpsilon();
386 int count = aOutline.SegmentCount();
387 int clearance = aConstraint.GetValue().Min();
388
389 if( aConstraint.GetSeverity() == RPT_SEVERITY_IGNORE || clearance - epsilon <= 0 )
390 return;
391
392 // Trigonometry is not cheap; cache seg angles
393 std::vector<double> angles;
394 angles.reserve( count );
395
396 auto angleDiff =
397 []( double a, double b ) -> double
398 {
399 if( a > b )
400 std::swap( a, b );
401
402 double diff = b - a;
403
404 if( diff > M_PI )
405 return 2 * M_PI - diff;
406 else
407 return diff;
408 };
409
410 for( int ii = 0; ii < count; ++ii )
411 {
412 const SEG& seg = aOutline.CSegment( ii );
413
414 // NB: don't store angles of really short segments (which could point anywhere)
415
416 if( seg.SquaredLength() > SEG::Square( epsilon * 2 ) )
417 {
418 angles.push_back( EDA_ANGLE( seg.B - seg.A ).AsRadians() );
419 }
420 else if( ii > 0 )
421 {
422 angles.push_back( angles.back() );
423 }
424 else
425 {
426 for( int jj = 1; jj < count; ++jj )
427 {
428 const SEG& following = aOutline.CSegment( jj );
429
430 if( following.SquaredLength() > SEG::Square( epsilon * 2 ) || jj == count - 1 )
431 {
432 angles.push_back( EDA_ANGLE( following.B - following.A ).AsRadians() );
433 break;
434 }
435 }
436 }
437 }
438
439 // Find collisions before reporting so that we can condense them into fewer reports.
440 std::vector< std::pair<VECTOR2I, int> > collisions;
441
442 for( int ii = 0; ii < count; ++ii )
443 {
444 const SEG seg = aOutline.CSegment( ii );
445 double segAngle = angles[ ii ];
446
447 // Exclude segments on either side of us until we reach the angle tolerance
448 int firstCandidate = ii + 1;
449 int lastCandidate = count - 1;
450
451 while( firstCandidate < count )
452 {
453 if( angleDiff( segAngle, angles[ firstCandidate ] ) < angleTolerance )
454 firstCandidate++;
455 else
456 break;
457 }
458
459 if( aOutline.IsClosed() )
460 {
461 if( ii > 0 )
462 lastCandidate = ii - 1;
463
464 while( lastCandidate != std::min( firstCandidate, count - 1 ) )
465 {
466 if( angleDiff( segAngle, angles[ lastCandidate ] ) < angleTolerance )
467 lastCandidate = ( lastCandidate == 0 ) ? count - 1 : lastCandidate - 1;
468 else
469 break;
470 }
471 }
472
473 // Now run the collision between seg and each candidate seg in the candidate range.
474 if( lastCandidate < ii )
475 lastCandidate = count - 1;
476
477 for( int jj = firstCandidate; jj <= lastCandidate; ++jj )
478 {
479 const SEG candidate = aOutline.CSegment( jj );
480 int actual;
481
482 if( seg.Collide( candidate, clearance + aLineWidth - epsilon, &actual ) )
483 {
484 VECTOR2I firstPoint = seg.NearestPoint( candidate );
485 VECTOR2I secondPoint = candidate.NearestPoint( seg );
486 VECTOR2I pos = ( firstPoint + secondPoint ) / 2;
487
488 if( !collisions.empty() && ( pos - collisions.back().first ).EuclideanNorm() < clearance * 2 )
489 {
490 if( actual < collisions.back().second )
491 {
492 collisions.back().first = pos;
493 collisions.back().second = actual;
494 }
495
496 continue;
497 }
498
499 collisions.push_back( { pos, actual } );
500 }
501 }
502 }
503
504 for( const std::pair<VECTOR2I, int>& collision : collisions )
505 {
506 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_CLEARANCE );
507 VECTOR2I pt = collision.first;
508
509 if( FOOTPRINT* parentFP = aParentItem->GetParentFootprint() )
510 {
511 RotatePoint( pt, parentFP->GetOrientation() );
512 pt += parentFP->GetPosition();
513 }
514
515 wxString msg = formatMsg( _( "Internal clearance violation (%s clearance %s; actual %s)" ),
516 aConstraint.GetName(),
517 clearance,
518 collision.second );
519
520 drcItem->SetErrorMessage( msg );
521 drcItem->SetItems( aParentItem );
522 drcItem->SetViolatingRule( aConstraint.GetParentRule() );
523
524 reportViolation( drcItem, pt, aLayer );
525 }
526}
527
528
530 DRC_CONSTRAINT& aConstraint )
531{
532 int epsilon = m_board->GetDesignSettings().GetDRCEpsilon();
533 int clearance = aConstraint.GetValue().Min();
534
535 if( aConstraint.GetSeverity() == RPT_SEVERITY_IGNORE || clearance - epsilon <= 0 )
536 return;
537
539
540 // Turn fractured fill into outlines and holes
541 fill.Simplify();
542
543 for( int outlineIdx = 0; outlineIdx < fill.OutlineCount(); ++outlineIdx )
544 {
545 SHAPE_LINE_CHAIN* firstOutline = &fill.Outline( outlineIdx );
546
547 //
548 // Step one: outline to outline clearance violations
549 //
550
551 for( int ii = outlineIdx + 1; ii < fill.OutlineCount(); ++ii )
552 {
553 SHAPE_LINE_CHAIN* secondOutline = &fill.Outline( ii );
554
555 for( int jj = 0; jj < secondOutline->SegmentCount(); ++jj )
556 {
557 SEG secondSeg = secondOutline->Segment( jj );
558 int actual;
559 VECTOR2I pos;
560
561 if( firstOutline->Collide( secondSeg, clearance - epsilon, &actual, &pos ) )
562 {
563 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_CLEARANCE );
564 drcItem->SetErrorDetail( formatMsg( _( "(%s clearance %s; actual %s)" ),
565 aConstraint.GetName(),
566 clearance,
567 actual ) );
568 drcItem->SetItems( aZone );
569 drcItem->SetViolatingRule( aConstraint.GetParentRule() );
570 reportViolation( drcItem, pos, aLayer );
571 }
572 }
573
574 if( m_drcEngine->IsCancelled() )
575 return;
576 }
577
578 //
579 // Step two: interior hole clearance violations
580 //
581
582 for( int holeIdx = 0; holeIdx < fill.HoleCount( outlineIdx ); ++holeIdx )
583 {
584 testShapeLineChain( fill.Hole( outlineIdx, holeIdx ), 0, aLayer, aZone, aConstraint );
585
586 if( m_drcEngine->IsCancelled() )
587 return;
588 }
589 }
590}
591
592
594 PCB_LAYER_ID aLayer, BOARD_ITEM* aOther )
595{
596 bool testClearance = !m_drcEngine->IsErrorLimitExceeded( DRCE_CLEARANCE );
597 bool testHoles = !m_drcEngine->IsErrorLimitExceeded( DRCE_HOLE_CLEARANCE );
598 DRC_CONSTRAINT constraint;
599 int clearance = 0;
600 int actual;
601 int violations = 0;
602 VECTOR2I pos;
603 LSET boardCopperLayers = LSET::AllCuMask( m_board->GetCopperLayerCount() );
604
605 std::shared_ptr<SHAPE> otherShapeStorage = aOther->GetEffectiveShape( aLayer );
606 SHAPE* otherShape = otherShapeStorage.get();
607
608 if( testClearance )
609 {
610 constraint = m_drcEngine->EvalRules( PHYSICAL_CLEARANCE_CONSTRAINT, aItem, aOther, aLayer );
611 clearance = constraint.GetValue().Min();
612 }
613
614 if( constraint.GetSeverity() != RPT_SEVERITY_IGNORE && clearance > 0 )
615 {
616 // Collide (and generate violations) based on a well-defined order so that exclusion
617 // checking against previously-generated violations will work.
618 if( aItem->m_Uuid > aOther->m_Uuid )
619 {
620 std::swap( aItem, aOther );
621 std::swap( aItemShape, otherShape );
622 }
623
624 if( aItemShape->Collide( otherShape, clearance, &actual, &pos ) )
625 {
626 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_CLEARANCE );
627 drcItem->SetErrorDetail( formatMsg( _( "(%s clearance %s; actual %s)" ),
628 constraint.GetName(),
629 clearance,
630 actual ) );
631 drcItem->SetItems( aItem, aOther );
632 drcItem->SetViolatingRule( constraint.GetParentRule() );
633 reportTwoShapeGeometry( drcItem, pos, aItemShape, otherShape, aLayer, actual );
634 ++violations;
635 }
636 }
637
638 if( testHoles )
639 {
640 std::shared_ptr<SHAPE_SEGMENT> itemHoleShape;
641 std::shared_ptr<SHAPE_SEGMENT> otherHoleShape;
642 clearance = 0;
643
644 if( aItem->Type() == PCB_VIA_T )
645 {
646 LSET layers = aItem->GetLayerSet();
647
648 if( layers.Contains( F_Cu ) )
649 layers |= LSET( LSET::FrontBoardTechMask() ).set( F_CrtYd );
650
651 if( layers.Contains( B_Cu ) )
652 layers |= LSET( LSET::BackBoardTechMask() ).set( B_CrtYd );
653
654 if( layers.Contains( F_Cu ) && layers.Contains( B_Cu ) )
655 layers |= boardCopperLayers;
656
657 wxCHECK_MSG( layers.Contains( aLayer ), violations,
658 wxT( "Bug! Vias should only be checked for layers on which they exist" ) );
659
660 itemHoleShape = aItem->GetEffectiveHoleShape();
661 }
662 else if( aItem->HasHole() )
663 {
664 itemHoleShape = aItem->GetEffectiveHoleShape();
665 }
666
667 if( aOther->Type() == PCB_VIA_T )
668 {
669 LSET layers = aOther->GetLayerSet();
670
671 if( layers.Contains( F_Cu ) )
672 layers |= LSET( LSET::FrontBoardTechMask() ).set( F_CrtYd );
673
674 if( layers.Contains( B_Cu ) )
675 layers |= LSET( LSET::BackBoardTechMask() ).set( B_CrtYd );
676
677 if( layers.Contains( F_Cu ) && layers.Contains( B_Cu ) )
678 layers |= boardCopperLayers;
679
680 wxCHECK_MSG( layers.Contains( aLayer ), violations,
681 wxT( "Bug! Vias should only be checked for layers on which they exist" ) );
682
683 otherHoleShape = aOther->GetEffectiveHoleShape();
684 }
685 else if( aOther->HasHole() )
686 {
687 otherHoleShape = aOther->GetEffectiveHoleShape();
688 }
689
690 if( itemHoleShape || otherHoleShape )
691 {
692 constraint = m_drcEngine->EvalRules( PHYSICAL_HOLE_CLEARANCE_CONSTRAINT, aOther, aItem, aLayer );
693 clearance = constraint.GetValue().Min();
694 }
695
696 if( constraint.GetSeverity() != RPT_SEVERITY_IGNORE && clearance > 0 )
697 {
698 if( itemHoleShape && itemHoleShape->Collide( otherShape, clearance, &actual, &pos ) )
699 {
700 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_HOLE_CLEARANCE );
701 drcItem->SetErrorDetail( formatMsg( _( "(%s clearance %s; actual %s)" ),
702 constraint.GetName(),
703 clearance ,
704 actual ) );
705 drcItem->SetItems( aItem, aOther );
706 drcItem->SetViolatingRule( constraint.GetParentRule() );
707 reportTwoShapeGeometry( drcItem, pos, itemHoleShape.get(), otherShape, aLayer, actual );
708 ++violations;
709 }
710
711 if( otherHoleShape && otherHoleShape->Collide( aItemShape, clearance, &actual, &pos ) )
712 {
713 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_HOLE_CLEARANCE );
714 drcItem->SetErrorDetail( formatMsg( _( "(%s clearance %s; actual %s)" ),
715 constraint.GetName(),
716 clearance,
717 actual ) );
718 drcItem->SetItems( aItem, aOther );
719 drcItem->SetViolatingRule( constraint.GetParentRule() );
720 reportTwoShapeGeometry( drcItem, pos, otherHoleShape.get(), aItemShape, aLayer, actual );
721 ++violations;
722 }
723 }
724 }
725
726 return violations;
727}
728
729
731 PCB_LAYER_ID aLayer )
732{
733 for( ZONE* zone : m_board->m_DRCZones )
734 {
735 if( !zone->GetLayerSet().test( aLayer ) )
736 continue;
737
738 BOX2I itemBBox = aItem->GetBoundingBox();
739 BOX2I worstCaseBBox = itemBBox;
740
741 worstCaseBBox.Inflate( m_board->m_DRCMaxClearance );
742
743 if( !worstCaseBBox.Intersects( zone->GetBoundingBox() ) )
744 continue;
745
746 bool testClearance = !m_drcEngine->IsErrorLimitExceeded( DRCE_CLEARANCE );
747 bool testHoles = !m_drcEngine->IsErrorLimitExceeded( DRCE_HOLE_CLEARANCE );
748
749 if( !testClearance && !testHoles )
750 return;
751
752 DRC_RTREE* zoneRTree = m_board->m_CopperZoneRTreeCache[ zone ].get();
753 DRC_CONSTRAINT constraint;
754 bool colliding;
755 int clearance = -1;
756 int actual;
757 VECTOR2I pos;
758
759 if( testClearance )
760 {
761 constraint = m_drcEngine->EvalRules( PHYSICAL_CLEARANCE_CONSTRAINT, aItem, zone, aLayer );
762 clearance = constraint.GetValue().Min();
763 }
764
765 if( constraint.GetSeverity() != RPT_SEVERITY_IGNORE && clearance > 0 )
766 {
767 std::shared_ptr<SHAPE> itemShape = aItem->GetEffectiveShape( aLayer );
768
769 if( aItem->Type() == PCB_PAD_T )
770 {
771 PAD* pad = static_cast<PAD*>( aItem );
772
773 if( !pad->FlashLayer( aLayer ) )
774 {
775 if( pad->GetDrillSize().x == 0 && pad->GetDrillSize().y == 0 )
776 continue;
777
778 std::shared_ptr<SHAPE_SEGMENT> hole = pad->GetEffectiveHoleShape();
779 int size = hole->GetWidth();
780
781 itemShape = std::make_shared<SHAPE_SEGMENT>( hole->GetSeg(), size );
782 }
783 }
784
785 if( IsCopperLayer( aLayer ) && zoneRTree )
786 {
787 colliding = zoneRTree->QueryColliding( itemBBox, itemShape.get(), aLayer, clearance,
788 &actual, &pos );
789 }
790 else
791 {
792 colliding = zone->Outline()->Collide( itemShape.get(), clearance, &actual, &pos );
793 }
794
795 if( colliding )
796 {
797 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_CLEARANCE );
798 drcItem->SetErrorDetail( formatMsg( _( "(%s clearance %s; actual %s)" ),
799 constraint.GetName(),
800 clearance,
801 actual ) );
802 drcItem->SetItems( aItem, zone );
803 drcItem->SetViolatingRule( constraint.GetParentRule() );
804 reportTwoItemGeometry( drcItem, pos, aItem, zone, aLayer, actual );
805 }
806 }
807
808 if( testHoles )
809 {
810 std::shared_ptr<SHAPE_SEGMENT> holeShape;
811
812 if( aItem->Type() == PCB_VIA_T )
813 {
814 if( aItem->GetLayerSet().Contains( aLayer ) )
815 holeShape = aItem->GetEffectiveHoleShape();
816 }
817 else if( aItem->HasHole() )
818 {
819 holeShape = aItem->GetEffectiveHoleShape();
820 }
821
822 if( holeShape )
823 {
824 constraint = m_drcEngine->EvalRules( PHYSICAL_HOLE_CLEARANCE_CONSTRAINT, aItem, zone, aLayer );
825 clearance = constraint.GetValue().Min();
826
827 if( constraint.GetSeverity() != RPT_SEVERITY_IGNORE && clearance > 0 )
828 {
829 if( IsCopperLayer( aLayer ) && zoneRTree )
830 {
831 colliding = zoneRTree->QueryColliding( itemBBox, holeShape.get(), aLayer,
832 clearance, &actual, &pos );
833 }
834 else
835 {
836 colliding = zone->Outline()->Collide( holeShape.get(), clearance, &actual, &pos );
837 }
838
839 if( colliding )
840 {
841 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_HOLE_CLEARANCE );
842 drcItem->SetErrorDetail( formatMsg( _( "(%s clearance %s; actual %s)" ),
843 constraint.GetName(),
844 clearance,
845 actual ) );
846 drcItem->SetItems( aItem, zone );
847 drcItem->SetViolatingRule( constraint.GetParentRule() );
848
849 std::shared_ptr<SHAPE> zoneShape = zone->GetEffectiveShape( aLayer );
850 reportTwoShapeGeometry( drcItem, pos, holeShape.get(), zoneShape.get(), aLayer, actual );
851 }
852 }
853 }
854 }
855
856 if( m_drcEngine->IsCancelled() )
857 return;
858 }
859}
860
861
862namespace detail
863{
865}
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
BASE_SET & set(size_t pos)
Definition base_set.h:116
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
virtual bool IsOnLayer(PCB_LAYER_ID aLayer) const
Test to see if this object is on the given layer.
Definition board_item.h:350
virtual std::shared_ptr< SHAPE > GetEffectiveShape(PCB_LAYER_ID aLayer=UNDEFINED_LAYER, FLASHING aFlash=FLASHING::DEFAULT) const
Some pad shapes can be complex (rounded/chamfered rectangle), even without considering custom shapes.
FOOTPRINT * GetParentFootprint() const
virtual LSET GetLayerSet() const
Return a std::bitset of all layers on which the item physically resides.
Definition board_item.h:288
BOARD_ITEM_CONTAINER * GetParent() const
Definition board_item.h:234
virtual std::shared_ptr< SHAPE_SEGMENT > GetEffectiveHoleShape() const
virtual bool HasHole() const
Definition board_item.h:180
constexpr BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition box2.h:558
constexpr bool Intersects(const BOX2< Vec > &aRect) const
Definition box2.h:311
wxString GetName() const
Definition drc_rule.h:204
SEVERITY GetSeverity() const
Definition drc_rule.h:217
const MINOPTMAX< int > & GetValue() const
Definition drc_rule.h:196
DRC_RULE * GetParentRule() const
Definition drc_rule.h:200
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Definition drc_item.cpp:407
Implement an R-tree for fast spatial and layer indexing of connectable items.
Definition drc_rtree.h:50
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:229
void testZoneLayer(ZONE *aZone, PCB_LAYER_ID aLayer, DRC_CONSTRAINT &aConstraint)
virtual ~DRC_TEST_PROVIDER_PHYSICAL_CLEARANCE()=default
void testItemAgainstZones(BOARD_ITEM *aItem, PCB_LAYER_ID aLayer)
virtual bool Run() override
Run this provider against the given PCB with configured options (if any).
void testShapeLineChain(const SHAPE_LINE_CHAIN &aOutline, int aLineWidth, PCB_LAYER_ID aLayer, BOARD_ITEM *aParentItem, DRC_CONSTRAINT &aConstraint)
int testItemAgainstItem(BOARD_ITEM *aItem, SHAPE *aItemShape, PCB_LAYER_ID aLayer, BOARD_ITEM *aOther)
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)
int forEachGeometryItem(const std::vector< KICAD_T > &aTypes, const LSET &aLayers, const std::function< bool(BOARD_ITEM *)> &aFunc)
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 *){})
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)
double AsRadians() const
Definition eda_angle.h:120
virtual const BOX2I GetBoundingBox() const
Return the orthogonal bounding box of this object for display purposes.
Definition eda_item.cpp:120
const KIID m_Uuid
Definition eda_item.h:528
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:112
EDA_ANGLE GetArcAngle() const
SHAPE_POLY_SET & GetPolyShape()
int GetRadius() const
SHAPE_T GetShape() const
Definition eda_shape.h:181
void RebuildBezierToSegmentsPointsList(int aMaxError)
Rebuild the m_bezierPoints vertex list that approximate the Bezier curve by a list of segments.
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition eda_shape.h:186
const std::vector< VECTOR2I > & GetBezierPoints() const
Definition eda_shape.h:333
wxString SHAPE_T_asString() const
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
static const LSET & AllCuMask()
return AllCuMask( MAX_CU_LAYERS );
Definition lset.cpp:608
static const LSET & FrontBoardTechMask()
Return a mask holding technical layers used in a board fabrication (no CU layer) on front side.
Definition lset.cpp:669
static const LSET & AllLayersMask()
Definition lset.cpp:641
static const LSET & PhysicalLayersMask()
Return a mask holding all layers which are physically realized.
Definition lset.cpp:697
static const LSET & BackBoardTechMask()
Return a mask holding technical layers used in a board fabrication (no CU layer) on Back side.
Definition lset.cpp:655
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:33
Definition pad.h:55
VECTOR2I GetCenter() const override
This defaults to the center of the bounding box if not overridden.
Definition pcb_shape.h:81
int GetWidth() const override
Definition seg.h:42
VECTOR2I A
Definition seg.h:49
VECTOR2I B
Definition seg.h:50
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Compute a point on the segment (this) that is closest to point aP.
Definition seg.cpp:633
static SEG::ecoord Square(int a)
Definition seg.h:123
bool Collide(const SEG &aSeg, int aClearance, int *aActual=nullptr) const
Definition seg.cpp:542
ecoord SquaredLength() const
Definition seg.h:348
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
bool IsClosed() const override
virtual bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if point aP lies closer to us than aClearance.
SEG Segment(int aIndex) const
Return a copy of the aIndex-th segment in the line chain.
void Append(int aX, int aY, bool aAllowDuplication=false)
Append a new point at the end of the line chain.
int SegmentCount() const
Return the number of segments in this line chain.
const SEG CSegment(int aIndex) const
Return a constant copy of the aIndex segment in the line chain.
Represent a set of closed polygons.
bool Collide(const SHAPE *aShape, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if the boundary of shape (this) lies closer to the shape aShape than aClearance,...
int HoleCount(int aOutline) const
Returns the number of holes in a given outline.
void Simplify()
Simplify the polyset (merges overlapping polys, eliminates degeneracy/self-intersections)
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
SHAPE_LINE_CHAIN & Hole(int aOutline, int aHole)
Return the reference to aHole-th hole in the aIndex-th outline.
int OutlineCount() const
Return the number of outlines in the set.
SHAPE_POLY_SET CloneDropTriangulation() const
An abstract shape on 2D plane.
Definition shape.h:126
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:74
bool GetIsRuleArea() const
Accessors to parameters used in Rule Area zones:
Definition zone.h:716
std::shared_ptr< SHAPE_POLY_SET > GetFilledPolysList(PCB_LAYER_ID aLayer) const
Definition zone.h:602
const BOX2I GetBoundingBox() const override
Definition zone.cpp:651
SHAPE_POLY_SET * Outline()
Definition zone.h:336
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition zone.h:137
virtual std::shared_ptr< SHAPE > GetEffectiveShape(PCB_LAYER_ID aLayer=UNDEFINED_LAYER, FLASHING aFlash=FLASHING::DEFAULT) const override
Some pad shapes can be complex (rounded/chamfered rectangle), even without considering custom shapes.
Definition zone.cpp:1643
The common library.
@ DRCE_HOLE_CLEARANCE
Definition drc_item.h:55
@ DRCE_CLEARANCE
Definition drc_item.h:44
#define ATOMIC_TABLES
Definition drc_rtree.h:43
@ PHYSICAL_HOLE_CLEARANCE_CONSTRAINT
Definition drc_rule.h:84
@ PHYSICAL_CLEARANCE_CONSTRAINT
Definition drc_rule.h:83
#define REPORT_AUX(s)
#define _(s)
@ SEGMENT
Definition eda_shape.h:47
@ RECTANGLE
Use RECTANGLE instead of RECT to avoid collision in a Windows header.
Definition eda_shape.h:48
a few functions useful in geometry calculations.
int GetArcToSegmentCount(int aRadius, int aErrorMax, const EDA_ANGLE &aArcAngle)
bool IsCopperLayer(int aLayerId)
Test whether a layer is a copper layer.
Definition layer_ids.h:679
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
@ F_CrtYd
Definition layer_ids.h:116
@ Edge_Cuts
Definition layer_ids.h:112
@ B_Cu
Definition layer_ids.h:65
@ B_CrtYd
Definition layer_ids.h:115
@ F_Cu
Definition layer_ids.h:64
This file contains miscellaneous commonly used macros and functions.
#define UNIMPLEMENTED_FOR(type)
Definition macros.h:96
static DRC_REGISTER_TEST_PROVIDER< DRC_TEST_PROVIDER_ANNULAR_WIDTH > dummy
@ RPT_SEVERITY_IGNORE
const double epsilon
VECTOR2I center
int clearance
int actual
#define M_PI
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition trigo.cpp:229
double DEG2RAD(double deg)
Definition trigo.h:166
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition typeinfo.h:85
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:94
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition typeinfo.h:90
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition typeinfo.h:105
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition typeinfo.h:89
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition typeinfo.h:87
@ PCB_BARCODE_T
class PCB_BARCODE, a barcode (graphic item)
Definition typeinfo.h:98
@ PCB_TABLECELL_T
class PCB_TABLECELL, PCB_TEXTBOX for use in tables
Definition typeinfo.h:92
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition typeinfo.h:83
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition typeinfo.h:84
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition typeinfo.h:95
@ PCB_DIMENSION_T
class PCB_DIMENSION_BASE: abstract dimension meta-type
Definition typeinfo.h:97
@ PCB_TABLE_T
class PCB_TABLE, table of PCB_TABLECELLs
Definition typeinfo.h:91
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:93
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687