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 );
163
164 return true;
165 } );
166
167 std::unordered_map<PTR_PTR_CACHE_KEY, LSET> checkedPairs;
168 progressDelta = 100;
169 ii = 0;
170
171 //
172 // Run clearance checks -between- items.
173 //
174
175 if( !m_drcEngine->IsErrorLimitExceeded( DRCE_CLEARANCE )
176 || !m_drcEngine->IsErrorLimitExceeded( DRCE_HOLE_CLEARANCE ) )
177 {
178 if( !reportPhase( _( "Checking physical clearances..." ) ) )
179 return false; // DRC cancelled
180
182 [&]( BOARD_ITEM* item ) -> bool
183 {
184 if( isInvisibleText( item ) )
185 return true;
186
187 if( !reportProgress( ii++, count, progressDelta ) )
188 return false;
189
190 LSET layers = item->GetLayerSet();
191
192 if( item->Type() == PCB_FOOTPRINT_T )
193 layers = courtyards;
194
195 for( PCB_LAYER_ID layer : layers )
196 {
197 std::shared_ptr<SHAPE> itemShape = item->GetEffectiveShape( layer );
198
199 m_itemTree.QueryColliding( item, layer, layer,
200 // Filter:
201 [&]( BOARD_ITEM* other ) -> bool
202 {
203 BOARD_ITEM* a = item;
204 BOARD_ITEM* b = other;
205
206 // store canonical order so we don't collide in both
207 // directions (a:b and b:a)
208 if( static_cast<void*>( a ) > static_cast<void*>( b ) )
209 std::swap( a, b );
210
211 auto it = checkedPairs.find( { a, b } );
212
213 if( it != checkedPairs.end() && it->second.test( layer ) )
214 {
215 return false;
216 }
217 else
218 {
219 checkedPairs[ { a, b } ].set( layer );
220 return true;
221 }
222 },
223 // Visitor:
224 [&]( BOARD_ITEM* other ) -> bool
225 {
226 if( testItemAgainstItem( item, itemShape.get(), layer,
227 other ) > 0 )
228 {
229 BOARD_ITEM* a = item;
230 BOARD_ITEM* b = other;
231
232 // store canonical order
233 if( static_cast<void*>( a ) > static_cast<void*>( b ) )
234 std::swap( a, b );
235
236 // Once we record one DRC for error for physical clearance
237 // we don't need to record more
238 checkedPairs[ { a, b } ].set();
239 }
240
241 return !m_drcEngine->IsCancelled();
242 },
243 m_board->m_DRCMaxPhysicalClearance );
244
245 testItemAgainstZones( item, layer );
246 }
247
248 return true;
249 } );
250 }
251
252 progressDelta = 100;
253 count = 0;
254 ii = 0;
255
256 //
257 // Generate a count for progress reporting.
258 //
259
260 forEachGeometryItem( { PCB_ZONE_T, PCB_SHAPE_T }, boardCopperLayers,
261 [&]( BOARD_ITEM* item ) -> bool
262 {
263 ZONE* zone = dynamic_cast<ZONE*>( item );
264
265 if( zone && zone->GetIsRuleArea() )
266 return true; // Continue with other items
267
268 count += ( item->GetLayerSet() & boardCopperLayers ).count();
269
270 return true;
271 } );
272
273 //
274 // Run clearance checks -within- polygonal items.
275 //
276
277 forEachGeometryItem( { PCB_ZONE_T, PCB_SHAPE_T }, boardCopperLayers,
278 [&]( BOARD_ITEM* item ) -> bool
279 {
280 PCB_SHAPE* shape = dynamic_cast<PCB_SHAPE*>( item );
281 ZONE* zone = dynamic_cast<ZONE*>( item );
282
283 if( zone && zone->GetIsRuleArea() )
284 return true; // Continue with other items
285
286 for( PCB_LAYER_ID layer : item->GetLayerSet() )
287 {
288 if( IsCopperLayer( layer ) )
289 {
290 if( !reportProgress( ii++, count, progressDelta ) )
291 return false;
292
293 DRC_CONSTRAINT c = m_drcEngine->EvalRules( PHYSICAL_CLEARANCE_CONSTRAINT, item, nullptr,
294 layer );
295
296 if( shape )
297 {
298 switch( shape->GetShape() )
299 {
300 case SHAPE_T::POLY:
301 testShapeLineChain( shape->GetPolyShape().Outline( 0 ), shape->GetWidth(), layer,
302 item, c );
303 break;
304
305 case SHAPE_T::BEZIER:
306 {
307 SHAPE_LINE_CHAIN asPoly;
308
309 shape->RebuildBezierToSegmentsPointsList( errorMax );
310
311 for( const VECTOR2I& pt : shape->GetBezierPoints() )
312 asPoly.Append( pt );
313
314 testShapeLineChain( asPoly, shape->GetWidth(), layer, item, c );
315 break;
316 }
317
318 case SHAPE_T::ARC:
319 {
320 SHAPE_LINE_CHAIN asPoly;
321
322 VECTOR2I center = shape->GetCenter();
323 EDA_ANGLE angle = -shape->GetArcAngle();
324 double r = shape->GetRadius();
325 int steps = GetArcToSegmentCount( r, errorMax, angle );
326
327 asPoly.Append( shape->GetStart() );
328
329 for( int step = 1; step <= steps; ++step )
330 {
331 EDA_ANGLE rotation = ( angle * step ) / steps;
332 VECTOR2I pt = shape->GetStart();
333
334 RotatePoint( pt, center, rotation );
335 asPoly.Append( pt );
336 }
337
338 testShapeLineChain( asPoly, shape->GetWidth(), layer, item, c );
339 break;
340 }
341
342 // Simple shapes can't create self-intersections, and I'm not sure a user
343 // would want a report that one side of their rectangle was too close to
344 // the other side.
346 case SHAPE_T::SEGMENT:
347 case SHAPE_T::CIRCLE:
348 break;
349
350 default:
352 }
353 }
354
355 if( zone )
356 testZoneLayer( static_cast<ZONE*>( item ), layer, c );
357 }
358
359 if( m_drcEngine->IsCancelled() )
360 return false;
361 }
362
363 return !m_drcEngine->IsCancelled();
364 } );
365
366 return !m_drcEngine->IsCancelled();
367}
368
369
371 int aLineWidth, PCB_LAYER_ID aLayer,
372 BOARD_ITEM* aParentItem,
373 DRC_CONSTRAINT& aConstraint )
374{
375 // We don't want to collide with neighboring segments forming a curve until the concavity
376 // approaches 180 degrees.
377 double angleTolerance = DEG2RAD( 180.0 - ADVANCED_CFG::GetCfg().m_SliverAngleTolerance );
378 int epsilon = m_board->GetDesignSettings().GetDRCEpsilon();
379 int count = aOutline.SegmentCount();
380 int clearance = aConstraint.GetValue().Min();
381
382 if( aConstraint.GetSeverity() == RPT_SEVERITY_IGNORE || clearance - epsilon <= 0 )
383 return;
384
385 // Trigonometry is not cheap; cache seg angles
386 std::vector<double> angles;
387 angles.reserve( count );
388
389 auto angleDiff =
390 []( double a, double b ) -> double
391 {
392 if( a > b )
393 std::swap( a, b );
394
395 double diff = b - a;
396
397 if( diff > M_PI )
398 return 2 * M_PI - diff;
399 else
400 return diff;
401 };
402
403 for( int ii = 0; ii < count; ++ii )
404 {
405 const SEG& seg = aOutline.CSegment( ii );
406
407 // NB: don't store angles of really short segments (which could point anywhere)
408
409 if( seg.SquaredLength() > SEG::Square( epsilon * 2 ) )
410 {
411 angles.push_back( EDA_ANGLE( seg.B - seg.A ).AsRadians() );
412 }
413 else if( ii > 0 )
414 {
415 angles.push_back( angles.back() );
416 }
417 else
418 {
419 for( int jj = 1; jj < count; ++jj )
420 {
421 const SEG& following = aOutline.CSegment( jj );
422
423 if( following.SquaredLength() > SEG::Square( epsilon * 2 ) || jj == count - 1 )
424 {
425 angles.push_back( EDA_ANGLE( following.B - following.A ).AsRadians() );
426 break;
427 }
428 }
429 }
430 }
431
432 // Find collisions before reporting so that we can condense them into fewer reports.
433 std::vector< std::pair<VECTOR2I, int> > collisions;
434
435 for( int ii = 0; ii < count; ++ii )
436 {
437 const SEG seg = aOutline.CSegment( ii );
438 double segAngle = angles[ ii ];
439
440 // Exclude segments on either side of us until we reach the angle tolerance
441 int firstCandidate = ii + 1;
442 int lastCandidate = count - 1;
443
444 while( firstCandidate < count )
445 {
446 if( angleDiff( segAngle, angles[ firstCandidate ] ) < angleTolerance )
447 firstCandidate++;
448 else
449 break;
450 }
451
452 if( aOutline.IsClosed() )
453 {
454 if( ii > 0 )
455 lastCandidate = ii - 1;
456
457 while( lastCandidate != std::min( firstCandidate, count - 1 ) )
458 {
459 if( angleDiff( segAngle, angles[ lastCandidate ] ) < angleTolerance )
460 lastCandidate = ( lastCandidate == 0 ) ? count - 1 : lastCandidate - 1;
461 else
462 break;
463 }
464 }
465
466 // Now run the collision between seg and each candidate seg in the candidate range.
467 if( lastCandidate < ii )
468 lastCandidate = count - 1;
469
470 for( int jj = firstCandidate; jj <= lastCandidate; ++jj )
471 {
472 const SEG candidate = aOutline.CSegment( jj );
473 int actual;
474
475 if( seg.Collide( candidate, clearance + aLineWidth - epsilon, &actual ) )
476 {
477 VECTOR2I firstPoint = seg.NearestPoint( candidate );
478 VECTOR2I secondPoint = candidate.NearestPoint( seg );
479 VECTOR2I pos = ( firstPoint + secondPoint ) / 2;
480
481 if( !collisions.empty() && ( pos - collisions.back().first ).EuclideanNorm() < clearance * 2 )
482 {
483 if( actual < collisions.back().second )
484 {
485 collisions.back().first = pos;
486 collisions.back().second = actual;
487 }
488
489 continue;
490 }
491
492 collisions.push_back( { pos, actual } );
493 }
494 }
495 }
496
497 for( const std::pair<VECTOR2I, int>& collision : collisions )
498 {
499 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_CLEARANCE );
500 VECTOR2I pt = collision.first;
501
502 if( FOOTPRINT* parentFP = aParentItem->GetParentFootprint() )
503 {
504 RotatePoint( pt, parentFP->GetOrientation() );
505 pt += parentFP->GetPosition();
506 }
507
508 wxString msg = formatMsg( _( "Internal clearance violation (%s clearance %s; actual %s)" ),
509 aConstraint.GetName(),
510 clearance,
511 collision.second );
512
513 drcItem->SetErrorMessage( msg );
514 drcItem->SetItems( aParentItem );
515 drcItem->SetViolatingRule( aConstraint.GetParentRule() );
516
517 reportViolation( drcItem, pt, aLayer );
518 }
519}
520
521
523 DRC_CONSTRAINT& aConstraint )
524{
525 int epsilon = m_board->GetDesignSettings().GetDRCEpsilon();
526 int clearance = aConstraint.GetValue().Min();
527
528 if( aConstraint.GetSeverity() == RPT_SEVERITY_IGNORE || clearance - epsilon <= 0 )
529 return;
530
532
533 // Turn fractured fill into outlines and holes
534 fill.Simplify();
535
536 for( int outlineIdx = 0; outlineIdx < fill.OutlineCount(); ++outlineIdx )
537 {
538 SHAPE_LINE_CHAIN* firstOutline = &fill.Outline( outlineIdx );
539
540 //
541 // Step one: outline to outline clearance violations
542 //
543
544 for( int ii = outlineIdx + 1; ii < fill.OutlineCount(); ++ii )
545 {
546 SHAPE_LINE_CHAIN* secondOutline = &fill.Outline( ii );
547
548 for( int jj = 0; jj < secondOutline->SegmentCount(); ++jj )
549 {
550 SEG secondSeg = secondOutline->Segment( jj );
551 int actual;
552 VECTOR2I pos;
553
554 if( firstOutline->Collide( secondSeg, clearance - epsilon, &actual, &pos ) )
555 {
556 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_CLEARANCE );
557 wxString msg = formatMsg( _( "(%s clearance %s; actual %s)" ),
558 aConstraint.GetName(),
559 clearance,
560 actual );
561
562 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
563 drcItem->SetItems( aZone );
564 drcItem->SetViolatingRule( aConstraint.GetParentRule() );
565 reportViolation( drcItem, pos, aLayer );
566 }
567 }
568
569 if( m_drcEngine->IsCancelled() )
570 return;
571 }
572
573 //
574 // Step two: interior hole clearance violations
575 //
576
577 for( int holeIdx = 0; holeIdx < fill.HoleCount( outlineIdx ); ++holeIdx )
578 {
579 testShapeLineChain( fill.Hole( outlineIdx, holeIdx ), 0, aLayer, aZone, aConstraint );
580
581 if( m_drcEngine->IsCancelled() )
582 return;
583 }
584 }
585}
586
587
589 PCB_LAYER_ID aLayer, BOARD_ITEM* aOther )
590{
591 bool testClearance = !m_drcEngine->IsErrorLimitExceeded( DRCE_CLEARANCE );
592 bool testHoles = !m_drcEngine->IsErrorLimitExceeded( DRCE_HOLE_CLEARANCE );
593 DRC_CONSTRAINT constraint;
594 int clearance = 0;
595 int actual;
596 int violations = 0;
597 VECTOR2I pos;
598 LSET boardCopperLayers = LSET::AllCuMask( m_board->GetCopperLayerCount() );
599
600 std::shared_ptr<SHAPE> otherShapeStorage = aOther->GetEffectiveShape( aLayer );
601 SHAPE* otherShape = otherShapeStorage.get();
602
603 if( testClearance )
604 {
605 constraint = m_drcEngine->EvalRules( PHYSICAL_CLEARANCE_CONSTRAINT, aItem, aOther, aLayer );
606 clearance = constraint.GetValue().Min();
607 }
608
609 if( constraint.GetSeverity() != RPT_SEVERITY_IGNORE && clearance > 0 )
610 {
611 // Collide (and generate violations) based on a well-defined order so that exclusion
612 // checking against previously-generated violations will work.
613 if( aItem->m_Uuid > aOther->m_Uuid )
614 {
615 std::swap( aItem, aOther );
616 std::swap( aItemShape, otherShape );
617 }
618
619 if( aItemShape->Collide( otherShape, clearance, &actual, &pos ) )
620 {
621 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_CLEARANCE );
622 wxString msg = formatMsg( _( "(%s clearance %s; actual %s)" ),
623 constraint.GetName(),
624 clearance,
625 actual );
626
627 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
628 drcItem->SetItems( aItem, aOther );
629 drcItem->SetViolatingRule( constraint.GetParentRule() );
630 reportTwoShapeGeometry( drcItem, pos, aItemShape, otherShape, aLayer, actual );
631 ++violations;
632 }
633 }
634
635 if( testHoles )
636 {
637 std::shared_ptr<SHAPE_SEGMENT> itemHoleShape;
638 std::shared_ptr<SHAPE_SEGMENT> otherHoleShape;
639 clearance = 0;
640
641 if( aItem->Type() == PCB_VIA_T )
642 {
643 LSET layers = aItem->GetLayerSet();
644
645 if( layers.Contains( F_Cu ) )
646 layers |= LSET( LSET::FrontBoardTechMask() ).set( F_CrtYd );
647
648 if( layers.Contains( B_Cu ) )
649 layers |= LSET( LSET::BackBoardTechMask() ).set( B_CrtYd );
650
651 if( layers.Contains( F_Cu ) && layers.Contains( B_Cu ) )
652 layers |= boardCopperLayers;
653
654 wxCHECK_MSG( layers.Contains( aLayer ), violations,
655 wxT( "Bug! Vias should only be checked for layers on which they exist" ) );
656
657 itemHoleShape = aItem->GetEffectiveHoleShape();
658 }
659 else if( aItem->HasHole() )
660 {
661 itemHoleShape = aItem->GetEffectiveHoleShape();
662 }
663
664 if( aOther->Type() == PCB_VIA_T )
665 {
666 LSET layers = aOther->GetLayerSet();
667
668 if( layers.Contains( F_Cu ) )
669 layers |= LSET( LSET::FrontBoardTechMask() ).set( F_CrtYd );
670
671 if( layers.Contains( B_Cu ) )
672 layers |= LSET( LSET::BackBoardTechMask() ).set( B_CrtYd );
673
674 if( layers.Contains( F_Cu ) && layers.Contains( B_Cu ) )
675 layers |= boardCopperLayers;
676
677 wxCHECK_MSG( layers.Contains( aLayer ), violations,
678 wxT( "Bug! Vias should only be checked for layers on which they exist" ) );
679
680 otherHoleShape = aOther->GetEffectiveHoleShape();
681 }
682 else if( aOther->HasHole() )
683 {
684 otherHoleShape = aOther->GetEffectiveHoleShape();
685 }
686
687 if( itemHoleShape || otherHoleShape )
688 {
689 constraint = m_drcEngine->EvalRules( PHYSICAL_HOLE_CLEARANCE_CONSTRAINT, aOther, aItem, aLayer );
690 clearance = constraint.GetValue().Min();
691 }
692
693 if( constraint.GetSeverity() != RPT_SEVERITY_IGNORE && clearance > 0 )
694 {
695 if( itemHoleShape && itemHoleShape->Collide( otherShape, clearance, &actual, &pos ) )
696 {
697 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_HOLE_CLEARANCE );
698 wxString msg = formatMsg( _( "(%s clearance %s; actual %s)" ),
699 constraint.GetName(),
700 clearance ,
701 actual );
702
703 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
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 wxString msg = formatMsg( _( "(%s clearance %s; actual %s)" ),
714 constraint.GetName(),
715 clearance,
716 actual );
717
718 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
719 drcItem->SetItems( aItem, aOther );
720 drcItem->SetViolatingRule( constraint.GetParentRule() );
721 reportTwoShapeGeometry( drcItem, pos, otherHoleShape.get(), aItemShape, aLayer, actual );
722 ++violations;
723 }
724 }
725 }
726
727 return violations;
728}
729
730
732 PCB_LAYER_ID aLayer )
733{
734 for( ZONE* zone : m_board->m_DRCZones )
735 {
736 if( !zone->GetLayerSet().test( aLayer ) )
737 continue;
738
739 BOX2I itemBBox = aItem->GetBoundingBox();
740 BOX2I worstCaseBBox = itemBBox;
741
742 worstCaseBBox.Inflate( m_board->m_DRCMaxClearance );
743
744 if( !worstCaseBBox.Intersects( zone->GetBoundingBox() ) )
745 continue;
746
747 bool testClearance = !m_drcEngine->IsErrorLimitExceeded( DRCE_CLEARANCE );
748 bool testHoles = !m_drcEngine->IsErrorLimitExceeded( DRCE_HOLE_CLEARANCE );
749
750 if( !testClearance && !testHoles )
751 return;
752
753 DRC_RTREE* zoneRTree = m_board->m_CopperZoneRTreeCache[ zone ].get();
754 DRC_CONSTRAINT constraint;
755 bool colliding;
756 int clearance = -1;
757 int actual;
758 VECTOR2I pos;
759
760 if( testClearance )
761 {
762 constraint = m_drcEngine->EvalRules( PHYSICAL_CLEARANCE_CONSTRAINT, aItem, zone, aLayer );
763 clearance = constraint.GetValue().Min();
764 }
765
766 if( constraint.GetSeverity() != RPT_SEVERITY_IGNORE && clearance > 0 )
767 {
768 std::shared_ptr<SHAPE> itemShape = aItem->GetEffectiveShape( aLayer );
769
770 if( aItem->Type() == PCB_PAD_T )
771 {
772 PAD* pad = static_cast<PAD*>( aItem );
773
774 if( !pad->FlashLayer( aLayer ) )
775 {
776 if( pad->GetDrillSize().x == 0 && pad->GetDrillSize().y == 0 )
777 continue;
778
779 std::shared_ptr<SHAPE_SEGMENT> hole = pad->GetEffectiveHoleShape();
780 int size = hole->GetWidth();
781
782 itemShape = std::make_shared<SHAPE_SEGMENT>( hole->GetSeg(), size );
783 }
784 }
785
786 if( IsCopperLayer( aLayer ) && zoneRTree )
787 {
788 colliding = zoneRTree->QueryColliding( itemBBox, itemShape.get(), aLayer, clearance,
789 &actual, &pos );
790 }
791 else
792 {
793 colliding = zone->Outline()->Collide( itemShape.get(), clearance, &actual, &pos );
794 }
795
796 if( colliding )
797 {
798 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_CLEARANCE );
799 wxString msg = formatMsg( _( "(%s clearance %s; actual %s)" ),
800 constraint.GetName(),
801 clearance,
802 actual );
803
804 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
805 drcItem->SetItems( aItem, zone );
806 drcItem->SetViolatingRule( constraint.GetParentRule() );
807 reportTwoItemGeometry( drcItem, pos, aItem, zone, aLayer, actual );
808 }
809 }
810
811 if( testHoles )
812 {
813 std::shared_ptr<SHAPE_SEGMENT> holeShape;
814
815 if( aItem->Type() == PCB_VIA_T )
816 {
817 if( aItem->GetLayerSet().Contains( aLayer ) )
818 holeShape = aItem->GetEffectiveHoleShape();
819 }
820 else if( aItem->HasHole() )
821 {
822 holeShape = aItem->GetEffectiveHoleShape();
823 }
824
825 if( holeShape )
826 {
827 constraint = m_drcEngine->EvalRules( PHYSICAL_HOLE_CLEARANCE_CONSTRAINT, aItem, zone, aLayer );
828 clearance = constraint.GetValue().Min();
829
830 if( constraint.GetSeverity() != RPT_SEVERITY_IGNORE && clearance > 0 )
831 {
832 if( IsCopperLayer( aLayer ) && zoneRTree )
833 {
834 colliding = zoneRTree->QueryColliding( itemBBox, holeShape.get(), aLayer,
835 clearance, &actual, &pos );
836 }
837 else
838 {
839 colliding = zone->Outline()->Collide( holeShape.get(), clearance, &actual, &pos );
840 }
841
842 if( colliding )
843 {
844 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_HOLE_CLEARANCE );
845 wxString msg = formatMsg( _( "(%s clearance %s; actual %s)" ),
846 constraint.GetName(),
847 clearance,
848 actual );
849
850 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
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: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:174
SEVERITY GetSeverity() const
Definition drc_rule.h:187
const MINOPTMAX< int > & GetValue() const
Definition drc_rule.h:166
DRC_RULE * GetParentRule() const
Definition drc_rule.h:170
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)
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: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 & GetStart() const
Return the starting point of the graphic.
Definition eda_shape.h:173
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
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
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:1568
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:676
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:108
@ 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_BARCODE_T
class PCB_BARCODE, a barcode (graphic item)
Definition typeinfo.h:101
@ 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