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_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 = 2000;
87 
89  {
90  reportAux( "Overlapping silk violations ignored. Tests not run." );
91  return true; // continue with other tests
92  }
93 
95 
96  DRC_CONSTRAINT worstClearanceConstraint;
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; // DRC cancelled
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 ) || isInvisibleText( aTestItem->parent ) )
144  return true;
145 
146  auto constraint = m_drcEngine->EvalRules( SILK_CLEARANCE_CONSTRAINT,
147  aRefItem->parent, aTestItem->parent,
148  aLayers.second );
149 
150  if( constraint.IsNull() )
151  return true;
152 
153  int minClearance = constraint.GetValue().Min();
154 
155  if( minClearance < 0 )
156  return true;
157 
158  int actual;
159  VECTOR2I pos;
160 
161  // Graphics are often compound shapes so ignore collisions between shapes in a
162  // single footprint or on the board.
163  PCB_SHAPE* refGraphic = dynamic_cast<PCB_SHAPE*>( aRefItem->parent );
164  PCB_SHAPE* testGraphic = dynamic_cast<PCB_SHAPE*>( aTestItem->parent );
165 
166  if( refGraphic && testGraphic )
167  {
168  FOOTPRINT *refParentFP = dynamic_cast<FOOTPRINT*>( refGraphic->GetParent() );
169  FOOTPRINT *testParentFP = dynamic_cast<FOOTPRINT*>( testGraphic->GetParent() );
170 
171  if( refParentFP == testParentFP ) // also true when both are nullptr
172  return true;
173  }
174 
175  if( aRefItem->shape->Collide( aTestItem->shape, minClearance, &actual, &pos ) )
176  {
177  std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_OVERLAPPING_SILK );
178 
179  if( minClearance > 0 )
180  {
181  m_msg.Printf( _( "(%s clearance %s; actual %s)" ),
182  constraint.GetParentRule()->m_Name,
183  MessageTextFromValue( userUnits(), minClearance ),
184  MessageTextFromValue( userUnits(), actual ) );
185 
186  drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + m_msg );
187  }
188 
189  drcItem->SetItems( aRefItem->parent, aTestItem->parent );
190  drcItem->SetViolatingRule( constraint.GetParentRule() );
191 
192  reportViolation( drcItem, (wxPoint) pos );
193 
194  *aCollisionDetected = true;
195  }
196 
197  return true;
198  };
199 
200  forEachGeometryItem( s_allBasicItems, LSET( 2, F_SilkS, B_SilkS ), addToSilkTree );
203  countTargets );
204 
205  targets *= 2; // One for adding to RTree; one for testing
206 
209  addToTargetTree );
210 
211  reportAux( _("Testing %d silkscreen features against %d board items."),
212  silkTree.size(),
213  targetTree.size() );
214 
215  const std::vector<DRC_RTREE::LAYER_PAIR> layerPairs =
216  {
235  };
236 
237  targetTree.QueryCollidingPairs( &silkTree, layerPairs, checkClearance, m_largestClearance,
238  [&]( int aCount, int aSize ) -> bool
239  {
240  return reportProgress( ++ii, targets, delta );
241  } );
242 
244 
245  return true;
246 }
247 
248 
250 {
251  return { SILK_CLEARANCE_CONSTRAINT };
252 }
253 
254 
255 namespace detail
256 {
258 }
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:125
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:245
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:82
static DRC_REGISTER_TEST_PROVIDER< DRC_TEST_PROVIDER_ANNULUS > dummy
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:294
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()
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 std::set< DRC_CONSTRAINT_T > GetConstraintTypes() const override
virtual const wxString GetName() const override
MINOPTMAX< int > m_Value
Definition: drc_rule.h:142
LSET is a set of PCB_LAYER_IDs.
bool QueryWorstConstraint(DRC_CONSTRAINT_T aRuleId, DRC_CONSTRAINT &aConstraint)
BOARD * GetBoard() const
Definition: drc_engine.h:87
size_t size() const
Returns the number of items in the tree.
Definition: drc_rtree.h:387
virtual bool reportPhase(const wxString &aStageName)
EDA_UNITS userUnits() const
DRC_CONSTRAINT EvalRules(DRC_CONSTRAINT_T aConstraintId, const BOARD_ITEM *a, const BOARD_ITEM *b, PCB_LAYER_ID aLayer, REPORTER *aReporter=nullptr)
Definition: drc_engine.cpp:736
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:309
const MINOPTMAX< int > & GetValue() const
Definition: drc_rule.h:121
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:190
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:881
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:168
virtual const wxString GetDescription() const override
virtual void reportAux(wxString fmt,...)