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