KiCad PCB EDA Suite
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 <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
83 const std::shared_ptr<SHAPE_POLY_SET>& zoneFill = aZone->GetFilledPolysList( aLayer );
84
85 for( FOOTPRINT* footprint : board->Footprints() )
86 {
87 for( PAD* pad : footprint->Pads() )
88 {
90 return;
91
93 return;
94
95 //
96 // Quick tests for "connected":
97 //
98
99 if( pad->GetNetCode() != aZone->GetNetCode() || pad->GetNetCode() <= 0 )
100 continue;
101
102 BOX2I item_bbox = pad->GetBoundingBox();
103
104 if( !item_bbox.Intersects( aZone->GetBoundingBox() ) )
105 continue;
106
107 if( !pad->FlashLayer( aLayer ) )
108 continue;
109
110 //
111 // If those passed, do a thorough test:
112 //
113
114 constraint = bds.m_DRCEngine->EvalZoneConnection( pad, aZone, aLayer );
115 ZONE_CONNECTION conn = constraint.m_ZoneConnection;
116
117 if( conn != ZONE_CONNECTION::THERMAL )
118 continue;
119
120 constraint = bds.m_DRCEngine->EvalRules( MIN_RESOLVED_SPOKES_CONSTRAINT, pad, aZone,
121 aLayer );
122 int minCount = constraint.m_Value.Min();
123
124 if( constraint.GetSeverity() == RPT_SEVERITY_IGNORE || minCount <= 0 )
125 continue;
126
127 SHAPE_POLY_SET padPoly;
128 pad->TransformShapeToPolygon( padPoly, aLayer, 0, ARC_LOW_DEF, ERROR_OUTSIDE );
129
130 SHAPE_LINE_CHAIN& padOutline = padPoly.Outline( 0 );
131 BOX2I padBBox( item_bbox );
132 std::vector<SHAPE_LINE_CHAIN::INTERSECTION> intersections;
133
134 for( int jj = 0; jj < zoneFill->OutlineCount(); ++jj )
135 zoneFill->Outline( jj ).Intersect( padOutline, intersections, true, &padBBox );
136
137 int spokes = intersections.size() / 2;
138
139 if( spokes <= 0 ) // Not connected at all
140 continue;
141
142 if( spokes >= minCount ) // We already have enough
143 continue;
144
145 //
146 // See if there are any other manual spokes added:
147 //
148
149 for( PCB_TRACK* track : connectivity->GetConnectedTracks( pad ) )
150 {
151 if( padOutline.PointInside( track->GetStart() ) )
152 {
153 if( aZone->GetFilledPolysList( aLayer )->Collide( track->GetEnd() ) )
154 spokes++;
155 }
156 else if( padOutline.PointInside( track->GetEnd() ) )
157 {
158 if( aZone->GetFilledPolysList( aLayer )->Collide( track->GetStart() ) )
159 spokes++;
160 }
161 }
162
163 //
164 // And finally report it if there aren't enough:
165 //
166
167 if( spokes < minCount )
168 {
169 std::shared_ptr<DRC_ITEM> drce = DRC_ITEM::Create( DRCE_STARVED_THERMAL );
170 wxString msg = wxString::Format( _( "(%s min spoke count %d; actual %d)" ),
171 constraint.GetName(),
172 minCount,
173 spokes );
174
175 drce->SetErrorMessage( drce->GetErrorText() + wxS( " " ) + msg );
176 drce->SetItems( aZone, pad );
177 drce->SetViolatingRule( constraint.GetParentRule() );
178
179 reportViolation( drce, pad->GetPosition(), aLayer );
180 }
181 }
182 }
183}
184
185
187{
188 BOARD* board = m_drcEngine->GetBoard();
189 std::shared_ptr<CONNECTIVITY_DATA> connectivity = board->GetConnectivity();
190 DRC_CONSTRAINT constraint;
191
192 if( !reportPhase( _( "Checking thermal reliefs..." ) ) )
193 return false; // DRC cancelled
194
195 std::vector< std::pair<ZONE*, PCB_LAYER_ID> > zoneLayers;
196 std::atomic<size_t> done( 1 );
197 size_t total_effort = 0;
198
199 for( ZONE* zone : board->m_DRCCopperZones )
200 {
201 for( PCB_LAYER_ID layer : zone->GetLayerSet().Seq() )
202 {
203 zoneLayers.push_back( { zone, layer } );
204 total_effort += zone->GetFilledPolysList( layer )->FullPointCount();
205 }
206 }
207
208 total_effort = std::max( (size_t) 1, total_effort );
209
211 std::vector<std::future<int>> returns;
212
213 returns.reserve( zoneLayers.size() );
214
215 for( const std::pair<ZONE*, PCB_LAYER_ID>& zonelayer : zoneLayers )
216 {
217 returns.emplace_back( tp.submit(
218 [&]( ZONE* aZone, PCB_LAYER_ID aLayer ) -> int
219 {
220 if( !m_drcEngine->IsCancelled() )
221 {
222 testZoneLayer( aZone, aLayer );
223 done.fetch_add( aZone->GetFilledPolysList( aLayer )->FullPointCount() );
224 }
225
226 return 0;
227 },
228 zonelayer.first, zonelayer.second ) );
229 }
230
231 for( const std::future<int>& ret : returns )
232 {
233 std::future_status status = ret.wait_for( std::chrono::milliseconds( 250 ) );
234
235 while( status != std::future_status::ready )
236 {
237 m_drcEngine->ReportProgress( static_cast<double>( done ) / total_effort );
238 status = ret.wait_for( std::chrono::milliseconds( 250 ) );
239 }
240 }
241
242 return !m_drcEngine->IsCancelled();
243}
244
245
246namespace detail
247{
249}
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:265
std::vector< ZONE * > m_DRCCopperZones
Definition: board.h:1151
FOOTPRINTS & Footprints()
Definition: board.h:307
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:686
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Return a list of missing connections between components/tracks.
Definition: board.h:424
bool Intersects(const BOX2< Vec > &aRect) const
Definition: box2.h:269
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_ITEMs and po...
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:411
T Min() const
Definition: minoptmax.h:33
Definition: pad.h:59
bool PointInside(const VECTOR2I &aPt, int aAccuracy=0, bool aUseBBoxCache=false) const
Check if point aP lies inside a polygon (any type) defined by the line chain.
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)
Handle a list of polygons defining a copper zone.
Definition: zone.h:57
const std::shared_ptr< SHAPE_POLY_SET > & GetFilledPolysList(PCB_LAYER_ID aLayer) const
Definition: zone.h:602
const BOX2I GetBoundingBox() const override
Definition: zone.cpp:323
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition: zone.h:122
@ DRCE_STARVED_THERMAL
Definition: drc_item.h:48
@ MIN_RESOLVED_SPOKES_CONSTRAINT
Definition: drc_rule.h:61
#define _(s)
@ ERROR_OUTSIDE
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:59
static DRC_REGISTER_TEST_PROVIDER< DRC_TEST_PROVIDER_ANNULAR_WIDTH > dummy
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
@ RPT_SEVERITY_IGNORE
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
Definition: thread_pool.cpp:32
static thread_pool * tp
Definition: thread_pool.cpp:30
BS::thread_pool thread_pool
Definition: thread_pool.h:30
ZONE_CONNECTION
How pads are covered by copper in zone.
Definition: zones.h:50
@ THERMAL
Use thermal relief for pads.