KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_drc_return_path_zone_bbox.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 F.Cu chains, each 30 mm long. CHAIN_COVERED is shadowed by a B.Cu zone in the
35// (0,0)..(30,1) corridor; CHAIN_UNCOVERED is at y=20 with no overlying B.Cu zone.
36// Pre-fix the global zone count is 1 board-wide so the return-path check never fires.
37// Post-fix the spatial test catches CHAIN_UNCOVERED while leaving CHAIN_COVERED clean.
38static const char* BOARD_TEXT = R"KICAD(
39(kicad_pcb
40 (version 20250904)
41 (generator "pcbnew")
42 (generator_version "9.99")
43 (layers
44 (0 "F.Cu" signal)
45 (2 "B.Cu" signal)
46 (44 "Edge.Cuts" user)
47 )
48 (net 0 "")
49 (net 1 "/CHAIN_COVERED")
50 (net 2 "/CHAIN_UNCOVERED")
51 (net 3 "/GND")
52 (gr_line (start 0 -5) (end 40 -5) (layer "Edge.Cuts") (width 0.05))
53 (gr_line (start 40 -5) (end 40 30) (layer "Edge.Cuts") (width 0.05))
54 (gr_line (start 40 30) (end 0 30) (layer "Edge.Cuts") (width 0.05))
55 (gr_line (start 0 30) (end 0 -5) (layer "Edge.Cuts") (width 0.05))
56 (segment (start 0 0) (end 30 0) (width 0.2) (layer "F.Cu") (net 1))
57 (segment (start 0 20) (end 30 20) (width 0.2) (layer "F.Cu") (net 2))
58 (zone (net 3) (net_name "/GND") (layer "B.Cu") (name "gnd_corridor") (hatch edge 0.508)
59 (connect_pads (clearance 0))
60 (min_thickness 0.254) (filled_areas_thickness no)
61 (fill (thermal_gap 0.508) (thermal_bridge_width 0.508))
62 (polygon
63 (pts
64 (xy -1 -1)
65 (xy 31 -1)
66 (xy 31 1)
67 (xy -1 1)
68 )
69 )
70 )
71)
72)KICAD";
73
74
75// length 0..200 mm passes; the only firing constraint is return_path which requires a
76// continuous B.Cu reference under the F.Cu chain.
77static const char* DRU_TEXT = R"KICAD((version 1)
78
79(rule "ReturnPath"
80 (condition "A.NetClass == 'Default'")
81 (constraint length (min 0mm) (max 200mm))
82 (constraint return_path (layer "B.Cu"))
83)
84)KICAD";
85
86
87BOOST_AUTO_TEST_SUITE( DRCReturnPathZoneBBox )
88
89
90BOOST_AUTO_TEST_CASE( ZoneSpatialFilterFiresForUnshadowedChain )
91{
92 namespace fs = std::filesystem;
93
94 fs::path tmpDir = fs::temp_directory_path() / "kicad_drc_return_path_bbox";
95 fs::create_directories( tmpDir );
96
97 fs::path pcbPath = tmpDir / "ret_path.kicad_pcb";
98 fs::path druPath = tmpDir / "ret_path.kicad_dru";
99
100 {
101 std::ofstream pcbOut( pcbPath );
102 pcbOut << BOARD_TEXT;
103 }
104
105 {
106 std::ofstream druOut( druPath );
107 druOut << DRU_TEXT;
108 }
109
110 PCB_IO_KICAD_SEXPR plugin;
111 std::unique_ptr<BOARD> board = std::make_unique<BOARD>();
112 plugin.LoadBoard( pcbPath.string(), board.get() );
113 board->BuildConnectivity();
114
115 NETINFO_ITEM* coveredNet = board->FindNet( wxS( "/CHAIN_COVERED" ) );
116 BOOST_REQUIRE( coveredNet );
117 coveredNet->SetNetChain( wxS( "COVERED" ) );
118
119 NETINFO_ITEM* uncoveredNet = board->FindNet( wxS( "/CHAIN_UNCOVERED" ) );
120 BOOST_REQUIRE( uncoveredNet );
121 uncoveredNet->SetNetChain( wxS( "UNCOVERED" ) );
122
123 BOARD_DESIGN_SETTINGS& bds = board->GetDesignSettings();
124
125 auto drcEngine = std::make_shared<DRC_ENGINE>( board.get(), &bds );
126 wxFileName ruleFile( druPath.string() );
127 drcEngine->InitEngine( ruleFile );
128 bds.m_DRCEngine = drcEngine;
129
137
138 std::vector<DRC_ITEM> returnPathViolations;
139
140 drcEngine->SetViolationHandler(
141 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I&, int,
142 const std::function<void( PCB_MARKER* )>& )
143 {
144 if( aItem->GetErrorCode() == DRCE_NET_CHAIN_RETURN_PATH_BREAK )
145 returnPathViolations.push_back( *aItem );
146 } );
147
148 drcEngine->RunTests( EDA_UNITS::MM, true, false );
149
150 // Exactly one chain (UNCOVERED) should report a return-path break. Pre-fix the
151 // board-wide zone count of 1 produced zero violations even though only COVERED is
152 // actually shadowed.
153 BOOST_CHECK_EQUAL( returnPathViolations.size(), 1u );
154
155 bool coveredFlagged = false;
156 bool uncoveredFlagged = false;
157
158 for( const DRC_ITEM& item : returnPathViolations )
159 {
160 const wxString msg = item.GetErrorMessage( false );
161
162 if( msg.Find( wxS( "COVERED" ) ) != wxNOT_FOUND
163 && msg.Find( wxS( "UNCOVERED" ) ) == wxNOT_FOUND )
164 {
165 coveredFlagged = true;
166 }
167
168 if( msg.Find( wxS( "UNCOVERED" ) ) != wxNOT_FOUND )
169 uncoveredFlagged = true;
170 }
171
172 BOOST_CHECK_MESSAGE( !coveredFlagged,
173 "Return-path break wrongly reported for chain shadowed by a zone" );
174 BOOST_CHECK_MESSAGE( uncoveredFlagged,
175 "Return-path break missing for chain with no overlying zone" );
176
177 std::error_code ec;
178 fs::remove( pcbPath, ec );
179 fs::remove( druPath, ec );
180}
181
182
Container for design settings for a BOARD object.
std::map< int, SEVERITY > m_DRCSeverities
std::shared_ptr< DRC_ENGINE > m_DRCEngine
Handle the data for a net.
Definition netinfo.h:46
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_NET_CHAIN_RETURN_PATH_BREAK
Definition drc_item.h:103
@ DRCE_DANGLING_VIA
Definition drc_item.h:47
@ 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")
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683