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