KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_pns_basics.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, 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
26#include <optional>
27
28#include <pcbnew/board.h>
29#include <pcbnew/pad.h>
30#include <pcbnew/pcb_track.h>
32
35#include <router/pns_item.h>
37#include <router/pns_node.h>
38#include <router/pns_router.h>
39#include <router/pns_segment.h>
40#include <router/pns_solid.h>
41#include <router/pns_via.h>
42
43static bool isCopper( const PNS::ITEM* aItem )
44{
45 if( !aItem )
46 return false;
47
48 BOARD_ITEM* parent = aItem->Parent();
49
50 if( parent && parent->Type() == PCB_PAD_T )
51 {
52 PAD* pad = static_cast<PAD*>( parent );
53
54 if( pad->IsAperturePad() || pad->IsNPTHWithNoCopper() )
55 return false;
56 }
57
58 return true;
59}
60
61
62static bool isHole( const PNS::ITEM* aItem )
63{
64 if( !aItem )
65 return false;
66
67 return aItem->OfKind( PNS::ITEM::HOLE_T );
68}
69
70
71static bool isEdge( const PNS::ITEM* aItem )
72{
73 if( !aItem )
74 return false;
75
76 const BOARD_ITEM *parent = aItem->BoardItem();
77
78 return parent && ( parent->IsOnLayer( Edge_Cuts ) || parent->IsOnLayer( Margin ) );
79}
80
81
83{
84public:
88
90
91 virtual int Clearance( const PNS::ITEM* aA, const PNS::ITEM* aB,
92 bool aUseClearanceEpsilon = true ) override
93 {
94 PNS::CONSTRAINT constraint;
95 int rv = 0;
96 PNS_LAYER_RANGE layers;
97
98 if( !aB )
99 layers = aA->Layers();
100 else if( isEdge( aA ) )
101 layers = aB->Layers();
102 else if( isEdge( aB ) )
103 layers = aA->Layers();
104 else
105 layers = aA->Layers().Intersection( aB->Layers() );
106
107 // Normalize layer range (no -1 magic numbers)
109
110 for( int layer = layers.Start(); layer <= layers.End(); ++layer )
111 {
112 if( isHole( aA ) && isHole( aB) )
113 {
114 if( QueryConstraint( PNS::CONSTRAINT_TYPE::CT_HOLE_TO_HOLE, aA, aB, layer, &constraint ) )
115 {
116 if( constraint.m_Value.Min() > rv )
117 rv = constraint.m_Value.Min();
118 }
119 }
120 else if( isHole( aA ) || isHole( aB ) )
121 {
122 if( QueryConstraint( PNS::CONSTRAINT_TYPE::CT_HOLE_CLEARANCE, aA, aB, layer, &constraint ) )
123 {
124 if( constraint.m_Value.Min() > rv )
125 rv = constraint.m_Value.Min();
126 }
127 }
128 else if( isCopper( aA ) && ( !aB || isCopper( aB ) ) )
129 {
130 if( QueryConstraint( PNS::CONSTRAINT_TYPE::CT_CLEARANCE, aA, aB, layer, &constraint ) )
131 {
132 if( constraint.m_Value.Min() > rv )
133 rv = constraint.m_Value.Min();
134 }
135 }
136 else if( isEdge( aA ) || ( aB && isEdge( aB ) ) )
137 {
138 if( QueryConstraint( PNS::CONSTRAINT_TYPE::CT_EDGE_CLEARANCE, aA, aB, layer, &constraint ) )
139 {
140 if( constraint.m_Value.Min() > rv )
141 rv = constraint.m_Value.Min();
142 }
143 }
144 }
145
146 return rv;
147 }
148
149 virtual PNS::NET_HANDLE DpCoupledNet( PNS::NET_HANDLE aNet ) override { return nullptr; }
150 virtual int DpNetPolarity( PNS::NET_HANDLE aNet ) override { return -1; }
151
152 virtual bool DpNetPair( const PNS::ITEM* aItem, PNS::NET_HANDLE& aNetP,
153 PNS::NET_HANDLE& aNetN ) override
154 {
155 return false;
156 }
157
158 virtual int NetCode( PNS::NET_HANDLE aNet ) override
159 {
160 return -1;
161 }
162
163 virtual wxString NetName( PNS::NET_HANDLE aNet ) override
164 {
165 return wxEmptyString;
166 }
167
168 virtual bool QueryConstraint( PNS::CONSTRAINT_TYPE aType, const PNS::ITEM* aItemA,
169 const PNS::ITEM* aItemB, int aLayer,
170 PNS::CONSTRAINT* aConstraint ) override
171 {
172 ITEM_KEY key;
173
174 key.a = aItemA;
175 key.b = aItemB;
176 key.type = aType;
177
178 auto it = m_ruleMap.find( key );
179
180 if( it == m_ruleMap.end() )
181 {
182 int cl;
183 switch( aType )
184 {
188 default: return false;
189 }
190
191 //printf("GetDef %s %s %d cl %d\n", aItemA->KindStr().c_str(), aItemB->KindStr().c_str(), aType, cl );
192
193 aConstraint->m_Type = aType;
194 aConstraint->m_Value.SetMin( cl );
195
196 return true;
197 }
198 else
199 {
200 *aConstraint = it->second;
201 }
202
203 return true;
204 }
205
206 int ClearanceEpsilon() const override { return m_clearanceEpsilon; }
207
208 struct ITEM_KEY
209 {
210 const PNS::ITEM* a = nullptr;
211 const PNS::ITEM* b = nullptr;
213
214 bool operator==( const ITEM_KEY& other ) const
215 {
216 return a == other.a && b == other.b && type == other.type;
217 }
218
219 bool operator<( const ITEM_KEY& other ) const
220 {
221 if( a < other.a )
222 {
223 return true;
224 }
225 else if ( a == other.a )
226 {
227 if( b < other.b )
228 return true;
229 else if ( b == other.b )
230 return type < other.type;
231 }
232
233 return false;
234 }
235 };
236
237 bool IsInNetTie( const PNS::ITEM* aA ) override { return false; }
238
239 bool IsNetTieExclusion( const PNS::ITEM* aItem, const VECTOR2I& aCollisionPos,
240 const PNS::ITEM* aCollidingItem ) override
241 {
242 return false;
243 }
244
245 bool IsDrilledHole( const PNS::ITEM* aItem ) override { return false; }
246
247 bool IsNonPlatedSlot( const PNS::ITEM* aItem ) override { return false; }
248
249 bool IsKeepout( const PNS::ITEM* aObstacle, const PNS::ITEM* aItem, bool* aEnforce ) override
250 {
251 return false;
252 }
253
254 void AddMockRule( PNS::CONSTRAINT_TYPE aType, const PNS::ITEM* aItemA, const PNS::ITEM* aItemB,
255 PNS::CONSTRAINT& aConstraint )
256 {
257 ITEM_KEY key;
258
259 key.a = aItemA;
260 key.b = aItemB;
261 key.type = aType;
262
263 m_ruleMap[key] = aConstraint;
264 }
265
266 int m_defaultClearance = 200000;
267 int m_defaultHole2Hole = 220000;
269
270private:
271 std::map<ITEM_KEY, PNS::CONSTRAINT> m_ruleMap;
273};
274
275struct PNS_TEST_FIXTURE;
276
278{
279public:
281 m_testFixture( aFixture )
282 {}
283
285
286 void HideItem( PNS::ITEM* aItem ) override {};
287 void DisplayItem( const PNS::ITEM* aItem, int aClearance, bool aEdit = false,
288 int aFlags = 0 ) override {};
290
291 bool TestInheritTrackWidth( PNS::ITEM* aItem, int* aInheritedWidth,
292 const VECTOR2I& aStartPosition = VECTOR2I() )
293 {
294 m_startLayer = aItem->Layer();
295 return inheritTrackWidth( aItem, aInheritedWidth, aStartPosition );
296 }
297
298private:
300};
301
302
318
319
324
325static void dumpObstacles( const PNS::NODE::OBSTACLES &obstacles )
326{
327 for( const PNS::OBSTACLE& obs : obstacles )
328 {
329 BOOST_TEST_MESSAGE( wxString::Format( "%p [%s] - %p [%s], clearance %d",
330 obs.m_head, obs.m_head->KindStr().c_str(),
331 obs.m_item, obs.m_item->KindStr().c_str(),
332 obs.m_clearance ) );
333 }
334}
335
337{
338 PNS::VIA* v1 = new PNS::VIA( VECTOR2I( 0, 1000000 ), PNS_LAYER_RANGE( F_Cu, B_Cu ), 50000, 10000 );
339 PNS::VIA* v2 = new PNS::VIA( VECTOR2I( 0, 2000000 ), PNS_LAYER_RANGE( F_Cu, B_Cu ), 50000, 10000 );
340
341 std::unique_ptr<PNS::NODE> world ( new PNS::NODE );
342
343 v1->SetNet( (PNS::NET_HANDLE) 1 );
344 v2->SetNet( (PNS::NET_HANDLE) 2 );
345
346 world->SetMaxClearance( 10000000 );
347 world->SetRuleResolver( &m_ruleResolver );
348
349 world->AddRaw( v1 );
350 world->AddRaw( v2 );
351
352 BOOST_TEST_MESSAGE( "via to via, no violations" );
353 {
354 PNS::NODE::OBSTACLES obstacles;
355 int count = world->QueryColliding( v1, obstacles );
356 dumpObstacles( obstacles );
357 BOOST_CHECK_EQUAL( obstacles.size(), 0 );
358 BOOST_CHECK_EQUAL( count, 0 );
359 }
360
361 BOOST_TEST_MESSAGE( "via to via, forced copper to copper violation" );
362 {
363 PNS::NODE::OBSTACLES obstacles;
364 m_ruleResolver.m_defaultClearance = 1000000;
365 world->QueryColliding( v1, obstacles );
366 dumpObstacles( obstacles );
367
368 BOOST_CHECK_EQUAL( obstacles.size(), 1 );
369 const auto& first = *obstacles.begin();
370
371 BOOST_CHECK_EQUAL( first.m_head, v1 );
372 BOOST_CHECK_EQUAL( first.m_item, v2 );
373 BOOST_CHECK_EQUAL( first.m_clearance, m_ruleResolver.m_defaultClearance );
374 }
375
376 BOOST_TEST_MESSAGE( "via to via, forced hole to hole violation" );
377 {
378 PNS::NODE::OBSTACLES obstacles;
379 m_ruleResolver.m_defaultClearance = 200000;
380 m_ruleResolver.m_defaultHole2Hole = 1000000;
381
382 world->QueryColliding( v1, obstacles );
383 dumpObstacles( obstacles );
384
385 BOOST_CHECK_EQUAL( obstacles.size(), 1 );
386 auto iter = obstacles.begin();
387 const auto& first = *iter++;
388
389 BOOST_CHECK_EQUAL( first.m_head, v1->Hole() );
390 BOOST_CHECK_EQUAL( first.m_item, v2->Hole() );
391 BOOST_CHECK_EQUAL( first.m_clearance, m_ruleResolver.m_defaultHole2Hole );
392 }
393
394 BOOST_TEST_MESSAGE( "via to via, forced copper to hole violation" );
395 {
396 PNS::NODE::OBSTACLES obstacles;
397 m_ruleResolver.m_defaultHole2Hole = 220000;
398 m_ruleResolver.m_defaultHole2Copper = 1000000;
399
400 world->QueryColliding( v1, obstacles );
401 dumpObstacles( obstacles );
402
403 BOOST_CHECK_EQUAL( obstacles.size(), 2 );
404 auto iter = obstacles.begin();
405 const auto& first = *iter++;
406
407 // There is no guarantee on what order the two collisions will be in...
408 BOOST_CHECK( ( first.m_head == v1 && first.m_item == v2->Hole() )
409 || ( first.m_head == v1->Hole() && first.m_item == v2 ) );
410
411 BOOST_CHECK_EQUAL( first.m_clearance, m_ruleResolver.m_defaultHole2Copper );
412 }
413}
414
415
416BOOST_FIXTURE_TEST_CASE( PNSViaBackdrillRetention, PNS_TEST_FIXTURE )
417{
418 PNS::VIA via( VECTOR2I( 1000, 2000 ), PNS_LAYER_RANGE( F_Cu, B_Cu ), 40000, 20000, nullptr,
420 via.SetHoleLayers( PNS_LAYER_RANGE( F_Cu, In2_Cu ) );
421 via.SetHolePostMachining( std::optional<PAD_DRILL_POST_MACHINING_MODE>( PAD_DRILL_POST_MACHINING_MODE::COUNTERSINK ) );
422 via.SetSecondaryDrill( std::optional<int>( 12000 ) );
423 via.SetSecondaryHoleLayers( std::optional<PNS_LAYER_RANGE>( PNS_LAYER_RANGE( F_Cu, In1_Cu ) ) );
424 via.SetSecondaryHolePostMachining( std::optional<PAD_DRILL_POST_MACHINING_MODE>( PAD_DRILL_POST_MACHINING_MODE::NOT_POST_MACHINED ) );
425
426 PNS::VIA viaCopy( via );
427 std::unique_ptr<PNS::VIA> viaClone( via.Clone() );
428
429 auto checkVia = [&]( const PNS::VIA& candidate )
430 {
431 BOOST_CHECK_EQUAL( candidate.HoleLayers().Start(), via.HoleLayers().Start() );
432 BOOST_CHECK_EQUAL( candidate.HoleLayers().End(), via.HoleLayers().End() );
433 BOOST_CHECK( candidate.HolePostMachining().has_value() );
434 BOOST_CHECK( candidate.HolePostMachining().value() == PAD_DRILL_POST_MACHINING_MODE::COUNTERSINK );
435 BOOST_CHECK( candidate.SecondaryDrill().has_value() );
436 BOOST_CHECK_EQUAL( candidate.SecondaryDrill().value(), via.SecondaryDrill().value() );
437 BOOST_CHECK( candidate.SecondaryHoleLayers().has_value() );
438 BOOST_CHECK_EQUAL( candidate.SecondaryHoleLayers()->Start(),
439 via.SecondaryHoleLayers()->Start() );
440 BOOST_CHECK_EQUAL( candidate.SecondaryHoleLayers()->End(),
441 via.SecondaryHoleLayers()->End() );
442 BOOST_CHECK( candidate.SecondaryHolePostMachining().has_value() );
443
444 // run this BOOST_CHECK only if possible to avoid crash
445 if( candidate.SecondaryHolePostMachining().has_value() )
446 BOOST_CHECK( candidate.SecondaryHolePostMachining().value() == via.SecondaryHolePostMachining().value() );
447 };
448
449 checkVia( viaCopy );
450 checkVia( *viaClone );
451}
452
453
454BOOST_AUTO_TEST_CASE( PCBViaBackdrillCloneRetainsData )
455{
456 BOARD board;
457 PCB_VIA via( &board );
458
459 via.SetPrimaryDrillStartLayer( F_Cu );
460 via.SetPrimaryDrillEndLayer( B_Cu );
461 via.SetFrontPostMachining( std::optional<PAD_DRILL_POST_MACHINING_MODE>( PAD_DRILL_POST_MACHINING_MODE::COUNTERSINK ) );
462 via.SetSecondaryDrillSize( std::optional<int>( 15000 ) );
463 via.SetSecondaryDrillStartLayer( F_Cu );
464 via.SetSecondaryDrillEndLayer( In2_Cu );
465
466 via.SetBackPostMachining( std::optional<PAD_DRILL_POST_MACHINING_MODE>( PAD_DRILL_POST_MACHINING_MODE::COUNTERBORE ) );
467 via.SetTertiaryDrillSize( std::optional<int>( 8000 ) );
468 via.SetTertiaryDrillStartLayer( B_Cu );
469 via.SetTertiaryDrillEndLayer( In4_Cu );
470
471 PCB_VIA viaCopy( via );
472 std::unique_ptr<PCB_VIA> viaClone( static_cast<PCB_VIA*>( via.Clone() ) );
473
474 auto checkVia = [&]( const PCB_VIA& candidate )
475 {
476 BOOST_CHECK_EQUAL( candidate.GetPrimaryDrillStartLayer(), via.GetPrimaryDrillStartLayer() );
477 BOOST_CHECK_EQUAL( candidate.GetPrimaryDrillEndLayer(), via.GetPrimaryDrillEndLayer() );
478 BOOST_CHECK( candidate.GetFrontPostMachining().has_value() );
479 BOOST_CHECK_EQUAL( static_cast<int>( candidate.GetFrontPostMachining().value() ),
480 static_cast<int>( via.GetFrontPostMachining().value() ) );
481 BOOST_CHECK( candidate.GetSecondaryDrillSize().has_value() );
482 BOOST_CHECK_EQUAL( candidate.GetSecondaryDrillSize().value(),
483 via.GetSecondaryDrillSize().value() );
484 BOOST_CHECK_EQUAL( candidate.GetSecondaryDrillStartLayer(),
485 via.GetSecondaryDrillStartLayer() );
486 BOOST_CHECK_EQUAL( candidate.GetSecondaryDrillEndLayer(),
487 via.GetSecondaryDrillEndLayer() );
488
489 BOOST_CHECK( candidate.GetBackPostMachining().has_value() );
490 BOOST_CHECK_EQUAL( static_cast<int>( candidate.GetBackPostMachining().value() ),
491 static_cast<int>( via.GetBackPostMachining().value() ) );
492 BOOST_CHECK( candidate.GetTertiaryDrillSize().has_value() );
493 BOOST_CHECK_EQUAL( candidate.GetTertiaryDrillSize().value(),
494 via.GetTertiaryDrillSize().value() );
495 BOOST_CHECK_EQUAL( candidate.GetTertiaryDrillStartLayer(),
496 via.GetTertiaryDrillStartLayer() );
497 BOOST_CHECK_EQUAL( candidate.GetTertiaryDrillEndLayer(),
498 via.GetTertiaryDrillEndLayer() );
499 };
500
501 checkVia( viaCopy );
502 checkVia( *viaClone );
503}
504
505
514BOOST_AUTO_TEST_CASE( PNSLayerRangeSwapBehavior )
515{
516 // On a 2-layer board with FRONT_INNER_BACK mode, BoardCopperLayerCount() returns 2.
517 // The code would calculate PNS_LAYER_RANGE(1, 2 - 2) = PNS_LAYER_RANGE(1, 0)
518 // Since start > end, the constructor swaps them to (0, 1), which would span
519 // both F_Cu and B_Cu incorrectly.
520
521 PNS_LAYER_RANGE innerLayersRange2Layer( 1, 0 ); // What would happen on 2-layer board
522
523 // Verify the swap behavior that causes the bug
524 BOOST_CHECK_EQUAL( innerLayersRange2Layer.Start(), 0 );
525 BOOST_CHECK_EQUAL( innerLayersRange2Layer.End(), 1 );
526 BOOST_CHECK( innerLayersRange2Layer.Overlaps( 0 ) ); // F_Cu
527 BOOST_CHECK( innerLayersRange2Layer.Overlaps( 1 ) ); // B_Cu
528
529 // On a 4-layer board, inner layers are 1 and 2, so PNS_LAYER_RANGE(1, 4-2) = (1, 2)
530 PNS_LAYER_RANGE innerLayersRange4Layer( 1, 2 ); // Correct for 4-layer board
531
532 BOOST_CHECK_EQUAL( innerLayersRange4Layer.Start(), 1 );
533 BOOST_CHECK_EQUAL( innerLayersRange4Layer.End(), 2 );
534 BOOST_CHECK( !innerLayersRange4Layer.Overlaps( 0 ) ); // F_Cu - should not overlap
535 BOOST_CHECK( innerLayersRange4Layer.Overlaps( 1 ) ); // In1_Cu
536 BOOST_CHECK( innerLayersRange4Layer.Overlaps( 2 ) ); // In2_Cu
537 BOOST_CHECK( !innerLayersRange4Layer.Overlaps( 3 ) ); // B_Cu - should not overlap
538}
539
540
548BOOST_FIXTURE_TEST_CASE( PNSSegmentSplitPreservesLockedState, PNS_TEST_FIXTURE )
549{
550 std::unique_ptr<PNS::NODE> world( new PNS::NODE );
551 world->SetMaxClearance( 10000000 );
552 world->SetRuleResolver( &m_ruleResolver );
553
555
556 VECTOR2I segStart( 0, 0 );
557 VECTOR2I segEnd( 10000000, 0 );
558 VECTOR2I splitPt( 5000000, 0 );
559
560 PNS::SEGMENT* lockedSeg = new PNS::SEGMENT( SEG( segStart, segEnd ), net );
561 lockedSeg->SetWidth( 250000 );
562 lockedSeg->SetLayers( PNS_LAYER_RANGE( F_Cu ) );
563 lockedSeg->Mark( PNS::MK_LOCKED );
564
565 BOOST_CHECK( lockedSeg->IsLocked() );
566
567 world->AddRaw( lockedSeg );
568
569 // Clone the locked segment and set up two halves (simulating SplitAdjacentSegments)
570 std::unique_ptr<PNS::SEGMENT> clone1( PNS::Clone( *lockedSeg ) );
571 std::unique_ptr<PNS::SEGMENT> clone2( PNS::Clone( *lockedSeg ) );
572
573 clone1->SetEnds( segStart, splitPt );
574 clone2->SetEnds( splitPt, segEnd );
575
576 BOOST_CHECK_MESSAGE( clone1->IsLocked(),
577 "First half of split locked segment must retain locked state" );
578 BOOST_CHECK_MESSAGE( clone2->IsLocked(),
579 "Second half of split locked segment must retain locked state" );
580 BOOST_CHECK_EQUAL( clone1->Width(), lockedSeg->Width() );
581 BOOST_CHECK_EQUAL( clone2->Width(), lockedSeg->Width() );
582}
583
584
592BOOST_FIXTURE_TEST_CASE( PNSInheritTrackWidthCursorProximity, PNS_TEST_FIXTURE )
593{
594 std::unique_ptr<PNS::NODE> world( new PNS::NODE );
595 world->SetMaxClearance( 10000000 );
596 world->SetRuleResolver( &m_ruleResolver );
597
598 VECTOR2I padPos( 0, 0 );
600
601 // Pad at origin with a small circular shape
603 pad->SetShape( new SHAPE_CIRCLE( padPos, 500000 ) );
604 pad->SetPos( padPos );
605 pad->SetLayers( PNS_LAYER_RANGE( F_Cu ) );
606 pad->SetNet( net );
607 world->AddRaw( pad );
608
609 // Narrow track going right (250um width)
610 int narrowWidth = 250000;
611 PNS::SEGMENT* narrowSeg = new PNS::SEGMENT( SEG( padPos, VECTOR2I( 5000000, 0 ) ), net );
612 narrowSeg->SetWidth( narrowWidth );
613 narrowSeg->SetLayers( PNS_LAYER_RANGE( F_Cu ) );
614 world->AddRaw( narrowSeg );
615
616 // Wide track going up (500um width)
617 int wideWidth = 500000;
618 PNS::SEGMENT* wideSeg = new PNS::SEGMENT( SEG( padPos, VECTOR2I( 0, -5000000 ) ), net );
619 wideSeg->SetWidth( wideWidth );
620 wideSeg->SetLayers( PNS_LAYER_RANGE( F_Cu ) );
621 world->AddRaw( wideSeg );
622
623 int inherited = 0;
624
625 // Without cursor position, should fall back to minimum width
626 BOOST_CHECK( m_iface->TestInheritTrackWidth( pad, &inherited ) );
627 BOOST_CHECK_EQUAL( inherited, narrowWidth );
628
629 // Cursor near the narrow track (to the right) should select narrow width
630 inherited = 0;
631 BOOST_CHECK( m_iface->TestInheritTrackWidth( pad, &inherited, VECTOR2I( 2000000, 0 ) ) );
632 BOOST_CHECK_EQUAL( inherited, narrowWidth );
633
634 // Cursor near the wide track (upward) should select wide width
635 inherited = 0;
636 BOOST_CHECK( m_iface->TestInheritTrackWidth( pad, &inherited, VECTOR2I( 0, -2000000 ) ) );
637 BOOST_CHECK_EQUAL( inherited, wideWidth );
638
639 // Cursor slightly offset toward narrow track should still select narrow
640 inherited = 0;
641 BOOST_CHECK( m_iface->TestInheritTrackWidth( pad, &inherited, VECTOR2I( 100000, 50000 ) ) );
642 BOOST_CHECK_EQUAL( inherited, narrowWidth );
643
644 // Cursor slightly offset toward wide track should select wide
645 inherited = 0;
646 BOOST_CHECK( m_iface->TestInheritTrackWidth( pad, &inherited, VECTOR2I( 50000, -100000 ) ) );
647 BOOST_CHECK_EQUAL( inherited, wideWidth );
648}
649
650
658BOOST_AUTO_TEST_CASE( PCBExprGeometryDependentFunctionDetection )
659{
660 PCBEXPR_COMPILER compiler( new PCBEXPR_UNIT_RESOLVER() );
661
662 auto compileAndCheck = [&]( const wxString& aExpr, bool aExpectGeometry )
663 {
664 PCBEXPR_UCODE ucode;
665 PCBEXPR_CONTEXT ctx( 0, F_Cu );
666
667 bool ok = compiler.Compile( aExpr.ToUTF8().data(), &ucode, &ctx );
668 BOOST_CHECK_MESSAGE( ok, "Failed to compile: " + aExpr );
669
670 if( ok )
671 {
672 BOOST_CHECK_MESSAGE( ucode.HasGeometryDependentFunctions() == aExpectGeometry,
673 wxString::Format( "Expression '%s': expected geometry=%s, got %s",
674 aExpr,
675 aExpectGeometry ? "true" : "false",
677 ? "true" : "false" ) );
678 }
679 };
680
681 // Property-based conditions should NOT be geometry-dependent
682 compileAndCheck( wxT( "A.NetClass == 'Power'" ), false );
683 compileAndCheck( wxT( "A.Type == 'via'" ), false );
684 compileAndCheck( wxT( "A.NetName == '/VCC'" ), false );
685
686 // Geometry-dependent functions SHOULD be detected
687 compileAndCheck( wxT( "A.intersectsCourtyard('U1')" ), true );
688 compileAndCheck( wxT( "A.intersectsArea('Zone1')" ), true );
689 compileAndCheck( wxT( "A.enclosedByArea('Zone1')" ), true );
690 compileAndCheck( wxT( "A.intersectsFrontCourtyard('U1')" ), true );
691 compileAndCheck( wxT( "A.intersectsBackCourtyard('U1')" ), true );
692
693 // Deprecated aliases should also be detected
694 compileAndCheck( wxT( "A.insideCourtyard('U1')" ), true );
695 compileAndCheck( wxT( "A.insideArea('Zone1')" ), true );
696}
697
698
707BOOST_FIXTURE_TEST_CASE( PNSCollideSimpleNullShapeGuard, PNS_TEST_FIXTURE )
708{
709 std::unique_ptr<PNS::NODE> world( new PNS::NODE );
710 world->SetMaxClearance( 10000000 );
711 world->SetRuleResolver( &m_ruleResolver );
712
715
716 PNS::SOLID* solid1 = new PNS::SOLID;
717 solid1->SetShape( new SHAPE_CIRCLE( VECTOR2I( 0, 0 ), 500000 ) );
718 solid1->SetPos( VECTOR2I( 0, 0 ) );
719 solid1->SetLayers( PNS_LAYER_RANGE( F_Cu ) );
720 solid1->SetNet( net1 );
721
722 PNS::SOLID* solid2 = new PNS::SOLID;
723 solid2->SetShape( new SHAPE_CIRCLE( VECTOR2I( 100000, 0 ), 500000 ) );
724 solid2->SetPos( VECTOR2I( 100000, 0 ) );
725 solid2->SetLayers( PNS_LAYER_RANGE( F_Cu ) );
726 solid2->SetNet( net2 );
727
728 world->AddRaw( solid1 );
729 world->AddRaw( solid2 );
730
731 // Verify collision works normally with valid shapes
732 PNS::NODE::OBSTACLES obstacles;
733 int count = world->QueryColliding( solid1, obstacles );
734 BOOST_CHECK( count > 0 );
735
736 // Exercise the null-shape guard in collideSimple by calling Collide() directly with a
737 // null-shape solid as the head item. This bypasses the spatial index (which requires a
738 // valid shape for bounding-box computation) and hits the exact code path the guard protects.
739 PNS::SOLID nullShapeSolid;
740 nullShapeSolid.SetPos( VECTOR2I( 0, 0 ) );
741 nullShapeSolid.SetLayers( PNS_LAYER_RANGE( F_Cu ) );
742 nullShapeSolid.SetNet( net2 );
743
744 bool collided = solid1->Collide( &nullShapeSolid, world.get(), F_Cu, nullptr );
745 BOOST_CHECK( !collided );
746}
747
748
757BOOST_FIXTURE_TEST_CASE( PNSComponentDraggerBasicDrag, PNS_TEST_FIXTURE )
758{
759 std::unique_ptr<PNS::NODE> world( new PNS::NODE );
760 world->SetMaxClearance( 10000000 );
761 world->SetRuleResolver( &m_ruleResolver );
762
764
765 VECTOR2I pad1Pos( 0, 0 );
766
767 PNS::SOLID* pad1 = new PNS::SOLID;
768 pad1->SetShape( new SHAPE_CIRCLE( pad1Pos, 500000 ) );
769 pad1->SetPos( pad1Pos );
770 pad1->SetLayers( PNS_LAYER_RANGE( F_Cu ) );
771 pad1->SetNet( net1 );
772 pad1->SetRoutable( true );
773
774 VECTOR2I traceEnd( 2500000, 2500000 );
775 PNS::SEGMENT* trace = new PNS::SEGMENT( SEG( pad1Pos, traceEnd ), net1 );
776 trace->SetWidth( 250000 );
777 trace->SetLayers( PNS_LAYER_RANGE( F_Cu ) );
778
779 world->AddRaw( pad1 );
780 world->AddRaw( trace );
781
782 PNS::COMPONENT_DRAGGER dragger( m_router );
783 dragger.SetWorld( world.get() );
784
785 PNS::ITEM_SET itemsToDrag;
786 itemsToDrag.Add( pad1 );
787
788 bool started = dragger.Start( pad1Pos, itemsToDrag );
789 BOOST_REQUIRE( started );
790
791 // Simulate multiple drag events (mimics mouse movement during drag)
792 VECTOR2I dragPositions[] = {
793 VECTOR2I( 100000, 100000 ),
794 VECTOR2I( 500000, 500000 ),
795 VECTOR2I( 1000000, 1000000 ),
796 VECTOR2I( 500000, 200000 ),
797 VECTOR2I( 0, 0 )
798 };
799
800 for( const VECTOR2I& pos : dragPositions )
801 {
802 bool dragOk = dragger.Drag( pos );
803 BOOST_CHECK( dragOk );
804
805 PNS::NODE* currentNode = dragger.CurrentNode();
806 BOOST_CHECK( currentNode != nullptr );
807 }
808
809 // Verify the dragged items set is populated
810 PNS::ITEM_SET traces = dragger.Traces();
811 BOOST_CHECK( traces.Size() > 0 );
812
813 // Clean up branch nodes before the world is destroyed
814 world->KillChildren();
815}
816
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
virtual bool IsOnLayer(PCB_LAYER_ID aLayer) const
Test to see if this object is on the given layer.
Definition board_item.h:350
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:323
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:112
bool Compile(const wxString &aString, UCODE *aCode, CONTEXT *aPreflightContext)
T Min() const
Definition minoptmax.h:33
void SetMin(T v)
Definition minoptmax.h:41
PNS::RULE_RESOLVER * GetRuleResolver() override
void DisplayItem(const PNS::ITEM *aItem, int aClearance, bool aEdit=false, int aFlags=0) override
void HideItem(PNS::ITEM *aItem) override
MOCK_PNS_KICAD_IFACE(PNS_TEST_FIXTURE *aFixture)
PNS_TEST_FIXTURE * m_testFixture
bool TestInheritTrackWidth(PNS::ITEM *aItem, int *aInheritedWidth, const VECTOR2I &aStartPosition=VECTOR2I())
virtual int NetCode(PNS::NET_HANDLE aNet) override
bool IsNetTieExclusion(const PNS::ITEM *aItem, const VECTOR2I &aCollisionPos, const PNS::ITEM *aCollidingItem) override
bool IsKeepout(const PNS::ITEM *aObstacle, const PNS::ITEM *aItem, bool *aEnforce) override
virtual int Clearance(const PNS::ITEM *aA, const PNS::ITEM *aB, bool aUseClearanceEpsilon=true) override
virtual bool QueryConstraint(PNS::CONSTRAINT_TYPE aType, const PNS::ITEM *aItemA, const PNS::ITEM *aItemB, int aLayer, PNS::CONSTRAINT *aConstraint) override
bool IsInNetTie(const PNS::ITEM *aA) override
std::map< ITEM_KEY, PNS::CONSTRAINT > m_ruleMap
virtual PNS::NET_HANDLE DpCoupledNet(PNS::NET_HANDLE aNet) override
bool IsDrilledHole(const PNS::ITEM *aItem) override
void AddMockRule(PNS::CONSTRAINT_TYPE aType, const PNS::ITEM *aItemA, const PNS::ITEM *aItemB, PNS::CONSTRAINT &aConstraint)
bool IsNonPlatedSlot(const PNS::ITEM *aItem) override
virtual bool DpNetPair(const PNS::ITEM *aItem, PNS::NET_HANDLE &aNetP, PNS::NET_HANDLE &aNetN) override
virtual wxString NetName(PNS::NET_HANDLE aNet) override
int ClearanceEpsilon() const override
virtual int DpNetPolarity(PNS::NET_HANDLE aNet) override
Definition pad.h:55
bool HasGeometryDependentFunctions() const
const ITEM_SET Traces() override
Function Traces()
NODE * CurrentNode() const override
Function CurrentNode()
bool Start(const VECTOR2I &aP, ITEM_SET &aPrimitives) override
Function Start()
bool Drag(const VECTOR2I &aP) override
Function Drag()
virtual void SetWorld(NODE *aWorld)
Function SetWorld()
int Size() const
void Add(const LINE &aLine)
Base class for PNS router board items.
Definition pns_item.h:98
BOARD_ITEM * Parent() const
Definition pns_item.h:199
void SetLayers(const PNS_LAYER_RANGE &aLayers)
Definition pns_item.h:213
const PNS_LAYER_RANGE & Layers() const
Definition pns_item.h:212
void SetNet(NET_HANDLE aNet)
Definition pns_item.h:209
virtual int Layer() const
Definition pns_item.h:216
bool Collide(const ITEM *aHead, const NODE *aNode, int aLayer, COLLISION_SEARCH_CONTEXT *aCtx=nullptr) const
Check for a collision (clearance violation) with between us and item aOther.
Definition pns_item.cpp:297
bool OfKind(int aKindMask) const
Definition pns_item.h:181
virtual void Mark(int aMarker) const
Definition pns_item.h:261
virtual BOARD_ITEM * BoardItem() const
Definition pns_item.h:207
void SetRoutable(bool aRoutable)
Definition pns_item.h:283
bool IsLocked() const
Definition pns_item.h:278
Keep the router "world" - i.e.
Definition pns_node.h:240
std::set< OBSTACLE > OBSTACLES
Definition pns_node.h:252
int Width() const override
Definition pns_segment.h:96
void SetWidth(int aWidth) override
Definition pns_segment.h:91
void SetPos(const VECTOR2I &aCenter)
Definition pns_solid.cpp:81
void SetShape(SHAPE *shape)
Definition pns_solid.h:113
bool inheritTrackWidth(PNS::ITEM *aItem, int *aInheritedWidth, const VECTOR2I &aStartPosition)
Represent a contiguous set of PCB layers.
int Start() const
bool Overlaps(const PNS_LAYER_RANGE &aOther) const
int End() const
PNS_LAYER_RANGE Intersection(const PNS_LAYER_RANGE &aOther) const
Shortcut for comparisons/overlap tests.
Definition seg.h:42
constexpr PCB_LAYER_ID PCBNEW_LAYER_ID_START
Definition layer_ids.h:174
@ Edge_Cuts
Definition layer_ids.h:112
@ B_Cu
Definition layer_ids.h:65
@ In2_Cu
Definition layer_ids.h:67
@ Margin
Definition layer_ids.h:113
@ In4_Cu
Definition layer_ids.h:69
@ In1_Cu
Definition layer_ids.h:66
@ PCB_LAYER_ID_COUNT
Definition layer_ids.h:171
@ F_Cu
Definition layer_ids.h:64
CONSTRAINT_TYPE
Definition pns_node.h:52
void * NET_HANDLE
Definition pns_item.h:55
@ MK_LOCKED
Definition pns_item.h:45
std::unique_ptr< typename std::remove_const< T >::type > Clone(const T &aItem)
Definition pns_item.h:344
bool operator<(const ITEM_KEY &other) const
bool operator==(const ITEM_KEY &other) const
An abstract function object, returning a design rule (clearance, diff pair gap, etc) required between...
Definition pns_node.h:73
MINOPTMAX< int > m_Value
Definition pns_node.h:75
CONSTRAINT_TYPE m_Type
Definition pns_node.h:74
Hold an object colliding with another object, along with some useful data about the collision.
Definition pns_node.h:88
SETTINGS_MANAGER m_settingsManager
MOCK_RULE_RESOLVER m_ruleResolver
PNS::ROUTER * m_router
MOCK_PNS_KICAD_IFACE * m_iface
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
VECTOR3I v1(5, 5, 5)
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))
static bool isEdge(const PNS::ITEM *aItem)
static bool isHole(const PNS::ITEM *aItem)
static void dumpObstacles(const PNS::NODE::OBSTACLES &obstacles)
BOOST_FIXTURE_TEST_CASE(PNSHoleCollisions, PNS_TEST_FIXTURE)
static bool isCopper(const PNS::ITEM *aItem)
BOOST_AUTO_TEST_CASE(PCBViaBackdrillCloneRetainsData)
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2I v2(1, 0)
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition typeinfo.h:84
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687