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 (C) 2004-2024 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 <core/thread_pool.h>
35#include <zone.h>
36#include <mutex>
37
38
39/*
40 "Disallow" test. Goes through all items, matching types/conditions drop errors.
41 Errors generated:
42 - DRCE_ALLOWED_ITEMS
43 - DRCE_TEXT_ON_EDGECUTS
44*/
45
47{
48public:
50 {
51 }
52
54 {
55 }
56
57 virtual bool Run() override;
58
59 virtual const wxString GetName() const override
60 {
61 return wxT( "disallow" );
62 };
63
64 virtual const wxString GetDescription() const override
65 {
66 return wxT( "Tests for disallowed items (e.g. keepouts)" );
67 }
68};
69
70
72{
73 if( !reportPhase( _( "Checking keepouts & disallow constraints..." ) ) )
74 return false; // DRC cancelled
75
76 BOARD* board = m_drcEngine->GetBoard();
78
79 // First build out the board's cache of copper-keepout to copper-zone caches. This is where
80 // the bulk of the time is spent, and we can do this in parallel.
81 //
82 std::vector<ZONE*> antiCopperKeepouts;
83 std::vector<ZONE*> copperZones;
84 std::vector<std::pair<ZONE*, ZONE*>> toCache;
85 std::atomic<size_t> done( 1 );
86 int totalCount = 0;
87
89 [&]( BOARD_ITEM* item ) -> bool
90 {
91 ZONE* zone = dynamic_cast<ZONE*>( item );
92
93 if( zone && zone->GetIsRuleArea()
94 && zone->GetRuleAreaType() == RULE_AREA_TYPE::KEEPOUT
95 && zone->GetDoNotAllowCopperPour() )
96 antiCopperKeepouts.push_back( zone );
97 else if( zone && zone->IsOnCopperLayer() )
98 copperZones.push_back( zone );
99
100 totalCount++;
101
102 return true;
103 } );
104
105 for( ZONE* ruleArea : antiCopperKeepouts )
106 {
107 for( ZONE* copperZone : copperZones )
108 {
109 toCache.push_back( { ruleArea, copperZone } );
110 totalCount++;
111 }
112 }
113
114 auto query_areas =
115 [&]( std::pair<ZONE* /* rule area */, ZONE* /* copper zone */> areaZonePair ) -> size_t
116 {
117 if( m_drcEngine->IsCancelled() )
118 return 0;
119
120 ZONE* ruleArea = areaZonePair.first;
121 ZONE* copperZone = areaZonePair.second;
122 BOX2I areaBBox = ruleArea->GetBoundingBox();
123 BOX2I copperBBox = copperZone->GetBoundingBox();
124 bool isInside = false;
125
126 if( copperZone->IsFilled() && areaBBox.Intersects( copperBBox ) )
127 {
128 // Collisions include touching, so we need to deflate outline by enough to
129 // exclude it. This is particularly important for detecting copper fills as
130 // they will be exactly touching along the entire exclusion border.
131 SHAPE_POLY_SET areaPoly = ruleArea->Outline()->CloneDropTriangulation();
133 areaPoly.Deflate( epsilon, CORNER_STRATEGY::ALLOW_ACUTE_CORNERS, ARC_LOW_DEF );
134
135 DRC_RTREE* zoneRTree = board->m_CopperZoneRTreeCache[ copperZone ].get();
136
137 if( zoneRTree )
138 {
139 for( size_t ii = 0; ii < ruleArea->GetLayerSet().size(); ++ii )
140 {
141 if( ruleArea->GetLayerSet().test( ii ) )
142 {
143 PCB_LAYER_ID layer = PCB_LAYER_ID( ii );
144
145 if( zoneRTree->QueryColliding( areaBBox, &areaPoly, layer ) )
146 {
147 isInside = true;
148 break;
149 }
150
151 if( m_drcEngine->IsCancelled() )
152 return 0;
153 }
154 }
155 }
156 }
157
158 if( m_drcEngine->IsCancelled() )
159 return 0;
160
161 PTR_PTR_LAYER_CACHE_KEY key = { ruleArea, copperZone, UNDEFINED_LAYER };
162
163 {
164 std::unique_lock<std::shared_mutex> writeLock( board->m_CachesMutex );
165 board->m_IntersectsAreaCache[ key ] = isInside;
166 }
167
168 done.fetch_add( 1 );
169
170 return 1;
171 };
172
174 std::vector<std::future<size_t>> returns;
175
176 returns.reserve( toCache.size() );
177
178 for( const std::pair<ZONE*, ZONE*>& areaZonePair : toCache )
179 returns.emplace_back( tp.submit( query_areas, areaZonePair ) );
180
181 for( const std::future<size_t>& ret : returns )
182 {
183 std::future_status status = ret.wait_for( std::chrono::milliseconds( 250 ) );
184
185 while( status != std::future_status::ready )
186 {
187 reportProgress( done, toCache.size() );
188 status = ret.wait_for( std::chrono::milliseconds( 250 ) );
189 }
190 }
191
192 if( m_drcEngine->IsCancelled() )
193 return false;
194
195 // Now go through all the board objects calling the DRC_ENGINE to run the actual disallow
196 // tests. These should be reasonably quick using the caches generated above.
197 //
198 const int progressDelta = 250;
199 int ii = static_cast<int>( toCache.size() );
200
201 auto checkTextOnEdgeCuts =
202 [&]( BOARD_ITEM* item )
203 {
204 if( item->Type() == PCB_FIELD_T || item->Type() == PCB_TEXT_T || item->Type() == PCB_TEXTBOX_T
205 || BaseType( item->Type() ) == PCB_DIMENSION_T )
206 {
207 if( item->GetLayer() == Edge_Cuts )
208 {
209 std::shared_ptr<DRC_ITEM> drc = DRC_ITEM::Create( DRCE_TEXT_ON_EDGECUTS );
210 drc->SetItems( item );
211 reportViolation( drc, item->GetPosition(), Edge_Cuts );
212 }
213 }
214 };
215
216 auto checkDisallow =
217 [&]( BOARD_ITEM* item )
218 {
220 nullptr, UNDEFINED_LAYER );
221
222 if( constraint.m_DisallowFlags && constraint.GetSeverity() != RPT_SEVERITY_IGNORE )
223 {
224 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_ALLOWED_ITEMS );
225 DRC_RULE* rule = constraint.GetParentRule();
226 VECTOR2I pos = item->GetPosition();
227 PCB_LAYER_ID layer = item->GetLayerSet().ExtractLayer();
228 wxString msg;
229
230 msg.Printf( drcItem->GetErrorText() + wxS( " (%s)" ), constraint.GetName() );
231
232 drcItem->SetErrorMessage( msg );
233 drcItem->SetItems( item );
234 drcItem->SetViolatingRule( rule );
235
236 if( rule->m_Implicit )
237 {
238 // Provide a better location for keepout area collisions.
239 BOARD_ITEM* ruleItem = board->GetItem( rule->m_ImplicitItemId );
240
241 if( ZONE* keepout = dynamic_cast<ZONE*>( ruleItem ) )
242 {
243 std::shared_ptr<SHAPE> shape = item->GetEffectiveShape( layer );
244 int dummyActual;
245
246 keepout->Outline()->Collide( shape.get(), board->m_DRCMaxClearance,
247 &dummyActual, &pos );
248 }
249 }
250
251 reportViolation( drcItem, pos, layer );
252 }
253 };
254
256 [&]( BOARD_ITEM* item ) -> bool
257 {
259 checkTextOnEdgeCuts( item );
260
262 {
263 ZONE* zone = dynamic_cast<ZONE*>( item );
264
265 if( zone && zone->GetIsRuleArea() && zone->GetRuleAreaType() == RULE_AREA_TYPE::KEEPOUT )
266 return true;
267
268 item->ClearFlags( HOLE_PROXY ); // Just in case
269
270 checkDisallow( item );
271
272 if( item->HasHole() )
273 {
274 item->SetFlags( HOLE_PROXY );
275 checkDisallow( item );
276 item->ClearFlags( HOLE_PROXY );
277 }
278 }
279
280 if( !reportProgress( ii++, totalCount, progressDelta ) )
281 return false;
282
283 return true;
284 } );
285
287
288 return !m_drcEngine->IsCancelled();
289}
290
291
292namespace detail
293{
295}
constexpr int ARC_LOW_DEF
Definition: base_units.h:119
bool test(size_t pos) const
Definition: base_set.h:48
size_t size() const
Definition: base_set.h:109
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:79
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.
Definition: board_item.cpp:246
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:289
BOARD_ITEM * GetItem(const KIID &aID) const
Definition: board.cpp:1391
int m_DRCMaxClearance
Definition: board.h:1294
std::unordered_map< ZONE *, std::unique_ptr< DRC_RTREE > > m_CopperZoneRTreeCache
Definition: board.h:1286
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:875
std::shared_mutex m_CachesMutex
Definition: board.h:1279
std::unordered_map< PTR_PTR_LAYER_CACHE_KEY, bool > m_IntersectsAreaCache
Definition: board.h:1283
bool Intersects(const BOX2< Vec > &aRect) const
Definition: box2.h:294
wxString GetName() const
Definition: drc_rule.h:156
int m_DisallowFlags
Definition: drc_rule.h:187
SEVERITY GetSeverity() const
Definition: drc_rule.h:169
DRC_RULE * GetParentRule() const
Definition: drc_rule.h:152
BOARD * GetBoard() const
Definition: drc_engine.h:89
bool IsErrorLimitExceeded(int error_code)
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:675
bool IsCancelled() const
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Definition: drc_item.cpp:357
Implement an R-tree for fast spatial and layer indexing of connectable items.
Definition: drc_rtree.h:48
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:213
KIID m_ImplicitItemId
Definition: drc_rule.h:113
bool m_Implicit
Definition: drc_rule.h:112
virtual const wxString GetName() const override
virtual bool Run() override
Run this provider against the given PCB with configured options (if any).
virtual const wxString GetDescription() const override
Represent a DRC "provider" which runs some DRC functions over a BOARD and spits out DRC_ITEM and posi...
virtual bool reportPhase(const wxString &aStageName)
int forEachGeometryItem(const std::vector< KICAD_T > &aTypes, LSET aLayers, const std::function< bool(BOARD_ITEM *)> &aFunc)
virtual void reportViolation(std::shared_ptr< DRC_ITEM > &item, const VECTOR2I &aMarkerPos, int aMarkerLayer)
DRC_ENGINE * m_drcEngine
virtual void reportRuleStatistics()
virtual bool reportProgress(size_t aCount, size_t aSize, size_t aDelta=1)
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: eda_item.h:129
static LSET AllLayersMask()
Definition: lset.cpp:767
Represent a set of closed polygons.
void Fracture(POLYGON_MODE aFastMode)
Convert a set of polygons with holes to a single outline with "slits"/"fractures" connecting the oute...
void Deflate(int aAmount, CORNER_STRATEGY aCornerStrategy, int aMaxError)
SHAPE_POLY_SET CloneDropTriangulation() const
Handle a list of polygons defining a copper zone.
Definition: zone.h:73
bool GetIsRuleArea() const
Accessors to parameters used in Rule Area zones:
Definition: zone.h:711
RULE_AREA_TYPE GetRuleAreaType() const
Definition: zone.h:712
const BOX2I GetBoundingBox() const override
Definition: zone.cpp:362
bool IsFilled() const
Definition: zone.h:261
SHAPE_POLY_SET * Outline()
Definition: zone.h:337
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition: zone.h:130
bool GetDoNotAllowCopperPour() const
Definition: zone.h:714
bool IsOnCopperLayer() const override
Definition: zone.cpp:273
The common library.
@ DRCE_TEXT_ON_EDGECUTS
Definition: drc_item.h:42
@ DRCE_ALLOWED_ITEMS
Definition: drc_item.h:41
@ DISALLOW_CONSTRAINT
Definition: drc_rule.h:64
#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:113
@ 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 thread_pool * tp
Definition: thread_pool.cpp:30
BS::thread_pool thread_pool
Definition: thread_pool.h:30
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
Definition: thread_pool.cpp:32
constexpr KICAD_T BaseType(const KICAD_T aType)
Return the underlying type of the given type.
Definition: typeinfo.h:248
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition: typeinfo.h:93
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition: typeinfo.h:92
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition: typeinfo.h:90
@ PCB_DIMENSION_T
class PCB_DIMENSION_BASE: abstract dimension meta-type
Definition: typeinfo.h:100