KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_test_provider_misc.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 (C) 2004-2023 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
25#include <drc/drc_engine.h>
26#include <drc/drc_item.h>
27#include <drc/drc_rule.h>
30#include <pad.h>
31#include <pcb_track.h>
32
35
36/*
37 Miscellaneous tests:
38
39 - DRCE_DISABLED_LAYER_ITEM, ///< item on a disabled layer
40 - DRCE_INVALID_OUTLINE, ///< invalid board outline
41 - DRCE_UNRESOLVED_VARIABLE,
42 - DRCE_ASSERTION_FAILURE ///< user-defined assertions
43*/
44
46{
47public:
49 m_board( nullptr )
50 {
51 m_isRuleDriven = false;
52 }
53
55 {
56 }
57
58 virtual bool Run() override;
59
60 virtual const wxString GetName() const override
61 {
62 return wxT( "miscellaneous" );
63 };
64
65 virtual const wxString GetDescription() const override
66 {
67 return wxT( "Misc checks (board outline, missing textvars)" );
68 }
69
70private:
71 void testOutline();
72 void testDisabledLayers();
73 void testTextVars();
74 void testAssertions();
75
77};
78
79
81{
82 SHAPE_POLY_SET dummyOutline;
83 bool errorHandled = false;
84
85 OUTLINE_ERROR_HANDLER errorHandler =
86 [&]( const wxString& msg, BOARD_ITEM* itemA, BOARD_ITEM* itemB, const VECTOR2I& pt )
87 {
88 errorHandled = true;
89
91 return;
92
93 if( !itemA ) // If we only have a single item, make sure it's A
94 std::swap( itemA, itemB );
95
96 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_INVALID_OUTLINE );
97
98 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
99 drcItem->SetItems( itemA, itemB );
100
101 reportViolation( drcItem, pt, Edge_Cuts );
102 };
103
104 // Test for very small graphic items (a few nm size) that can create issues
105 // when trying to build the board outlines, and they are not easy to locate onn screen.
106 const int minSizeForValideGraphics = pcbIUScale.mmToIU( 0.001 );
107
108 if( !TestBoardOutlinesGraphicItems(m_board, minSizeForValideGraphics, &errorHandler ) )
109 {
110 if( errorHandled )
111 {
112 // if there are invalid items on Edge.Cuts, they are already reported
113 }
114 else
115 {
116 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_INVALID_OUTLINE );
117 wxString msg;
118
119 msg.Printf( _( "(Suspicious items found on Edge.Cuts layer)" ) );
120
121 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
122 drcItem->SetItems( m_board );
123
125 }
126 }
127
128
129 // Use the standard chaining epsilon here so that we report errors that might affect
130 // other tools (such as 3D viewer).
131 int chainingEpsilon = m_board->GetOutlinesChainingEpsilon();
132
134 chainingEpsilon, &errorHandler ) )
135 {
136 if( errorHandled )
137 {
138 // if there is an invalid outline, then there must be an outline
139 }
140 else
141 {
142 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_INVALID_OUTLINE );
143 wxString msg;
144
145 msg.Printf( _( "(no edges found on Edge.Cuts layer)" ) );
146
147 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
148 drcItem->SetItems( m_board );
149
151 }
152 }
153}
154
155
157{
158 const int progressDelta = 2000;
159 int ii = 0;
160 int items = 0;
161
162 auto countItems =
163 [&]( BOARD_ITEM* item ) -> bool
164 {
165 ++items;
166 return true;
167 };
168
169 LSET disabledLayers = m_board->GetEnabledLayers().flip();
170
171 // Perform the test only for copper layers
172 disabledLayers &= LSET::AllCuMask();
173
174 auto checkDisabledLayers =
175 [&]( BOARD_ITEM* item ) -> bool
176 {
178 return false;
179
180 if( !reportProgress( ii++, items, progressDelta ) )
181 return false;
182
183 PCB_LAYER_ID badLayer = UNDEFINED_LAYER;
184
185 if( item->Type() == PCB_PAD_T )
186 {
187 PAD* pad = static_cast<PAD*>( item );
188
189 if( pad->GetAttribute() == PAD_ATTRIB::SMD
190 || pad->GetAttribute() == PAD_ATTRIB::CONN )
191 {
192 if( disabledLayers.test( pad->GetPrincipalLayer() ) )
193 badLayer = item->GetLayer();
194 }
195 else
196 {
197 // Through hole pad pierces all physical layers.
198 }
199 }
200 else if( item->Type() == PCB_VIA_T )
201 {
202 PCB_VIA* via = static_cast<PCB_VIA*>( item );
203 PCB_LAYER_ID top;
204 PCB_LAYER_ID bottom;
205
206 via->LayerPair( &top, &bottom );
207
208 if( disabledLayers.test( top ) )
209 badLayer = top;
210 else if( disabledLayers.test( bottom ) )
211 badLayer = bottom;
212 }
213 else if( item->Type() == PCB_ZONE_T )
214 {
215 // Footprint zones just get a top/bottom/inner setting, so they're on
216 // whatever inner layers there are.
217 }
218 else
219 {
220 LSET badLayers = disabledLayers & item->GetLayerSet();
221
222 if( badLayers.any() )
223 badLayer = badLayers.Seq().front();
224 }
225
226 if( badLayer != UNDEFINED_LAYER )
227 {
229 wxString msg;
230
231 msg.Printf( _( "(layer %s)" ), LayerName( badLayer ) );
232
233 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
234 drcItem->SetItems( item );
235
236 reportViolation( drcItem, item->GetPosition(), UNDEFINED_LAYER );
237 }
238
239 return true;
240 };
241
244}
245
246
248{
249 const int progressDelta = 2000;
250 int ii = 0;
251 int items = 0;
252
253 auto countItems =
254 [&]( BOARD_ITEM* item ) -> bool
255 {
256 ++items;
257 return true;
258 };
259
260 auto checkAssertions =
261 [&]( BOARD_ITEM* item ) -> bool
262 {
264 return false;
265
266 if( !reportProgress( ii++, items, progressDelta ) )
267 return false;
268
270 [&]( const DRC_CONSTRAINT* c )
271 {
273 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " (" )
274 + c->GetName() + wxS( ")" ) );
275 drcItem->SetItems( item );
276 drcItem->SetViolatingRule( c->GetParentRule() );
277
278 reportViolation( drcItem, item->GetPosition(), item->GetLayer() );
279 } );
280
281 return true;
282 };
283
284 forEachGeometryItem( {}, LSET::AllLayersMask(), countItems );
285 forEachGeometryItem( {}, LSET::AllLayersMask(), checkAssertions );
286}
287
288
290{
291 const int progressDelta = 2000;
292 int ii = 0;
293 int items = 0;
294
295 static const std::vector<KICAD_T> itemTypes = {
300 };
301
303 [&]( BOARD_ITEM* item ) -> bool
304 {
305 ++items;
306 return true;
307 } );
308
310 [&]( BOARD_ITEM* item ) -> bool
311 {
313 return false;
314
315 if( !reportProgress( ii++, items, progressDelta ) )
316 return false;
317
318 BOARD_ITEM* boardItem = dynamic_cast<BOARD_ITEM*>( item );
319 EDA_TEXT* textItem = dynamic_cast<EDA_TEXT*>( boardItem );
320
321 if( !textItem )
322 return true;
323
324 wxString resolved = ExpandEnvVarSubstitutions( textItem->GetShownText( true ),
325 nullptr /*project already done*/ );
326
327 if( resolved.Matches( wxT( "*${*}*" ) ) )
328 {
329 std::shared_ptr<DRC_ITEM>drcItem = DRC_ITEM::Create( DRCE_UNRESOLVED_VARIABLE );
330 drcItem->SetItems( item );
331
332 reportViolation( drcItem, boardItem->GetPosition(), boardItem->GetLayer() );
333 }
334
335 return true;
336 } );
337
339 DS_DRAW_ITEM_LIST drawItems( pcbIUScale );
340
342 return;
343
344 drawItems.SetPageNumber( wxT( "1" ) );
345 drawItems.SetSheetCount( 1 );
346 drawItems.SetFileName( wxT( "dummyFilename" ) );
347 drawItems.SetSheetName( wxT( "dummySheet" ) );
348 drawItems.SetSheetLayer( wxT( "dummyLayer" ) );
349 drawItems.SetProject( m_board->GetProject() );
350 drawItems.BuildDrawItemsList( drawingSheet->GetPageInfo(), drawingSheet->GetTitleBlock() );
351
352 for( DS_DRAW_ITEM_BASE* item = drawItems.GetFirst(); item; item = drawItems.GetNext() )
353 {
355 break;
356
357 if( m_drcEngine->IsCancelled() )
358 return;
359
360 DS_DRAW_ITEM_TEXT* text = dynamic_cast<DS_DRAW_ITEM_TEXT*>( item );
361
362 if( text && text->GetShownText( true ).Matches( wxT( "*${*}*" ) ) )
363 {
364 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_UNRESOLVED_VARIABLE );
365 drcItem->SetItems( drawingSheet );
366
367 reportViolation( drcItem, text->GetPosition(), LAYER_DRAWINGSHEET );
368 }
369 }
370}
371
372
374{
376
378 {
379 if( !reportPhase( _( "Checking board outline..." ) ) )
380 return false; // DRC cancelled
381
382 testOutline();
383 }
384
386 {
387 if( !reportPhase( _( "Checking disabled layers..." ) ) )
388 return false; // DRC cancelled
389
391 }
392
394 {
395 if( !reportPhase( _( "Checking text variables..." ) ) )
396 return false; // DRC cancelled
397
398 testTextVars();
399 }
400
402 {
403 if( !reportPhase( _( "Checking assertions..." ) ) )
404 return false; // DRC cancelled
405
407 }
408
409 return !m_drcEngine->IsCancelled();
410}
411
412
413namespace detail
414{
416}
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:108
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:77
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:226
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:281
LSET GetEnabledLayers() const
A proxy function that calls the corresponding function in m_BoardSettings.
Definition: board.cpp:680
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition: board.h:894
int GetOutlinesChainingEpsilon()
Definition: board.h:718
PROJECT * GetProject() const
Definition: board.h:475
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:797
Vec Centre() const
Definition: box2.h:87
wxString GetName() const
Definition: drc_rule.h:149
DRC_RULE * GetParentRule() const
Definition: drc_rule.h:145
DS_PROXY_VIEW_ITEM * GetDrawingSheet() const
Definition: drc_engine.h:98
BOARD * GetBoard() const
Definition: drc_engine.h:89
bool IsErrorLimitExceeded(int error_code)
void ProcessAssertions(const BOARD_ITEM *a, std::function< void(const DRC_CONSTRAINT *)> aFailureHandler, REPORTER *aReporter=nullptr)
bool IsCancelled() const
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Definition: drc_item.cpp:331
virtual const wxString GetName() const override
virtual const wxString GetDescription() const override
virtual bool Run() override
Run this provider against the given PCB with configured options (if any).
Represent a DRC "provider" which runs some DRC functions over a BOARD and spits out DRC_ITEM and posi...
virtual bool reportPhase(const wxString &aStageName)
int forEachGeometryItem(const std::vector< KICAD_T > &aTypes, LSET aLayers, const std::function< bool(BOARD_ITEM *)> &aFunc)
virtual void reportViolation(std::shared_ptr< DRC_ITEM > &item, const VECTOR2I &aMarkerPos, int aMarkerLayer)
static std::vector< KICAD_T > s_allBasicItems
DRC_ENGINE * m_drcEngine
virtual bool reportProgress(size_t aCount, size_t aSize, size_t aDelta=1)
Base class to handle basic graphic items.
Definition: ds_draw_item.h:59
Store the list of graphic items: rect, lines, polygons and texts to draw/plot the title block and fra...
Definition: ds_draw_item.h:401
DS_DRAW_ITEM_BASE * GetFirst()
Definition: ds_draw_item.h:511
void BuildDrawItemsList(const PAGE_INFO &aPageInfo, const TITLE_BLOCK &aTitleBlock)
Drawing or plot the drawing sheet.
void SetFileName(const wxString &aFileName)
Set the filename to draw/plot.
Definition: ds_draw_item.h:444
void SetSheetName(const wxString &aSheetName)
Set the sheet name to draw/plot.
Definition: ds_draw_item.h:449
void SetSheetLayer(const wxString &aSheetLayer)
Set the sheet layer to draw/plot.
Definition: ds_draw_item.h:459
void SetSheetCount(int aSheetCount)
Set the value of the count of sheets, for basic inscriptions.
Definition: ds_draw_item.h:498
void SetPageNumber(const wxString &aPageNumber)
Set the value of the sheet number.
Definition: ds_draw_item.h:488
DS_DRAW_ITEM_BASE * GetNext()
Definition: ds_draw_item.h:521
void SetProject(const PROJECT *aProject)
Definition: ds_draw_item.h:424
A graphic text.
Definition: ds_draw_item.h:313
virtual VECTOR2I GetPosition() const
Definition: eda_item.h:242
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:83
virtual wxString GetShownText(bool aAllowExtraText, int aDepth=0) const
Return the string actually shown after processing of the base text.
Definition: eda_text.h:109
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:574
static LSET AllLayersMask()
Definition: lset.cpp:898
LSEQ Seq(const PCB_LAYER_ID *aWishListSequence, unsigned aCount) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition: lset.cpp:418
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:863
Definition: pad.h:59
PCB_LAYER_ID GetLayer() const override
Return the primary layer this item is on.
Definition: pad.cpp:392
Represent a set of closed polygons.
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition: common.cpp:334
bool TestBoardOutlinesGraphicItems(BOARD *aBoard, int aMinDist, OUTLINE_ERROR_HANDLER *aErrorHandler)
Test a board graphic items on edge cut layer for validity.
bool BuildBoardPolygonOutlines(BOARD *aBoard, SHAPE_POLY_SET &aOutlines, int aErrorMax, int aChainingEpsilon, OUTLINE_ERROR_HANDLER *aErrorHandler, bool aAllowUseArcsInPolygons)
Extract the board outlines and build a closed polygon from lines, arcs and circle items on edge cut l...
const std::function< void(const wxString &msg, BOARD_ITEM *itemA, BOARD_ITEM *itemB, const VECTOR2I &pt)> OUTLINE_ERROR_HANDLER
@ DRCE_DISABLED_LAYER_ITEM
Definition: drc_item.h:67
@ DRCE_INVALID_OUTLINE
Definition: drc_item.h:68
@ DRCE_UNRESOLVED_VARIABLE
Definition: drc_item.h:82
@ DRCE_ASSERTION_FAILURE
Definition: drc_item.h:83
#define _(s)
wxString LayerName(int aLayer)
Returns the default display name for a given layer.
Definition: layer_id.cpp:30
@ LAYER_DRAWINGSHEET
drawingsheet frame and titleblock
Definition: layer_ids.h:221
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
@ Edge_Cuts
Definition: layer_ids.h:113
@ UNDEFINED_LAYER
Definition: layer_ids.h:61
static DRC_REGISTER_TEST_PROVIDER< DRC_TEST_PROVIDER_ANNULAR_WIDTH > dummy
constexpr int mmToIU(double mm) const
Definition: base_units.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:107
@ 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_PAD_T
class PAD, a pad in a footprint
Definition: typeinfo.h:87
@ PCB_DIMENSION_T
class PCB_DIMENSION_BASE: abstract dimension meta-type
Definition: typeinfo.h:100