25#include <math_for_graphics.h>
69 virtual bool Run()
override;
71 virtual const wxString
GetName()
const override
73 return wxT(
"clearance" );
78 return wxT(
"Tests copper item clearance" );
126 reportAux( wxT(
"No Clearance constraints found. Tests not run." ) );
134 if( !
reportPhase(
_(
"Checking track & via clearances..." ) ) )
141 if( !
reportPhase(
_(
"Checking hole clearances..." ) ) )
165 if( !
reportPhase(
_(
"Checking copper zone clearances..." ) ) )
194 bool has_error =
false;
203 testClearance =
false;
223 drcItem->SetItems( track, other );
242 wxString msg =
formatMsg(
_(
"(%s clearance %s; actual %s)" ),
247 drce->SetErrorMessage( drce->GetErrorText() + wxS(
" " ) + msg );
248 drce->SetItems( track, other );
262 std::array<BOARD_ITEM*, 2> a{ track, other };
263 std::array<BOARD_ITEM*, 2> b{ other, track };
264 std::array<SHAPE*, 2> a_shape{ trackShape, otherShape.get() };
266 for(
size_t ii = 0; ii < 2; ++ii )
268 std::shared_ptr<SHAPE_SEGMENT> holeShape;
272 if( !(
dynamic_cast<PCB_TRACK*
>( a[ii] ) ) || !b[ii]->HasHole() )
278 if( b[ii]->GetLayerSet().Contains( layer ) )
279 holeShape = b[ii]->GetEffectiveHoleShape();
283 holeShape = b[ii]->GetEffectiveHoleShape();
297 wxString msg =
formatMsg( clearance ?
_(
"(%s clearance %s; actual %s)" )
298 :
_(
"(%s clearance %s; actual < 0)" ),
303 drce->SetErrorMessage( drce->GetErrorText() + wxS(
" " ) + msg );
304 drce->SetItems( a[ii], b[ii] );
331 BOX2I worstCaseBBox = itemBBox;
341 if( !testClearance && !testHoles )
357 bool flashedPad =
pad->FlashLayer( aLayer );
360 if( !flashedPad && !platedHole )
361 testClearance =
false;
375 std::max( 0, clearance -
m_drcEpsilon ), &actual, &pos ) )
378 wxString msg =
formatMsg(
_(
"(%s clearance %s; actual %s)" ),
383 drce->SetErrorMessage( drce->GetErrorText() + wxS(
" " ) + msg );
384 drce->SetItems( aItem, aZone );
391 if( testHoles && aItem->
HasHole() )
393 std::shared_ptr<SHAPE_SEGMENT> holeShape;
417 wxString msg =
formatMsg(
_(
"(%s clearance %s; actual %s)" ),
422 drce->SetErrorMessage( drce->GetErrorText() + wxS(
" " ) + msg );
423 drce->SetItems( aItem, aZone );
437 const int progressDelta = 100;
442 std::map<BOARD_ITEM*, int> freePadsUsageMap;
443 std::unordered_map<PTR_PTR_CACHE_KEY, layers_checked> checkedPairs;
452 std::shared_ptr<SHAPE> trackShape = track->GetEffectiveShape( layer );
460 if( otherCItem && otherCItem->GetNetCode() == track->GetNetCode() )
468 if(
static_cast<void*
>( a ) >
static_cast<void*
>( b ) )
471 auto it = checkedPairs.find( { a, b } );
473 if( it != checkedPairs.end() && ( it->second.layers.test( layer )
480 checkedPairs[ { a, b } ].layers.set( layer );
491 auto it = freePadsUsageMap.find( other );
493 if( it == freePadsUsageMap.end() )
495 freePadsUsageMap[ other ] = track->GetNetCode();
498 else if( it->second == track->GetNetCode() )
510 if(
static_cast<void*
>( a ) >
static_cast<void*
>( b ) )
513 auto it = checkedPairs.find( { a, b } );
519 if( it != checkedPairs.end() )
520 it->second.has_error =
true;
554 int padGroupIdx = padToNetTieGroupMap[
pad->GetNumber() ];
558 PAD* otherPad =
static_cast<PAD*
>( other );
560 if( padGroupIdx >= 0 && padGroupIdx == padToNetTieGroupMap[ otherPad->
GetNumber() ] )
561 testClearance =
false;
563 if(
pad->SameLogicalPadAs( otherPad ) )
568 testClearance =
false;
571 PAD* otherPad =
nullptr;
575 otherPad =
static_cast<PAD*
>( other );
578 otherVia =
static_cast<PCB_VIA*
>( other );
581 testClearance =
false;
585 testClearance =
false;
588 testClearance =
false;
592 testClearance =
false;
594 int padNet =
pad->GetNetCode();
595 int otherPadNet = otherPad ? otherPad->
GetNetCode() : 0;
596 int otherViaNet = otherVia ? otherVia->
GetNetCode() : 0;
599 if( ( otherPadNet && otherPadNet == padNet ) || ( otherViaNet && otherViaNet == padNet ) )
601 testClearance =
false;
605 if( !(
pad->GetDrillSize().x > 0 )
607 && !( otherVia && otherVia->
GetDrill() > 0 ) )
612 if( !testClearance && !testShorting && !testHoles )
621 if( otherPad &&
pad->SameLogicalPadAs( otherPad ) )
633 msg.Printf(
_(
"(nets %s and %s)" ),
637 drce->SetErrorMessage( drce->GetErrorText() + wxS(
" " ) + msg );
638 drce->SetItems(
pad, otherPad );
664 wxString msg =
formatMsg(
_(
"(%s clearance %s; actual %s)" ),
669 drce->SetErrorMessage( drce->GetErrorText() + wxS(
" " ) + msg );
670 drce->SetItems(
pad, other );
689 if( testHoles && otherPad &&
pad->FlashLayer( aLayer ) && otherPad->
HasHole() )
696 wxString msg =
formatMsg(
_(
"(%s clearance %s; actual %s)" ),
701 drce->SetErrorMessage( drce->GetErrorText() + wxS(
" " ) + msg );
702 drce->SetItems(
pad, other );
710 if( testHoles && otherPad && otherPad->
FlashLayer( aLayer ) &&
pad->HasHole() )
712 if( clearance > 0 && otherShape->Collide(
pad->GetEffectiveHoleShape().get(),
717 wxString msg =
formatMsg(
_(
"(%s clearance %s; actual %s)" ),
722 drce->SetErrorMessage( drce->GetErrorText() + wxS(
" " ) + msg );
723 drce->SetItems(
pad, other );
731 if( testHoles && otherVia && otherVia->
IsOnLayer( aLayer ) )
738 wxString msg =
formatMsg(
_(
"(%s clearance %s; actual %s)" ),
743 drce->SetErrorMessage( drce->GetErrorText() + wxS(
" " ) + msg );
744 drce->SetItems(
pad, otherVia );
757 const int progressDelta = 100;
762 count += footprint->Pads().size();
764 reportAux( wxT(
"Testing %d pads..." ), count );
766 std::unordered_map<PTR_PTR_CACHE_KEY, int> checkedPairs;
770 for(
PAD*
pad : footprint->Pads() )
774 std::shared_ptr<SHAPE> padShape =
pad->GetEffectiveShape( layer );
785 if(
static_cast<void*
>( a ) >
static_cast<void*
>( b ) )
788 if( checkedPairs.find( { a, b } ) != checkedPairs.end() )
794 checkedPairs[ { a, b } ] = 1;
826 const int progressDelta = 50;
834 int zone2zoneClearance;
837 boardOutline = &buffer;
839 for(
int layer_id =
F_Cu; layer_id <=
B_Cu; ++layer_id )
842 std::vector<SHAPE_POLY_SET> smoothed_polys;
904 for(
auto it = smoothed_polys[ia].IterateWithHoles(); it; it++ )
907 wxPoint pt( currentVertex.
x, currentVertex.
y );
909 if( smoothed_polys[ia2].Contains( currentVertex ) )
912 drce->SetItems( zoneA, zoneB );
920 for(
auto it = smoothed_polys[ia2].IterateWithHoles(); it; it++ )
923 wxPoint pt( currentVertex.
x, currentVertex.
y );
925 if( smoothed_polys[ia].Contains( currentVertex ) )
928 drce->SetItems( zoneB, zoneA );
937 std::map<VECTOR2I, int> conflictPoints;
939 for(
auto refIt = smoothed_polys[ia].IterateSegmentsWithHoles(); refIt; refIt++ )
942 SEG refSegment = *refIt;
945 for(
auto it = smoothed_polys[ia2].IterateSegmentsWithHoles(); it; it++ )
948 SEG testSegment = *it;
951 int ax1, ay1, ax2, ay2;
952 ax1 = refSegment.
A.
x;
953 ay1 = refSegment.
A.
y;
954 ax2 = refSegment.
B.
x;
955 ay2 = refSegment.
B.
y;
957 int bx1, by1, bx2, by2;
958 bx1 = testSegment.
A.
x;
959 by1 = testSegment.
A.
y;
960 bx2 = testSegment.
B.
x;
961 by2 = testSegment.
B.
y;
963 int d = GetClearanceBetweenSegments( bx1, by1, bx2, by2, 0,
964 ax1, ay1, ax2, ay2, 0,
965 zone2zoneClearance, &pt.
x, &pt.
y );
967 if( d < zone2zoneClearance )
969 if( conflictPoints.count( pt ) )
970 conflictPoints[ pt ] = std::min( conflictPoints[ pt ], d );
972 conflictPoints[ pt ] = d;
977 for(
const std::pair<const VECTOR2I, int>& conflict : conflictPoints )
979 int actual = conflict.second;
980 std::shared_ptr<DRC_ITEM> drce;
982 if( actual <= 0 && testIntersects )
986 else if( testClearance )
989 wxString msg =
formatMsg(
_(
"(%s clearance %s; actual %s)" ),
992 std::max( actual, 0 ) );
994 drce->SetErrorMessage( drce->GetErrorText() + wxS(
" " ) + msg );
999 drce->SetItems( zoneA, zoneB );
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
wxString GetNetname() const
int GetDRCEpsilon() const
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
virtual bool IsConnected() const
Returns information if the object is derived from BOARD_CONNECTED_ITEM.
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.
virtual LSET GetLayerSet() const
Return a std::bitset of all layers on which the item physically resides.
BOARD_ITEM_CONTAINER * GetParent() const
virtual std::shared_ptr< SHAPE_SEGMENT > GetEffectiveHoleShape() const
virtual bool HasHole() const
std::vector< ZONE * > m_DRCCopperZones
bool IsLayerEnabled(PCB_LAYER_ID aLayer) const
A proxy function that calls the correspondent function in m_BoardSettings tests whether a given layer...
bool GetBoardPolygonOutlines(SHAPE_POLY_SET &aOutlines, OUTLINE_ERROR_HANDLER *aErrorHandler=nullptr)
Extract the board outlines and build a closed polygon from lines, arcs and circle items on edge cut l...
FOOTPRINTS & Footprints()
std::unique_ptr< DRC_RTREE > m_CopperItemRTreeCache
std::unordered_map< ZONE *, std::unique_ptr< DRC_RTREE > > m_CopperZoneRTreeCache
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
bool Intersects(const BOX2< Vec > &aRect) const
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
SEVERITY GetSeverity() const
const MINOPTMAX< int > & GetValue() const
DRC_RULE * GetParentRule() const
bool GetReportAllTrackErrors() const
bool IsErrorLimitExceeded(int error_code)
DRC_CONSTRAINT EvalRules(DRC_CONSTRAINT_T aConstraintType, const BOARD_ITEM *a, const BOARD_ITEM *b, PCB_LAYER_ID aLayer, REPORTER *aReporter=nullptr)
static bool IsNetTieExclusion(int aTrackNetCode, PCB_LAYER_ID aTrackLayer, const VECTOR2I &aCollisionPos, BOARD_ITEM *aCollidingItem)
Check if the given collision between a track and another item occurs during the track's entry into a ...
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Implement an R-tree for fast spatial and layer indexing of connectable items.
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.
struct DRC_TEST_PROVIDER_COPPER_CLEARANCE::checked layers_checked
virtual bool Run() override
Run this provider against the given PCB with configured options (if any).
DRC_TEST_PROVIDER_COPPER_CLEARANCE()
void testItemAgainstZone(BOARD_ITEM *aItem, ZONE *aZone, PCB_LAYER_ID aLayer)
bool testTrackAgainstItem(PCB_TRACK *track, SHAPE *trackShape, PCB_LAYER_ID layer, BOARD_ITEM *other)
Checks for track/via/hole <-> clearance.
void testTrackClearances()
virtual ~DRC_TEST_PROVIDER_COPPER_CLEARANCE()
virtual const wxString GetDescription() const override
virtual const wxString GetName() const override
bool testPadAgainstItem(PAD *pad, SHAPE *padShape, PCB_LAYER_ID layer, BOARD_ITEM *other)
virtual bool reportPhase(const wxString &aStageName)
virtual bool reportProgress(int aCount, int aSize, int aDelta)
virtual void reportViolation(std::shared_ptr< DRC_ITEM > &item, const VECTOR2I &aMarkerPos, int aMarkerLayer)
virtual void reportAux(wxString fmt,...)
wxString formatMsg(const wxString &aFormatString, const wxString &aSource, int aConstraint, int aActual)
virtual void reportRuleStatistics()
virtual const BOX2I GetBoundingBox() const
Return the orthogonal bounding box of this object for display purposes.
KICAD_T Type() const
Returns the type of object.
LSET is a set of PCB_LAYER_IDs.
LSEQ Seq(const PCB_LAYER_ID *aWishListSequence, unsigned aCount) const
Return an LSEQ from the union of this LSET and a desired sequence.
bool Contains(PCB_LAYER_ID aLayer)
See if the layer set contains a PCB layer.
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
bool FlashLayer(int aLayer, bool aOnlyCheckIfPermitted=false) const
Check to see whether the pad should be flashed on the specific layer.
const VECTOR2I & GetDrillSize() const
PAD_ATTRIB GetAttribute() const
const wxString & GetNumber() const
VECTOR2I GetPosition() const override
bool HasHole() const override
std::shared_ptr< SHAPE_SEGMENT > GetEffectiveHoleShape() const override
Return a SHAPE_SEGMENT object representing the pad's hole.
const VECTOR2I & GetStart() const
const VECTOR2I & GetEnd() const
int GetDrill() const
Function GetDrill returns the local drill setting for this PCB_VIA.
std::shared_ptr< SHAPE_SEGMENT > GetEffectiveHoleShape() const override
bool IsOnLayer(PCB_LAYER_ID aLayer) const override
Test to see if this object is on the given layer.
OPT_VECTOR2I Intersect(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
Compute intersection point of segment (this) with segment aSeg.
Represent a set of closed polygons.
An abstract shape on 2D plane.
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,...
Handle a list of polygons defining a copper zone.
bool GetIsRuleArea() const
Accessors to parameters used in Rule Area zones:
const BOX2I GetBoundingBox() const override
virtual bool IsOnLayer(PCB_LAYER_ID) const override
Test to see if this object is on the given layer.
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
unsigned GetAssignedPriority() const
@ HOLE_CLEARANCE_CONSTRAINT
bool IsCopperLayer(int aLayerId)
Tests whether a layer is a copper layer.
PCB_LAYER_ID
A quick note on layer IDs:
static DRC_REGISTER_TEST_PROVIDER< DRC_TEST_PROVIDER_ANNULAR_WIDTH > dummy
@ NPTH
like PAD_PTH, but not plated
@ PTH
Plated through hole pad.
std::optional< VECTOR2I > OPT_VECTOR2I
static bool Collide(const SHAPE_CIRCLE &aA, const SHAPE_CIRCLE &aB, int aClearance, int *aActual, VECTOR2I *aLocation, VECTOR2I *aMTV)
checked(PCB_LAYER_ID aLayer)
@ PCB_FP_SHAPE_T
class FP_SHAPE, a footprint edge
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
@ PCB_PAD_T
class PAD, a pad in a footprint
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)