KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_test_provider_disallow.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 The 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 <atomic>
25#include <common.h>
27#include <drc/drc_rtree.h>
28#include <drc/drc_engine.h>
29#include <drc/drc_item.h>
30#include <drc/drc_rule.h>
32#include <pad.h>
33#include <progress_reporter.h>
34#include <thread_pool.h>
35#include <zone.h>
36#include <pcb_track.h>
37#include <mutex>
38
39
40/*
41 "Disallow" test. Goes through all items, matching types/conditions drop errors.
42 Errors generated:
43 - DRCE_ALLOWED_ITEMS
44 - DRCE_TEXT_ON_EDGECUTS
45*/
46
48{
49public:
52
53 virtual ~DRC_TEST_PROVIDER_DISALLOW() = default;
54
55 virtual bool Run() override;
56
57 virtual const wxString GetName() const override { return wxT( "disallow" ); };
58};
59
60
62{
63 if( !reportPhase( _( "Checking keepouts & disallow constraints..." ) ) )
64 return false; // DRC cancelled
65
66 BOARD* board = m_drcEngine->GetBoard();
68
69 // First build out the board's cache of copper-keepout to copper-zone caches. This is where
70 // the bulk of the time is spent, and we can do this in parallel.
71 //
72 std::vector<ZONE*> antiCopperKeepouts;
73 std::vector<ZONE*> copperZones;
74 std::vector<std::pair<ZONE*, ZONE*>> toCache;
75 std::atomic<size_t> done( 1 );
76 int totalCount = 0;
77 std::unique_ptr<DRC_RTREE> antiTrackKeepouts = std::make_unique<DRC_RTREE>();
78
80 [&]( BOARD_ITEM* item ) -> bool
81 {
82 ZONE* zone = static_cast<ZONE*>( item );
83
84 if( zone->GetIsRuleArea() )
85 {
86 if( zone->GetDoNotAllowZoneFills() )
87 antiCopperKeepouts.push_back( zone );
88
89 if( zone->GetDoNotAllowTracks() )
90 {
91 for( PCB_LAYER_ID layer : zone->GetLayerSet() )
92 antiTrackKeepouts->Insert( zone, layer );
93 }
94 }
95 else if( zone->IsOnCopperLayer() )
96 {
97 copperZones.push_back( zone );
98 }
99
100 totalCount++;
101
102 return true;
103 } );
104
105 antiTrackKeepouts->Build();
106
107 for( ZONE* ruleArea : antiCopperKeepouts )
108 {
109 for( ZONE* copperZone : copperZones )
110 {
111 toCache.push_back( { ruleArea, copperZone } );
112 totalCount++;
113 }
114 }
115
116 auto query_areas =
117 [&]( const int idx ) -> size_t
118 {
119 if( m_drcEngine->IsCancelled() )
120 return 0;
121 const auto& areaZonePair = toCache[idx];
122 ZONE* ruleArea = areaZonePair.first;
123 ZONE* copperZone = areaZonePair.second;
124 BOX2I areaBBox = ruleArea->GetBoundingBox();
125 BOX2I copperBBox = copperZone->GetBoundingBox();
126 bool isInside = false;
127
128 if( copperZone->IsFilled() && areaBBox.Intersects( copperBBox ) )
129 {
130 // Collisions include touching, so we need to deflate outline by enough to
131 // exclude it. This is particularly important for detecting copper fills as
132 // they will be exactly touching along the entire exclusion border.
133 SHAPE_POLY_SET areaPoly = ruleArea->Outline()->CloneDropTriangulation();
134 areaPoly.Fracture();
136
137 DRC_RTREE* zoneRTree = board->m_CopperZoneRTreeCache[ copperZone ].get();
138
139 if( zoneRTree )
140 {
141 for( size_t ii = 0; ii < ruleArea->GetLayerSet().size(); ++ii )
142 {
143 if( ruleArea->GetLayerSet().test( ii ) )
144 {
145 PCB_LAYER_ID layer = PCB_LAYER_ID( ii );
146
147 if( zoneRTree->QueryColliding( areaBBox, &areaPoly, layer ) )
148 {
149 isInside = true;
150 break;
151 }
152
153 if( m_drcEngine->IsCancelled() )
154 return 0;
155 }
156 }
157 }
158 }
159
160 if( m_drcEngine->IsCancelled() )
161 return 0;
162
163 PTR_PTR_LAYER_CACHE_KEY key = { ruleArea, copperZone, UNDEFINED_LAYER };
164
165 {
166 std::unique_lock<std::shared_mutex> writeLock( board->m_CachesMutex );
167 board->m_IntersectsAreaCache[ key ] = isInside;
168 }
169
170 done.fetch_add( 1 );
171
172 return 1;
173 };
174
176 auto futures = tp.submit_loop( 0, toCache.size(), query_areas, toCache.size() );
177
178 for( auto& ret : futures )
179 {
180 std::future_status status = ret.wait_for( std::chrono::milliseconds( 250 ) );
181
182 while( status != std::future_status::ready )
183 {
184 reportProgress( done, toCache.size() );
185 status = ret.wait_for( std::chrono::milliseconds( 250 ) );
186 }
187 }
188
189 if( m_drcEngine->IsCancelled() )
190 return false;
191
192 // Now go through all the board objects calling the DRC_ENGINE to run the actual disallow
193 // tests. These should be reasonably quick using the caches generated above.
194 //
195 // Collect items first, then process in parallel.
196 std::vector<BOARD_ITEM*> allItems;
197
199 [&]( BOARD_ITEM* item ) -> bool
200 {
201 allItems.push_back( item );
202 return true;
203 } );
204
205 std::atomic<size_t> itemsDone( 0 );
206 size_t itemCount = allItems.size();
207
208 auto checkTextOnEdgeCuts =
209 []( BOARD_ITEM* item ) -> bool
210 {
211 if( item->Type() == PCB_FIELD_T
212 || item->Type() == PCB_TEXT_T
213 || item->Type() == PCB_TEXTBOX_T
214 || BaseType( item->Type() ) == PCB_DIMENSION_T )
215 {
216 return item->GetLayer() == Edge_Cuts;
217 }
218
219 return false;
220 };
221
222 auto processItem =
223 [&]( const int idx ) -> size_t
224 {
225 if( m_drcEngine->IsCancelled() )
226 {
227 itemsDone.fetch_add( 1 );
228 return 0;
229 }
230
231 bool testTextOnEdge = !m_drcEngine->IsErrorLimitExceeded( DRCE_TEXT_ON_EDGECUTS );
232 bool testDisallow = !m_drcEngine->IsErrorLimitExceeded( DRCE_ALLOWED_ITEMS );
233
234 if( !testTextOnEdge && !testDisallow )
235 {
236 itemsDone.fetch_add( 1 );
237 return 0;
238 }
239
240 BOARD_ITEM* item = allItems[idx];
241
242 if( testTextOnEdge && checkTextOnEdgeCuts( item ) )
243 {
244 std::shared_ptr<DRC_ITEM> drc = DRC_ITEM::Create( DRCE_TEXT_ON_EDGECUTS );
245 drc->SetItems( item );
247 }
248
249 if( testDisallow )
250 {
251 if( item->Type() == PCB_ZONE_T )
252 {
253 ZONE* zone = static_cast<ZONE*>( item );
254
255 if( zone->GetIsRuleArea() && zone->HasKeepoutParametersSet() )
256 {
257 itemsDone.fetch_add( 1 );
258 return 1;
259 }
260 }
261
262 item->ClearFlags( HOLE_PROXY );
263
264 if( item->Type() == PCB_TRACE_T || item->Type() == PCB_ARC_T )
265 {
266 PCB_TRACK* track = static_cast<PCB_TRACK*>( item );
267 PCB_LAYER_ID layer = track->GetLayer();
268
269 antiTrackKeepouts->QueryColliding( track, layer, layer,
270 [&]( BOARD_ITEM* other ) -> bool
271 {
272 return true;
273 },
274 [&]( BOARD_ITEM* other ) -> bool
275 {
276 std::shared_ptr<SHAPE> shape = track->GetEffectiveShape();
277 int dummyActual;
278 VECTOR2I pos;
279
280 if( static_cast<ZONE*>( other )->Outline()->Collide(
281 shape.get(), board->m_DRCMaxClearance,
282 &dummyActual, &pos ) )
283 {
284 std::shared_ptr<DRC_ITEM> drcItem =
286 drcItem->SetItems( track );
287 reportViolation( drcItem, pos,
288 track->GetLayerSet().ExtractLayer() );
289 }
290
291 return !m_drcEngine->IsCancelled();
292 },
294 }
295
296 // Tracks and arcs against keepout areas that disallow tracks are already
297 // reported above via antiTrackKeepouts (which collides every crossing, not
298 // just one per rule match). Skip the track/arc case for implicit keepout
299 // rules here to avoid duplicate markers, but still let EvalRules produce
300 // markers for all other item types against implicit keepout rules.
301 bool isTrackOrArc = ( item->Type() == PCB_TRACE_T || item->Type() == PCB_ARC_T );
302
303 auto reportDisallow =
304 [&]( const DRC_CONSTRAINT& aConstraint )
305 {
306 DRC_RULE* rule = aConstraint.GetParentRule();
307
308 if( !rule )
309 return;
310
311 if( isTrackOrArc && rule->IsImplicit() )
312 return;
313
314 std::shared_ptr<DRC_ITEM> drcItem =
316 PCB_LAYER_ID layer = item->GetLayerSet().ExtractLayer();
317 VECTOR2I pos = item->GetPosition();
318
319 // Provide a better location for keepout area collisions by
320 // snapping to where the item actually crosses the keepout outline.
321 // Use the cached BOARD_ITEM* rather than a UUID lookup, since
322 // ResolveItem mutates an unsynchronized cache and this lambda
323 // runs inside the parallel DRC worker pool.
324 if( rule->IsImplicit() )
325 {
326 if( ZONE* keepout = dynamic_cast<ZONE*>( rule->m_ImplicitItem ) )
327 {
328 std::shared_ptr<SHAPE> shape =
329 item->GetEffectiveShape( layer );
330 int dummyActual;
331
332 keepout->Outline()->Collide( shape.get(),
333 board->m_DRCMaxClearance,
334 &dummyActual, &pos );
335 }
336 }
337
338 drcItem->SetErrorDetail(
339 wxString::Format( wxS( "(%s)" ), aConstraint.GetName() ) );
340 drcItem->SetItems( item );
341 drcItem->SetViolatingRule( rule );
342 reportViolation( drcItem, pos, layer );
343 };
344
345 DRC_CONSTRAINT constraint = m_drcEngine->EvalRules( DISALLOW_CONSTRAINT,
346 item, nullptr,
348
349 if( constraint.m_DisallowFlags
350 && constraint.GetSeverity() != RPT_SEVERITY_IGNORE )
351 {
352 reportDisallow( constraint );
353 }
354
355 // N.B. HOLE_PROXY is set/cleared on the item's flags for
356 // EvalRules to distinguish hole-specific disallow constraints.
357 // This is a non-atomic read-modify-write on m_flags, so this
358 // provider must run with each item processed by only one thread
359 // at a time (guaranteed by submit_loop's work partitioning).
360 if( item->HasHole() )
361 {
362 item->SetFlags( HOLE_PROXY );
363
364 constraint = m_drcEngine->EvalRules( DISALLOW_CONSTRAINT, item,
365 nullptr, UNDEFINED_LAYER );
366
367 if( constraint.m_DisallowFlags
368 && constraint.GetSeverity() != RPT_SEVERITY_IGNORE )
369 {
370 reportDisallow( constraint );
371 }
372
373 item->ClearFlags( HOLE_PROXY );
374 }
375 }
376
377 itemsDone.fetch_add( 1 );
378 return 1;
379 };
380
381 auto itemFutures = tp.submit_loop( 0, itemCount, processItem, itemCount );
382
383 while( itemsDone < itemCount )
384 {
385 reportProgress( itemsDone, itemCount );
386
387 if( m_drcEngine->IsCancelled() )
388 {
389 for( auto& f : itemFutures )
390 f.wait();
391
392 break;
393 }
394
395 itemFutures.wait_for( std::chrono::milliseconds( 250 ) );
396 }
397
398 return !m_drcEngine->IsCancelled();
399}
400
401
402namespace detail
403{
405}
constexpr int ARC_LOW_DEF
Definition base_units.h:140
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
PCB_LAYER_ID GetLayer() const override
Return the primary layer this item is on.
int GetDRCEpsilon() const
Return an epsilon which accounts for rounding errors, etc.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
virtual std::shared_ptr< SHAPE > GetEffectiveShape(PCB_LAYER_ID aLayer=UNDEFINED_LAYER, FLASHING aFlash=FLASHING::DEFAULT) const
Some pad shapes can be complex (rounded/chamfered rectangle), even without considering custom shapes.
virtual LSET GetLayerSet() const
Return a std::bitset of all layers on which the item physically resides.
Definition board_item.h:288
virtual bool HasHole() const
Definition board_item.h:180
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:323
int m_DRCMaxPhysicalClearance
Definition board.h:1566
int m_DRCMaxClearance
Definition board.h:1565
std::unordered_map< ZONE *, std::unique_ptr< DRC_RTREE > > m_CopperZoneRTreeCache
Definition board.h:1546
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition board.cpp:1101
std::shared_mutex m_CachesMutex
Definition board.h:1539
std::unordered_map< PTR_PTR_LAYER_CACHE_KEY, bool > m_IntersectsAreaCache
Definition board.h:1543
constexpr bool Intersects(const BOX2< Vec > &aRect) const
Definition box2.h:311
int m_DisallowFlags
Definition drc_rule.h:242
SEVERITY GetSeverity() const
Definition drc_rule.h:218
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Definition drc_item.cpp:407
Implement an R-tree for fast spatial and layer indexing of connectable items.
Definition drc_rtree.h:49
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:229
bool IsImplicit() const
Definition drc_rule.h:144
BOARD_ITEM * m_ImplicitItem
Definition drc_rule.h:153
virtual ~DRC_TEST_PROVIDER_DISALLOW()=default
virtual const wxString GetName() const override
virtual bool Run() override
Run this provider against the given PCB with configured options (if any).
virtual bool reportPhase(const wxString &aStageName)
int forEachGeometryItem(const std::vector< KICAD_T > &aTypes, const LSET &aLayers, const std::function< bool(BOARD_ITEM *)> &aFunc)
void reportViolation(std::shared_ptr< DRC_ITEM > &item, const VECTOR2I &aMarkerPos, int aMarkerLayer, const std::function< void(PCB_MARKER *)> &aPathGenerator=[](PCB_MARKER *){})
virtual bool reportProgress(size_t aCount, size_t aSize, size_t aDelta=1)
virtual VECTOR2I GetPosition() const
Definition eda_item.h:279
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition eda_item.h:149
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:112
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition eda_item.h:151
PCB_LAYER_ID ExtractLayer() const
Find the first set PCB_LAYER_ID.
Definition lset.cpp:542
static const LSET & AllLayersMask()
Definition lset.cpp:641
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
std::shared_ptr< SHAPE > GetEffectiveShape(PCB_LAYER_ID aLayer=UNDEFINED_LAYER, FLASHING aFlash=FLASHING::DEFAULT) const override
Some pad shapes can be complex (rounded/chamfered rectangle), even without considering custom shapes.
Represent a set of closed polygons.
void Deflate(int aAmount, CORNER_STRATEGY aCornerStrategy, int aMaxError)
void Fracture(bool aSimplify=true)
Convert a set of polygons with holes to a single outline with "slits"/"fractures" connecting the oute...
SHAPE_POLY_SET CloneDropTriangulation() const
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:181
Handle a list of polygons defining a copper zone.
Definition zone.h:74
bool GetIsRuleArea() const
Accessors to parameters used in Rule Area zones:
Definition zone.h:716
const BOX2I GetBoundingBox() const override
Definition zone.cpp:650
bool GetDoNotAllowTracks() const
Definition zone.h:728
bool IsFilled() const
Definition zone.h:293
SHAPE_POLY_SET * Outline()
Definition zone.h:336
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition zone.h:137
bool HasKeepoutParametersSet() const
Accessor to determine if any keepout parameters are set.
Definition zone.h:707
bool GetDoNotAllowZoneFills() const
Definition zone.h:726
bool IsOnCopperLayer() const override
Definition zone.cpp:543
The common library.
@ ALLOW_ACUTE_CORNERS
just inflate the polygon. Acute angles create spikes
@ DRCE_TEXT_ON_EDGECUTS
Definition drc_item.h:43
@ DRCE_ALLOWED_ITEMS
Definition drc_item.h:42
@ DISALLOW_CONSTRAINT
Definition drc_rule.h:75
#define _(s)
#define HOLE_PROXY
Indicates the BOARD_ITEM is a proxy for its hole.
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
@ Edge_Cuts
Definition layer_ids.h:112
@ UNDEFINED_LAYER
Definition layer_ids.h:61
static DRC_REGISTER_TEST_PROVIDER< DRC_TEST_PROVIDER_ANNULAR_WIDTH > dummy
@ RPT_SEVERITY_IGNORE
const double epsilon
static bool Collide(const SHAPE_CIRCLE &aA, const SHAPE_CIRCLE &aB, int aClearance, int *aActual, VECTOR2I *aLocation, VECTOR2I *aMTV)
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
static thread_pool * tp
BS::priority_thread_pool thread_pool
Definition thread_pool.h:31
constexpr KICAD_T BaseType(const KICAD_T aType)
Return the underlying type of the given type.
Definition typeinfo.h:251
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition typeinfo.h:90
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition typeinfo.h:105
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition typeinfo.h:89
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition typeinfo.h:87
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition typeinfo.h:95
@ PCB_DIMENSION_T
class PCB_DIMENSION_BASE: abstract dimension meta-type
Definition typeinfo.h:97
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:93
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687