KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_cache_generator.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 <common.h>
26#include <pad.h>
27#include <pcb_board_outline.h>
28#include <footprint.h>
29#include <thread_pool.h>
30#include <zone.h>
32#include <drc/drc_engine.h>
33#include <drc/drc_rtree.h>
35#include <mutex>
36
38{
39 m_board = m_drcEngine->GetBoard();
40
41 int& largestClearance = m_board->m_DRCMaxClearance;
42 int& largestPhysicalClearance = m_board->m_DRCMaxPhysicalClearance;
43 DRC_CONSTRAINT worstConstraint;
44 LSET boardCopperLayers = LSET::AllCuMask( m_board->GetCopperLayerCount() );
46
47 largestClearance = std::max( largestClearance, m_board->GetMaxClearanceValue() );
48
49 // Only consider unconditional constraints for the global maximum. Conditional constraints
50 // (like the barcode physical clearance default) apply only to specific item types and
51 // should not inflate the R-tree query radius for all items on the board.
52 if( m_drcEngine->QueryWorstConstraint( PHYSICAL_CLEARANCE_CONSTRAINT, worstConstraint, true ) )
53 largestPhysicalClearance = worstConstraint.GetValue().Min();
54
55 if( m_drcEngine->QueryWorstConstraint( PHYSICAL_HOLE_CLEARANCE_CONSTRAINT, worstConstraint, true ) )
56 largestPhysicalClearance = std::max( largestPhysicalClearance, worstConstraint.GetValue().Min() );
57
58 // If the unconditional max is 0, check for conditional constraints that may still apply.
59 // User-defined conditional rules always need the test to run. The implicit barcode rule
60 // only needs the test if barcodes actually exist on the board.
61 if( largestPhysicalClearance <= 0 )
62 {
63 int conditionalMax = 0;
64
65 if( m_drcEngine->QueryWorstConstraint( PHYSICAL_CLEARANCE_CONSTRAINT, worstConstraint ) )
66 conditionalMax = worstConstraint.GetValue().Min();
67
68 if( m_drcEngine->QueryWorstConstraint( PHYSICAL_HOLE_CLEARANCE_CONSTRAINT, worstConstraint ) )
69 conditionalMax = std::max( conditionalMax, worstConstraint.GetValue().Min() );
70
71 if( conditionalMax > 0 )
72 {
73 if( m_drcEngine->HasUserDefinedPhysicalConstraint() )
74 {
75 largestPhysicalClearance = conditionalMax;
76 }
77 else
78 {
79 bool hasMatchingItems = false;
80
82 [&]( BOARD_ITEM* item ) -> bool
83 {
84 hasMatchingItems = true;
85 return false;
86 } );
87
88 if( hasMatchingItems )
89 largestPhysicalClearance = conditionalMax;
90 }
91 }
92 }
93
94 // Ensure algorithmic safety
95 largestClearance = std::min( largestClearance, INT_MAX / 3 );
96 largestPhysicalClearance = std::min( largestPhysicalClearance, INT_MAX / 3 );
97
98 std::set<ZONE*> allZones;
99
100 auto cacheBBoxes =
101 []( ZONE* zone, const LSET& copperLayers )
102 {
103 zone->Outline()->BuildBBoxCaches();
104
105 for( PCB_LAYER_ID layer : copperLayers )
106 {
107 if( SHAPE_POLY_SET* fill = zone->GetFill( layer ) )
108 fill->BuildBBoxCaches();
109 }
110 };
111
112 for( ZONE* zone : m_board->Zones() )
113 {
114 allZones.insert( zone );
115
116 if( !zone->GetIsRuleArea() )
117 {
118 m_board->m_DRCZones.push_back( zone );
119
120 LSET zoneCopperLayers = zone->GetLayerSet() & boardCopperLayers;
121
122 if( zoneCopperLayers.any() )
123 {
124 cacheBBoxes( zone, zoneCopperLayers );
125 m_board->m_DRCCopperZones.push_back( zone );
126 }
127 }
128 }
129
130 for( FOOTPRINT* footprint : m_board->Footprints() )
131 {
132 for( ZONE* zone : footprint->Zones() )
133 {
134 allZones.insert( zone );
135
136 if( !zone->GetIsRuleArea() )
137 {
138 m_board->m_DRCZones.push_back( zone );
139
140 LSET zoneCopperLayers = zone->GetLayerSet() & boardCopperLayers;
141
142 if( zoneCopperLayers.any() )
143 {
144 cacheBBoxes( zone, zoneCopperLayers );
145 m_board->m_DRCCopperZones.push_back( zone );
146 }
147 }
148 }
149 }
150
151 for( ZONE* zone : m_board->m_DRCCopperZones )
152 {
153 LSET zoneCopperLayers = zone->GetLayerSet() & boardCopperLayers;
154
155 for( PCB_LAYER_ID layer : zoneCopperLayers )
156 m_board->m_DRCCopperZonesByLayer[layer].push_back( zone );
157 }
158
159 size_t count = 0;
160 std::atomic<size_t> done( 1 );
161
162 auto countItems =
163 [&]( BOARD_ITEM* item ) -> bool
164 {
165 ++count;
166 return true;
167 };
168
169 auto addToCopperTree =
170 [&]( BOARD_ITEM* item ) -> bool
171 {
172 if( m_drcEngine->IsCancelled() )
173 return false;
174
175 LSET copperLayers = item->GetLayerSet() & boardCopperLayers;
176
177 // Special-case pad holes which pierce all the copper layers
178 if( item->Type() == PCB_PAD_T )
179 {
180 PAD* pad = static_cast<PAD*>( item );
181
182 if( pad->HasHole() )
183 copperLayers = boardCopperLayers;
184 }
185
186 copperLayers.RunOnLayers(
187 [&]( PCB_LAYER_ID layer )
188 {
189 m_board->m_CopperItemRTreeCache->Insert( item, layer, largestClearance );
190 } );
191
192 done.fetch_add( 1 );
193 return true;
194 };
195
196 if( !reportPhase( _( "Gathering copper items..." ) ) )
197 return false; // DRC cancelled
198
199 static const std::vector<KICAD_T> itemTypes = {
201 PCB_PAD_T,
207 };
208
209 forEachGeometryItem( itemTypes, boardCopperLayers, countItems );
210
211 std::future<void> retn = tp.submit_task(
212 [&]()
213 {
214 std::unique_lock<std::shared_mutex> writeLock( m_board->m_CachesMutex );
215
216 if( !m_board->m_CopperItemRTreeCache )
217 m_board->m_CopperItemRTreeCache = std::make_shared<DRC_RTREE>();
218
219 forEachGeometryItem( itemTypes, boardCopperLayers, addToCopperTree );
220 m_board->m_CopperItemRTreeCache->Build();
221 } );
222
223 std::future_status status = retn.wait_for( std::chrono::milliseconds( 250 ) );
224
225 while( status != std::future_status::ready )
226 {
227 reportProgress( done, count );
228 status = retn.wait_for( std::chrono::milliseconds( 250 ) );
229 }
230
231 if( !reportPhase( _( "Tessellating copper zones..." ) ) )
232 return false; // DRC cancelled
233
234 // Cache zone bounding boxes, triangulation, copper zone rtrees, and footprint courtyards
235 // before we start.
236
237 for( FOOTPRINT* footprint : m_board->Footprints() )
238 {
239 footprint->BuildCourtyardCaches();
240 footprint->BuildNetTieCache();
241 }
242
243 std::vector<std::future<size_t>> returns;
244
245 returns.reserve( allZones.size() );
246
247 auto cache_zones =
248 [this, &done]( ZONE* aZone ) -> size_t
249 {
250 if( m_drcEngine->IsCancelled() )
251 return 0;
252
253 aZone->CacheBoundingBox();
254 aZone->CacheTriangulation();
255
256 if( !aZone->GetIsRuleArea() && aZone->IsOnCopperLayer() )
257 {
258 std::unique_ptr<DRC_RTREE> rtree = std::make_unique<DRC_RTREE>();
259
260 aZone->GetLayerSet().RunOnLayers(
261 [&]( PCB_LAYER_ID layer )
262 {
263 if( IsCopperLayer( layer ) )
264 rtree->Insert( aZone, layer );
265 } );
266
267 rtree->Build();
268
269 {
270 std::unique_lock<std::shared_mutex> writeLock( m_board->m_CachesMutex );
271 m_board->m_CopperZoneRTreeCache[ aZone ] = std::move( rtree );
272 }
273
274 done.fetch_add( 1 );
275 }
276
277 return 1;
278 };
279
280 for( ZONE* zone : allZones )
281 {
282 returns.emplace_back( tp.submit_task(
283 [cache_zones, zone]
284 {
285 return cache_zones( zone );
286 } ) );
287 }
288
289 done.store( 1 );
290
291 for( const std::future<size_t>& ret : returns )
292 {
293 status = ret.wait_for( std::chrono::milliseconds( 250 ) );
294
295 while( status != std::future_status::ready )
296 {
297 reportProgress( done, allZones.size() );
298 status = ret.wait_for( std::chrono::milliseconds( 250 ) );
299 }
300 }
301
302 m_board->m_ZoneIsolatedIslandsMap.clear();
303
304 for( ZONE* zone : m_board->Zones() )
305 {
306 if( !zone->GetIsRuleArea() && !zone->IsTeardropArea() )
307 {
308 zone->GetLayerSet().RunOnLayers(
309 [&]( PCB_LAYER_ID layer )
310 {
311 m_board->m_ZoneIsolatedIslandsMap[ zone ][ layer ] = ISOLATED_ISLANDS();
312 } );
313 }
314 }
315
316 m_board->UpdateBoardOutline();
317
318 if( m_board->BoardOutline() )
319 m_board->BoardOutline()->GetOutline().BuildBBoxCaches();
320
321 std::shared_ptr<CONNECTIVITY_DATA> connectivity = m_board->GetConnectivity();
322
323 connectivity->ClearRatsnest();
324 connectivity->Build( m_board, m_drcEngine->GetProgressReporter() );
325 connectivity->FillIsolatedIslandsMap( m_board->m_ZoneIsolatedIslandsMap, true );
326
327 return !m_drcEngine->IsCancelled();
328}
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
virtual bool Run() override
Run this provider against the given PCB with configured options (if any).
const MINOPTMAX< int > & GetValue() const
Definition drc_rule.h:196
virtual bool reportPhase(const wxString &aStageName)
int forEachGeometryItem(const std::vector< KICAD_T > &aTypes, const LSET &aLayers, const std::function< bool(BOARD_ITEM *)> &aFunc)
virtual bool reportProgress(size_t aCount, size_t aSize, size_t aDelta=1)
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
static const LSET & AllCuMask()
return AllCuMask( MAX_CU_LAYERS );
Definition lset.cpp:608
void RunOnLayers(const std::function< void(PCB_LAYER_ID)> &aFunction) const
Execute a function on each layer of the LSET.
Definition lset.h:263
static const LSET & AllLayersMask()
Definition lset.cpp:641
T Min() const
Definition minoptmax.h:33
Definition pad.h:55
Represent a set of closed polygons.
void BuildBBoxCaches() const
Construct BBoxCaches for Contains(), below.
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
SHAPE_POLY_SET * Outline()
Definition zone.h:336
SHAPE_POLY_SET * GetFill(PCB_LAYER_ID aLayer)
Definition zone.h:609
bool IsTeardropArea() const
Definition zone.h:691
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition zone.h:137
The common library.
@ PHYSICAL_HOLE_CLEARANCE_CONSTRAINT
Definition drc_rule.h:84
@ PHYSICAL_CLEARANCE_CONSTRAINT
Definition drc_rule.h:83
#define _(s)
bool IsCopperLayer(int aLayerId)
Test whether a layer is a copper layer.
Definition layer_ids.h:679
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
A struct recording the isolated and single-pad islands within a zone.
Definition zone.h:61
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
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition typeinfo.h:85
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:94
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition typeinfo.h:90
@ 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_BARCODE_T
class PCB_BARCODE, a barcode (graphic item)
Definition typeinfo.h:98
@ PCB_TABLECELL_T
class PCB_TABLECELL, PCB_TEXTBOX for use in tables
Definition typeinfo.h:92
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition typeinfo.h:84
@ 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_TABLE_T
class PCB_TABLE, table of PCB_TABLECELLs
Definition typeinfo.h:91
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:93