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