KiCad PCB EDA Suite
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 (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 <common.h>
25 #include <board.h>
26 #include <footprint.h>
27 #include <pcb_shape.h>
28 
29 #include <geometry/seg.h>
30 #include <geometry/shape_segment.h>
31 
32 #include <drc/drc_engine.h>
33 #include <drc/drc_item.h>
34 #include <drc/drc_rule.h>
36 
37 #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_OVERLAPPING_SILK
43 
44 */
45 
47 {
48 public:
50  m_board( nullptr ),
52  {
53  }
54 
56  {
57  }
58 
59  virtual bool Run() override;
60 
61  virtual const wxString GetName() const override
62  {
63  return "silk_clearance";
64  };
65 
66  virtual const wxString GetDescription() const override
67  {
68  return "Tests for overlapping silkscreen features.";
69  }
70 
71  virtual int GetNumPhases() const override
72  {
73  return 1;
74  }
75 
76  virtual std::set<DRC_CONSTRAINT_T> GetConstraintTypes() const override;
77 
78 private:
79 
82 };
83 
84 
86 {
87  // This is the number of tests between 2 calls to the progress bar
88  const int delta = 500;
89 
91  {
92  reportAux( "Overlapping silk violations ignored. Tests not run." );
93  return true; // continue with other tests
94  }
95 
97 
98  DRC_CONSTRAINT worstClearanceConstraint;
100 
101  if( m_drcEngine->QueryWorstConstraint( SILK_CLEARANCE_CONSTRAINT, worstClearanceConstraint ) )
102  m_largestClearance = worstClearanceConstraint.m_Value.Min();
103 
104  reportAux( "Worst clearance : %d nm", m_largestClearance );
105 
106  if( !reportPhase( _( "Checking silkscreen for overlapping items..." ) ) )
107  return false; // DRC cancelled
108 
109  DRC_RTREE silkTree;
110  DRC_RTREE targetTree;
111  int ii = 0;
112  int items = 0;
113 
114  auto countItems =
115  [&]( BOARD_ITEM* item ) -> bool
116  {
117  ++items;
118  return true;
119  };
120 
121  auto addToSilkTree =
122  [&]( BOARD_ITEM* item ) -> bool
123  {
124  if( !reportProgress( ii++, items, delta ) )
125  return false;
126 
127  for( PCB_LAYER_ID layer : { F_SilkS, B_SilkS } )
128  {
129  if( item->IsOnLayer( layer ) )
130  silkTree.Insert( item, layer );
131  }
132 
133  return true;
134  };
135 
136  auto addToTargetTree =
137  [&]( BOARD_ITEM* item ) -> bool
138  {
139  if( !reportProgress( ii++, items, delta ) )
140  return false;
141 
142  for( PCB_LAYER_ID layer : item->GetLayerSet().Seq() )
143  targetTree.Insert( item, layer );
144 
145  return true;
146  };
147 
148  forEachGeometryItem( s_allBasicItems, LSET( 2, F_SilkS, B_SilkS ), countItems );
149 
152  countItems );
153 
154  forEachGeometryItem( s_allBasicItems, LSET( 2, F_SilkS, B_SilkS ), addToSilkTree );
155 
158  addToTargetTree );
159 
160  reportAux( _("Testing %d silkscreen features against %d board items."),
161  silkTree.size(),
162  targetTree.size() );
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* aRefItem,
188  DRC_RTREE::ITEM_WITH_SHAPE* aTestItem, bool* aCollisionDetected ) -> bool
189  {
191  return false;
192 
193  if( isInvisibleText( aRefItem->parent ) || isInvisibleText( aTestItem->parent ) )
194  return true;
195 
196  auto constraint = m_drcEngine->EvalRules( SILK_CLEARANCE_CONSTRAINT,
197  aRefItem->parent, aTestItem->parent,
198  aLayers.second );
199 
200  if( constraint.IsNull() )
201  return true;
202 
203  int minClearance = constraint.GetValue().Min();
204 
205  if( minClearance < 0 )
206  return true;
207 
208  int actual;
209  VECTOR2I pos;
210 
211  // Graphics are often compound shapes so ignore collisions between shapes in a
212  // single footprint or on the board.
213  PCB_SHAPE* refGraphic = dynamic_cast<PCB_SHAPE*>( aRefItem->parent );
214  PCB_SHAPE* testGraphic = dynamic_cast<PCB_SHAPE*>( aTestItem->parent );
215 
216  if( refGraphic && testGraphic )
217  {
218  FOOTPRINT *refParentFP = dynamic_cast<FOOTPRINT*>( refGraphic->GetParent() );
219  FOOTPRINT *testParentFP = dynamic_cast<FOOTPRINT*>( testGraphic->GetParent() );
220 
221  if( refParentFP == testParentFP ) // also true when both are nullptr
222  return true;
223  }
224 
225  if( aRefItem->shape->Collide( aTestItem->shape, minClearance, &actual, &pos ) )
226  {
227  std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_OVERLAPPING_SILK );
228 
229  if( minClearance > 0 )
230  {
231  m_msg.Printf( _( "(%s clearance %s; actual %s)" ),
232  constraint.GetParentRule()->m_Name,
233  MessageTextFromValue( userUnits(), minClearance ),
234  MessageTextFromValue( userUnits(), actual ) );
235 
236  drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + m_msg );
237  }
238 
239  drcItem->SetItems( aRefItem->parent, aTestItem->parent );
240  drcItem->SetViolatingRule( constraint.GetParentRule() );
241 
242  reportViolation( drcItem, (wxPoint) pos );
243 
244  *aCollisionDetected = true;
245  }
246 
247  return true;
248  },
250  [&]( int aCount, int aSize ) -> bool
251  {
252  return reportProgress( aCount, aSize, delta );
253  } );
254 
256 
257  return true;
258 }
259 
260 
262 {
263  return { SILK_CLEARANCE_CONSTRAINT };
264 }
265 
266 
267 namespace detail
268 {
270 }
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 bool Run() override
Run this provider against the given PCB with configured options (if any).
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Definition: drc_item.cpp:254
bool isInvisibleText(const BOARD_ITEM *aItem) const
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:80
virtual void reportViolation(std::shared_ptr< DRC_ITEM > &item, const wxPoint &aMarkerPos)
bool IsErrorLimitExceeded(int error_code)
virtual bool reportProgress(int aCount, int aSize, int aDelta)
T Min() const
Definition: minoptmax.h:33
std::pair< PCB_LAYER_ID, PCB_LAYER_ID > LAYER_PAIR
Definition: drc_rtree.h:316
static LSET FrontMask()
Return a mask holding all technical layers and the external CU layer on front side.
Definition: lset.cpp:874
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
virtual std::set< DRC_CONSTRAINT_T > GetConstraintTypes() const override
virtual const wxString GetName() const override
static DRC_REGISTER_TEST_PROVIDER< DRC_TEST_PROVIDER_ANNULAR_WIDTH > dummy
MINOPTMAX< int > m_Value
Definition: drc_rule.h:145
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:502
bool QueryWorstConstraint(DRC_CONSTRAINT_T aRuleId, DRC_CONSTRAINT &aConstraint)
BOARD * GetBoard() const
Definition: drc_engine.h:88
size_t size() const
Return the number of items in the tree.
Definition: drc_rtree.h:410
virtual bool reportPhase(const wxString &aStageName)
#define _(s)
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
int forEachGeometryItem(const std::vector< KICAD_T > &aTypes, LSET aLayers, const std::function< bool(BOARD_ITEM *)> &aFunc)
Represent a DRC "provider" which runs some DRC functions over a BOARD and spits out #DRC_ITEMs and po...
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:331
const MINOPTMAX< int > & GetValue() const
Definition: drc_rule.h:122
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:190
static std::vector< KICAD_T > s_allBasicItems
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:64
DRC_ENGINE * m_drcEngine
Definition: layer_ids.h:70
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
static LSET BackMask()
Return a mask holding all technical layers and the external CU layer on back side.
Definition: lset.cpp:881
constexpr int delta
Implement an R-tree for fast spatial and layer indexing of connectable items.
Definition: drc_rtree.h:44
BOARD_ITEM_CONTAINER * GetParent() const
Definition: board_item.h:166
virtual const wxString GetDescription() const override
virtual void reportAux(wxString fmt,...)