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-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 <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( item_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 // And finally report it if there aren't enough:
201 //
202
203 if( spokes < minCount )
204 {
205 std::shared_ptr<DRC_ITEM> drce = DRC_ITEM::Create( DRCE_STARVED_THERMAL );
206 VECTOR2I pos;
207
208 if( ignoredSpokes )
209 {
210 msg = wxString::Format( _( "(layer %s; %d spokes connected to isolated island)" ),
211 board->GetLayerName( aLayer ),
212 ignoredSpokes );
213 pos = ignoredSpokePos;
214 }
215 else
216 {
217 msg = wxString::Format( _( "(layer %s; %s min spoke count %d; actual %d)" ),
218 board->GetLayerName( aLayer ),
219 constraint.GetName(),
220 minCount,
221 spokes );
222 pos = pad->GetPosition();
223 }
224
225 drce->SetErrorMessage( drce->GetErrorText() + wxS( " " ) + msg );
226 drce->SetItems( aZone, pad );
227 drce->SetViolatingRule( constraint.GetParentRule() );
228
229 reportViolation( drce, pos, aLayer );
230 }
231 }
232 }
233}
234
235
237{
238 BOARD* board = m_drcEngine->GetBoard();
239 std::shared_ptr<CONNECTIVITY_DATA> connectivity = board->GetConnectivity();
240 DRC_CONSTRAINT constraint;
241
242 if( !reportPhase( _( "Checking thermal reliefs..." ) ) )
243 return false; // DRC cancelled
244
245 std::vector< std::pair<ZONE*, PCB_LAYER_ID> > zoneLayers;
246 std::atomic<size_t> done( 1 );
247 size_t total_effort = 0;
248
249 for( ZONE* zone : board->m_DRCCopperZones )
250 {
251 if( !zone->IsTeardropArea() )
252 {
253 for( PCB_LAYER_ID layer : zone->GetLayerSet().Seq() )
254 {
255 zoneLayers.push_back( { zone, layer } );
256 total_effort += zone->GetFilledPolysList( layer )->FullPointCount();
257 }
258 }
259 }
260
261 total_effort = std::max( (size_t) 1, total_effort );
262
264 std::vector<std::future<int>> returns;
265
266 returns.reserve( zoneLayers.size() );
267
268 for( const std::pair<ZONE*, PCB_LAYER_ID>& zonelayer : zoneLayers )
269 {
270 returns.emplace_back( tp.submit(
271 [&]( ZONE* aZone, PCB_LAYER_ID aLayer ) -> int
272 {
273 if( !m_drcEngine->IsCancelled() )
274 {
275 testZoneLayer( aZone, aLayer );
276 done.fetch_add( aZone->GetFilledPolysList( aLayer )->FullPointCount() );
277 }
278
279 return 0;
280 },
281 zonelayer.first, zonelayer.second ) );
282 }
283
284 for( const std::future<int>& ret : returns )
285 {
286 std::future_status status = ret.wait_for( std::chrono::milliseconds( 250 ) );
287
288 while( status != std::future_status::ready )
289 {
290 m_drcEngine->ReportProgress( static_cast<double>( done ) / total_effort );
291 status = ret.wait_for( std::chrono::milliseconds( 250 ) );
292 }
293 }
294
295 return !m_drcEngine->IsCancelled();
296}
297
298
299namespace detail
300{
302}
constexpr int ARC_LOW_DEF
Definition: base_units.h:120
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:276
std::map< ZONE *, std::map< PCB_LAYER_ID, ISOLATED_ISLANDS > > m_ZoneIsolatedIslandsMap
Definition: board.h:1242
std::vector< ZONE * > m_DRCCopperZones
Definition: board.h:1238
FOOTPRINTS & Footprints()
Definition: board.h:318
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
Definition: board.cpp:536
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:766
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Return a list of missing connections between components/tracks.
Definition: board.h:441
bool Intersects(const BOX2< Vec > &aRect) const
Definition: box2.h:270
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:325
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:58
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...
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:354
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:49