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 (C) 2022 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 <core/thread_pool.h>
28#include <zone.h>
30#include <drc/drc_engine.h>
31#include <drc/drc_rtree.h>
33
35{
37
38 int& largestClearance = m_board->m_DRCMaxClearance;
39 int& largestPhysicalClearance = m_board->m_DRCMaxPhysicalClearance;
40 DRC_CONSTRAINT worstConstraint;
41 LSET boardCopperLayers = LSET::AllCuMask( m_board->GetCopperLayerCount() );
42
43 largestClearance = std::max( largestClearance, m_board->GetMaxClearanceValue() );
44
46 largestPhysicalClearance = worstConstraint.GetValue().Min();
47
49 largestPhysicalClearance = std::max( largestPhysicalClearance, worstConstraint.GetValue().Min() );
50
51 std::set<ZONE*> allZones;
52
53 for( ZONE* zone : m_board->Zones() )
54 {
55 allZones.insert( zone );
56
57 if( !zone->GetIsRuleArea() )
58 {
59 m_board->m_DRCZones.push_back( zone );
60
61 if( ( zone->GetLayerSet() & boardCopperLayers ).any() )
62 {
63 m_board->m_DRCCopperZones.push_back( zone );
64 }
65 }
66 }
67
68 for( FOOTPRINT* footprint : m_board->Footprints() )
69 {
70 for( ZONE* zone : footprint->Zones() )
71 {
72 allZones.insert( zone );
73
74 if( !zone->GetIsRuleArea() )
75 {
76 m_board->m_DRCZones.push_back( zone );
77
78 if( ( zone->GetLayerSet() & boardCopperLayers ).any() )
79 m_board->m_DRCCopperZones.push_back( zone );
80 }
81 }
82 }
83
84 // This is the number of tests between 2 calls to the progress bar
85 size_t progressDelta = 200;
86 size_t count = 0;
87 size_t ii = 0;
88
89 auto countItems =
90 [&]( BOARD_ITEM* item ) -> bool
91 {
92 ++count;
93 return true;
94 };
95
96 auto addToCopperTree =
97 [&]( BOARD_ITEM* item ) -> bool
98 {
99 if( !reportProgress( ii++, count, progressDelta ) )
100 return false;
101
102 LSET copperLayers = item->GetLayerSet() & boardCopperLayers;
103
104 // Special-case pad holes which pierce all the copper layers
105 if( item->Type() == PCB_PAD_T )
106 {
107 PAD* pad = static_cast<PAD*>( item );
108
109 if( pad->HasHole() )
110 copperLayers = boardCopperLayers;
111 }
112
113 for( PCB_LAYER_ID layer : copperLayers.Seq() )
114 {
115 if( IsCopperLayer( layer ) )
116 m_board->m_CopperItemRTreeCache->Insert( item, layer, largestClearance );
117 }
118
119 return true;
120 };
121
122 if( !reportPhase( _( "Gathering copper items..." ) ) )
123 return false; // DRC cancelled
124
125 static const std::vector<KICAD_T> itemTypes = {
127 PCB_PAD_T,
131 };
132
133 forEachGeometryItem( itemTypes, LSET::AllCuMask(), countItems );
134
135 {
136 std::unique_lock<std::mutex> cacheLock( m_board->m_CachesMutex );
137
139 m_board->m_CopperItemRTreeCache = std::make_shared<DRC_RTREE>();
140
141 forEachGeometryItem( itemTypes, LSET::AllCuMask(), addToCopperTree );
142 }
143
144 if( !reportPhase( _( "Tessellating copper zones..." ) ) )
145 return false; // DRC cancelled
146
147 // Cache zone bounding boxes, triangulation, copper zone rtrees, and footprint courtyards
148 // before we start.
149
150 for( FOOTPRINT* footprint : m_board->Footprints() )
151 footprint->BuildCourtyardCaches();
152
154 std::vector<std::future<size_t>> returns;
155 std::atomic<size_t> done( 1 );
156
157 returns.reserve( allZones.size() );
158
159 auto cache_zones =
160 [this, &done]( ZONE* aZone ) -> size_t
161 {
162 if( m_drcEngine->IsCancelled() )
163 return 0;
164
165 aZone->CacheBoundingBox();
166 aZone->CacheTriangulation();
167
168 if( !aZone->GetIsRuleArea() && aZone->IsOnCopperLayer() )
169 {
170 std::unique_ptr<DRC_RTREE> rtree = std::make_unique<DRC_RTREE>();
171
172 for( PCB_LAYER_ID layer : aZone->GetLayerSet().Seq() )
173 {
174 if( IsCopperLayer( layer ) )
175 rtree->Insert( aZone, layer );
176 }
177
178 std::unique_lock<std::mutex> cacheLock( m_board->m_CachesMutex );
179 m_board->m_CopperZoneRTreeCache[ aZone ] = std::move( rtree );
180
181 done.fetch_add( 1 );
182 }
183
184 return 1;
185 };
186
187 for( ZONE* zone : allZones )
188 returns.emplace_back( tp.submit( cache_zones, zone ) );
189
190 for( const std::future<size_t>& ret : returns )
191 {
192 std::future_status status = ret.wait_for( std::chrono::milliseconds( 250 ) );
193
194 while( status != std::future_status::ready )
195 {
196 m_drcEngine->ReportProgress( static_cast<double>( done ) / allZones.size() );
197 status = ret.wait_for( std::chrono::milliseconds( 250 ) );
198 }
199 }
200
202
203 for( ZONE* zone : m_board->Zones() )
204 {
205 if( !zone->GetIsRuleArea() && !zone->IsTeardropArea() )
206 {
207 for( PCB_LAYER_ID layer : zone->GetLayerSet().Seq() )
209 }
210 }
211
212 std::shared_ptr<CONNECTIVITY_DATA> connectivity = m_board->GetConnectivity();
213
214 connectivity->ClearRatsnest();
215 connectivity->Build( m_board, m_drcEngine->GetProgressReporter() );
216 connectivity->FillIsolatedIslandsMap( m_board->m_ZoneIsolatedIslandsMap, true );
217
218 return !m_drcEngine->IsCancelled();
219}
220
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:77
std::map< ZONE *, std::map< PCB_LAYER_ID, ISOLATED_ISLANDS > > m_ZoneIsolatedIslandsMap
Definition: board.h:1208
std::vector< ZONE * > m_DRCCopperZones
Definition: board.h:1204
ZONES & Zones()
Definition: board.h:319
int GetMaxClearanceValue() const
Returns the maximum clearance value for any object on the board.
Definition: board.cpp:748
FOOTPRINTS & Footprints()
Definition: board.h:313
int GetCopperLayerCount() const
Definition: board.cpp:590
int m_DRCMaxPhysicalClearance
Definition: board.h:1206
std::shared_ptr< DRC_RTREE > m_CopperItemRTreeCache
Definition: board.h:1199
int m_DRCMaxClearance
Definition: board.h:1205
std::unordered_map< ZONE *, std::unique_ptr< DRC_RTREE > > m_CopperZoneRTreeCache
Definition: board.h:1198
std::vector< ZONE * > m_DRCZones
Definition: board.h:1203
std::mutex m_CachesMutex
Definition: board.h:1191
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Return a list of missing connections between components/tracks.
Definition: board.h:433
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:141
BOARD * GetBoard() const
Definition: drc_engine.h:89
bool ReportProgress(double aProgress)
PROGRESS_REPORTER * GetProgressReporter() const
Definition: drc_engine.h:124
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, LSET aLayers, const std::function< bool(BOARD_ITEM *)> &aFunc)
virtual bool reportProgress(int aCount, int aSize, int aDelta)
DRC_ENGINE * m_drcEngine
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:552
LSEQ Seq(const PCB_LAYER_ID *aWishListSequence, unsigned aCount) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition: lset.cpp:411
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:773
T Min() const
Definition: minoptmax.h:33
Definition: pad.h:58
Handle a list of polygons defining a copper zone.
Definition: zone.h:72
The common library.
@ PHYSICAL_HOLE_CLEARANCE_CONSTRAINT
Definition: drc_rule.h:71
@ PHYSICAL_CLEARANCE_CONSTRAINT
Definition: drc_rule.h:70
#define _(s)
bool IsCopperLayer(int aLayerId)
Tests whether a layer is a copper layer.
Definition: layer_ids.h:847
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:59
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
@ 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:94
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition: typeinfo.h:92
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition: typeinfo.h:91
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition: typeinfo.h:90
@ 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: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