KiCad PCB EDA Suite
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-2020 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 <drc/drc_engine.h>
25 #include <drc/drc_item.h>
26 #include <drc/drc_rule.h>
27 #include <drc/drc_test_provider.h>
28 
31 
32 /*
33  Miscellaneous tests:
34 
35  - DRCE_DISABLED_LAYER_ITEM, ///< item on a disabled layer
36  - DRCE_INVALID_OUTLINE, ///< invalid board outline
37  - DRCE_UNRESOLVED_VARIABLE,
38 
39  TODO:
40  - if grows too big, split into separate providers
41 */
42 
44 {
45 public:
47  m_board( nullptr )
48  {
49  m_isRuleDriven = false;
50  }
51 
53  {
54  }
55 
56  virtual bool Run() override;
57 
58  virtual const wxString GetName() const override
59  {
60  return "miscellaneous";
61  };
62 
63  virtual const wxString GetDescription() const override
64  {
65  return "Misc checks (board outline, missing textvars)";
66  }
67 
68  virtual std::set<DRC_CONSTRAINT_T> GetConstraintTypes() const override;
69 
70  int GetNumPhases() const override;
71 
72 private:
73  void testOutline();
74  void testDisabledLayers();
75  void testTextVars();
76 
78 };
79 
80 
82 {
83  SHAPE_POLY_SET dummyOutline;
84  bool errorHandled = false;
85 
86  OUTLINE_ERROR_HANDLER errorHandler =
87  [&]( const wxString& msg, BOARD_ITEM* itemA, BOARD_ITEM* itemB, const wxPoint& pt )
88  {
89  std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_INVALID_OUTLINE );
90 
91  drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
92  drcItem->SetItems( itemA, itemB );
93 
94  reportViolation( drcItem, pt );
95  errorHandled = true;
96  };
97 
98  // Use a really tight chaining epsilon here so that we report errors that might affect
99  // other tools (such as STEP export).
100  constexpr int chainingEpsilon = Millimeter2iu( 0.02 ) / 100;
101 
103  chainingEpsilon, &errorHandler ) )
104  {
105  if( errorHandled )
106  {
107  // if there is an invalid outline, then there must be an outline
108  }
109  else
110  {
111  std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_INVALID_OUTLINE );
112 
113  m_msg.Printf( _( "(no edges found on Edge.Cuts layer)" ) );
114 
115  drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + m_msg );
116  drcItem->SetItems( m_board );
117 
118  reportViolation( drcItem, m_board->GetBoundingBox().Centre() );
119  }
120  }
121 }
122 
123 
125 {
126  LSET disabledLayers = m_board->GetEnabledLayers().flip();
127 
128  // Perform the test only for copper layers
129  disabledLayers &= LSET::AllCuMask();
130 
131  auto checkDisabledLayers =
132  [&]( BOARD_ITEM* item ) -> bool
133  {
134  LSET refLayers ( item->GetLayer() );
135 
136  if( ( disabledLayers & refLayers ).any() )
137  {
138  std::shared_ptr<DRC_ITEM>drcItem = DRC_ITEM::Create( DRCE_DISABLED_LAYER_ITEM );
139 
140  m_msg.Printf( _( "(layer %s)" ),
141  item->GetLayerName() );
142 
143  drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + m_msg );
144  drcItem->SetItems( item );
145 
146  reportViolation( drcItem, item->GetPosition() );
147  }
148  return true;
149  };
150 
151  // fixme: what about graphical items?
153  LSET::AllLayersMask(), checkDisabledLayers );
154 }
155 
157 {
158  auto checkUnresolvedTextVar =
159  [&]( EDA_ITEM* item ) -> bool
160  {
162  return false;
163 
164  EDA_TEXT* text = dynamic_cast<EDA_TEXT*>( item );
165 
166  if( text && text->GetShownText().Matches( wxT( "*${*}*" ) ) )
167  {
168  std::shared_ptr<DRC_ITEM>drcItem = DRC_ITEM::Create( DRCE_UNRESOLVED_VARIABLE );
169  drcItem->SetItems( item );
170 
171  reportViolation( drcItem, item->GetPosition() );
172  }
173  return true;
174  };
175 
177  checkUnresolvedTextVar );
178 
180  DS_DRAW_ITEM_LIST drawItems;
181 
183  return;
184 
185  drawItems.SetMilsToIUfactor( IU_PER_MILS );
186  drawItems.SetPageNumber( "1" );
187  drawItems.SetSheetCount( 1 );
188  drawItems.SetFileName( "dummyFilename" );
189  drawItems.SetSheetName( "dummySheet" );
190  drawItems.SetSheetLayer( "dummyLayer" );
191  drawItems.SetProject( m_board->GetProject() );
192  drawItems.BuildDrawItemsList( drawingSheet->GetPageInfo(), drawingSheet->GetTitleBlock() );
193 
194  for( DS_DRAW_ITEM_BASE* item = drawItems.GetFirst(); item; item = drawItems.GetNext() )
195  {
197  break;
198 
199  DS_DRAW_ITEM_TEXT* text = dynamic_cast<DS_DRAW_ITEM_TEXT*>( item );
200 
201  if( text && text->GetShownText().Matches( wxT( "*${*}*" ) ) )
202  {
203  std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_UNRESOLVED_VARIABLE );
204  drcItem->SetItems( text );
205 
206  reportViolation( drcItem, text->GetPosition() );
207  }
208  }
209 }
210 
211 
213 {
215 
217  {
218  if( !reportPhase( _( "Checking board outline..." ) ) )
219  return false; // DRC cancelled
220 
221  testOutline();
222  }
223 
225  {
226  if( !reportPhase( _( "Checking disabled layers..." ) ) )
227  return false; // DRC cancelled
228 
230  }
231 
233  {
234  if( !reportPhase( _( "Checking text variables..." ) ) )
235  return false; // DRC cancelled
236 
237  testTextVars();
238  }
239 
240  return true;
241 }
242 
243 
245 {
246  return 3;
247 }
248 
249 
250 std::set<DRC_CONSTRAINT_T> DRC_TEST_PROVIDER_MISC::GetConstraintTypes() const
251 {
252  return {};
253 }
254 
255 
256 namespace detail
257 {
259 }
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:750
const EDA_RECT GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition: board.h:798
DS_PROXY_VIEW_ITEM * GetDrawingSheet() const
Definition: drc_engine.h:96
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Definition: drc_item.cpp:245
class FP_TEXT, text in a footprint
Definition: typeinfo.h:92
void SetMilsToIUfactor(double aMils2Iu)
Set the scalar to convert pages units (mils) to draw/plot units.
Definition: ds_draw_item.h:442
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:82
void SetFileName(const wxString &aFileName)
Set the filename to draw/plot.
Definition: ds_draw_item.h:415
static DRC_REGISTER_TEST_PROVIDER< DRC_TEST_PROVIDER_ANNULUS > dummy
bool IsErrorLimitExceeded(int error_code)
PROJECT * GetProject() const
Definition: board.h:432
class PCB_TEXT, text on a layer
Definition: typeinfo.h:91
class ARC, an arc track segment on a copper layer
Definition: typeinfo.h:97
virtual std::set< DRC_CONSTRAINT_T > GetConstraintTypes() const override
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.h:593
class PAD, a pad in a footprint
Definition: typeinfo.h:89
LSET GetEnabledLayers() const
A proxy function that calls the corresponding function in m_BoardSettings Returns a bit-mask of all t...
Definition: board.cpp:447
virtual const wxString GetDescription() const override
int GetNumPhases() const override
class TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:95
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:119
Base class to handle basic graphic items.
Definition: ds_draw_item.h:58
A graphic text.
Definition: ds_draw_item.h:297
LSET is a set of PCB_LAYER_IDs.
DS_DRAW_ITEM_BASE * GetNext()
Definition: ds_draw_item.h:494
BOARD * GetBoard() const
Definition: drc_engine.h:87
virtual bool reportPhase(const wxString &aStageName)
Represent a set of closed polygons.
void SetPageNumber(const wxString &aPageNumber)
Set the value of the sheet number.
Definition: ds_draw_item.h:455
bool BuildBoardPolygonOutlines(BOARD *aBoard, SHAPE_POLY_SET &aOutlines, int aErrorMax, int aChainingEpsilon, OUTLINE_ERROR_HANDLER *aErrorHandler)
Extracts the board outlines and build a closed polygon from lines, arcs and circle items on edge cut ...
virtual const wxString GetName() const override
static LSET AllLayersMask()
Definition: lset.cpp:787
class ZONE, a copper pour area
Definition: typeinfo.h:105
void SetSheetName(const wxString &aSheetName)
Set the sheet name to draw/plot.
Definition: ds_draw_item.h:423
virtual void reportViolation(std::shared_ptr< DRC_ITEM > &item, wxPoint aMarkerPos)
int forEachGeometryItem(const std::vector< KICAD_T > &aTypes, LSET aLayers, const std::function< bool(BOARD_ITEM *)> &aFunc)
void BuildDrawItemsList(const PAGE_INFO &aPageInfo, const TITLE_BLOCK &aTitleBlock)
Drawing or plot the page layout.
DRC_TEST_PROVIDER is a base class that represents a DRC "provider" which runs some DRC functions over...
void SetSheetLayer(const wxString &aSheetLayer)
Set the sheet layer to draw/plot.
Definition: ds_draw_item.h:431
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:190
#define _(s)
Definition: 3d_actions.cpp:33
DRC_ENGINE * m_drcEngine
void SetProject(const PROJECT *aProject)
Definition: ds_draw_item.h:400
#define IU_PER_MILS
Definition: plotter.cpp:137
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:150
const std::function< void(const wxString &msg, BOARD_ITEM *itemA, BOARD_ITEM *itemB, const wxPoint &pt)> OUTLINE_ERROR_HANDLER
wxPoint Centre() const
Definition: eda_rect.h:60
virtual bool Run() override
Runs this provider against the given PCB with configured options (if any).
void SetSheetCount(int aSheetCount)
Set the value of the count of sheets, for basic inscriptions.
Definition: ds_draw_item.h:468
Store the list of graphic items: rect, lines, polygons and texts to draw/plot the title block and fra...
Definition: ds_draw_item.h:376
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:96
DS_DRAW_ITEM_BASE * GetFirst()
Definition: ds_draw_item.h:484
static constexpr int Millimeter2iu(double mm)