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 <pcb_shape.h>
27 
28 #include <geometry/seg.h>
29 #include <geometry/shape_segment.h>
30 
31 #include <drc/drc_item.h>
32 #include <drc/drc_rule.h>
34 
35 #include <drc/drc_rtree.h>
36 
37 /*
38  Silk to silk clearance test. Check all silkscreen features against each other.
39  Errors generated:
40  - DRCE_OVERLAPPING_SILK
41 
42 */
43 
45 {
46 public:
48  m_board( nullptr ),
50  {
51  }
52 
54  {
55  }
56 
57  virtual bool Run() override;
58 
59  virtual const wxString GetName() const override
60  {
61  return "silk_clearance";
62  };
63 
64  virtual const wxString GetDescription() const override
65  {
66  return "Tests for overlapping silkscreen features.";
67  }
68 
69  virtual int GetNumPhases() const override
70  {
71  return 1;
72  }
73 
74  virtual std::set<DRC_CONSTRAINT_TYPE_T> GetConstraintTypes() const override;
75 
76 private:
77 
80 };
81 
82 
84 {
85  // This is the number of tests between 2 calls to the progress bar
86  const int delta = 250;
87 
89 
90  DRC_CONSTRAINT worstClearanceConstraint;
92 
94  {
95  reportAux( "Silkscreen clearance testing not run." );
96  return true;
97  }
98 
99  if( m_drcEngine->QueryWorstConstraint( SILK_CLEARANCE_CONSTRAINT, worstClearanceConstraint ) )
100  m_largestClearance = worstClearanceConstraint.m_Value.Min();
101 
102  reportAux( "Worst clearance : %d nm", m_largestClearance );
103 
104  if( !reportPhase( _( "Checking silkscreen for overlapping items..." ) ) )
105  return false;
106 
107  DRC_RTREE silkTree;
108  DRC_RTREE targetTree;
109  int ii = 0;
110  int targets = 0;
111 
112  auto addToSilkTree =
113  [&silkTree]( BOARD_ITEM* item ) -> bool
114  {
115  silkTree.Insert( item );
116  return true;
117  };
118 
119  auto countTargets =
120  [&targets]( BOARD_ITEM* item ) -> bool
121  {
122  ++targets;
123  return true;
124  };
125 
126  auto addToTargetTree =
127  [&]( BOARD_ITEM* item ) -> bool
128  {
129  if( !reportProgress( ii++, targets, delta ) )
130  return false;
131 
132  targetTree.Insert( item );
133  return true;
134  };
135 
136  auto checkClearance =
137  [&]( const DRC_RTREE::LAYER_PAIR& aLayers, DRC_RTREE::ITEM_WITH_SHAPE* aRefItem,
138  DRC_RTREE::ITEM_WITH_SHAPE* aTestItem, bool* aCollisionDetected ) -> bool
139  {
141  return false;
142 
143  if ( isInvisibleText( aRefItem->parent ) )
144  return true;
145 
146  if ( isInvisibleText( aTestItem->parent ) )
147  return true;
148 
150  aRefItem->parent,
151  aTestItem->parent,
152  aLayers.second );
153 
154  if( constraint.IsNull() )
155  return true;
156 
157  int minClearance = constraint.GetValue().Min();
158  int actual;
159  VECTOR2I pos;
160 
161  accountCheck( constraint );
162 
163  // Graphics are often compound shapes so ignore collisions between shapes in a
164  // single footprint or on the board.
165  PCB_SHAPE* refGraphic = dynamic_cast<PCB_SHAPE*>( aRefItem->parent );
166  PCB_SHAPE* testGraphic = dynamic_cast<PCB_SHAPE*>( aTestItem->parent );
167 
168  if( refGraphic && testGraphic )
169  {
170  FOOTPRINT *refParentFP = dynamic_cast<FOOTPRINT*>( refGraphic->GetParent() );
171  FOOTPRINT *testParentFP = dynamic_cast<FOOTPRINT*>( testGraphic->GetParent() );
172 
173  if( refParentFP == testParentFP ) // also true when both are nullptr
174  return true;
175  }
176 
177  if( !aRefItem->shape->Collide( aTestItem->shape, minClearance, &actual, &pos ) )
178  return true;
179 
180  std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_OVERLAPPING_SILK );
181 
182  if( minClearance > 0 )
183  {
184  m_msg.Printf( _( "(%s clearance %s; actual %s)" ),
185  constraint.GetParentRule()->m_Name,
186  MessageTextFromValue( userUnits(), minClearance ),
187  MessageTextFromValue( userUnits(), actual ) );
188 
189  drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + m_msg );
190  }
191 
192  drcItem->SetItems( aRefItem->parent, aTestItem->parent );
193  drcItem->SetViolatingRule( constraint.GetParentRule() );
194 
195  reportViolation( drcItem, (wxPoint) pos );
196 
197  *aCollisionDetected = true;
198  return true;
199  };
200 
201  forEachGeometryItem( s_allBasicItems, LSET( 2, F_SilkS, B_SilkS ), addToSilkTree );
204  countTargets );
205 
206  targets *= 2; // One for adding to RTree; one for testing
207 
210  addToTargetTree );
211 
212  reportAux( _("Testing %d silkscreen features against %d board items."),
213  silkTree.size(),
214  targetTree.size() );
215 
216  const std::vector<DRC_RTREE::LAYER_PAIR> layerPairs =
217  {
236  };
237 
238  targetTree.QueryCollidingPairs( &silkTree, layerPairs, checkClearance, m_largestClearance,
239  [&]( int aCount, int aSize ) -> bool
240  {
241  return reportProgress( ++ii, targets, delta );
242  } );
243 
245 
246  return true;
247 }
248 
249 
250 std::set<DRC_CONSTRAINT_TYPE_T> DRC_TEST_PROVIDER_SILK_CLEARANCE::GetConstraintTypes() const
251 {
252  return { SILK_CLEARANCE_CONSTRAINT };
253 }
254 
255 
256 namespace detail
257 {
259 }
wxString MessageTextFromValue(EDA_UNITS aUnits, int aValue, bool aAddUnitLabel, EDA_DATA_TYPE aType)
Definition: base_units.cpp:123
virtual bool Run() override
Runs 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:239
bool isInvisibleText(const BOARD_ITEM *aItem) const
BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
Definition: board_item.h:86
static DRC_REGISTER_TEST_PROVIDER< DRC_TEST_PROVIDER_ANNULUS > dummy
bool IsErrorLimitExceeded(int error_code)
bool QueryWorstConstraint(DRC_CONSTRAINT_TYPE_T aRuleId, DRC_CONSTRAINT &aConstraint)
virtual bool reportProgress(int aCount, int aSize, int aDelta)
T Min() const
Definition: minoptmax.h:31
std::pair< PCB_LAYER_ID, PCB_LAYER_ID > LAYER_PAIR
Definition: drc_rtree.h:280
static LSET FrontMask()
Return a mask holding all technical layers and the external CU layer on front side.
Definition: lset.cpp:867
virtual void reportRuleStatistics()
void Insert(BOARD_ITEM *aItem, int aWorstClearance=0, int aLayer=UNDEFINED_LAYER)
Function Insert() Inserts an item into the tree.
Definition: drc_rtree.h:86
virtual const wxString GetName() const override
MINOPTMAX< int > m_Value
Definition: drc_rule.h:142
LSET is a set of PCB_LAYER_IDs.
BOARD * GetBoard() const
Definition: drc_engine.h:85
size_t size() const
Returns the number of items in the tree.
Definition: drc_rtree.h:373
virtual bool reportPhase(const wxString &aStageName)
virtual void accountCheck(const DRC_RULE *ruleToTest)
EDA_UNITS userUnits() const
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)
DRC_TEST_PROVIDER is a base class that represents a DRC "provider" which runs some DRC functions over...
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:295
const MINOPTMAX< int > & GetValue() const
Definition: drc_rule.h:121
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:186
static std::vector< KICAD_T > s_allBasicItems
#define _(s)
Definition: 3d_actions.cpp:33
DRC_ENGINE * m_drcEngine
The common library.
static LSET BackMask()
Return a mask holding all technical layers and the external CU layer on back side.
Definition: lset.cpp:874
DRC_CONSTRAINT EvalRulesForItems(DRC_CONSTRAINT_TYPE_T ruleID, const BOARD_ITEM *a, const BOARD_ITEM *b=nullptr, PCB_LAYER_ID aLayer=UNDEFINED_LAYER, REPORTER *aReporter=nullptr)
Definition: drc_engine.cpp:707
DRC_RTREE - Implements an R-tree for fast spatial and layer indexing of connectable items.
Definition: drc_rtree.h:43
BOARD_ITEM_CONTAINER * GetParent() const
Definition: board_item.h:179
virtual const wxString GetDescription() const override
virtual std::set< DRC_CONSTRAINT_TYPE_T > GetConstraintTypes() const override
virtual void reportAux(wxString fmt,...)