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
23
24#include <filesystem>
25#include <fstream>
26
27#include <board.h>
29#include <drc/drc_engine.h>
30#include <drc/drc_item.h>
31#include <netinfo.h>
32#include <pcb_marker.h>
34
35
36// Two-net board. CHAIN_NET is 25 mm long and tagged with chain "SIG"; PLAIN_NET is 25 mm long
37// and not in any chain. The .kicad_dru rule matches the default netclass that both nets
38// inherit.
39static const char* BOARD_FILE = "net_chains/length_with_chain.kicad_pcb";
40
41
42// length 0..5 mm fails for any 25 mm net. net_chain_length 0..200 mm passes for the chain.
43// The bug: pre-fix, presence of net_chain_length suppresses the per-net length check on every
44// net the rule matches, so neither CHAIN_NET nor PLAIN_NET reports a length violation.
45static const char* DRU_TEXT = R"KICAD((version 1)
46
47(rule "FastTopo"
48 (condition "A.NetClass == 'Default'")
49 (constraint length (min 0mm) (max 5mm))
50 (constraint net_chain_length (min 0mm) (max 200mm))
51)
52)KICAD";
53
54
55BOOST_AUTO_TEST_SUITE( DRCLengthWithChain )
56
57
58BOOST_AUTO_TEST_CASE( PerNetLengthFiresAlongsideChainLength )
59{
60 namespace fs = std::filesystem;
61
62 fs::path tmpDir = fs::temp_directory_path() / "kicad_drc_length_chain";
63 fs::create_directories( tmpDir );
64
65 fs::path druPath = tmpDir / "len_chain.kicad_dru";
66
67 {
68 std::ofstream druOut( druPath );
69 druOut << DRU_TEXT;
70 }
71
72 PCB_IO_KICAD_SEXPR plugin;
73 std::unique_ptr<BOARD> board = std::make_unique<BOARD>();
74 plugin.LoadBoard( KI_TEST::GetPcbnewTestDataDir() + BOARD_FILE, board.get() );
75 board->BuildConnectivity();
76
77 // Tag CHAIN_NET into a signal chain so the chain branch fires; leave PLAIN_NET unchained
78 NETINFO_ITEM* chainNet = board->FindNet( wxS( "/CHAIN_NET" ) );
79 BOOST_REQUIRE( chainNet );
80 chainNet->SetNetChain( wxS( "SIG" ) );
81
82 NETINFO_ITEM* plainNet = board->FindNet( wxS( "/PLAIN_NET" ) );
83 BOOST_REQUIRE( plainNet );
84 BOOST_CHECK( plainNet->GetNetChain().IsEmpty() );
85
86 BOARD_DESIGN_SETTINGS& bds = board->GetDesignSettings();
87
88 auto drcEngine = std::make_shared<DRC_ENGINE>( board.get(), &bds );
89 wxFileName ruleFile( druPath.string() );
90 drcEngine->InitEngine( ruleFile );
91 bds.m_DRCEngine = drcEngine;
92
100
101 std::vector<DRC_ITEM> lengthViolations;
102
103 drcEngine->SetViolationHandler(
104 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I&, int,
105 const std::function<void( PCB_MARKER* )>& )
106 {
107 if( aItem->GetErrorCode() == DRCE_LENGTH_OUT_OF_RANGE )
108 lengthViolations.push_back( *aItem );
109 } );
110
111 drcEngine->RunTests( EDA_UNITS::MM, true, false );
112
113 // Pre-fix behaviour: 0 violations (the chain branch suppresses length on every matched net).
114 // Post-fix behaviour: at least one length violation per matched net (CHAIN_NET, PLAIN_NET).
115 BOOST_CHECK_MESSAGE( lengthViolations.size() >= 2,
116 "Expected per-net length violations on both chain and plain nets, got "
117 << lengthViolations.size() );
118
119 bool chainNetFlagged = false;
120 bool plainNetFlagged = false;
121
122 for( const DRC_ITEM& item : lengthViolations )
123 {
124 for( const KIID& id : item.GetIDs() )
125 {
126 BOARD_ITEM* bi = board->ResolveItem( id );
127
128 if( !bi )
129 continue;
130
131 if( BOARD_CONNECTED_ITEM* bci = dynamic_cast<BOARD_CONNECTED_ITEM*>( bi ) )
132 {
133 if( bci->GetNetCode() == chainNet->GetNetCode() )
134 chainNetFlagged = true;
135
136 if( bci->GetNetCode() == plainNet->GetNetCode() )
137 plainNetFlagged = true;
138 }
139 }
140 }
141
142 BOOST_CHECK_MESSAGE( chainNetFlagged, "Length violation missing for chain-bearing net" );
143 BOOST_CHECK_MESSAGE( plainNetFlagged, "Length violation missing for non-chain net" );
144
145 std::error_code ec;
146 fs::remove( druPath, ec );
147}
148
149
General utilities for PCB file IO for QA programs.
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:83
Definition kiid.h:46
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:37
@ DRCE_LIB_FOOTPRINT_ISSUES
Definition drc_item.h:80
@ DRCE_INVALID_OUTLINE
Definition drc_item.h:70
@ DRCE_DRILL_OUT_OF_RANGE
Definition drc_item.h:58
@ DRCE_DANGLING_VIA
Definition drc_item.h:48
@ DRCE_LENGTH_OUT_OF_RANGE
Definition drc_item.h:102
@ DRCE_LIB_FOOTPRINT_MISMATCH
Definition drc_item.h:81
std::string GetPcbnewTestDataDir()
Utility which returns a path to the data directory where the test board files are stored.
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_IGNORE
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
static const char * BOARD_FILE
static const char * DRU_TEXT
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683