KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 <footprint.h>
27#include <thread_pool.h>
28#include <zone.h>
30#include <drc/drc_engine.h>
31#include <drc/drc_rtree.h>
33#include <mutex>
34
36{
38
39 int& largestClearance = m_board->m_DRCMaxClearance;
40 int& largestPhysicalClearance = m_board->m_DRCMaxPhysicalClearance;
41 DRC_CONSTRAINT worstConstraint;
42 LSET boardCopperLayers = LSET::AllCuMask( m_board->GetCopperLayerCount() );
44
45
46 largestClearance = std::max( largestClearance, m_board->GetMaxClearanceValue() );
47
49 largestPhysicalClearance = worstConstraint.GetValue().Min();
50
52 largestPhysicalClearance = std::max( largestPhysicalClearance, worstConstraint.GetValue().Min() );
53
54 std::set<ZONE*> allZones;
55
56 for( ZONE* zone : m_board->Zones() )
57 {
58 allZones.insert( zone );
59
60 if( !zone->GetIsRuleArea() )
61 {
62 m_board->m_DRCZones.push_back( zone );
63
64 if( ( zone->GetLayerSet() & boardCopperLayers ).any() )
65 {
66 m_board->m_DRCCopperZones.push_back( zone );
67 }
68 }
69 }
70
71 for( FOOTPRINT* footprint : m_board->Footprints() )
72 {
73 for( ZONE* zone : footprint->Zones() )
74 {
75 allZones.insert( zone );
76
77 if( !zone->GetIsRuleArea() )
78 {
79 m_board->m_DRCZones.push_back( zone );
80
81 if( ( zone->GetLayerSet() & boardCopperLayers ).any() )
82 m_board->m_DRCCopperZones.push_back( zone );
83 }
84 }
85 }
86
87 size_t count = 0;
88 std::atomic<size_t> done( 1 );
89
90 auto countItems =
91 [&]( BOARD_ITEM* item ) -> bool
92 {
93 ++count;
94 return true;
95 };
96
97 auto addToCopperTree =
98 [&]( BOARD_ITEM* item ) -> bool
99 {
100 if( m_drcEngine->IsCancelled() )
101 return false;
102
103 LSET copperLayers = item->GetLayerSet() & boardCopperLayers;
104
105 // Special-case pad holes which pierce all the copper layers
106 if( item->Type() == PCB_PAD_T )
107 {
108 PAD* pad = static_cast<PAD*>( item );
109
110 if( pad->HasHole() )
111 copperLayers = boardCopperLayers;
112 }
113
114 copperLayers.RunOnLayers(
115 [&]( PCB_LAYER_ID layer )
116 {
117 m_board->m_CopperItemRTreeCache->Insert( item, layer, largestClearance );
118 } );
119
120 done.fetch_add( 1 );
121 return true;
122 };
123
124 if( !reportPhase( _( "Gathering copper items..." ) ) )
125 return false; // DRC cancelled
126
127 static const std::vector<KICAD_T> itemTypes = {
129 PCB_PAD_T,
134 };
135
136 forEachGeometryItem( itemTypes, LSET::AllCuMask(), countItems );
137
138 std::future<void> retn = tp.submit(
139 [&]()
140 {
141 std::unique_lock<std::shared_mutex> writeLock( m_board->m_CachesMutex );
142
144 m_board->m_CopperItemRTreeCache = std::make_shared<DRC_RTREE>();
145
146 forEachGeometryItem( itemTypes, LSET::AllCuMask(), addToCopperTree );
147 } );
148
149 std::future_status status = retn.wait_for( std::chrono::milliseconds( 250 ) );
150
151 while( status != std::future_status::ready )
152 {
153 reportProgress( done, count );
154 status = retn.wait_for( std::chrono::milliseconds( 250 ) );
155 }
156
157 if( !reportPhase( _( "Tessellating copper zones..." ) ) )
158 return false; // DRC cancelled
159
160 // Cache zone bounding boxes, triangulation, copper zone rtrees, and footprint courtyards
161 // before we start.
162
163 for( FOOTPRINT* footprint : m_board->Footprints() )
164 {
165 footprint->BuildCourtyardCaches();
166 footprint->BuildNetTieCache();
167 }
168
169 std::vector<std::future<size_t>> returns;
170
171 returns.reserve( allZones.size() );
172
173 auto cache_zones =
174 [this, &done]( ZONE* aZone ) -> size_t
175 {
176 if( m_drcEngine->IsCancelled() )
177 return 0;
178
179 aZone->CacheBoundingBox();
180 aZone->CacheTriangulation();
181
182 if( !aZone->GetIsRuleArea() && aZone->IsOnCopperLayer() )
183 {
184 std::unique_ptr<DRC_RTREE> rtree = std::make_unique<DRC_RTREE>();
185
186 aZone->GetLayerSet().RunOnLayers(
187 [&]( PCB_LAYER_ID layer )
188 {
189 if( IsCopperLayer( layer ) )
190 rtree->Insert( aZone, layer );
191 } );
192
193 {
194 std::unique_lock<std::shared_mutex> writeLock( m_board->m_CachesMutex );
195 m_board->m_CopperZoneRTreeCache[ aZone ] = std::move( rtree );
196 }
197
198 done.fetch_add( 1 );
199 }
200
201 return 1;
202 };
203
204 for( ZONE* zone : allZones )
205 returns.emplace_back( tp.submit( cache_zones, zone ) );
206
207 done.store( 1 );
208
209 for( const std::future<size_t>& ret : returns )
210 {
211 status = ret.wait_for( std::chrono::milliseconds( 250 ) );
212
213 while( status != std::future_status::ready )
214 {
215 reportProgress( done, allZones.size() );
216 status = ret.wait_for( std::chrono::milliseconds( 250 ) );
217 }
218 }
219
221
222 for( ZONE* zone : m_board->Zones() )
223 {
224 if( !zone->GetIsRuleArea() && !zone->IsTeardropArea() )
225 {
226 zone->GetLayerSet().RunOnLayers(
227 [&]( PCB_LAYER_ID layer )
228 {
230 } );
231 }
232 }
233
234 std::shared_ptr<CONNECTIVITY_DATA> connectivity = m_board->GetConnectivity();
235
236 connectivity->ClearRatsnest();
237 connectivity->Build( m_board, m_drcEngine->GetProgressReporter() );
238 connectivity->FillIsolatedIslandsMap( m_board->m_ZoneIsolatedIslandsMap, true );
239
240 return !m_drcEngine->IsCancelled();
241}
242
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:78
std::map< ZONE *, std::map< PCB_LAYER_ID, ISOLATED_ISLANDS > > m_ZoneIsolatedIslandsMap
Definition: board.h:1352
std::vector< ZONE * > m_DRCCopperZones
Definition: board.h:1348
const ZONES & Zones() const
Definition: board.h:342
int GetMaxClearanceValue() const
Returns the maximum clearance value for any object on the board.
Definition: board.cpp:958
int GetCopperLayerCount() const
Definition: board.cpp:781
const FOOTPRINTS & Footprints() const
Definition: board.h:338
int m_DRCMaxPhysicalClearance
Definition: board.h:1350
std::shared_ptr< DRC_RTREE > m_CopperItemRTreeCache
Definition: board.h:1342
int m_DRCMaxClearance
Definition: board.h:1349
std::unordered_map< ZONE *, std::unique_ptr< DRC_RTREE > > m_CopperZoneRTreeCache
Definition: board.h:1341
std::vector< ZONE * > m_DRCZones
Definition: board.h:1347
std::shared_mutex m_CachesMutex
Definition: board.h:1334
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Return a list of missing connections between components/tracks.
Definition: board.h:495
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:152
BOARD * GetBoard() const
Definition: drc_engine.h:96
PROGRESS_REPORTER * GetProgressReporter() const
Definition: drc_engine.h:131
bool IsCancelled() const
bool QueryWorstConstraint(DRC_CONSTRAINT_T aRuleId, DRC_CONSTRAINT &aConstraint)
virtual bool reportPhase(const wxString &aStageName)
int forEachGeometryItem(const std::vector< KICAD_T > &aTypes, const LSET &aLayers, const std::function< bool(BOARD_ITEM *)> &aFunc)
DRC_ENGINE * m_drcEngine
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
void RunOnLayers(const std::function< void(PCB_LAYER_ID)> &aFunction) const
Execute a function on each layer of the LSET.
Definition: lset.h:255
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:572
T Min() const
Definition: minoptmax.h:33
Definition: pad.h:54
Handle a list of polygons defining a copper zone.
Definition: zone.h:74
The common library.
@ PHYSICAL_HOLE_CLEARANCE_CONSTRAINT
Definition: drc_rule.h:75
@ PHYSICAL_CLEARANCE_CONSTRAINT
Definition: drc_rule.h:74
#define _(s)
bool IsCopperLayer(int aLayerId)
Test whether a layer is a copper layer.
Definition: layer_ids.h:663
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.
Definition: thread_pool.cpp:30
static thread_pool * tp
Definition: thread_pool.cpp:28
BS::thread_pool thread_pool
Definition: thread_pool.h:31
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition: typeinfo.h:88
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:97
@ 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_TABLECELL_T
class PCB_TABLECELL, PCB_TEXTBOX for use in tables
Definition: typeinfo.h:95
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition: typeinfo.h:87
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition: typeinfo.h:98
@ PCB_DIMENSION_T
class PCB_DIMENSION_BASE: abstract dimension meta-type
Definition: typeinfo.h:100
@ PCB_TABLE_T
class PCB_TABLE, table of PCB_TABLECELLs
Definition: typeinfo.h:94
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:96