KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_test_provider_zone_connections.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) 2021-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 <board.h>
27#include <zone.h>
28#include <footprint.h>
29#include <pad.h>
30#include <pcb_track.h>
31#include <core/thread_pool.h>
32
35#include <drc/drc_rule.h>
36#include <drc/drc_item.h>
38
39
40/*
41 This loads some rule resolvers for the ZONE_FILLER, and checks that pad thermal relief
42 connections have at least the required number of spokes.
43
44 Errors generated:
45 - DRCE_STARVED_THERMAL
46*/
47
49{
50public:
52 {
53 }
54
56 {
57 }
58
59 virtual bool Run() override;
60
61 virtual const wxString GetName() const override
62 {
63 return wxT( "zone connections" );
64 };
65
66 virtual const wxString GetDescription() const override
67 {
68 return wxT( "Checks thermal reliefs for a sufficient number of connecting spokes" );
69 }
70
71private:
72 void testZoneLayer( ZONE* aZone, PCB_LAYER_ID aLayer );
73};
74
75
77{
78 BOARD* board = m_drcEngine->GetBoard();
80 std::shared_ptr<CONNECTIVITY_DATA> connectivity = board->GetConnectivity();
81 DRC_CONSTRAINT constraint;
82 wxString msg;
83
84 const std::shared_ptr<SHAPE_POLY_SET>& zoneFill = aZone->GetFilledPolysList( aLayer );
85 ISOLATED_ISLANDS isolatedIslands;
86
87 auto zoneIter = board->m_ZoneIsolatedIslandsMap.find( aZone );
88
89 if( zoneIter != board->m_ZoneIsolatedIslandsMap.end() )
90 {
91 auto layerIter = zoneIter->second.find( aLayer );
92
93 if( layerIter != zoneIter->second.end() )
94 isolatedIslands = layerIter->second;
95 }
96
97 for( FOOTPRINT* footprint : board->Footprints() )
98 {
99 for( PAD* pad : footprint->Pads() )
100 {
102 return;
103
104 if( m_drcEngine->IsCancelled() )
105 return;
106
107 //
108 // Quick tests for "connected":
109 //
110
111 if( pad->GetNetCode() != aZone->GetNetCode() || pad->GetNetCode() <= 0 )
112 continue;
113
114 BOX2I item_bbox = pad->GetBoundingBox();
115
116 if( !item_bbox.Intersects( aZone->GetBoundingBox() ) )
117 continue;
118
119 if( !pad->FlashLayer( aLayer ) )
120 continue;
121
122 //
123 // If those passed, do a thorough test:
124 //
125
126 constraint = bds.m_DRCEngine->EvalZoneConnection( pad, aZone, aLayer );
127 ZONE_CONNECTION conn = constraint.m_ZoneConnection;
128
129 if( conn != ZONE_CONNECTION::THERMAL )
130 continue;
131
132 constraint = bds.m_DRCEngine->EvalRules( MIN_RESOLVED_SPOKES_CONSTRAINT, pad, aZone,
133 aLayer );
134 int minCount = constraint.m_Value.Min();
135
136 if( constraint.GetSeverity() == RPT_SEVERITY_IGNORE || minCount <= 0 )
137 continue;
138
139 constraint = bds.m_DRCEngine->EvalRules( THERMAL_RELIEF_GAP_CONSTRAINT, pad, aZone,
140 aLayer );
141 int mid_gap = constraint.m_Value.Min() / 2;
142
143 SHAPE_POLY_SET padPoly;
144 pad->TransformShapeToPolygon( padPoly, aLayer, mid_gap, ARC_LOW_DEF, ERROR_OUTSIDE );
145
146 SHAPE_LINE_CHAIN& padOutline = padPoly.Outline( 0 );
147 BOX2I padBBox( padOutline.BBox() );
148 int spokes = 0;
149 int ignoredSpokes = 0;
150 VECTOR2I ignoredSpokePos;
151
152 for( int jj = 0; jj < zoneFill->OutlineCount(); ++jj )
153 {
154 std::vector<SHAPE_LINE_CHAIN::INTERSECTION> intersections;
155
156 zoneFill->Outline( jj ).Intersect( padOutline, intersections, true, &padBBox );
157
158 // If we connect to an island that only connects to a single item then we *are*
159 // that item. Thermal spokes to this (otherwise isolated) island don't provide
160 // electrical connectivity to anything, so we don't count them.
161 if( intersections.size() >= 2 )
162 {
163 if( alg::contains( isolatedIslands.m_SingleConnectionOutlines, jj ) )
164 {
165 ignoredSpokes += (int) intersections.size() / 2;
166 ignoredSpokePos = ( intersections[0].p + intersections[1].p ) / 2;
167 }
168 else
169 {
170 spokes += (int) intersections.size() / 2;
171 }
172 }
173 }
174
175 if( spokes == 0 && ignoredSpokes == 0 ) // Not connected at all
176 continue;
177
178 if( spokes >= minCount ) // We already have enough
179 continue;
180
181 //
182 // See if there are any other manual spokes added:
183 //
184
185 for( PCB_TRACK* track : connectivity->GetConnectedTracks( pad ) )
186 {
187 if( padOutline.PointInside( track->GetStart() ) )
188 {
189 if( aZone->GetFilledPolysList( aLayer )->Collide( track->GetEnd() ) )
190 spokes++;
191 }
192 else if( padOutline.PointInside( track->GetEnd() ) )
193 {
194 if( aZone->GetFilledPolysList( aLayer )->Collide( track->GetStart() ) )
195 spokes++;
196 }
197 }
198
199 //
200 // If we're *only* connected to isolated islands, then ignore the fact that they're
201 // isolated. (We leave that for the connectivity tester, which checks connections on
202 // all layers.)
203 //
204
205 if( spokes == 0 )
206 {
207 spokes += ignoredSpokes;
208 ignoredSpokes = 0;
209 }
210
211 //
212 // And finally report it if there aren't enough:
213 //
214
215 if( spokes < minCount )
216 {
217 std::shared_ptr<DRC_ITEM> drce = DRC_ITEM::Create( DRCE_STARVED_THERMAL );
218 VECTOR2I pos;
219
220 if( ignoredSpokes )
221 {
222 msg = wxString::Format( _( "(layer %s; %d spokes connected to isolated island)" ),
223 board->GetLayerName( aLayer ),
224 ignoredSpokes );
225 pos = ignoredSpokePos;
226 }
227 else
228 {
229 msg = wxString::Format( _( "(layer %s; %s min spoke count %d; actual %d)" ),
230 board->GetLayerName( aLayer ),
231 constraint.GetName(),
232 minCount,
233 spokes );
234 pos = pad->GetPosition();
235 }
236
237 drce->SetErrorMessage( drce->GetErrorText() + wxS( " " ) + msg );
238 drce->SetItems( aZone, pad );
239 drce->SetViolatingRule( constraint.GetParentRule() );
240
241 reportViolation( drce, pos, aLayer );
242 }
243 }
244 }
245}
246
247
249{
250 BOARD* board = m_drcEngine->GetBoard();
251 std::shared_ptr<CONNECTIVITY_DATA> connectivity = board->GetConnectivity();
252 DRC_CONSTRAINT constraint;
253
254 if( !reportPhase( _( "Checking thermal reliefs..." ) ) )
255 return false; // DRC cancelled
256
257 std::vector< std::pair<ZONE*, PCB_LAYER_ID> > zoneLayers;
258 std::atomic<size_t> done( 1 );
259 size_t total_effort = 0;
260
261 for( ZONE* zone : board->m_DRCCopperZones )
262 {
263 if( !zone->IsTeardropArea() )
264 {
265 for( PCB_LAYER_ID layer : zone->GetLayerSet().Seq() )
266 {
267 zoneLayers.push_back( { zone, layer } );
268 total_effort += zone->GetFilledPolysList( layer )->FullPointCount();
269 }
270 }
271 }
272
273 total_effort = std::max( (size_t) 1, total_effort );
274
276 std::vector<std::future<int>> returns;
277
278 returns.reserve( zoneLayers.size() );
279
280 for( const std::pair<ZONE*, PCB_LAYER_ID>& zonelayer : zoneLayers )
281 {
282 returns.emplace_back( tp.submit(
283 [&]( ZONE* aZone, PCB_LAYER_ID aLayer ) -> int
284 {
285 if( !m_drcEngine->IsCancelled() )
286 {
287 testZoneLayer( aZone, aLayer );
288 done.fetch_add( aZone->GetFilledPolysList( aLayer )->FullPointCount() );
289 }
290
291 return 0;
292 },
293 zonelayer.first, zonelayer.second ) );
294 }
295
296 for( const std::future<int>& ret : returns )
297 {
298 std::future_status status = ret.wait_for( std::chrono::milliseconds( 250 ) );
299
300 while( status != std::future_status::ready )
301 {
302 reportProgress( done, total_effort );
303 status = ret.wait_for( std::chrono::milliseconds( 250 ) );
304 }
305 }
306
307 return !m_drcEngine->IsCancelled();
308}
309
310
311namespace detail
312{
314}
constexpr int ARC_LOW_DEF
Definition: base_units.h:119
Container for design settings for a BOARD object.
std::shared_ptr< DRC_ENGINE > m_DRCEngine
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:282
std::map< ZONE *, std::map< PCB_LAYER_ID, ISOLATED_ISLANDS > > m_ZoneIsolatedIslandsMap
Definition: board.h:1278
std::vector< ZONE * > m_DRCCopperZones
Definition: board.h:1274
const FOOTPRINTS & Footprints() const
Definition: board.h:323
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
Definition: board.cpp:564
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:794
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Return a list of missing connections between components/tracks.
Definition: board.h:460
bool Intersects(const BOX2< Vec > &aRect) const
Definition: box2.h:294
wxString GetName() const
Definition: drc_rule.h:149
SEVERITY GetSeverity() const
Definition: drc_rule.h:162
ZONE_CONNECTION m_ZoneConnection
Definition: drc_rule.h:174
MINOPTMAX< int > m_Value
Definition: drc_rule.h:172
DRC_RULE * GetParentRule() const
Definition: drc_rule.h:145
BOARD * GetBoard() const
Definition: drc_engine.h:89
bool IsErrorLimitExceeded(int error_code)
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:338
virtual bool Run() override
Run this provider against the given PCB with configured options (if any).
virtual const wxString GetName() const override
void testZoneLayer(ZONE *aZone, PCB_LAYER_ID aLayer)
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)
virtual void reportViolation(std::shared_ptr< DRC_ITEM > &item, const VECTOR2I &aMarkerPos, int aMarkerLayer)
DRC_ENGINE * m_drcEngine
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:418
T Min() const
Definition: minoptmax.h:33
Definition: pad.h:53
bool PointInside(const VECTOR2I &aPt, int aAccuracy=0, bool aUseBBoxCache=false) const override
Check if point aP lies inside a closed shape.
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
Represent a set of closed polygons.
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
Handle a list of polygons defining a copper zone.
Definition: zone.h:72
const std::shared_ptr< SHAPE_POLY_SET > & GetFilledPolysList(PCB_LAYER_ID aLayer) const
Definition: zone.h:615
const BOX2I GetBoundingBox() const override
Definition: zone.cpp:343
bool IsTeardropArea() const
Definition: zone.h:694
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition: zone.h:129
@ DRCE_STARVED_THERMAL
Definition: drc_item.h:48
@ MIN_RESOLVED_SPOKES_CONSTRAINT
Definition: drc_rule.h:61
@ THERMAL_RELIEF_GAP_CONSTRAINT
Definition: drc_rule.h:59
#define _(s)
@ ERROR_OUTSIDE
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition: kicad_algo.h:100
static DRC_REGISTER_TEST_PROVIDER< DRC_TEST_PROVIDER_ANNULAR_WIDTH > dummy
@ RPT_SEVERITY_IGNORE
A struct recording the isolated and single-pad islands within a zone.
Definition: zone.h:59
std::vector< int > m_SingleConnectionOutlines
Definition: zone.h:61
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
ZONE_CONNECTION
How pads are covered by copper in zone.
Definition: zones.h:47