KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_drc_length_with_chain.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
20#include <boost/test/unit_test.hpp>
21
22#include <filesystem>
23#include <fstream>
24
25#include <board.h>
27#include <drc/drc_engine.h>
28#include <drc/drc_item.h>
29#include <netinfo.h>
30#include <pcb_marker.h>
32
33
34// Two-net board. CHAIN_NET is 25 mm long and tagged with chain "SIG"; PLAIN_NET is 25 mm long
35// and not in any chain. The .kicad_dru rule matches the default netclass that both nets
36// inherit.
37static const char* BOARD_TEXT = R"KICAD(
38(kicad_pcb
39 (version 20250904)
40 (generator "pcbnew")
41 (generator_version "9.99")
42 (layers
43 (0 "F.Cu" signal)
44 (2 "B.Cu" signal)
45 )
46 (net 0 "")
47 (net 1 "/CHAIN_NET")
48 (net 2 "/PLAIN_NET")
49 (segment (start 0 0) (end 25 0) (width 0.2) (layer "F.Cu") (net 1))
50 (segment (start 0 5) (end 25 5) (width 0.2) (layer "F.Cu") (net 2))
51)
52)KICAD";
53
54
55// length 0..5 mm fails for any 25 mm net. net_chain_length 0..200 mm passes for the chain.
56// The bug: pre-fix, presence of net_chain_length suppresses the per-net length check on every
57// net the rule matches, so neither CHAIN_NET nor PLAIN_NET reports a length violation.
58static const char* DRU_TEXT = R"KICAD((version 1)
59
60(rule "FastTopo"
61 (condition "A.NetClass == 'Default'")
62 (constraint length (min 0mm) (max 5mm))
63 (constraint net_chain_length (min 0mm) (max 200mm))
64)
65)KICAD";
66
67
68BOOST_AUTO_TEST_SUITE( DRCLengthWithChain )
69
70
71BOOST_AUTO_TEST_CASE( PerNetLengthFiresAlongsideChainLength )
72{
73 namespace fs = std::filesystem;
74
75 fs::path tmpDir = fs::temp_directory_path() / "kicad_drc_length_chain";
76 fs::create_directories( tmpDir );
77
78 fs::path pcbPath = tmpDir / "len_chain.kicad_pcb";
79 fs::path druPath = tmpDir / "len_chain.kicad_dru";
80
81 {
82 std::ofstream pcbOut( pcbPath );
83 pcbOut << BOARD_TEXT;
84 }
85
86 {
87 std::ofstream druOut( druPath );
88 druOut << DRU_TEXT;
89 }
90
91 PCB_IO_KICAD_SEXPR plugin;
92 std::unique_ptr<BOARD> board = std::make_unique<BOARD>();
93 plugin.LoadBoard( pcbPath.string(), board.get() );
94 board->BuildConnectivity();
95
96 // Tag CHAIN_NET into a signal chain so the chain branch fires; leave PLAIN_NET unchained
97 NETINFO_ITEM* chainNet = board->FindNet( wxS( "/CHAIN_NET" ) );
98 BOOST_REQUIRE( chainNet );
99 chainNet->SetNetChain( wxS( "SIG" ) );
100
101 NETINFO_ITEM* plainNet = board->FindNet( wxS( "/PLAIN_NET" ) );
102 BOOST_REQUIRE( plainNet );
103 BOOST_CHECK( plainNet->GetNetChain().IsEmpty() );
104
105 BOARD_DESIGN_SETTINGS& bds = board->GetDesignSettings();
106
107 auto drcEngine = std::make_shared<DRC_ENGINE>( board.get(), &bds );
108 wxFileName ruleFile( druPath.string() );
109 drcEngine->InitEngine( ruleFile );
110 bds.m_DRCEngine = drcEngine;
111
119
120 std::vector<DRC_ITEM> lengthViolations;
121
122 drcEngine->SetViolationHandler(
123 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I&, int,
124 const std::function<void( PCB_MARKER* )>& )
125 {
126 if( aItem->GetErrorCode() == DRCE_LENGTH_OUT_OF_RANGE )
127 lengthViolations.push_back( *aItem );
128 } );
129
130 drcEngine->RunTests( EDA_UNITS::MM, true, false );
131
132 // Pre-fix behaviour: 0 violations (the chain branch suppresses length on every matched net).
133 // Post-fix behaviour: at least one length violation per matched net (CHAIN_NET, PLAIN_NET).
134 BOOST_CHECK_MESSAGE( lengthViolations.size() >= 2,
135 "Expected per-net length violations on both chain and plain nets, got "
136 << lengthViolations.size() );
137
138 bool chainNetFlagged = false;
139 bool plainNetFlagged = false;
140
141 for( const DRC_ITEM& item : lengthViolations )
142 {
143 for( const KIID& id : item.GetIDs() )
144 {
145 BOARD_ITEM* bi = board->ResolveItem( id );
146
147 if( !bi )
148 continue;
149
150 if( BOARD_CONNECTED_ITEM* bci = dynamic_cast<BOARD_CONNECTED_ITEM*>( bi ) )
151 {
152 if( bci->GetNetCode() == chainNet->GetNetCode() )
153 chainNetFlagged = true;
154
155 if( bci->GetNetCode() == plainNet->GetNetCode() )
156 plainNetFlagged = true;
157 }
158 }
159 }
160
161 BOOST_CHECK_MESSAGE( chainNetFlagged, "Length violation missing for chain-bearing net" );
162 BOOST_CHECK_MESSAGE( plainNetFlagged, "Length violation missing for non-chain net" );
163
164 std::error_code ec;
165 fs::remove( pcbPath, ec );
166 fs::remove( druPath, ec );
167}
168
169
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
Container for design settings for a BOARD object.
std::map< int, SEVERITY > m_DRCSeverities
std::shared_ptr< DRC_ENGINE > m_DRCEngine
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:81
Definition kiid.h:44
Handle the data for a net.
Definition netinfo.h:46
const wxString & GetNetChain() const
Definition netinfo.h:112
int GetNetCode() const
Definition netinfo.h:94
void SetNetChain(const wxString &aNetChain)
Definition netinfo.h:113
A #PLUGIN derivation for saving and loading Pcbnew s-expression formatted files.
BOARD * LoadBoard(const wxString &aFileName, BOARD *aAppendToMe, const std::map< std::string, UTF8 > *aProperties=nullptr, PROJECT *aProject=nullptr) override
Load information from some input file format that this PCB_IO implementation knows about into either ...
@ DRCE_UNCONNECTED_ITEMS
Definition drc_item.h:36
@ DRCE_LIB_FOOTPRINT_ISSUES
Definition drc_item.h:79
@ DRCE_INVALID_OUTLINE
Definition drc_item.h:69
@ DRCE_DRILL_OUT_OF_RANGE
Definition drc_item.h:57
@ DRCE_DANGLING_VIA
Definition drc_item.h:47
@ DRCE_LENGTH_OUT_OF_RANGE
Definition drc_item.h:101
@ DRCE_LIB_FOOTPRINT_MISMATCH
Definition drc_item.h:80
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_IGNORE
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
static const char * DRU_TEXT
static const char * BOARD_TEXT
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_MESSAGE(totalMismatches==0, std::to_string(totalMismatches)+" board(s) with strategy disagreements")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683