KiCad PCB EDA Suite
drc_test_provider_edge_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 (C) 2004-2021 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 <pcb_shape.h>
26 #include <geometry/seg.h>
27 #include <geometry/shape_segment.h>
28 #include <drc/drc_engine.h>
29 #include <drc/drc_item.h>
30 #include <drc/drc_rule.h>
32 #include "drc_rtree.h"
33 
34 /*
35  Board edge clearance test. Checks all items for their mechanical clearances against the board
36  edge.
37  Errors generated:
38  - DRCE_EDGE_CLEARANCE
39 
40  TODO:
41  - separate holes to edge check
42  - tester only looks for edge crossings. it doesn't check if items are inside/outside the board
43  area.
44  - pad test missing!
45 */
46 
48 {
49 public:
52  {
53  }
54 
56  {
57  }
58 
59  virtual bool Run() override;
60 
61  virtual const wxString GetName() const override
62  {
63  return "edge_clearance";
64  }
65 
66  virtual const wxString GetDescription() const override
67  {
68  return "Tests items vs board edge clearance";
69  }
70 
71  virtual std::set<DRC_CONSTRAINT_T> GetConstraintTypes() const override;
72 
73  int GetNumPhases() const override;
74 
75 private:
76  bool testAgainstEdge( BOARD_ITEM* item, SHAPE* itemShape, BOARD_ITEM* other,
77  DRC_CONSTRAINT_T aConstraintType, PCB_DRC_CODE aErrorCode );
78 };
79 
80 
82  BOARD_ITEM* edge,
83  DRC_CONSTRAINT_T aConstraintType,
84  PCB_DRC_CODE aErrorCode )
85 {
86  const std::shared_ptr<SHAPE>& edgeShape = edge->GetEffectiveShape( Edge_Cuts );
87 
88  auto constraint = m_drcEngine->EvalRules( aConstraintType, edge, item, item->GetLayer() );
89  int minClearance = constraint.GetValue().Min();
90  int actual;
91  VECTOR2I pos;
92 
93  if( minClearance >= 0 && itemShape->Collide( edgeShape.get(), minClearance, &actual, &pos ) )
94  {
95  std::shared_ptr<DRC_ITEM> drce = DRC_ITEM::Create( aErrorCode );
96 
97  // Only report clearance info if there is any; otherwise it's just a straight collision
98  if( minClearance > 0 )
99  {
100  m_msg.Printf( _( "(%s clearance %s; actual %s)" ),
101  constraint.GetName(),
102  MessageTextFromValue( userUnits(), minClearance ),
103  MessageTextFromValue( userUnits(), actual ) );
104 
105  drce->SetErrorMessage( drce->GetErrorText() + wxS( " " ) + m_msg );
106  }
107 
108  drce->SetItems( edge->m_Uuid, item->m_Uuid );
109  drce->SetViolatingRule( constraint.GetParentRule() );
110 
111  reportViolation( drce, (wxPoint) pos );
112  return false; // don't report violations with multiple edges; one is enough
113  }
114 
115  return true;
116 }
117 
118 
120 {
122  {
123  if( !reportPhase( _( "Checking copper to board edge clearances..." ) ) )
124  return false; // DRC cancelled
125  }
127  {
128  if( !reportPhase( _( "Checking silk to board edge clearances..." ) ) )
129  return false; // DRC cancelled
130  }
131  else
132  {
133  reportAux( "Edge clearance violations ignored. Tests not run." );
134  return true; // continue with other tests
135  }
136 
138 
139  DRC_CONSTRAINT worstClearanceConstraint;
140 
141  if( m_drcEngine->QueryWorstConstraint( EDGE_CLEARANCE_CONSTRAINT, worstClearanceConstraint ) )
142  m_largestClearance = worstClearanceConstraint.GetValue().Min();
143 
144  reportAux( "Worst clearance : %d nm", m_largestClearance );
145 
146  std::vector<std::unique_ptr<PCB_SHAPE>> edges; // we own these
147  DRC_RTREE edgesTree;
148  std::vector<BOARD_ITEM*> boardItems; // we don't own these
149 
150  auto queryBoardOutlineItems =
151  [&]( BOARD_ITEM *item ) -> bool
152  {
153  PCB_SHAPE* shape = static_cast<PCB_SHAPE*>( item );
154 
155  if( shape->GetShape() == SHAPE_T::RECT )
156  {
157  // A single rectangle for the board would make the RTree useless, so convert
158  // to 4 edges
159  edges.emplace_back( static_cast<PCB_SHAPE*>( shape->Clone() ) );
160  edges.back()->SetShape( SHAPE_T::SEGMENT );
161  edges.back()->SetEndX( shape->GetStartX() );
162  edges.back()->SetWidth( 0 );
163  edges.emplace_back( static_cast<PCB_SHAPE*>( shape->Clone() ) );
164  edges.back()->SetShape( SHAPE_T::SEGMENT );
165  edges.back()->SetEndY( shape->GetStartY() );
166  edges.back()->SetWidth( 0 );
167  edges.emplace_back( static_cast<PCB_SHAPE*>( shape->Clone() ) );
168  edges.back()->SetShape( SHAPE_T::SEGMENT );
169  edges.back()->SetStartX( shape->GetEndX() );
170  edges.back()->SetWidth( 0 );
171  edges.emplace_back( static_cast<PCB_SHAPE*>( shape->Clone() ) );
172  edges.back()->SetShape( SHAPE_T::SEGMENT );
173  edges.back()->SetStartY( shape->GetEndY() );
174  edges.back()->SetWidth( 0 );
175  return true;
176  }
177  else if( shape->GetShape() == SHAPE_T::POLY )
178  {
179  // A single polygon for the board would make the RTree useless, so convert
180  // to n edges.
181  SHAPE_LINE_CHAIN poly = shape->GetPolyShape().Outline( 0 );
182 
183  for( size_t ii = 0; ii < poly.GetSegmentCount(); ++ii )
184  {
185  SEG seg = poly.CSegment( ii );
186  edges.emplace_back( static_cast<PCB_SHAPE*>( shape->Clone() ) );
187  edges.back()->SetShape( SHAPE_T::SEGMENT );
188  edges.back()->SetStart((wxPoint) seg.A );
189  edges.back()->SetEnd((wxPoint) seg.B );
190  edges.back()->SetWidth( 0 );
191  }
192  }
193 
194  edges.emplace_back( static_cast<PCB_SHAPE*>( shape->Clone() ) );
195  edges.back()->SetWidth( 0 );
196  return true;
197  };
198 
199  auto queryBoardGeometryItems =
200  [&]( BOARD_ITEM *item ) -> bool
201  {
202  if( !isInvisibleText( item ) )
203  boardItems.push_back( item );
204 
205  return true;
206  };
207 
209  queryBoardOutlineItems );
210  forEachGeometryItem( s_allBasicItemsButZones, LSET::AllLayersMask(), queryBoardGeometryItems );
211 
212  for( const std::unique_ptr<PCB_SHAPE>& edge : edges )
213  {
214  for( PCB_LAYER_ID layer : { Edge_Cuts, Margin } )
215  {
216  if( edge->IsOnLayer( layer ) )
217  edgesTree.Insert( edge.get(), layer, m_largestClearance );
218  }
219  }
220 
221  wxString val;
222  wxGetEnv( "WXTRACE", &val );
223 
224  drc_dbg( 2, "outline: %d items, board: %d items\n",
225  (int) edges.size(), (int) boardItems.size() );
226 
227  // This is the number of tests between 2 calls to the progress bar
228  const int delta = 50;
229  int ii = 0;
230 
231  for( BOARD_ITEM* item : boardItems )
232  {
235 
236  if( !testCopper && !testSilk )
237  break;
238 
239  if( !reportProgress( ii++, boardItems.size(), delta ) )
240  return false; // DRC cancelled
241 
242  const std::shared_ptr<SHAPE>& itemShape = item->GetEffectiveShape();
243 
244  for( PCB_LAYER_ID testLayer : { Edge_Cuts, Margin } )
245  {
246  if( testCopper && item->IsOnCopperLayer() )
247  {
248  edgesTree.QueryColliding( item, UNDEFINED_LAYER, testLayer, nullptr,
249  [&]( BOARD_ITEM* edge ) -> bool
250  {
251  return testAgainstEdge( item, itemShape.get(), edge,
254  },
256  }
257 
258  if( testSilk && ( item->GetLayer() == F_SilkS || item->GetLayer() == B_SilkS ) )
259  {
260  if( edgesTree.QueryColliding( item, UNDEFINED_LAYER, testLayer, nullptr,
261  [&]( BOARD_ITEM* edge ) -> bool
262  {
263  return testAgainstEdge( item, itemShape.get(), edge,
264  SILK_CLEARANCE_CONSTRAINT,
265  DRCE_SILK_MASK_CLEARANCE );
266  },
268  {
269  // violations reported during QueryColliding
270  }
271  else
272  {
273  // TODO: check postion being outside board boundary
274  }
275  }
276  }
277  }
278 
280 
281  return true;
282 }
283 
284 
286 {
287  return 1;
288 }
289 
290 
292 {
294 }
295 
296 
297 namespace detail
298 {
300 }
wxString MessageTextFromValue(EDA_UNITS aUnits, int aValue, bool aAddUnitLabel, EDA_DATA_TYPE aType)
Convert a value to a string using double notation.
Definition: base_units.cpp:104
virtual size_t GetSegmentCount() const override
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Definition: drc_item.cpp:254
polygon (not yet used for tracks, but could be in microwave apps)
SHAPE_POLY_SET & GetPolyShape()
Definition: pcb_shape.h:240
bool isInvisibleText(const BOARD_ITEM *aItem) const
int GetEndY()
Definition: pcb_shape.h:135
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:80
virtual const wxString GetName() const override
SHAPE_T GetShape() const
Definition: pcb_shape.h:110
virtual void reportViolation(std::shared_ptr< DRC_ITEM > &item, const wxPoint &aMarkerPos)
virtual EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: pcb_shape.cpp:1022
virtual std::set< DRC_CONSTRAINT_T > GetConstraintTypes() const override
bool IsErrorLimitExceeded(int error_code)
usual segment : line with rounded ends
virtual bool reportProgress(int aCount, int aSize, int aDelta)
class FP_SHAPE, a footprint edge
Definition: typeinfo.h:93
T Min() const
Definition: minoptmax.h:33
PCB_DRC_CODE
Definition: drc_item.h:34
virtual void reportRuleStatistics()
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:165
static DRC_REGISTER_TEST_PROVIDER< DRC_TEST_PROVIDER_ANNULAR_WIDTH > dummy
DRC_CONSTRAINT_T
Definition: drc_rule.h:41
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:502
bool QueryWorstConstraint(DRC_CONSTRAINT_T aRuleId, DRC_CONSTRAINT &aConstraint)
bool testAgainstEdge(BOARD_ITEM *item, SHAPE *itemShape, BOARD_ITEM *other, DRC_CONSTRAINT_T aConstraintType, PCB_DRC_CODE aErrorCode)
BOARD * GetBoard() const
Definition: drc_engine.h:88
virtual bool reportPhase(const wxString &aStageName)
SHAPE_LINE_CHAIN & Outline(int aIndex)
virtual const wxString GetDescription() const override
int GetEndX()
Definition: pcb_shape.h:136
#define _(s)
static LSET AllLayersMask()
Definition: lset.cpp:787
An abstract shape on 2D plane.
Definition: shape.h:116
EDA_UNITS userUnits() const
DRC_CONSTRAINT EvalRules(DRC_CONSTRAINT_T aConstraintType, const BOARD_ITEM *a, const BOARD_ITEM *b, PCB_LAYER_ID aLayer, REPORTER *aReporter=nullptr)
Definition: drc_engine.cpp:765
const KIID m_Uuid
Definition: eda_item.h:475
Definition: seg.h:40
int forEachGeometryItem(const std::vector< KICAD_T > &aTypes, LSET aLayers, const std::function< bool(BOARD_ITEM *)> &aFunc)
virtual bool Run() override
Run this provider against the given PCB with configured options (if any).
const MINOPTMAX< int > & GetValue() const
Definition: drc_rule.h:122
const SEG CSegment(int aIndex) const
Return a constant copy of the aIndex segment in the line chain.
Represent a polyline (an zero-thickness chain of connected line segments).
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:64
static std::vector< KICAD_T > s_allBasicItemsButZones
DRC_ENGINE * m_drcEngine
VECTOR2I A
Definition: seg.h:48
The common library.
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:86
virtual std::shared_ptr< SHAPE > GetEffectiveShape(PCB_LAYER_ID aLayer=UNDEFINED_LAYER) const
Some pad shapes can be complex (rounded/chamfered rectangle), even without considering custom shapes.
Definition: board_item.cpp:181
constexpr int delta
#define drc_dbg(level, fmt,...)
Definition: drc_engine.h:57
segment with non rounded ends
Implement an R-tree for fast spatial and layer indexing of connectable items.
Definition: drc_rtree.h:44
class PCB_SHAPE, a segment not on copper layers
Definition: typeinfo.h:90
int GetStartY()
Definition: pcb_shape.h:125
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:171
virtual void reportAux(wxString fmt,...)
int GetStartX()
Definition: pcb_shape.h:126
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.
Definition: drc_rtree.h:165
VECTOR2I B
Definition: seg.h:49