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 <common.h>
25#include <board.h>
26#include <pcb_track.h>
28#include <geometry/seg.h>
29#include <drc/drc_engine.h>
30#include <drc/drc_item.h>
31#include <drc/drc_rule.h>
33#include <drc/drc_rtree.h>
35
36/*
37 Silk to silk clearance test. Check all silkscreen features against each other.
38 Errors generated:
39 - DRCE_OVERLAPPING_SILK
40
41*/
42
44{
45public:
50
52
53 virtual bool Run() override;
54
55 virtual const wxString GetName() const override { return wxT( "silk_clearance" ); };
56
57private:
58
61};
62
63
65{
66 const int progressDelta = 500;
67
68 m_board = m_drcEngine->GetBoard();
69
70 // If the soldermask min width is greater than 0 then we must use a healing algorithm to generate
71 // a whole-board soldermask poly, and then test against that. However, that can't deal well with
72 // DRC exclusions (as any change anywhere on the board that affects the soldermask will null the
73 // associated exclusions), so we only use that when soldermask min width is > 0.
74 bool checkIndividualMaskItems = m_board->GetDesignSettings().m_SolderMaskMinWidth <= 0;
75
76 if( m_drcEngine->IsErrorLimitExceeded( DRCE_OVERLAPPING_SILK )
77 && m_drcEngine->IsErrorLimitExceeded( DRCE_SILK_MASK_CLEARANCE) )
78 {
79 return true; // continue with other tests
80 }
81
82 DRC_CONSTRAINT worstClearanceConstraint;
84
85 if( m_drcEngine->QueryWorstConstraint( SILK_CLEARANCE_CONSTRAINT, worstClearanceConstraint ) )
86 m_largestClearance = worstClearanceConstraint.m_Value.Min();
87
88 if( !reportPhase( _( "Checking silkscreen for overlapping items..." ) ) )
89 return false; // DRC cancelled
90
91 DRC_RTREE silkTree;
92 DRC_RTREE targetTree;
93 int ii = 0;
94 int items = 0;
95 LSET silkLayers = LSET( { F_SilkS, B_SilkS } );
96 LSET targetLayers = LSET::FrontMask() | LSET::BackMask() | LSET( { Edge_Cuts, Margin } );
97
98 auto countItems =
99 [&]( BOARD_ITEM* item ) -> bool
100 {
101 ++items;
102 return true;
103 };
104
105 auto addToSilkTree =
106 [&]( BOARD_ITEM* item ) -> bool
107 {
108 if( !reportProgress( ii++, items, progressDelta ) )
109 return false;
110
111 for( PCB_LAYER_ID layer : { F_SilkS, B_SilkS } )
112 {
113 if( item->IsOnLayer( layer ) )
114 silkTree.Insert( item, layer );
115 }
116
117 return true;
118 };
119
120 auto addToTargetTree =
121 [&]( BOARD_ITEM* item ) -> bool
122 {
123 if( !reportProgress( ii++, items, progressDelta ) )
124 return false;
125
126 for( PCB_LAYER_ID layer : LSET( item->GetLayerSet() & targetLayers ) )
127 targetTree.Insert( item, layer );
128
129 return true;
130 };
131
132 forEachGeometryItem( s_allBasicItems, silkLayers, countItems );
133 forEachGeometryItem( s_allBasicItems, targetLayers, countItems );
134
135 forEachGeometryItem( s_allBasicItems, silkLayers, addToSilkTree );
136 forEachGeometryItem( s_allBasicItems, targetLayers, addToTargetTree );
137
138 REPORT_AUX( wxString::Format( wxT( "Testing %d silkscreen features against %d board items." ),
139 silkTree.size(),
140 targetTree.size() ) );
141
142 const std::vector<DRC_RTREE::LAYER_PAIR> layerPairs =
143 {
162 };
163
164 targetTree.QueryCollidingPairs( &silkTree, layerPairs,
165 [&]( const DRC_RTREE::LAYER_PAIR& aLayers, DRC_RTREE::ITEM_WITH_SHAPE* aRefItemShape,
166 DRC_RTREE::ITEM_WITH_SHAPE* aTestItemShape, bool* aCollisionDetected ) -> bool
167 {
168 BOARD_ITEM* refItem = aRefItemShape->parent;
169 const SHAPE* refShape = aRefItemShape->shape;
170 BOARD_ITEM* testItem = aTestItemShape->parent;
171 const SHAPE* testShape = aTestItemShape->shape;
172
173 std::shared_ptr<SHAPE> hole;
174
175 if( m_drcEngine->IsErrorLimitExceeded( DRCE_OVERLAPPING_SILK )
176 && m_drcEngine->IsErrorLimitExceeded( DRCE_SILK_MASK_CLEARANCE ) )
177 {
178 return false;
179 }
180
181 if( isInvisibleText( refItem ) || isInvisibleText( testItem ) )
182 return true;
183
184 if( testItem->IsTented( aLayers.first ) )
185 {
186 if( testItem->HasHole() )
187 {
188 hole = testItem->GetEffectiveHoleShape();
189 testShape = hole.get();
190 }
191 else
192 {
193 return true;
194 }
195 }
196
197 int errorCode = DRCE_OVERLAPPING_SILK;
199 refItem, testItem, aLayers.second );
200 int minClearance = -1;
201
202 if( !constraint.IsNull() && constraint.GetSeverity() != RPT_SEVERITY_IGNORE )
203 minClearance = constraint.GetValue().Min();
204
205 if( aLayers.second == F_Mask || aLayers.second == B_Mask )
206 {
207 if( checkIndividualMaskItems )
208 minClearance = std::max( minClearance, 0 );
209
210 errorCode = DRCE_SILK_MASK_CLEARANCE;
211 }
212
213 if( minClearance < 0 || m_drcEngine->IsErrorLimitExceeded( errorCode ) )
214 return true;
215
216 int actual;
217 VECTOR2I pos;
218
219 // Graphics are often compound shapes so ignore collisions between shapes in a
220 // single footprint or on the board (both parent footprints will be nullptr).
221 if( refItem->Type() == PCB_SHAPE_T && testItem->Type() == PCB_SHAPE_T
222 && refItem->GetParentFootprint() == testItem->GetParentFootprint() )
223 {
224 return true;
225 }
226
227 // Collide (and generate violations) based on a well-defined order so that
228 // exclusion checking against previously-generated violations will work.
229 if( aLayers.first == aLayers.second )
230 {
231 if( refItem->m_Uuid > testItem->m_Uuid )
232 {
233 std::swap( refItem, testItem );
234 std::swap( refShape, testShape );
235 }
236 }
237
238 if( refShape->Collide( testShape, minClearance, &actual, &pos ) )
239 {
240 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( errorCode );
241
242 if( minClearance > 0 )
243 {
244 wxString msg = formatMsg( _( "(%s clearance %s; actual %s)" ),
245 constraint.GetParentRule()->m_Name,
246 minClearance,
247 actual );
248
249 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
250 }
251
252 drcItem->SetItems( refItem, testItem );
253 drcItem->SetViolatingRule( constraint.GetParentRule() );
254 reportTwoShapeGeometry( drcItem, pos, refShape, testShape, aLayers.second, actual );
255 *aCollisionDetected = true;
256 }
257
258 return true;
259 },
261 [&]( int aCount, int aSize ) -> bool
262 {
263 return reportProgress( aCount, aSize, progressDelta );
264 } );
265
266 return !m_drcEngine->IsCancelled();
267}
268
269
270namespace detail
271{
273}
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:79
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:173
virtual std::shared_ptr< SHAPE_SEGMENT > GetEffectiveHoleShape() const
virtual bool HasHole() const
Definition board_item.h:156
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:322
SEVERITY GetSeverity() const
Definition drc_rule.h:187
const MINOPTMAX< int > & GetValue() const
Definition drc_rule.h:166
MINOPTMAX< int > m_Value
Definition drc_rule.h:208
DRC_RULE * GetParentRule() const
Definition drc_rule.h:170
bool IsNull() const
Definition drc_rule.h:161
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
void Insert(BOARD_ITEM *aItem, PCB_LAYER_ID aLayer, int aWorstClearance=0)
Insert an item into the tree on a particular layer with an optional worst clearance.
Definition drc_rtree.h:104
size_t size() const
Return the number of items in the tree.
Definition drc_rtree.h:535
std::pair< PCB_LAYER_ID, PCB_LAYER_ID > LAYER_PAIR
Definition drc_rtree.h:440
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:455
wxString m_Name
Definition drc_rule.h:126
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)
const KIID m_Uuid
Definition eda_item.h:516
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
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:705
static const LSET & BackMask()
Return a mask holding all technical layers and the external CU layer on back side.
Definition lset.cpp:712
T Min() const
Definition minoptmax.h:33
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:96
@ DRCE_OVERLAPPING_SILK
Definition drc_item.h:101
@ SILK_CLEARANCE_CONSTRAINT
Definition drc_rule.h:56
#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:88
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695