KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_drc_creepage_issue24543.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
41
44
45#include <board.h>
47#include <layer_ids.h>
48#include <drc/drc_item.h>
49#include <drc/drc_engine.h>
50#include <footprint.h>
51#include <pad.h>
52#include <pcb_marker.h>
55
59
60
62{
64
66 {
67 if( m_board && m_board->GetDesignSettings().m_DRCEngine )
68 m_board->GetDesignSettings().m_DRCEngine->ClearViolationHandler();
69
70 if( m_board )
71 {
72 m_board->SetProject( nullptr );
73 m_board = nullptr;
74 }
75 }
76
78 std::unique_ptr<BOARD> m_board;
79};
80
81
83{
84 KI_TEST::LoadBoard( m_settingsManager, "issue24543/issue24543", m_board );
85
86 BOOST_REQUIRE_MESSAGE( m_board, "Failed to load board issue24543" );
87
88 struct ViolationInfo
89 {
90 std::shared_ptr<DRC_ITEM> item;
91 VECTOR2I pos;
92 std::vector<PCB_SHAPE> pathShapes;
93 int layer = 0;
94 };
95
96 std::vector<ViolationInfo> violations;
97 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
98
99 BOOST_REQUIRE_MESSAGE( bds.m_DRCEngine, "DRC engine not initialized" );
100
101 for( int ii = DRCE_FIRST; ii <= DRCE_LAST; ++ii )
103
105
107 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos, int aLayer,
108 const std::function<void( PCB_MARKER* )>& aPathGenerator )
109 {
110 if( bds.GetSeverity( aItem->GetErrorCode() ) != SEVERITY::RPT_SEVERITY_ERROR )
111 return;
112
113 ViolationInfo vi;
114 vi.item = aItem;
115 vi.pos = aPos;
116 vi.layer = aLayer;
117
118 if( aPathGenerator )
119 {
120 PCB_MARKER marker( aItem, aPos, aLayer );
121 aPathGenerator( &marker );
122 vi.pathShapes = marker.GetPath();
123 }
124
125 violations.push_back( vi );
126 } );
127
128 bds.m_DRCEngine->RunTests( EDA_UNITS::MM, true, false );
129
131
132 BOOST_TEST_MESSAGE( wxString::Format( "Found %d creepage violations",
133 (int) violations.size() ) );
134
135 // Locate the C4 pads to drive the geometric assertions.
136 PAD* pad1 = nullptr;
137 PAD* pad2 = nullptr;
138
139 for( FOOTPRINT* fp : m_board->Footprints() )
140 {
141 if( fp->GetReference() != wxT( "C4" ) )
142 continue;
143
144 for( PAD* p : fp->Pads() )
145 {
146 if( p->GetNumber() == wxT( "1" ) )
147 pad1 = p;
148 else if( p->GetNumber() == wxT( "2" ) )
149 pad2 = p;
150 }
151 }
152
153 BOOST_REQUIRE_MESSAGE( pad1 && pad2, "C4 pads 1 and 2 not found in board" );
154
155 // The bug is specific to a non-orthogonal rotation, so confirm at least one C4 pad
156 // is rotated off-axis. If neither were, GetEffectiveShape() would emit a SHAPE_RECT
157 // and the original (axis-aligned) path would already be correct.
158 auto isOrthogonal =
159 []( const PAD* aPad )
160 {
161 double deg = aPad->GetOrientation().Normalize().AsDegrees();
162 double mod = std::fmod( deg, 90.0 );
163 return mod < 0.01 || mod > 89.99;
164 };
165
166 BOOST_REQUIRE_MESSAGE( !isOrthogonal( pad1 ) || !isOrthogonal( pad2 ),
167 "Expected at least one C4 pad to be rotated off-axis" );
168
169 const ViolationInfo* c4Violation = nullptr;
170
171 for( const ViolationInfo& vi : violations )
172 {
173 if( vi.layer != F_Cu )
174 continue;
175
176 const KIID idA = vi.item->GetMainItemID();
177 const KIID idB = vi.item->GetAuxItemID();
178 const bool matchA = ( idA == pad1->m_Uuid || idA == pad2->m_Uuid );
179 const bool matchB = ( idB == pad1->m_Uuid || idB == pad2->m_Uuid );
180
181 if( matchA && matchB && idA != idB )
182 {
183 c4Violation = &vi;
184 break;
185 }
186 }
187
188 BOOST_REQUIRE_MESSAGE( c4Violation,
189 "No F.Cu creepage violation reported between C4 pad1 and pad2" );
190
191 BOOST_REQUIRE_GE( c4Violation->pathShapes.size(), 1u );
192
193 // Collect every endpoint of the reported creepage path.
194 std::vector<VECTOR2I> endpoints;
195
196 for( const PCB_SHAPE& s : c4Violation->pathShapes )
197 {
198 endpoints.push_back( s.GetStart() );
199 endpoints.push_back( s.GetEnd() );
200 }
201
202 const std::shared_ptr<SHAPE_POLY_SET>& poly1 = pad1->GetEffectivePolygon( F_Cu );
203 const std::shared_ptr<SHAPE_POLY_SET>& poly2 = pad2->GetEffectivePolygon( F_Cu );
204
205 BOOST_REQUIRE( poly1 && poly2 );
206 BOOST_REQUIRE( poly1->OutlineCount() > 0 && poly2->OutlineCount() > 0 );
207
208 // Distance from a point to the copper edge (outline), not the solid interior. A creepage
209 // anchor sits on the copper boundary, so the outline distance is ~0 there. We measure
210 // against the outline (rather than SHAPE_POLY_SET::SquaredDistance with aOutlineOnly,
211 // which is unimplemented) so a point inside the copper annulus does not score as 0.
212 const SHAPE_LINE_CHAIN& edge1 = poly1->COutline( 0 );
213 const SHAPE_LINE_CHAIN& edge2 = poly2->COutline( 0 );
214
215 // Distance of a point from the nearest NPTH hole centre. The footprint slot is an oval
216 // hole centred between the two pads; a path endpoint that snapped to "the hole" sits
217 // near the hole boundary, well inside the pad-to-pad gap and far from any pad copper.
218 auto distToNearestHoleMM =
219 [&]( const VECTOR2I& aPt ) -> double
220 {
221 double best = std::numeric_limits<double>::max();
222
223 for( const PAD* p : m_board->GetPads() )
224 {
225 if( p->GetAttribute() != PAD_ATTRIB::NPTH )
226 continue;
227
228 std::shared_ptr<SHAPE_SEGMENT> hole = p->GetEffectiveHoleShape();
229
230 if( !hole )
231 continue;
232
233 int r = hole->GetWidth() / 2;
234 SEG::ecoord d = hole->GetSeg().SquaredDistance( aPt );
235 double edgeDist = std::abs( std::sqrt( (double) d ) - r ) / 1e6;
236 best = std::min( best, edgeDist );
237 }
238
239 return best;
240 };
241
242 // Examine the two extreme endpoints (closest to each pad). The reported creepage path
243 // should anchor on the pad copper edges. Allow a generous tolerance for the rotated-pad
244 // outline approximation but flag the failure mode where an endpoint sits at the hole.
245 double closestToPad1 = std::numeric_limits<double>::max();
246 double closestToPad2 = std::numeric_limits<double>::max();
247 VECTOR2I anchor1, anchor2;
248
249 for( const VECTOR2I& pt : endpoints )
250 {
251 double d1 = std::sqrt( (double) edge1.SquaredDistance( pt ) ) / 1e6;
252 double d2 = std::sqrt( (double) edge2.SquaredDistance( pt ) ) / 1e6;
253
254 if( d1 < closestToPad1 )
255 {
256 closestToPad1 = d1;
257 anchor1 = pt;
258 }
259
260 if( d2 < closestToPad2 )
261 {
262 closestToPad2 = d2;
263 anchor2 = pt;
264 }
265 }
266
267 BOOST_TEST_MESSAGE( wxString::Format(
268 "Path anchor nearest pad1 at (%.4f,%.4f) mm, dist-to-copper %.4f mm, "
269 "dist-to-hole %.4f mm",
270 anchor1.x / 1e6, anchor1.y / 1e6, closestToPad1,
271 distToNearestHoleMM( anchor1 ) ) );
272 BOOST_TEST_MESSAGE( wxString::Format(
273 "Path anchor nearest pad2 at (%.4f,%.4f) mm, dist-to-copper %.4f mm, "
274 "dist-to-hole %.4f mm",
275 anchor2.x / 1e6, anchor2.y / 1e6, closestToPad2,
276 distToNearestHoleMM( anchor2 ) ) );
277
278 // Core assertion: the creepage path must terminate on each pad's copper outline.
279 // Before the fix the rotated pad contributes no copper anchor, so the path endpoint
280 // nearest that pad lands on the NPTH hole boundary instead - tens of mm of copper
281 // distance away, but right on the hole.
282 BOOST_CHECK_MESSAGE( closestToPad1 < 0.05,
283 wxString::Format( "Creepage path endpoint nearest C4 pad1 is %.4f mm from the pad "
284 "copper outline; it should anchor on the copper. dist-to-hole=%.4f mm",
285 closestToPad1, distToNearestHoleMM( anchor1 ) ) );
286
287 BOOST_CHECK_MESSAGE( closestToPad2 < 0.05,
288 wxString::Format( "Creepage path endpoint nearest C4 pad2 is %.4f mm from the pad "
289 "copper outline; it should anchor on the copper. dist-to-hole=%.4f mm",
290 closestToPad2, distToNearestHoleMM( anchor2 ) ) );
291
292 // The path must reach the copper of BOTH pads. The #24543 signature is that the rotated
293 // pad contributes no copper anchor at all, so its endpoint snaps onto the NPTH hole edge
294 // (right on the hole, far from any copper). Anchoring around the hole on intermediate
295 // vertices is legitimate; what is not legitimate is an anchor sitting on the hole instead
296 // of the copper. Verify each pad anchor is on copper and not pinned to the hole.
297 BOOST_CHECK_MESSAGE( distToNearestHoleMM( anchor1 ) > 0.1 || closestToPad1 < 0.05,
298 wxString::Format( "Creepage path anchor for C4 pad1 is pinned to the NPTH hole edge "
299 "(%.4f mm) instead of the pad copper (%.4f mm).",
300 distToNearestHoleMM( anchor1 ), closestToPad1 ) );
301
302 BOOST_CHECK_MESSAGE( distToNearestHoleMM( anchor2 ) > 0.1 || closestToPad2 < 0.05,
303 wxString::Format( "Creepage path anchor for C4 pad2 is pinned to the NPTH hole edge "
304 "(%.4f mm) instead of the pad copper (%.4f mm).",
305 distToNearestHoleMM( anchor2 ), closestToPad2 ) );
306}
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)
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
const KIID m_Uuid
Definition eda_item.h:531
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition eda_shape.h:232
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition eda_shape.h:190
Definition kiid.h:44
Definition pad.h:61
const std::shared_ptr< SHAPE_POLY_SET > & GetEffectivePolygon(PCB_LAYER_ID aLayer, ERROR_LOC aErrorLoc=ERROR_INSIDE) const
Definition pad.cpp:948
const std::vector< PCB_SHAPE > & GetPath() const
Definition pcb_marker.h:158
VECTOR2I::extended_type ecoord
Definition seg.h:40
SEG::ecoord SquaredDistance(const VECTOR2I &aP, bool aOutlineOnly=false) const override
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
@ DRCE_CREEPAGE
Definition drc_item.h:41
@ DRCE_FIRST
Definition drc_item.h:35
@ DRCE_LAST
Definition drc_item.h:120
@ F_Cu
Definition layer_ids.h:60
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
@ NPTH
like PAD_PTH, but not plated mechanical use only, no connection allowed
Definition padstack.h:103
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_IGNORE
BOOST_FIXTURE_TEST_CASE(CreepageRotatedRectPadIssue24543, DRC_CREEPAGE_ROTATED_PAD_FIXTURE)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
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))
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683