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:
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{
78 m_board = m_drcEngine->GetBoard();
79 m_reportedPairs.clear();
80
81 if( !m_drcEngine->HasRulesForConstraintType( CREEPAGE_CONSTRAINT ) )
82 {
83 REPORT_AUX( wxT( "No creepage constraints found. Tests not run." ) );
84 return true; // continue with other tests
85 }
86
87 if( !m_drcEngine->IsErrorLimitExceeded( DRCE_CREEPAGE ) )
88 {
89 if( !reportPhase( _( "Checking creepage..." ) ) )
90 return false; // DRC cancelled
91
93 }
94
95 return !m_drcEngine->IsCancelled();
96}
97
98
99int DRC_TEST_PROVIDER_CREEPAGE::testCreepage( CREEPAGE_GRAPH& aGraph, int aNetCodeA, int aNetCodeB,
100 PCB_LAYER_ID aLayer )
101{
102 PCB_TRACK bci1( m_board );
103 PCB_TRACK bci2( m_board );
104 bci1.SetNetCode( aNetCodeA );
105 bci2.SetNetCode( aNetCodeB );
106 bci1.SetLayer( aLayer );
107 bci2.SetLayer( aLayer );
108
109 DRC_CONSTRAINT constraint;
110 constraint = m_drcEngine->EvalRules( CREEPAGE_CONSTRAINT, &bci1, &bci2, aLayer );
111 double creepageValue = constraint.Value().Min();
112 aGraph.SetTarget( creepageValue );
113
114 if( creepageValue <= 0 )
115 return 0;
116
117 // Let's make a quick "clearance test"
118 NETINFO_ITEM* netA = m_board->FindNet( aNetCodeA );
119 NETINFO_ITEM* netB = m_board->FindNet( aNetCodeB );
120
121 if ( !netA || !netB )
122 return 0;
123
124 if ( netA->GetBoundingBox().Distance( netB->GetBoundingBox() ) > creepageValue )
125 return 0;
126
127 std::shared_ptr<GRAPH_NODE> NetA = aGraph.AddNetElements( aNetCodeA, aLayer, creepageValue );
128 std::shared_ptr<GRAPH_NODE> NetB = aGraph.AddNetElements( aNetCodeB, aLayer, creepageValue );
129
130
131 aGraph.GeneratePaths( creepageValue, aLayer );
132
133 std::vector<std::shared_ptr<GRAPH_NODE>> temp_nodes;
134
135 std::copy_if( aGraph.m_nodes.begin(), aGraph.m_nodes.end(), std::back_inserter( temp_nodes ),
136 []( std::shared_ptr<GRAPH_NODE> aNode )
137 {
138 return !!aNode && aNode->m_parent && aNode->m_parent->IsConductive()
139 && aNode->m_connectDirectly && aNode->m_type == GRAPH_NODE::POINT;
140 } );
141
142 alg::for_all_pairs( temp_nodes.begin(), temp_nodes.end(),
143 [&]( std::shared_ptr<GRAPH_NODE> aN1, std::shared_ptr<GRAPH_NODE> aN2 )
144 {
145 if( aN1 == aN2 )
146 return;
147
148 if( !aN1 || !aN2 )
149 return;
150
151 if( !( aN1->m_parent ) || !( aN2->m_parent ) )
152 return;
153
154 if( ( aN1->m_parent ) != ( aN2->m_parent ) )
155 return;
156
157
158 if( aN1->m_parent->IsConductive() )
159 return;
160
161 if( aN1->m_connectDirectly || aN2->m_connectDirectly )
162 return;
163
164 // We are only looking for points on circles and arcs
165
166 if( aN1->m_type != GRAPH_NODE::POINT )
167 return;
168
169 if( aN2->m_type != GRAPH_NODE::POINT )
170 return;
171
172 aN1->m_parent->ConnectChildren( aN1, aN2, aGraph );
173 } );
174
175 std::vector<std::shared_ptr<GRAPH_CONNECTION>> shortestPath;
176 shortestPath.clear();
177 double distance = aGraph.Solve( NetA, NetB, shortestPath );
178
179 if( !shortestPath.empty() && ( shortestPath.size() >= 4 ) && ( distance - creepageValue < 0 ) )
180 {
181 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_CREEPAGE );
182 drcItem->SetErrorDetail( formatMsg( _( "(%s creepage %s; actual %s)" ),
183 constraint.GetName(),
184 creepageValue,
185 distance ) );
186 drcItem->SetViolatingRule( constraint.GetParentRule() );
187
188 std::shared_ptr<GRAPH_CONNECTION> gc1 = shortestPath[1];
189 std::shared_ptr<GRAPH_CONNECTION> gc2 = shortestPath[shortestPath.size() - 2];
190
191 if( gc1->n1 && gc2->n2 )
192 {
193 const BOARD_ITEM* item1 = gc1->n1->m_parent->GetParent();
194 const BOARD_ITEM* item2 = gc2->n2->m_parent->GetParent();
195
196 if( m_reportedPairs.insert( std::make_pair( item1, item2 ) ).second )
197 drcItem->SetItems( item1, item2 );
198 else
199 return 1;
200 }
201
202 VECTOR2I startPoint = gc1->m_path.a2;
203 VECTOR2I endPoint = gc2->m_path.a2;
204 std::vector<PCB_SHAPE> path;
205
206 for( const std::shared_ptr<GRAPH_CONNECTION>& gc : shortestPath )
207 gc->GetShapes( path );
208
209 reportViolation( drcItem, gc1->m_path.a2, aLayer,
210 [&]( PCB_MARKER* aMarker )
211 {
212 aMarker->SetPath( path, startPoint, endPoint );
213 } );
214 }
215
216 return 1;
217}
218
219
220double DRC_TEST_PROVIDER_CREEPAGE::GetMaxConstraint( const std::vector<int>& aNetCodes )
221{
222 double maxConstraint = 0;
223 DRC_CONSTRAINT constraint;
224
225 PCB_TRACK bci1( m_board );
226 PCB_TRACK bci2( m_board );
227
228 alg::for_all_pairs( aNetCodes.begin(), aNetCodes.end(),
229 [&]( int aNet1, int aNet2 )
230 {
231 if( aNet1 == aNet2 )
232 return;
233
234 bci1.SetNetCode( aNet1 );
235 bci2.SetNetCode( aNet2 );
236
237 for( PCB_LAYER_ID layer : LSET::AllCuMask( m_board->GetCopperLayerCount() ) )
238 {
239 bci1.SetLayer( layer );
240 bci2.SetLayer( layer );
241 constraint = m_drcEngine->EvalRules( CREEPAGE_CONSTRAINT, &bci1, &bci2, layer );
242 double value = constraint.Value().Min();
243 maxConstraint = value > maxConstraint ? value : maxConstraint;
244 }
245 } );
246
247 return maxConstraint;
248}
249
250
251void DRC_TEST_PROVIDER_CREEPAGE::CollectNetCodes( std::vector<int>& aVector )
252{
253 NETCODES_MAP nets = m_board->GetNetInfo().NetsByNetcode();
254
255 for( auto it = nets.begin(); it != nets.end(); it++ )
256 aVector.push_back( it->first );
257}
258
259
260void DRC_TEST_PROVIDER_CREEPAGE::CollectBoardEdges( std::vector<BOARD_ITEM*>& aVector )
261{
262 if( !m_board )
263 return;
264
265 for( BOARD_ITEM* drawing : m_board->Drawings() )
266 {
267 if( !drawing )
268 continue;
269
270 if( drawing->IsOnLayer( Edge_Cuts ) )
271 aVector.push_back( drawing );
272 }
273
274 for( FOOTPRINT* fp : m_board->Footprints() )
275 {
276 if( !fp )
277 continue;
278
279 for( BOARD_ITEM* drawing : fp->GraphicalItems() )
280 {
281 if( !drawing )
282 continue;
283
284 if( drawing->IsOnLayer( Edge_Cuts ) )
285 aVector.push_back( drawing );
286 }
287 }
288
289 for( const PAD* p : m_board->GetPads() )
290 {
291 if( !p )
292 continue;
293
294 if( p->GetAttribute() != PAD_ATTRIB::NPTH )
295 continue;
296
297 PCB_SHAPE* s = new PCB_SHAPE( NULL, SHAPE_T::CIRCLE );
298 s->SetRadius( p->GetDrillSize().x / 2 );
299 s->SetPosition( p->GetPosition() );
300 aVector.push_back( s );
301 }
302}
303
304
306{
307 if( !m_board )
308 return -1;
309
310 std::vector<int> netcodes;
311
312 this->CollectNetCodes( netcodes );
313 double maxConstraint = GetMaxConstraint( netcodes );
314
315 if( maxConstraint <= 0 )
316 return 0;
317
318 SHAPE_POLY_SET outline;
319
320 if( !m_board->GetBoardPolygonOutlines( outline, false ) )
321 return -1;
322
323 const DRAWINGS drawings = m_board->Drawings();
324 CREEPAGE_GRAPH graph( *m_board );
325
326 if( ADVANCED_CFG::GetCfg().m_EnableCreepageSlot )
327 graph.m_minGrooveWidth = m_board->GetDesignSettings().m_MinGrooveWidth;
328 else
329 graph.m_minGrooveWidth = 0;
330
331 graph.m_boardOutline = &outline;
332
333 this->CollectBoardEdges( graph.m_boardEdge );
337
338 graph.GeneratePaths( maxConstraint, Edge_Cuts );
339
340 int beNodeSize = graph.m_nodes.size();
341 int beConnectionsSize = graph.m_connections.size();
342 bool prevTestChangedGraph = false;
343
344 size_t current = 0;
345 size_t total = ( netcodes.size() * ( netcodes.size() - 1 ) ) / 2 * m_board->GetCopperLayerCount();
346 LSET layers = m_board->GetLayerSet();
347
348 alg::for_all_pairs( netcodes.begin(), netcodes.end(),
349 [&]( int aNet1, int aNet2 )
350 {
351 if( aNet1 == aNet2 )
352 return;
353
354 for( auto it = layers.copper_layers_begin(); it != layers.copper_layers_end(); ++it )
355 {
356 PCB_LAYER_ID layer = *it;
357
358 reportProgress( current++, total );
359
360 if( prevTestChangedGraph )
361 {
362 size_t vectorSize = graph.m_connections.size();
363
364 for( size_t i = beConnectionsSize; i < vectorSize; i++ )
365 {
366 // We need to remove the connection from its endpoints' lists.
367 graph.RemoveConnection( graph.m_connections[i], false );
368 }
369
370 graph.m_connections.resize( beConnectionsSize, nullptr );
371
372 vectorSize = graph.m_nodes.size();
373 graph.m_nodes.resize( beNodeSize, nullptr );
374 }
375
376 prevTestChangedGraph = testCreepage( graph, aNet1, aNet2, layer );
377 }
378 } );
379
380 return 1;
381}
382
383
384namespace detail
385{
387}
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.
void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
ecoord_type Distance(const Vec &aP) const
Definition box2.h:797
A graph with nodes and connections for creepage calculation.
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
std::vector< BOARD_ITEM * > m_boardEdge
void GeneratePaths(double aMaxWeight, PCB_LAYER_ID aLayer)
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:195
MINOPTMAX< int > & Value()
Definition drc_rule.h:188
DRC_RULE * GetParentRule() const
Definition drc_rule.h:191
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Definition drc_item.cpp:400
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)
void reportViolation(std::shared_ptr< DRC_ITEM > &item, const VECTOR2I &aMarkerPos, int aMarkerLayer, const std::function< void(PCB_MARKER *)> &aPathGenerator=[](PCB_MARKER *){})
wxString formatMsg(const wxString &aFormatString, const wxString &aSource, double aConstraint, double aActual, EDA_DATA_TYPE aDataType=EDA_DATA_TYPE::DISTANCE)
void SetRadius(int aX)
Definition eda_shape.h:241
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:54
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition pad.h:55
void SetPosition(const VECTOR2I &aPos) override
Definition pcb_shape.h:78
Represent a set of closed polygons.
The common library.
@ DRCE_CREEPAGE
Definition drc_item.h:45
@ CREEPAGE_CONSTRAINT
Definition drc_rule.h:50
#define REPORT_AUX(s)
#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
@ NPTH
like PAD_PTH, but not plated mechanical use only, no connection allowed
Definition padstack.h:103
static float distance(const SFVEC2UI &a, const SFVEC2UI &b)
std::string path
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695