KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_test_provider_silk_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 <unordered_map>
25
26#include <common.h>
27#include <board.h>
28#include <pcb_board_outline.h>
29#include <pcb_track.h>
31#include <geometry/seg.h>
32#include <drc/drc_engine.h>
33#include <drc/drc_item.h>
34#include <drc/drc_rule.h>
36#include <drc/drc_rtree.h>
38
39/*
40 Silk to silk clearance test. Check all silkscreen features against each other.
41 Errors generated:
42 - DRCE_SILK_CLEARANCE
43
44*/
45
47{
48public:
53
55
56 virtual bool Run() override;
57
58 virtual const wxString GetName() const override { return wxT( "silk_clearance" ); };
59
60private:
61
64};
65
66
68{
69 const int progressDelta = 500;
70
71 m_board = m_drcEngine->GetBoard();
72
73 // If the soldermask min width is greater than 0 then we must use a healing algorithm to generate
74 // a whole-board soldermask poly, and then test against that. However, that can't deal well with
75 // DRC exclusions (as any change anywhere on the board that affects the soldermask will null the
76 // associated exclusions), so we only use that when soldermask min width is > 0.
77 bool checkIndividualMaskItems = m_board->GetDesignSettings().m_SolderMaskMinWidth <= 0;
78
79 if( m_drcEngine->IsErrorLimitExceeded( DRCE_SILK_CLEARANCE )
80 && m_drcEngine->IsErrorLimitExceeded( DRCE_SILK_MASK_CLEARANCE) )
81 {
82 return true; // continue with other tests
83 }
84
85 DRC_CONSTRAINT worstClearanceConstraint;
87
88 if( m_drcEngine->QueryWorstConstraint( SILK_CLEARANCE_CONSTRAINT, worstClearanceConstraint ) )
89 m_largestClearance = worstClearanceConstraint.m_Value.Min();
90
91 if( !reportPhase( _( "Checking silkscreen for overlapping items..." ) ) )
92 return false; // DRC cancelled
93
94 DRC_RTREE silkTree;
95 DRC_RTREE targetTree;
96 int ii = 0;
97 int items = 0;
98 LSET silkLayers = LSET( { F_SilkS, B_SilkS } );
99 LSET targetLayers = LSET::FrontMask() | LSET::BackMask() | LSET( { Edge_Cuts, Margin } );
100
101 auto countItems =
102 [&]( BOARD_ITEM* item ) -> bool
103 {
104 ++items;
105 return true;
106 };
107
108 auto addToSilkTree =
109 [&]( BOARD_ITEM* item ) -> bool
110 {
111 if( !reportProgress( ii++, items, progressDelta ) )
112 return false;
113
114 for( PCB_LAYER_ID layer : { F_SilkS, B_SilkS } )
115 {
116 if( item->IsOnLayer( layer ) )
117 silkTree.Insert( item, layer, 0, ATOMIC_TABLES );
118 }
119
120 return true;
121 };
122
123 auto addToTargetTree =
124 [&]( BOARD_ITEM* item ) -> bool
125 {
126 if( !reportProgress( ii++, items, progressDelta ) )
127 return false;
128
129 for( PCB_LAYER_ID layer : LSET( item->GetLayerSet() & targetLayers ) )
130 targetTree.Insert( item, layer, 0, ATOMIC_TABLES );
131
132 return true;
133 };
134
135 forEachGeometryItem( s_allBasicItems, silkLayers, countItems );
136 forEachGeometryItem( s_allBasicItems, targetLayers, countItems );
137
138 forEachGeometryItem( s_allBasicItems, silkLayers, addToSilkTree );
139 forEachGeometryItem( s_allBasicItems, targetLayers, addToTargetTree );
140
141 silkTree.Build();
142 targetTree.Build();
143
144 REPORT_AUX( wxString::Format( wxT( "Testing %d silkscreen features against %d board items." ),
145 silkTree.size(),
146 targetTree.size() ) );
147
148 // Cache the board-outline bounding box and per-subshape collision results so that each
149 // subshape is only tested against the outline once during the visitor sweep. Without
150 // caching, QueryCollidingPairs invokes the visitor O(silk * target) times and the outline
151 // Collide (which walks the outline's triangulation) was dominating DRC runtime on boards
152 // with many silkscreen/mask polygons (see issue 24007).
153 PCB_BOARD_OUTLINE* boardOutline = m_board->BoardOutline();
154 BOX2I outlineBBox;
155
156 if( boardOutline && !boardOutline->HasOutline() )
157 boardOutline = nullptr;
158
159 if( boardOutline )
160 outlineBBox = boardOutline->GetOutline().BBoxFromCaches();
161
162 std::unordered_map<const SHAPE*, bool> outlineCollisionCache;
163
164 const std::vector<DRC_RTREE::LAYER_PAIR> layerPairs =
165 {
184 };
185
186 targetTree.QueryCollidingPairs( &silkTree, layerPairs,
187 [&]( const DRC_RTREE::LAYER_PAIR& aLayers, DRC_RTREE::ITEM_WITH_SHAPE* aRefItemShape,
188 DRC_RTREE::ITEM_WITH_SHAPE* aTestItemShape, bool* aCollisionDetected ) -> bool
189 {
190 BOARD_ITEM* refItem = aRefItemShape->parent;
191 const SHAPE* refShape = aRefItemShape->shape;
192 BOARD_ITEM* testItem = aTestItemShape->parent;
193 const SHAPE* testShape = aTestItemShape->shape;
194
195 std::shared_ptr<SHAPE> hole;
196
197 if( m_drcEngine->IsErrorLimitExceeded( DRCE_SILK_CLEARANCE )
198 && m_drcEngine->IsErrorLimitExceeded( DRCE_SILK_MASK_CLEARANCE ) )
199 {
200 return false;
201 }
202
203 if( isInvisibleText( refItem ) || isInvisibleText( testItem ) )
204 return true;
205
206 if( testItem->IsTented( aLayers.first ) )
207 {
208 if( testItem->HasHole() )
209 {
210 hole = testItem->GetEffectiveHoleShape();
211 testShape = hole.get();
212 }
213 else
214 {
215 return true;
216 }
217 }
218
219 if( boardOutline )
220 {
221 if( !testItem->GetBoundingBox().Intersects( outlineBBox ) )
222 return true;
223
224 // Only cache for shapes owned by the R-tree (stable pointers). Hole
225 // shapes are freshly created per visitor call via shared_ptr, so their
226 // raw addresses cannot be safely used as cache keys.
227 bool collidesOutline;
228
229 if( testShape == aTestItemShape->shape )
230 {
231 auto [it, inserted] = outlineCollisionCache.try_emplace( testShape, false );
232
233 if( inserted )
234 it->second = testShape->Collide( &boardOutline->GetOutline() );
235
236 collidesOutline = it->second;
237 }
238 else
239 {
240 collidesOutline = testShape->Collide( &boardOutline->GetOutline() );
241 }
242
243 if( !collidesOutline )
244 return true;
245 }
246
247 int errorCode = DRCE_SILK_CLEARANCE;
249 refItem, testItem, aLayers.second );
250 int minClearance = -1;
251
252 if( !constraint.IsNull() && constraint.GetSeverity() != RPT_SEVERITY_IGNORE )
253 minClearance = constraint.GetValue().Min();
254
255 if( aLayers.second == F_Mask || aLayers.second == B_Mask )
256 {
257 if( checkIndividualMaskItems )
258 minClearance = std::max( minClearance, 0 );
259
260 errorCode = DRCE_SILK_MASK_CLEARANCE;
261 }
262
263 if( minClearance < 0 || m_drcEngine->IsErrorLimitExceeded( errorCode ) )
264 return true;
265
266 int actual;
267 VECTOR2I pos;
268
269 // Graphics are often compound shapes so ignore collisions between shapes in a
270 // single footprint or on the board (both parent footprints will be nullptr).
271 if( refItem->Type() == PCB_SHAPE_T && testItem->Type() == PCB_SHAPE_T
272 && refItem->GetParentFootprint() == testItem->GetParentFootprint() )
273 {
274 return true;
275 }
276
277 // Collide (and generate violations) based on a well-defined order so that
278 // exclusion checking against previously-generated violations will work.
279 if( aLayers.first == aLayers.second )
280 {
281 if( refItem->m_Uuid > testItem->m_Uuid )
282 {
283 std::swap( refItem, testItem );
284 std::swap( refShape, testShape );
285 }
286 }
287
288 if( refShape->Collide( testShape, minClearance, &actual, &pos ) )
289 {
290 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( errorCode );
291
292 if( minClearance > 0 )
293 {
294 drcItem->SetErrorDetail( formatMsg( _( "(%s clearance %s; actual %s)" ),
295 constraint.GetParentRule()->m_Name,
296 minClearance,
297 actual ) );
298 }
299
300 drcItem->SetItems( refItem, testItem );
301 drcItem->SetViolatingRule( constraint.GetParentRule() );
302 reportTwoShapeGeometry( drcItem, pos, refShape, testShape, aLayers.second, actual );
303 *aCollisionDetected = true;
304 }
305
306 return true;
307 },
309 [&]( int aCount, int aSize ) -> bool
310 {
311 return reportProgress( aCount, aSize, progressDelta );
312 } );
313
314 return !m_drcEngine->IsCancelled();
315}
316
317
318namespace detail
319{
321}
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
FOOTPRINT * GetParentFootprint() const
virtual bool IsTented(PCB_LAYER_ID aLayer) const
Checks if the given object is tented (its copper shape is covered by solder mask) on a given side of ...
Definition board_item.h:197
virtual std::shared_ptr< SHAPE_SEGMENT > GetEffectiveHoleShape() const
virtual bool HasHole() const
Definition board_item.h:180
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:323
constexpr bool Intersects(const BOX2< Vec > &aRect) const
Definition box2.h:311
SEVERITY GetSeverity() const
Definition drc_rule.h:218
const MINOPTMAX< int > & GetValue() const
Definition drc_rule.h:197
MINOPTMAX< int > m_Value
Definition drc_rule.h:241
DRC_RULE * GetParentRule() const
Definition drc_rule.h:201
bool IsNull() const
Definition drc_rule.h:192
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Definition drc_item.cpp:407
Implement an R-tree for fast spatial and layer indexing of connectable items.
Definition drc_rtree.h:49
size_t size() const
Return the number of items in the tree.
Definition drc_rtree.h:554
std::pair< PCB_LAYER_ID, PCB_LAYER_ID > LAYER_PAIR
Definition drc_rtree.h:459
int QueryCollidingPairs(DRC_RTREE *aRefTree, std::vector< LAYER_PAIR > aLayerPairs, std::function< bool(const LAYER_PAIR &, ITEM_WITH_SHAPE *, ITEM_WITH_SHAPE *, bool *aCollision)> aVisitor, int aMaxClearance, std::function< bool(int, int)> aProgressReporter) const
Definition drc_rtree.h:474
void Build()
Finalize all pending inserts by bulk-building packed R-trees from the staged items.
Definition drc_rtree.h:168
void Insert(BOARD_ITEM *aItem, PCB_LAYER_ID aLayer, int aWorstClearance=0, bool aAtomicTables=false)
Insert an item into the tree on a particular layer with an optional worst clearance.
Definition drc_rtree.h:95
wxString m_Name
Definition drc_rule.h:154
virtual const wxString GetName() const override
virtual bool Run() override
Run this provider against the given PCB with configured options (if any).
virtual ~DRC_TEST_PROVIDER_SILK_CLEARANCE()=default
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)
static std::vector< KICAD_T > s_allBasicItems
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)
virtual const BOX2I GetBoundingBox() const
Return the orthogonal bounding box of this object for display purposes.
Definition eda_item.cpp:120
const KIID m_Uuid
Definition eda_item.h:528
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:112
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
static const LSET & FrontMask()
Return a mask holding all technical layers and the external CU layer on front side.
Definition lset.cpp:722
static const LSET & BackMask()
Return a mask holding all technical layers and the external CU layer on back side.
Definition lset.cpp:729
T Min() const
Definition minoptmax.h:33
bool HasOutline() const
const SHAPE_POLY_SET & GetOutline() const
const BOX2I BBoxFromCaches() 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
The common library.
@ DRCE_SILK_MASK_CLEARANCE
Definition drc_item.h:97
@ DRCE_SILK_CLEARANCE
Definition drc_item.h:100
#define ATOMIC_TABLES
Definition drc_rtree.h:42
@ SILK_CLEARANCE_CONSTRAINT
Definition drc_rule.h:62
#define REPORT_AUX(s)
#define _(s)
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
@ F_CrtYd
Definition layer_ids.h:116
@ B_Adhes
Definition layer_ids.h:103
@ Edge_Cuts
Definition layer_ids.h:112
@ F_Paste
Definition layer_ids.h:104
@ F_Adhes
Definition layer_ids.h:102
@ B_Mask
Definition layer_ids.h:98
@ B_Cu
Definition layer_ids.h:65
@ F_Mask
Definition layer_ids.h:97
@ B_Paste
Definition layer_ids.h:105
@ F_Fab
Definition layer_ids.h:119
@ Margin
Definition layer_ids.h:113
@ F_SilkS
Definition layer_ids.h:100
@ B_CrtYd
Definition layer_ids.h:115
@ B_SilkS
Definition layer_ids.h:101
@ F_Cu
Definition layer_ids.h:64
@ B_Fab
Definition layer_ids.h:118
static DRC_REGISTER_TEST_PROVIDER< DRC_TEST_PROVIDER_ANNULAR_WIDTH > dummy
@ RPT_SEVERITY_IGNORE
int actual
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition typeinfo.h:85
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687