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->GetCachedBoundingBox() ) )
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->TransformShapeWithClearanceToPolygon( padPoly, aLayer, 0, ARC_LOW_DEF,
130
131 SHAPE_LINE_CHAIN& padOutline = padPoly.Outline( 0 );
132 BOX2I padBBox( item_bbox );
133 std::vector<SHAPE_LINE_CHAIN::INTERSECTION> intersections;
134
135 for( int jj = 0; jj < zoneFill->OutlineCount(); ++jj )
136 zoneFill->Outline( jj ).Intersect( padOutline, intersections, true, &padBBox );
137
138 int spokes = intersections.size() / 2;
139
140 if( spokes <= 0 ) // Not connected at all
141 continue;
142
143 if( spokes >= minCount ) // We already have enough
144 continue;
145
146 //
147 // See if there are any other manual spokes added:
148 //
149
150 for( PCB_TRACK* track : connectivity->GetConnectedTracks( pad ) )
151 {
152 if( padOutline.PointInside( track->GetStart() ) )
153 {
154 if( aZone->GetFilledPolysList( aLayer )->Collide( track->GetEnd() ) )
155 spokes++;
156 }
157 else if( padOutline.PointInside( track->GetEnd() ) )
158 {
159 if( aZone->GetFilledPolysList( aLayer )->Collide( track->GetStart() ) )
160 spokes++;
161 }
162 }
163
164 //
165 // And finally report it if there aren't enough:
166 //
167
168 if( spokes < minCount )
169 {
170 std::shared_ptr<DRC_ITEM> drce = DRC_ITEM::Create( DRCE_STARVED_THERMAL );
171 wxString msg;
172
173 msg.Printf( _( "(%s min spoke count %d; actual %d)" ),
174 constraint.GetName(),
175 minCount,
176 spokes );
177
178 drce->SetErrorMessage( drce->GetErrorText() + wxS( " " ) + msg );
179 drce->SetItems( aZone, pad );
180 drce->SetViolatingRule( constraint.GetParentRule() );
181
182 reportViolation( drce, pad->GetPosition(), UNDEFINED_LAYER );
183 }
184 }
185 }
186}
187
188
190{
191 BOARD* board = m_drcEngine->GetBoard();
192 std::shared_ptr<CONNECTIVITY_DATA> connectivity = board->GetConnectivity();
193 DRC_CONSTRAINT constraint;
194
195 if( !reportPhase( _( "Checking thermal reliefs..." ) ) )
196 return false; // DRC cancelled
197
198 std::vector< std::pair<ZONE*, PCB_LAYER_ID> > zoneLayers;
199 std::atomic<size_t> done( 1 );
200 size_t total_effort = 0;
201
202 for( ZONE* zone : board->m_DRCCopperZones )
203 {
204 for( PCB_LAYER_ID layer : zone->GetLayerSet().Seq() )
205 {
206 zoneLayers.push_back( { zone, layer } );
207 total_effort += zone->GetFilledPolysList( layer )->FullPointCount();
208 }
209 }
210
211 total_effort = std::max( (size_t) 1, total_effort );
212
214 std::vector<std::future<int>> returns;
215
216 returns.reserve( zoneLayers.size() );
217
218 for( const std::pair<ZONE*, PCB_LAYER_ID>& zonelayer : zoneLayers )
219 {
220 returns.emplace_back( tp.submit(
221 [&]( ZONE* aZone, PCB_LAYER_ID aLayer ) -> int
222 {
223 if( !m_drcEngine->IsCancelled() )
224 {
225 testZoneLayer( aZone, aLayer );
226 done.fetch_add( aZone->GetFilledPolysList( aLayer )->FullPointCount() );
227 }
228
229 return 0;
230 },
231 zonelayer.first, zonelayer.second ) );
232 }
233
234 for( const std::future<int>& ret : returns )
235 {
236 std::future_status status = ret.wait_for( std::chrono::milliseconds( 250 ) );
237
238 while( status != std::future_status::ready )
239 {
240 m_drcEngine->ReportProgress( static_cast<double>( done ) / total_effort );
241 status = ret.wait_for( std::chrono::milliseconds( 250 ) );
242 }
243 }
244
245 return !m_drcEngine->IsCancelled();
246}
247
248
249namespace detail
250{
252}
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:1157
FOOTPRINTS & Footprints()
Definition: board.h:307
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:617
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:147
SEVERITY GetSeverity() const
Definition: drc_rule.h:160
ZONE_CONNECTION m_ZoneConnection
Definition: drc_rule.h:172
MINOPTMAX< int > m_Value
Definition: drc_rule.h:170
DRC_RULE * GetParentRule() const
Definition: drc_rule.h:143
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:58
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:596
const BOX2I GetCachedBoundingBox() const
ONLY TO BE USED BY CLIENTS WHICH SET UP THE CACHE!
Definition: zone.h:133
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition: zone.h:115
@ DRCE_STARVED_THERMAL
Definition: drc_item.h:48
@ MIN_RESOLVED_SPOKES_CONSTRAINT
Definition: drc_rule.h:60
#define _(s)
@ ERROR_OUTSIDE
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:59
@ UNDEFINED_LAYER
Definition: layer_ids.h:60
static DRC_REGISTER_TEST_PROVIDER< DRC_TEST_PROVIDER_ANNULAR_WIDTH > dummy
@ 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.