25#include <math_for_graphics.h>
72 virtual bool Run()
override;
74 virtual const wxString
GetName()
const override
76 return wxT(
"clearance" );
81 return wxT(
"Tests copper item clearance" );
133 reportAux( wxT(
"No Clearance constraints found. Tests not run." ) );
141 if( !
reportPhase(
_(
"Checking track & via clearances..." ) ) )
148 if( !
reportPhase(
_(
"Checking hole clearances..." ) ) )
172 if( !
reportPhase(
_(
"Checking copper graphic clearances..." ) ) )
180 if( !
reportPhase(
_(
"Checking copper zone clearances..." ) ) )
211 bool has_error =
false;
215 otherNet = connectedItem->GetNetCode();
223 if(
pad->GetAttribute() == PAD_ATTRIB::NPTH && !
pad->FlashLayer( layer ) )
224 testClearance = testShorting =
false;
227 if( testClearance || testShorting )
247 drcItem->SetItems( item, other );
263 else if( actual == 0 && otherNet && testShorting )
268 msg.Printf(
_(
"(nets %s and %s)" ), item->
GetNetname(),
271 drce->SetErrorMessage( drce->GetErrorText() + wxS(
" " ) + msg );
272 drce->SetItems( item, other );
280 else if( testClearance )
283 wxString msg =
formatMsg(
_(
"(%s clearance %s; actual %s)" ),
288 drce->SetErrorMessage( drce->GetErrorText() + wxS(
" " ) + msg );
289 drce->SetItems( item, other );
303 std::array<BOARD_ITEM*, 2> a{ item, other };
304 std::array<BOARD_ITEM*, 2> b{ other, item };
305 std::array<SHAPE*, 2> a_shape{ itemShape, otherShape.get() };
307 for(
size_t ii = 0; ii < 2; ++ii )
309 std::shared_ptr<SHAPE_SEGMENT> holeShape;
313 if( !(
dynamic_cast<PCB_TRACK*
>( a[ii] ) ) || !b[ii]->HasHole() )
319 if( b[ii]->GetLayerSet().Contains( layer ) )
320 holeShape = b[ii]->GetEffectiveHoleShape();
324 holeShape = b[ii]->GetEffectiveHoleShape();
338 wxString msg =
formatMsg( clearance ?
_(
"(%s clearance %s; actual %s)" )
339 :
_(
"(%s clearance %s; actual < 0)" ),
344 drce->SetErrorMessage( drce->GetErrorText() + wxS(
" " ) + msg );
345 drce->SetItems( a[ii], b[ii] );
372 BOX2I worstCaseBBox = itemBBox;
382 if( !testClearance && !testHoles )
398 bool flashedPad =
pad->FlashLayer( aLayer );
399 bool platedHole =
pad->HasHole() &&
pad->GetAttribute() == PAD_ATTRIB::PTH;
401 if( !flashedPad && !platedHole )
402 testClearance =
false;
413 std::shared_ptr<SHAPE> itemShape = aItem->
GetEffectiveShape( aLayer, FLASHING::DEFAULT );
416 std::max( 0, clearance -
m_drcEpsilon ), &actual, &pos ) )
419 wxString msg =
formatMsg(
_(
"(%s clearance %s; actual %s)" ),
424 drce->SetErrorMessage( drce->GetErrorText() + wxS(
" " ) + msg );
425 drce->SetItems( aItem, aZone );
432 if( testHoles && aItem->
HasHole() )
434 std::shared_ptr<SHAPE_SEGMENT> holeShape;
458 wxString msg =
formatMsg(
_(
"(%s clearance %s; actual %s)" ),
463 drce->SetErrorMessage( drce->GetErrorText() + wxS(
" " ) + msg );
464 drce->SetItems( aItem, aZone );
487 if( !testClearance && !testShorts )
496 BOX2I worstCaseBBox = itemBBox;
508 std::shared_ptr<SHAPE> itemShape = aText->
GetEffectiveShape( layer, FLASHING::DEFAULT );
510 if( *aInheritedNet ==
nullptr )
512 if( zoneTree->
QueryColliding( itemBBox, itemShape.get(), layer ) )
513 *aInheritedNet = aZone->
GetNet();
516 if( *aInheritedNet == aZone->
GetNet() )
527 std::max( 0, clearance -
m_drcEpsilon ), &actual, &pos ) )
529 std::shared_ptr<DRC_ITEM> drce;
532 if( testShorts && actual == 0 && *aInheritedNet )
535 msg.Printf(
_(
"(nets %s and %s)" ),
536 ( *aInheritedNet )->GetNetname(),
542 msg =
formatMsg(
_(
"(%s clearance %s; actual %s)" ),
548 drce->SetErrorMessage( drce->GetErrorText() + wxS(
" " ) + msg );
549 drce->SetItems( aText, aZone );
561 const int progressDelta = 100;
565 std::map<BOARD_ITEM*, int> freePadsUsageMap;
566 std::unordered_map<PTR_PTR_CACHE_KEY, layers_checked> checkedPairs;
567 std::mutex checkedPairsMutex;
568 std::mutex freePadsUsageMapMutex;
569 std::atomic<int> tracks_checked( 0 );
573 auto testTrack = [&](
const int start_idx,
const int end_idx )
575 for(
int trackIdx = start_idx; trackIdx < end_idx; ++trackIdx )
589 if( otherCItem && otherCItem->GetNetCode() == track->
GetNetCode() )
597 if(
static_cast<void*
>( a ) >
static_cast<void*
>( b ) )
600 std::lock_guard<std::mutex> lock( checkedPairsMutex );
601 auto it = checkedPairs.find( { a, b } );
603 if( it != checkedPairs.end() && ( it->second.layers.test( layer )
610 checkedPairs[ { a, b } ].layers.set( layer );
621 std::lock_guard<std::mutex> lock( freePadsUsageMapMutex );
622 auto it = freePadsUsageMap.find( other );
624 if( it == freePadsUsageMap.end() )
626 freePadsUsageMap[ other ] = track->
GetNetCode();
641 if(
static_cast<void*
>( a ) >
static_cast<void*
>( b ) )
649 std::lock_guard<std::mutex> lock( checkedPairsMutex );
650 auto it = checkedPairs.find( { a, b } );
652 if( it != checkedPairs.end() )
653 it->second.has_error =
true;
679 while( tracks_checked <
static_cast<int>(
m_board->
Tracks().size() ) )
687 std::this_thread::sleep_for( std::chrono::milliseconds( 100 ) );
705 int padGroupIdx = padToNetTieGroupMap[
pad->GetNumber() ];
709 PAD* otherPad =
static_cast<PAD*
>( other );
711 if( padGroupIdx >= 0 && padGroupIdx == padToNetTieGroupMap[ otherPad->
GetNumber() ] )
712 testClearance = testShorting =
false;
714 if(
pad->SameLogicalPadAs( otherPad ) )
719 testClearance = testShorting =
false;
722 PAD* otherPad =
nullptr;
726 otherPad =
static_cast<PAD*
>( other );
729 otherVia =
static_cast<PCB_VIA*
>( other );
732 testClearance = testShorting =
false;
735 if(
pad->GetAttribute() == PAD_ATTRIB::NPTH && !
pad->FlashLayer( aLayer ) )
736 testClearance = testShorting =
false;
739 testClearance = testShorting =
false;
743 testClearance = testShorting =
false;
745 int padNet =
pad->GetNetCode();
749 otherNet = connectedItem->GetNetCode();
752 if( otherNet && otherNet == padNet )
754 testClearance = testShorting =
false;
758 if( !(
pad->GetDrillSize().x > 0 )
760 && !( otherVia && otherVia->
GetDrill() > 0 ) )
765 if( !testClearance && !testShorting && !testHoles )
774 if( otherPad &&
pad->SameLogicalPadAs( otherPad ) )
784 if(
pad->GetShortNetname().StartsWith( wxS(
"unconnected-(" ) )
793 msg.Printf(
_(
"(nets %s and %s)" ),
797 drce->SetErrorMessage( drce->GetErrorText() + wxS(
" " ) + msg );
798 drce->SetItems(
pad, otherPad );
806 if( testClearance || testShorting )
821 else if( actual == 0 && otherNet && testShorting )
826 msg.Printf(
_(
"(nets %s and %s)" ),
830 drce->SetErrorMessage( drce->GetErrorText() + wxS(
" " ) + msg );
831 drce->SetItems(
pad, other );
836 else if( testClearance )
839 wxString msg =
formatMsg(
_(
"(%s clearance %s; actual %s)" ),
844 drce->SetErrorMessage( drce->GetErrorText() + wxS(
" " ) + msg );
845 drce->SetItems(
pad, other );
864 if( testHoles && otherPad &&
pad->FlashLayer( aLayer ) && otherPad->
HasHole() )
871 wxString msg =
formatMsg(
_(
"(%s clearance %s; actual %s)" ),
876 drce->SetErrorMessage( drce->GetErrorText() + wxS(
" " ) + msg );
877 drce->SetItems(
pad, other );
885 if( testHoles && otherPad && otherPad->
FlashLayer( aLayer ) &&
pad->HasHole() )
887 if( clearance > 0 && otherShape->Collide(
pad->GetEffectiveHoleShape().get(),
892 wxString msg =
formatMsg(
_(
"(%s clearance %s; actual %s)" ),
897 drce->SetErrorMessage( drce->GetErrorText() + wxS(
" " ) + msg );
898 drce->SetItems(
pad, other );
906 if( testHoles && otherVia && otherVia->
IsOnLayer( aLayer ) )
913 wxString msg =
formatMsg(
_(
"(%s clearance %s; actual %s)" ),
918 drce->SetErrorMessage( drce->GetErrorText() + wxS(
" " ) + msg );
919 drce->SetItems(
pad, otherVia );
932 const int progressDelta = 100;
937 count += footprint->Pads().size();
939 reportAux( wxT(
"Testing %d pads..." ), count );
941 std::unordered_map<PTR_PTR_CACHE_KEY, int> checkedPairs;
947 for(
PAD*
pad : footprint->Pads() )
951 std::shared_ptr<SHAPE> padShape =
pad->GetEffectiveShape( layer );
962 if(
static_cast<void*
>( a ) >
static_cast<void*
>( b ) )
965 if( checkedPairs.find( { a, b } ) != checkedPairs.end() )
971 checkedPairs[ { a, b } ] = 1;
1003 const int progressDelta = 100;
1008 count += footprint->GraphicalItems().size();
1010 reportAux( wxT(
"Testing %d graphics..." ), count );
1012 auto isKnockoutText =
1018 auto testGraphicAgainstZone =
1031 if( isKnockoutText( item ) )
1041 std::unordered_map<PTR_PTR_CACHE_KEY, layers_checked> checkedPairs;
1043 auto testCopperGraphic =
1054 if( otherCItem && otherCItem->GetNetCode() == aShape->GetNetCode() )
1069 if(
static_cast<void*
>( a ) >
static_cast<void*
>( b ) )
1072 auto it = checkedPairs.find( { a, b } );
1074 if( it != checkedPairs.end() && it->second.layers.test( layer ) )
1080 checkedPairs[ { a, b } ].layers.set( layer );
1092 if(
static_cast<void*
>( a ) >
static_cast<void*
>( b ) )
1095 auto it = checkedPairs.find( { a, b } );
1098 aShape->GetEffectiveShape().get(),
1101 if( it != checkedPairs.end() )
1102 it->second.has_error =
true;
1112 testGraphicAgainstZone( item );
1114 if( item->Type() ==
PCB_SHAPE_T && item->IsOnCopperLayer() )
1115 testCopperGraphic(
static_cast<PCB_SHAPE*
>( item ) );
1123 for(
BOARD_ITEM* item : footprint->GraphicalItems() )
1125 testGraphicAgainstZone( item );
1136 const int progressDelta = 50;
1142 bool cancelled =
false;
1143 std::vector<std::map<PCB_LAYER_ID, std::vector<SEG>>> poly_segments;
1148 using report_data = std::tuple<int, int, VECTOR2I, int, int, PCB_LAYER_ID>;
1149 const int invalid_zone = -1;
1151 std::vector<std::future<report_data>> futures;
1154 auto checkZones = [testClearance, testIntersects, &poly_segments, &cancelled, invalid_zone]
1155 (
int zoneA,
int zoneB,
1156 int zone2zoneClearance,
PCB_LAYER_ID layer ) -> report_data
1159 std::map<VECTOR2I, int> conflictPoints;
1161 std::vector<SEG>& refSegments = poly_segments[zoneA][layer];
1162 std::vector<SEG>& testSegments = poly_segments[zoneB][layer];
1163 bool reported =
false;
1164 auto invalid_result = std::make_tuple( invalid_zone, invalid_zone,
VECTOR2I(), 0, 0,
F_Cu );
1166 for(
SEG& refSegment : refSegments )
1168 int ax1 = refSegment.A.x;
1169 int ay1 = refSegment.A.y;
1170 int ax2 = refSegment.B.x;
1171 int ay2 = refSegment.B.y;
1174 for(
SEG& testSegment : testSegments )
1179 int bx1 = testSegment.A.
x;
1180 int by1 = testSegment.A.y;
1181 int bx2 = testSegment.B.x;
1182 int by2 = testSegment.B.y;
1189 int d = GetClearanceBetweenSegments( bx1, by1, bx2, by2, 0,
1190 ax1, ay1, ax2, ay2, 0,
1191 zone2zoneClearance, &pt.
x, &pt.
y );
1193 if( d < zone2zoneClearance )
1195 if( d == 0 && testIntersects )
1197 else if( testClearance )
1201 return std::make_tuple( zoneA, zoneB, pt, d, zone2zoneClearance, layer );
1205 return invalid_result;
1209 return invalid_result;
1212 for(
int layer_id =
F_Cu; layer_id <=
B_Cu; ++layer_id )
1215 int zone2zoneClearance;
1227 std::vector<SEG>& poly_segs = poly_segments[ii][layer];
1237 if( seg.
A.
x > seg.
B.
x )
1240 poly_segs.push_back( seg );
1243 std::sort( poly_segs.begin(), poly_segs.end() );
1247 std::vector<std::pair<int, int>> zonePairs;
1292 futures.push_back(
tp.submit( checkZones, ia, ia2, zone2zoneClearance, layer ) );
1297 for(
auto& task : futures )
1302 std::future_status result;
1306 result = task.wait_for( std::chrono::milliseconds( 200 ) );
1314 if( result == std::future_status::ready )
1316 report_data data = task.get();
1317 int zoneA_id = std::get<0>( data );
1318 int zoneB_id = std::get<1>( data );
1320 int actual = std::get<3>( data );
1321 int required = std::get<4>( data );
1324 if( zoneA_id != invalid_zone )
1330 std::shared_ptr<DRC_ITEM> drce;
1332 if( actual <= 0 && testIntersects )
1336 else if( testClearance )
1339 wxString msg =
formatMsg(
_(
"(%s clearance %s; actual %s)" ),
1342 std::max( actual, 0 ) );
1344 drce->SetErrorMessage( drce->GetErrorText() + wxS(
" " ) + msg );
1349 drce->SetItems( zoneA, zoneB );
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
wxString GetNetname() const
NETINFO_ITEM * GetNet() const
Return #NET_INFO object for a given item.
wxString GetShortNetname() const
int GetDRCEpsilon() const
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
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...
FOOTPRINTS & Footprints()
int GetCopperLayerCount() const
std::shared_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)
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)
void testGraphicClearances()
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)
void testKnockoutTextAgainstZone(BOARD_ITEM *aText, NETINFO_ITEM **aInheritedNet, ZONE *aZone)
bool testSingleLayerItemAgainstItem(BOARD_CONNECTED_ITEM *item, SHAPE *itemShape, PCB_LAYER_ID layer, BOARD_ITEM *other)
Checks for track/via/hole <-> clearance.
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)
void reportAux(const wxString &aMsg)
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.
Handle the data for a net.
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
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.
const VECTOR2I & GetEnd() const
int GetDrill() const
Return 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.
void Fracture(POLYGON_MODE aFastMode)
Convert a set of polygons with holes to a single outline with "slits"/"fractures" connecting the oute...
int FullPointCount() const
Return the number of points in the shape poly set.
void BuildBBoxCaches() const
Construct BBoxCaches for Contains(), below.
SEGMENT_ITERATOR IterateSegmentsWithHoles()
Returns an iterator object, for all outlines in the set (with holes)
const BOX2I BBoxFromCaches() const
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 PCB_LAYER_ID GetLayer() const override
Return the primary layer this item is on.
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.
@ 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
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)
BS::thread_pool thread_pool
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
@ PCB_PAD_T
class PAD, a pad in a footprint
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)