KiCad PCB EDA Suite
drc_test_provider_silk_to_mask.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 
27 #include <geometry/seg.h>
28 #include <geometry/shape_segment.h>
29 
30 #include <drc/drc_engine.h>
31 #include <drc/drc_item.h>
32 #include <drc/drc_rule.h>
34 
35 #include <drc/drc_rtree.h>
36 
37 /*
38  Silk to pads clearance test. Check all pads against silkscreen (mask opening in the pad vs silkscreen)
39  Errors generated:
40  - DRCE_SILK_MASK_CLEARANCE
41 */
42 
44 {
45 public:
47  m_board( nullptr ),
49  {
50  }
51 
53  {
54  }
55 
56  virtual bool Run() override;
57 
58  virtual const wxString GetName() const override
59  {
60  return "silk_to_mask";
61  };
62 
63  virtual const wxString GetDescription() const override
64  {
65  return "Tests for silkscreen being clipped by solder mask";
66  }
67 
68  virtual int GetNumPhases() const override
69  {
70  return 1;
71  }
72 
73  virtual std::set<DRC_CONSTRAINT_T> GetConstraintTypes() const override;
74 
75 private:
76 
79 };
80 
81 
83 {
85 
87  {
88  reportAux( "Silkscreen clipping violations ignored. Tests not run." );
89  return true; // continue with other tests
90  }
91 
92  DRC_CONSTRAINT worstClearanceConstraint;
94 
95  if( m_drcEngine->QueryWorstConstraint( SILK_CLEARANCE_CONSTRAINT, worstClearanceConstraint ) )
96  m_largestClearance = worstClearanceConstraint.m_Value.Min();
97 
98  reportAux( "Worst clearance : %d nm", m_largestClearance );
99 
100  if( !reportPhase( _( "Checking silkscreen for potential soldermask clipping..." ) ) )
101  return false; // DRC cancelled
102 
103  DRC_RTREE maskTree, silkTree;
104 
105  auto addMaskToTree =
106  [&maskTree]( BOARD_ITEM *item ) -> bool
107  {
108  for( PCB_LAYER_ID layer : { F_Mask, B_Mask } )
109  {
110  if( item->IsOnLayer( layer ) )
111  maskTree.Insert( item, layer );
112  }
113 
114  return true;
115  };
116 
117  auto addSilkToTree =
118  [&silkTree]( BOARD_ITEM *item ) -> bool
119  {
120  for( PCB_LAYER_ID layer : { F_SilkS, B_SilkS } )
121  {
122  if( item->IsOnLayer( layer ) )
123  silkTree.Insert( item, layer );
124  }
125 
126  return true;
127  };
128 
129  auto checkClearance =
130  [&]( const DRC_RTREE::LAYER_PAIR& aLayers, DRC_RTREE::ITEM_WITH_SHAPE* aRefItem,
131  DRC_RTREE::ITEM_WITH_SHAPE* aTestItem, bool* aCollisionDetected ) -> bool
132  {
134  return false;
135 
136  if( isInvisibleText( aRefItem->parent ) )
137  return true;
138 
139  if( isInvisibleText( aTestItem->parent ) )
140  return true;
141 
142  auto constraint = m_drcEngine->EvalRules( SILK_CLEARANCE_CONSTRAINT,
143  aRefItem->parent, aTestItem->parent,
144  aLayers.first );
145 
146  int minClearance = constraint.GetValue().Min();
147 
148  if( minClearance < 0 )
149  return true;
150 
151  int actual;
152  VECTOR2I pos;
153 
154  if( aRefItem->shape->Collide( aTestItem->shape, minClearance, &actual, &pos ) )
155  {
156  std::shared_ptr<DRC_ITEM> drce = DRC_ITEM::Create( DRCE_SILK_MASK_CLEARANCE );
157 
158  if( minClearance > 0 )
159  {
160  m_msg.Printf( _( "(%s clearance %s; actual %s)" ),
161  constraint.GetName(),
162  MessageTextFromValue( userUnits(), minClearance ),
163  MessageTextFromValue( userUnits(), actual ) );
164 
165  drce->SetErrorMessage( drce->GetErrorText() + wxS( " " ) + m_msg );
166  }
167 
168  drce->SetItems( aRefItem->parent, aTestItem->parent );
169  drce->SetViolatingRule( constraint.GetParentRule() );
170 
171  reportViolation( drce, (wxPoint) pos );
172 
173  *aCollisionDetected = true;
174  }
175 
176  return true;
177  };
178 
179  int numMask = forEachGeometryItem( s_allBasicItems, LSET( 2, F_Mask, B_Mask ), addMaskToTree );
180  int numSilk = forEachGeometryItem( s_allBasicItems, LSET( 2, F_SilkS, B_SilkS ), addSilkToTree );
181 
182  reportAux( _("Testing %d mask apertures against %d silkscreen features."), numMask, numSilk );
183 
184  const std::vector<DRC_RTREE::LAYER_PAIR> layerPairs =
185  {
188  };
189 
190  // This is the number of tests between 2 calls to the progress bar
191  const int delta = 250;
192 
193  maskTree.QueryCollidingPairs( &silkTree, layerPairs, checkClearance, m_largestClearance,
194  [&]( int aCount, int aSize ) -> bool
195  {
196  return reportProgress( aCount, aSize, delta );
197  } );
198 
200 
201  return true;
202 }
203 
204 
205 std::set<DRC_CONSTRAINT_T> DRC_TEST_PROVIDER_SILK_TO_MASK::GetConstraintTypes() const
206 {
207  return { SILK_CLEARANCE_CONSTRAINT };
208 }
209 
210 
211 namespace detail
212 {
214 }
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
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Definition: drc_item.cpp:266
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:49
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:329
virtual const wxString GetDescription() const override
virtual void reportRuleStatistics()
static DRC_REGISTER_TEST_PROVIDER< DRC_TEST_PROVIDER_ANNULAR_WIDTH > dummy
virtual bool Run() override
Run this provider against the given PCB with configured options (if any).
MINOPTMAX< int > m_Value
Definition: drc_rule.h:145
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:504
bool QueryWorstConstraint(DRC_CONSTRAINT_T aRuleId, DRC_CONSTRAINT &aConstraint)
BOARD * GetBoard() const
Definition: drc_engine.h:88
virtual bool reportPhase(const wxString &aStageName)
virtual const wxString GetName() const override
virtual std::set< DRC_CONSTRAINT_T > GetConstraintTypes() const override
#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:760
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:344
const MINOPTMAX< int > & GetValue() const
Definition: drc_rule.h:122
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:191
static std::vector< KICAD_T > s_allBasicItems
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:65
DRC_ENGINE * m_drcEngine
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:88
constexpr int delta
Implement an R-tree for fast spatial and layer indexing of connectable items.
Definition: drc_rtree.h:46
virtual void reportAux(wxString fmt,...)