KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_drc_creepage_issue23576.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 The KiCad Developers, see AUTHORS.txt for contributors.
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, see <https://www.gnu.org/licenses/>.
18 */
19
30
33
34#include <board.h>
36#include <drc/drc_item.h>
37#include <drc/drc_engine.h>
38#include <footprint.h>
39#include <pcb_marker.h>
40#include <pcb_track.h>
41#include <pad.h>
44
45
47{
49
51 {
52 if( m_board && m_board->GetDesignSettings().m_DRCEngine )
53 m_board->GetDesignSettings().m_DRCEngine->ClearViolationHandler();
54
55 if( m_board )
56 {
57 m_board->SetProject( nullptr );
58 m_board = nullptr;
59 }
60 }
61
63 std::unique_ptr<BOARD> m_board;
64};
65
66
67BOOST_FIXTURE_TEST_CASE( CreepagePathStartPointIssue23576, DRC_CREEPAGE_PATH_FIXTURE )
68{
69 KI_TEST::LoadBoard( m_settingsManager, "issue23389/issue23389", m_board );
70
71 BOOST_REQUIRE_MESSAGE( m_board, "Failed to load board issue23389" );
72
73 struct ViolationInfo
74 {
75 std::shared_ptr<DRC_ITEM> item;
76 VECTOR2I pos;
77 std::vector<PCB_SHAPE> pathShapes;
78 };
79
80 std::vector<ViolationInfo> violations;
81 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
82
83 BOOST_REQUIRE_MESSAGE( bds.m_DRCEngine, "DRC engine not initialized" );
84
85 for( int ii = DRCE_FIRST; ii <= DRCE_LAST; ++ii )
87
89
91 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos, int aLayer,
92 const std::function<void( PCB_MARKER* )>& aPathGenerator )
93 {
94 if( bds.GetSeverity( aItem->GetErrorCode() ) != SEVERITY::RPT_SEVERITY_ERROR )
95 return;
96
97 ViolationInfo vi;
98 vi.item = aItem;
99 vi.pos = aPos;
100
101 if( aPathGenerator )
102 {
103 PCB_MARKER marker( aItem, aPos, aLayer );
104 aPathGenerator( &marker );
105 vi.pathShapes = marker.GetShapes();
106 }
107
108 violations.push_back( vi );
109 } );
110
111 bds.m_DRCEngine->RunTests( EDA_UNITS::MM, true, false );
112
114
115 BOOST_REQUIRE_GE( violations.size(), 1u );
116
117 // Build a lookup of board items by UUID for resolving DRC item references
118 std::map<KIID, BOARD_ITEM*> itemMap;
119
120 for( PCB_TRACK* track : m_board->Tracks() )
121 itemMap[track->m_Uuid] = track;
122
123 for( FOOTPRINT* fp : m_board->Footprints() )
124 {
125 itemMap[fp->m_Uuid] = fp;
126
127 for( PAD* pad : fp->Pads() )
128 itemMap[pad->m_Uuid] = pad;
129 }
130
131 for( size_t i = 0; i < violations.size(); i++ )
132 {
133 const ViolationInfo& vi = violations[i];
134
135 BOOST_TEST_MESSAGE( wxString::Format( "Violation %zu: %s", i,
136 vi.item->GetErrorMessage( false ) ) );
137 BOOST_TEST_MESSAGE( wxString::Format( " Pos: (%.4f, %.4f) mm, shapes: %d",
138 vi.pos.x / 1e6, vi.pos.y / 1e6,
139 (int) vi.pathShapes.size() ) );
140
141 for( size_t j = 0; j < vi.pathShapes.size(); j++ )
142 {
143 const PCB_SHAPE& s = vi.pathShapes[j];
144
145 if( s.GetShape() == SHAPE_T::SEGMENT )
146 {
147 BOOST_TEST_MESSAGE( wxString::Format(
148 " [%zu] SEG: (%.4f,%.4f)->(%.4f,%.4f) mm", j,
149 s.GetStart().x / 1e6, s.GetStart().y / 1e6,
150 s.GetEnd().x / 1e6, s.GetEnd().y / 1e6 ) );
151 }
152 else if( s.GetShape() == SHAPE_T::ARC )
153 {
154 BOOST_TEST_MESSAGE( wxString::Format(
155 " [%zu] ARC: (%.4f,%.4f)->(%.4f,%.4f) c=(%.4f,%.4f) mm", j,
156 s.GetStart().x / 1e6, s.GetStart().y / 1e6,
157 s.GetEnd().x / 1e6, s.GetEnd().y / 1e6,
158 s.GetCenter().x / 1e6, s.GetCenter().y / 1e6 ) );
159 }
160 }
161
162 if( vi.pathShapes.empty() )
163 continue;
164
165 // Resolve the items from the violation
166 BOARD_ITEM* itemA = nullptr;
167 BOARD_ITEM* itemB = nullptr;
168
169 KIID idA = vi.item->GetMainItemID();
170 KIID idB = vi.item->GetAuxItemID();
171
172 auto itA = itemMap.find( idA );
173 auto itB = itemMap.find( idB );
174
175 if( itA != itemMap.end() )
176 itemA = itA->second;
177
178 if( itB != itemMap.end() )
179 itemB = itB->second;
180
181 if( itemA )
182 {
183 BOOST_TEST_MESSAGE( wxString::Format( " ItemA: %s at (%.4f, %.4f) mm",
184 itemA->GetClass(),
185 itemA->GetPosition().x / 1e6, itemA->GetPosition().y / 1e6 ) );
186 }
187
188 if( itemB )
189 {
190 BOOST_TEST_MESSAGE( wxString::Format( " ItemB: %s at (%.4f, %.4f) mm",
191 itemB->GetClass(),
192 itemB->GetPosition().x / 1e6, itemB->GetPosition().y / 1e6 ) );
193 }
194
195 // The reported violation position (vi.pos) is m_pathStart — the arrow tip the user
196 // sees. For a track-to-track or pad-to-track violation, this point must lie on the
197 // outer copper boundary of the net, not inside overlapping copper from an adjacent
198 // track/pad that shares an endpoint. We validate two conditions:
199 //
200 // (a) The point is at least halfWidth from every track's centerline of the same
201 // net as itemA (or itemB respectively). Less than halfWidth would mean the
202 // point is strictly inside that track's copper body.
203 // (b) For the track that the path originates from, the point is at halfWidth
204 // from its centerline (on the copper edge).
205
206 auto validateOnOuterBoundary =
207 [&]( const BOARD_ITEM* anchor, const VECTOR2I& pt, const char* label )
208 {
209 if( !anchor || anchor->Type() != PCB_TRACE_T )
210 return;
211
212 const PCB_TRACK* anchorTrack = static_cast<const PCB_TRACK*>( anchor );
213 int anchorHalfWidth = anchorTrack->GetWidth() / 2;
214 int netCode = anchorTrack->GetNetCode();
215 int tolerance = 10000; // 10um
216
217 SEG anchorSeg( anchorTrack->GetStart(), anchorTrack->GetEnd() );
218 int distToAnchor = anchorSeg.Distance( pt );
219
220 BOOST_TEST_MESSAGE( wxString::Format(
221 " %s at (%.4f, %.4f): dist to anchor centerline=%d (hw=%d)",
222 label, pt.x / 1e6, pt.y / 1e6, distToAnchor, anchorHalfWidth ) );
223
225 std::abs( distToAnchor - anchorHalfWidth ) <= tolerance,
226 wxString::Format( "Violation %zu %s: path endpoint is %d nm from "
227 "anchor track centerline, expected %d nm (halfWidth)",
228 i, label, distToAnchor, anchorHalfWidth ) );
229
230 // Ensure the point is not inside any other same-net track's copper body.
231 for( PCB_TRACK* other : m_board->Tracks() )
232 {
233 if( other == anchorTrack )
234 continue;
235
236 if( other->GetNetCode() != netCode )
237 continue;
238
239 if( !other->IsOnLayer( anchorTrack->GetLayer() ) )
240 continue;
241
242 SEG otherSeg( other->GetStart(), other->GetEnd() );
243 int otherHalfWidth = other->GetWidth() / 2;
244 int distToOther = otherSeg.Distance( pt );
245
246 // A point strictly inside another same-net track's copper body
247 // (distance less than halfWidth by more than tolerance) means the
248 // creepage path starts at an "interior" point that isn't a physical
249 // copper edge once adjacent segments are accounted for.
251 distToOther >= otherHalfWidth - tolerance,
252 wxString::Format(
253 "Violation %zu %s: path endpoint (%.4f, %.4f) is "
254 "%d nm inside adjacent same-net track "
255 "(%.4f,%.4f)->(%.4f,%.4f) hw=%d. The path starts "
256 "at an interior point, not a physical copper edge.",
257 i, label,
258 pt.x / 1e6, pt.y / 1e6,
259 otherHalfWidth - distToOther,
260 other->GetStart().x / 1e6,
261 other->GetStart().y / 1e6,
262 other->GetEnd().x / 1e6,
263 other->GetEnd().y / 1e6,
264 otherHalfWidth ) );
265 }
266 };
267
268 validateOnOuterBoundary( itemA, vi.pos, "startPoint" );
269
270 if( itemB && itemB->Type() == PCB_TRACE_T )
271 {
272 const PCB_TRACK* trackB = static_cast<const PCB_TRACK*>( itemB );
273 SEG segB( trackB->GetStart(), trackB->GetEnd() );
274
275 // The path vertex closest to trackB is taken as the path end point.
276 int bestDist = std::numeric_limits<int>::max();
277 VECTOR2I endPt;
278
279 for( const PCB_SHAPE& s : vi.pathShapes )
280 {
281 int d = segB.Distance( s.GetStart() );
282
283 if( d < bestDist )
284 {
285 bestDist = d;
286 endPt = s.GetStart();
287 }
288
289 d = segB.Distance( s.GetEnd() );
290
291 if( d < bestDist )
292 {
293 bestDist = d;
294 endPt = s.GetEnd();
295 }
296 }
297
298 validateOnOuterBoundary( itemB, endPt, "endPoint" );
299 }
300 }
301}
PCB_LAYER_ID GetLayer() const override
Return the primary layer this item is on.
Container for design settings for a BOARD object.
std::map< int, SEVERITY > m_DRCSeverities
std::shared_ptr< DRC_ENGINE > m_DRCEngine
SEVERITY GetSeverity(int aDRCErrorCode)
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:81
void RunTests(EDA_UNITS aUnits, bool aReportAllTrackErrors, bool aTestFootprints, BOARD_COMMIT *aCommit=nullptr)
Run the DRC tests.
void SetViolationHandler(DRC_VIOLATION_HANDLER aHandler)
Set an optional DRC violation handler (receives DRC_ITEMs and positions).
Definition drc_engine.h:164
void ClearViolationHandler()
Definition drc_engine.h:169
virtual VECTOR2I GetPosition() const
Definition eda_item.h:282
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:108
SHAPE_T GetShape() const
Definition eda_shape.h:185
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition eda_shape.h:240
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition eda_shape.h:190
virtual wxString GetClass() const =0
Return the class name.
Definition kiid.h:44
Definition pad.h:61
std::vector< PCB_SHAPE > GetShapes() const
VECTOR2I GetCenter() const override
This defaults to the center of the bounding box if not overridden.
Definition pcb_shape.h:78
const VECTOR2I & GetStart() const
Definition pcb_track.h:93
const VECTOR2I & GetEnd() const
Definition pcb_track.h:90
virtual int GetWidth() const
Definition pcb_track.h:87
Definition seg.h:38
int Distance(const SEG &aSeg) const
Compute minimum Euclidean distance to segment aSeg.
Definition seg.cpp:698
@ DRCE_CREEPAGE
Definition drc_item.h:41
@ DRCE_FIRST
Definition drc_item.h:35
@ DRCE_LAST
Definition drc_item.h:121
@ SEGMENT
Definition eda_shape.h:46
void LoadBoard(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< BOARD > &aBoard)
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:400
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_IGNORE
DRC_CREEPAGE_PATH_FIXTURE()=default
BOOST_FIXTURE_TEST_CASE(CreepagePathStartPointIssue23576, DRC_CREEPAGE_PATH_FIXTURE)
BOOST_CHECK_MESSAGE(totalMismatches==0, std::to_string(totalMismatches)+" board(s) with strategy disagreements")
BOOST_TEST_MESSAGE("\n=== Real-World Polygon PIP Benchmark ===\n"<< formatTable(table))
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:89
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683