KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_test_provider_creepage.cpp
Go to the documentation of this file.
1/*
2 * Copyright The KiCad Developers.
3 * Copyright (C) 2024 Fabien Corona f.corona<at>laposte.net
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, you may find one here:
17 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
18 * or you may search the http://www.gnu.org website for the version 2 license,
19 * or you may write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 */
22
23#include <common.h>
24#include <macros.h>
26#include <footprint.h>
27#include <pad.h>
28#include <pcb_track.h>
29#include <pcb_shape.h>
30#include <zone.h>
31#include <advanced_config.h>
32#include <geometry/shape_rect.h>
33#include <geometry/seg.h>
35#include <drc/drc_item.h>
36#include <drc/drc_rule.h>
39
41
42
43/*
44 Physical creepage tests.
45
46 Errors generated:
47 - DRCE_CREEPAGE
48 */
49
51{
52public:
55 {}
56
57 virtual ~DRC_TEST_PROVIDER_CREEPAGE() = default;
58
59 virtual bool Run() override;
60
61 virtual const wxString GetName() const override { return wxT( "creepage" ); };
62
63 double GetMaxConstraint( const std::vector<int>& aNetCodes );
64
65private:
66 int testCreepage();
67 int testCreepage( CREEPAGE_GRAPH& aGraph, int aNetCodeA, int aNetCodeB, PCB_LAYER_ID aLayer );
68
69 void CollectBoardEdges( std::vector<BOARD_ITEM*>& aVector );
70 void CollectNetCodes( std::vector<int>& aVector );
71
72 std::set<std::pair<const BOARD_ITEM*, const BOARD_ITEM*>> m_reportedPairs;
73};
74
75
77{
79 m_reportedPairs.clear();
80
82 {
83 if( !reportPhase( _( "Checking creepage..." ) ) )
84 return false; // DRC cancelled
85
87 }
88 return !m_drcEngine->IsCancelled();
89}
90
91
92std::shared_ptr<GRAPH_NODE> FindInGraphNodes( std::shared_ptr<GRAPH_NODE> aNode,
93 std::vector<std::shared_ptr<GRAPH_NODE>>& aGraph )
94{
95 if( !aNode )
96 return nullptr;
97
98 for( std::shared_ptr<GRAPH_NODE> gn : aGraph )
99 {
100 if( aNode->m_pos == gn->m_pos )
101 return gn;
102 }
103 return nullptr;
104}
105
106
107int DRC_TEST_PROVIDER_CREEPAGE::testCreepage( CREEPAGE_GRAPH& aGraph, int aNetCodeA, int aNetCodeB,
108 PCB_LAYER_ID aLayer )
109{
110 PCB_TRACK bci1( m_board );
111 PCB_TRACK bci2( m_board );
112 bci1.SetNetCode( aNetCodeA );
113 bci2.SetNetCode( aNetCodeB );
114 bci1.SetLayer( aLayer );
115 bci2.SetLayer( aLayer );
116
117
118 DRC_CONSTRAINT constraint;
119 constraint = m_drcEngine->EvalRules( CREEPAGE_CONSTRAINT, &bci1, &bci2, aLayer );
120 double creepageValue = constraint.Value().Min();
121 aGraph.SetTarget( creepageValue );
122
123 if( creepageValue <= 0 )
124 return 0;
125
126 // Let's make a quick "clearance test"
127 NETINFO_ITEM* netA = m_board->FindNet( aNetCodeA );
128 NETINFO_ITEM* netB = m_board->FindNet( aNetCodeB );
129
130 if ( !netA || !netB )
131 return 0;
132
133 if ( netA->GetBoundingBox().Distance( netB->GetBoundingBox() ) > creepageValue )
134 return 0;
135
136 std::shared_ptr<GRAPH_NODE> NetA = aGraph.AddNetElements( aNetCodeA, aLayer, creepageValue );
137 std::shared_ptr<GRAPH_NODE> NetB = aGraph.AddNetElements( aNetCodeB, aLayer, creepageValue );
138
139
140 aGraph.GeneratePaths( creepageValue, aLayer, false );
141
142 std::vector<std::shared_ptr<GRAPH_NODE>> temp_nodes;
143
144 std::copy_if( aGraph.m_nodes.begin(), aGraph.m_nodes.end(), std::back_inserter( temp_nodes ),
145 []( std::shared_ptr<GRAPH_NODE> aNode )
146 {
147 return !!aNode && aNode->m_parent && aNode->m_parent->IsConductive()
148 && aNode->m_connectDirectly && aNode->m_type == GRAPH_NODE::POINT;
149 } );
150
151 alg::for_all_pairs( temp_nodes.begin(), temp_nodes.end(),
152 [&]( std::shared_ptr<GRAPH_NODE> aN1, std::shared_ptr<GRAPH_NODE> aN2 )
153 {
154 if( aN1 == aN2 )
155 return;
156
157 if( !aN1 || !aN2 )
158 return;
159
160 if( !( aN1->m_parent ) || !( aN2->m_parent ) )
161 return;
162
163 if( ( aN1->m_parent ) != ( aN2->m_parent ) )
164 return;
165
166
167 if( aN1->m_parent->IsConductive() )
168 return;
169
170 if( aN1->m_connectDirectly || aN2->m_connectDirectly )
171 return;
172
173 // We are only looking for points on circles and arcs
174
175 if( aN1->m_type != GRAPH_NODE::POINT )
176 return;
177
178 if( aN2->m_type != GRAPH_NODE::POINT )
179 return;
180
181 aN1->m_parent->ConnectChildren( aN1, aN2, aGraph );
182 } );
183
184 std::vector<std::shared_ptr<GRAPH_CONNECTION>> shortestPath;
185 shortestPath.clear();
186 double distance = aGraph.Solve( NetA, NetB, shortestPath );
187
188 if( !shortestPath.empty() && ( shortestPath.size() >= 4 ) && ( distance - creepageValue < 0 ) )
189 {
190 std::shared_ptr<DRC_ITEM> drce = DRC_ITEM::Create( DRCE_CREEPAGE );
191 wxString msg = formatMsg( _( "(%s creepage %s; actual %s)" ), constraint.GetName(),
192 creepageValue, distance );
193 drce->SetErrorMessage( drce->GetErrorText() + wxS( " " ) + msg );
194 drce->SetViolatingRule( constraint.GetParentRule() );
195
196 std::shared_ptr<GRAPH_CONNECTION> gc1 = shortestPath[1];
197 std::shared_ptr<GRAPH_CONNECTION> gc2 = shortestPath[shortestPath.size() - 2];
198
199 if( gc1->n1 && gc2->n2 )
200 {
201 const BOARD_ITEM* item1 = gc1->n1->m_parent->GetParent();
202 const BOARD_ITEM* item2 = gc2->n2->m_parent->GetParent();
203
204 if( m_reportedPairs.insert( std::make_pair( item1, item2 ) ).second )
205 drce->SetItems( item1, item2 );
206 else
207 return 1;
208 }
209
210 std::vector<PCB_SHAPE> shortestPathShapes1, shortestPathShapes2;
211
212 VECTOR2I startPoint = gc1->m_path.a2;
213 VECTOR2I endPoint = gc2->m_path.a2;
214 std::vector<PCB_SHAPE> path;
215
216 for( const std::shared_ptr<GRAPH_CONNECTION>& gc : shortestPath )
217 {
218 if( !gc )
219 continue;
220
221 std::vector<PCB_SHAPE> shapes = gc->GetShapes();
222
223 for( const PCB_SHAPE& sh : shapes )
224 path.push_back( sh );
225 }
226
227 DRC_CUSTOM_MARKER_HANDLER handler = GetGraphicsHandler( path, startPoint, endPoint, distance );
228 reportViolation( drce, gc1->m_path.a2, aLayer, &handler );
229 }
230
231 return 1;
232}
233
234double DRC_TEST_PROVIDER_CREEPAGE::GetMaxConstraint( const std::vector<int>& aNetCodes )
235{
236 double maxConstraint = 0;
237 DRC_CONSTRAINT constraint;
238
239 PCB_TRACK bci1( m_board );
240 PCB_TRACK bci2( m_board );
241
242 alg::for_all_pairs( aNetCodes.begin(), aNetCodes.end(),
243 [&]( int aNet1, int aNet2 )
244 {
245 if( aNet1 == aNet2 )
246 return;
247
248 bci1.SetNetCode( aNet1 );
249 bci2.SetNetCode( aNet2 );
250
251 for( PCB_LAYER_ID layer : LSET::AllCuMask( m_board->GetCopperLayerCount() ) )
252 {
253 bci1.SetLayer( layer );
254 bci2.SetLayer( layer );
255 constraint = m_drcEngine->EvalRules( CREEPAGE_CONSTRAINT, &bci1,
256 &bci2, layer );
257 double value = constraint.Value().Min();
258 maxConstraint = value > maxConstraint ? value : maxConstraint;
259 }
260 } );
261
262 return maxConstraint;
263}
264
265void DRC_TEST_PROVIDER_CREEPAGE::CollectNetCodes( std::vector<int>& aVector )
266{
267 NETCODES_MAP nets = m_board->GetNetInfo().NetsByNetcode();
268
269 for( auto it = nets.begin(); it != nets.end(); it++ )
270 aVector.push_back( it->first );
271}
272
273void DRC_TEST_PROVIDER_CREEPAGE::CollectBoardEdges( std::vector<BOARD_ITEM*>& aVector )
274{
275 if( !m_board )
276 return;
277
278 for( BOARD_ITEM* drawing : m_board->Drawings() )
279 {
280 if( !drawing )
281 continue;
282
283 if( drawing->IsOnLayer( Edge_Cuts ) )
284 aVector.push_back( drawing );
285 }
286
287 for( FOOTPRINT* fp : m_board->Footprints() )
288 {
289 if( !fp )
290 continue;
291
292 for( BOARD_ITEM* drawing : fp->GraphicalItems() )
293 {
294 if( !drawing )
295 continue;
296
297 if( drawing->IsOnLayer( Edge_Cuts ) )
298 aVector.push_back( drawing );
299 }
300 }
301
302 for( const PAD* p : m_board->GetPads() )
303 {
304 if( !p )
305 continue;
306
307 if( p->GetAttribute() != PAD_ATTRIB::NPTH )
308 continue;
309
310 PCB_SHAPE* s = new PCB_SHAPE( NULL, SHAPE_T::CIRCLE );
311 s->SetRadius( p->GetDrillSize().x / 2 );
312 s->SetPosition( p->GetPosition() );
313 aVector.push_back( s );
314 }
315}
316
318{
319 if( !m_board )
320 return -1;
321
322 std::vector<int> netcodes;
323
324 this->CollectNetCodes( netcodes );
325 double maxConstraint = GetMaxConstraint( netcodes );
326
327 if( maxConstraint <= 0 )
328 return 0;
329
330 SHAPE_POLY_SET outline;
331
332 if( !m_board->GetBoardPolygonOutlines( outline ) )
333 return -1;
334
335 const DRAWINGS drawings = m_board->Drawings();
336 CREEPAGE_GRAPH graph( *m_board );
337
338 if( ADVANCED_CFG::GetCfg().m_EnableCreepageSlot )
340 else
341 graph.m_minGrooveWidth = 0;
342
343 graph.m_boardOutline = &outline;
344
345 this->CollectBoardEdges( graph.m_boardEdge );
349
350 graph.GeneratePaths( maxConstraint, Edge_Cuts, false );
351
352 int beNodeSize = graph.m_nodes.size();
353 int beConnectionsSize = graph.m_connections.size();
354 bool prevTestChangedGraph = false;
355
356 size_t current = 0;
357 size_t total = ( netcodes.size() * ( netcodes.size() - 1 ) ) / 2 * m_board->GetCopperLayerCount();
358 LSET layers = m_board->GetLayerSet();
359
360 alg::for_all_pairs( netcodes.begin(), netcodes.end(),
361 [&]( int aNet1, int aNet2 )
362 {
363 if( aNet1 == aNet2 )
364 return;
365
366 for( auto it = layers.copper_layers_begin(); it != layers.copper_layers_end(); ++it )
367 {
368 PCB_LAYER_ID layer = *it;
369
370 reportProgress( current++, total );
371
372 if ( prevTestChangedGraph )
373 {
374 size_t vectorSize = graph.m_connections.size();
375
376 for( size_t i = beConnectionsSize; i < vectorSize; i++ )
377 {
378 // We need to remove the connection from its endpoints' lists.
379 graph.RemoveConnection( graph.m_connections[i], false );
380 }
381 graph.m_connections.resize( beConnectionsSize, nullptr );
382
383 vectorSize = graph.m_nodes.size();
384 graph.m_nodes.resize( beNodeSize, nullptr );
385 }
386
387 prevTestChangedGraph = testCreepage( graph, aNet1, aNet2, layer );
388 }
389 } );
390
391 return 1;
392}
393
394
395namespace detail
396{
398}
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
bool SetNetCode(int aNetCode, bool aNoAssert)
Set net using a net code.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:79
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition: board_item.h:283
bool GetBoardPolygonOutlines(SHAPE_POLY_SET &aOutlines, OUTLINE_ERROR_HANDLER *aErrorHandler=nullptr, bool aAllowUseArcsInPolygons=false, bool aIncludeNPTHAsOutlines=false)
Extract the board outlines and build a closed polygon from lines, arcs and circle items on edge cut l...
Definition: board.cpp:2583
const NETINFO_LIST & GetNetInfo() const
Definition: board.h:922
LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition: board.h:637
NETINFO_ITEM * FindNet(int aNetcode) const
Search for a net with the given netcode.
Definition: board.cpp:2078
const std::vector< PAD * > GetPads() const
Return a reference to a list of all the pads.
Definition: board.cpp:2691
int GetCopperLayerCount() const
Definition: board.cpp:846
const FOOTPRINTS & Footprints() const
Definition: board.h:358
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:1011
const DRAWINGS & Drawings() const
Definition: board.h:360
ecoord_type Distance(const Vec &aP) const
Definition: box2.h:797
void SetTarget(double aTarget)
double Solve(std::shared_ptr< GRAPH_NODE > &aFrom, std::shared_ptr< GRAPH_NODE > &aTo, std::vector< std::shared_ptr< GRAPH_CONNECTION > > &aResult)
std::vector< CREEP_SHAPE * > m_shapeCollection
void TransformCreepShapesToNodes(std::vector< CREEP_SHAPE * > &aShapes)
SHAPE_POLY_SET * m_boardOutline
void GeneratePaths(double aMaxWeight, PCB_LAYER_ID aLayer, bool aClearance)
std::vector< BOARD_ITEM * > m_boardEdge
std::vector< std::shared_ptr< GRAPH_NODE > > m_nodes
std::vector< std::shared_ptr< GRAPH_CONNECTION > > m_connections
std::shared_ptr< GRAPH_NODE > AddNetElements(int aNetCode, PCB_LAYER_ID aLayer, int aMaxCreepage)
wxString GetName() const
Definition: drc_rule.h:168
MINOPTMAX< int > & Value()
Definition: drc_rule.h:161
DRC_RULE * GetParentRule() const
Definition: drc_rule.h:164
BOARD * GetBoard() const
Definition: drc_engine.h:95
bool IsErrorLimitExceeded(int error_code)
DRC_CONSTRAINT EvalRules(DRC_CONSTRAINT_T aConstraintType, const BOARD_ITEM *a, const BOARD_ITEM *b, PCB_LAYER_ID aLayer, REPORTER *aReporter=nullptr)
Definition: drc_engine.cpp:706
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:393
DRC_CUSTOM_MARKER_HANDLER GetGraphicsHandler(const std::vector< PCB_SHAPE > &aShapes, const VECTOR2I &aStart, const VECTOR2I &aEnd, int aLength)
void CollectBoardEdges(std::vector< BOARD_ITEM * > &aVector)
virtual ~DRC_TEST_PROVIDER_CREEPAGE()=default
double GetMaxConstraint(const std::vector< int > &aNetCodes)
std::set< std::pair< const BOARD_ITEM *, const BOARD_ITEM * > > m_reportedPairs
virtual const wxString GetName() const override
virtual bool Run() override
Run this provider against the given PCB with configured options (if any).
void CollectNetCodes(std::vector< int > &aVector)
virtual bool reportPhase(const wxString &aStageName)
virtual void reportViolation(std::shared_ptr< DRC_ITEM > &item, const VECTOR2I &aMarkerPos, int aMarkerLayer, DRC_CUSTOM_MARKER_HANDLER *aCustomHandler=nullptr)
DRC_ENGINE * m_drcEngine
wxString formatMsg(const wxString &aFormatString, const wxString &aSource, double aConstraint, double aActual, EDA_DATA_TYPE aDataType=EDA_DATA_TYPE::DISTANCE)
EDA_ITEM * GetParent() const
Definition: eda_item.h:111
EDA_ITEM * m_parent
Owner.
Definition: eda_item.h:514
void SetRadius(int aX)
Definition: eda_shape.h:240
LSET is a set of PCB_LAYER_IDs.
Definition: lset.h:37
T Min() const
Definition: minoptmax.h:33
Handle the data for a net.
Definition: netinfo.h:56
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
const NETCODES_MAP & NetsByNetcode() const
Return the netcode map, at least for python.
Definition: netinfo.h:375
Definition: pad.h:54
void SetPosition(const VECTOR2I &aPos) override
Definition: pcb_shape.h:78
Represent a set of closed polygons.
The common library.
std::function< void(PCB_MARKER *aMarker)> DRC_CUSTOM_MARKER_HANDLER
Definition: drc_engine.h:68
@ DRCE_CREEPAGE
Definition: drc_item.h:44
@ CREEPAGE_CONSTRAINT
Definition: drc_rule.h:50
std::shared_ptr< GRAPH_NODE > FindInGraphNodes(std::shared_ptr< GRAPH_NODE > aNode, std::vector< std::shared_ptr< GRAPH_NODE > > &aGraph)
#define _(s)
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
@ Edge_Cuts
Definition: layer_ids.h:112
This file contains miscellaneous commonly used macros and functions.
void for_all_pairs(_InputIterator __first, _InputIterator __last, _Function __f)
Apply a function to every possible pair of elements of a sequence.
Definition: kicad_algo.h:84
static DRC_REGISTER_TEST_PROVIDER< DRC_TEST_PROVIDER_ANNULAR_WIDTH > dummy
static float distance(const SFVEC2UI &a, const SFVEC2UI &b)