KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_drc_creepage.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
27
28#include <qa_utils/file_utils.h>
31
32#include <board.h>
34#include <drc/drc_item.h>
35#include <drc/drc_engine.h>
38#include <base_units.h>
39#include <netinfo.h>
40#include <pcb_shape.h>
41#include <pcb_track.h>
42#include <zone.h>
43
44#include <filesystem>
45#include <fstream>
46
47
49{
51
53 {
54 if( m_board && m_board->GetDesignSettings().m_DRCEngine )
55 m_board->GetDesignSettings().m_DRCEngine->ClearViolationHandler();
56
57 if( m_board )
58 {
59 m_board->SetProject( nullptr );
60 m_board = nullptr;
61 }
62 }
63
65 std::unique_ptr<BOARD> m_board;
66};
67
68
70{
71 KI_TEST::LoadBoard( m_settingsManager, "creepage/creepage", m_board );
72
73 BOOST_REQUIRE_MESSAGE( m_board, "Failed to load board creepage" );
74
75 std::vector<std::shared_ptr<DRC_ITEM>> violations;
76 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
77
78 BOOST_REQUIRE_MESSAGE( bds.m_DRCEngine, "DRC engine not initialized" );
79
80 for( int ii = DRCE_FIRST; ii <= DRCE_LAST; ++ii )
82
84
86 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos, int aLayer,
87 const std::function<void( PCB_MARKER* )>& aPathGenerator )
88 {
89 if( bds.GetSeverity( aItem->GetErrorCode() ) == SEVERITY::RPT_SEVERITY_ERROR )
90 violations.push_back( aItem );
91 } );
92
93 bds.m_DRCEngine->RunTests( EDA_UNITS::MM, true, false );
94
96
97 BOOST_TEST_MESSAGE( wxString::Format( "Found %d creepage violations", (int) violations.size() ) );
98
99 for( const auto& v : violations )
100 BOOST_TEST_MESSAGE( wxString::Format( " Violation: %s", v->GetErrorMessage( false ) ) );
101
102 // The board has HV and GND netclass traces ~3.25mm apart. The custom rule requires
103 // 8mm creepage between HV and GND netclasses, so at least one violation must be detected.
104 BOOST_CHECK_GE( violations.size(), 1 );
105}
106
107
116{
117 KI_TEST::LoadBoard( m_settingsManager, "creepage/creepage_malformed_edge", m_board );
118
119 BOOST_REQUIRE_MESSAGE( m_board, "Failed to load board creepage_malformed_edge" );
120
121 std::vector<std::shared_ptr<DRC_ITEM>> violations;
122 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
123
124 BOOST_REQUIRE_MESSAGE( bds.m_DRCEngine, "DRC engine not initialized" );
125
126 for( int ii = DRCE_FIRST; ii <= DRCE_LAST; ++ii )
128
130
132 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos, int aLayer,
133 const std::function<void( PCB_MARKER* )>& aPathGenerator )
134 {
135 if( bds.GetSeverity( aItem->GetErrorCode() ) == SEVERITY::RPT_SEVERITY_ERROR )
136 violations.push_back( aItem );
137 } );
138
139 bds.m_DRCEngine->RunTests( EDA_UNITS::MM, true, false );
140
142
143 BOOST_TEST_MESSAGE( wxString::Format( "Found %d creepage violations (malformed edge)",
144 (int) violations.size() ) );
145
146 for( const auto& v : violations )
147 BOOST_TEST_MESSAGE( wxString::Format( " Violation: %s", v->GetErrorMessage( false ) ) );
148
149 // Same board geometry as CreepageHVvsGND but with an extra malformed Edge.Cuts line.
150 // The creepage DRC must still detect violations even when the board outline is invalid.
151 BOOST_CHECK_GE( violations.size(), 1 );
152}
153
154
163{
164 KI_TEST::SCOPED_TEMP_DIR tmpDir( "kicad_creepage_rule_area" );
165
166 const std::filesystem::path rulePath = tmpDir.Path() / "creepage_rule_area.kicad_dru";
167
168 {
169 std::ofstream ruleFile( rulePath );
170 ruleFile << "(version 1)\n"
171 << "(rule OVC_3_Reinforced (constraint creepage (min 5.5mm)))\n";
172 }
173
174 m_board = std::make_unique<BOARD>();
175 m_board->SetCopperLayerCount( 2 );
176
177 PCB_SHAPE* edge = new PCB_SHAPE( m_board.get(), SHAPE_T::RECTANGLE );
178 edge->SetLayer( Edge_Cuts );
179 edge->SetStart( VECTOR2I( pcbIUScale.mmToIU( 100 ), pcbIUScale.mmToIU( 80 ) ) );
180 edge->SetEnd( VECTOR2I( pcbIUScale.mmToIU( 170 ), pcbIUScale.mmToIU( 130 ) ) );
181 m_board->Add( edge );
182
183 // Restricts nothing and spans every copper layer, like the areas the Multi-Channel tool
184 // generates.
185 ZONE* ruleArea = new ZONE( m_board.get() );
186 ruleArea->SetIsRuleArea( true );
187 ruleArea->SetLayerSet( LSET( { F_Cu, B_Cu } ) );
188 ruleArea->SetDoNotAllowZoneFills( false );
189 ruleArea->SetDoNotAllowVias( false );
190 ruleArea->SetDoNotAllowTracks( false );
191 ruleArea->SetDoNotAllowPads( false );
192 ruleArea->SetDoNotAllowFootprints( false );
193 ruleArea->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( 117 ), pcbIUScale.mmToIU( 97 ) ), -1 );
194 ruleArea->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( 153 ), pcbIUScale.mmToIU( 97 ) ), -1 );
195 ruleArea->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( 153 ), pcbIUScale.mmToIU( 103 ) ), -1 );
196 ruleArea->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( 117 ), pcbIUScale.mmToIU( 103 ) ), -1 );
197 m_board->Add( ruleArea );
198
199 NETINFO_ITEM* hvNet = new NETINFO_ITEM( m_board.get(), wxT( "HV" ), 1 );
200 m_board->Add( hvNet );
201
202 auto addTrack = [&]( const VECTOR2I& aStart, const VECTOR2I& aEnd, NETINFO_ITEM* aNet )
203 {
204 PCB_TRACK* track = new PCB_TRACK( m_board.get() );
205 track->SetLayer( F_Cu );
206 track->SetWidth( pcbIUScale.mmToIU( 0.2 ) );
207 track->SetStart( aStart );
208 track->SetEnd( aEnd );
209
210 if( aNet )
211 track->SetNet( aNet );
212
213 m_board->Add( track );
214 };
215
216 addTrack( VECTOR2I( pcbIUScale.mmToIU( 120 ), pcbIUScale.mmToIU( 100 ) ),
217 VECTOR2I( pcbIUScale.mmToIU( 150 ), pcbIUScale.mmToIU( 100 ) ), hvNet );
218
219 // Netless, and at right angles so netcode 0's bounding box spans the HV track. Two parallel
220 // segments would give a flat box that the reject still throws out.
221 addTrack( VECTOR2I( pcbIUScale.mmToIU( 103 ), pcbIUScale.mmToIU( 127 ) ),
222 VECTOR2I( pcbIUScale.mmToIU( 167 ), pcbIUScale.mmToIU( 127 ) ), nullptr );
223 addTrack( VECTOR2I( pcbIUScale.mmToIU( 167 ), pcbIUScale.mmToIU( 127 ) ),
224 VECTOR2I( pcbIUScale.mmToIU( 167 ), pcbIUScale.mmToIU( 83 ) ), nullptr );
225
226 m_board->BuildConnectivity();
227
228 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
229
230 bds.m_DRCEngine = std::make_shared<DRC_ENGINE>( m_board.get(), &bds );
231 bds.m_DRCEngine->InitEngine( wxFileName( rulePath.string() ) );
232
233 for( int ii = DRCE_FIRST; ii <= DRCE_LAST; ++ii )
235
237
238 std::vector<std::shared_ptr<DRC_ITEM>> violations;
239
241 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos, int aLayer,
242 const std::function<void( PCB_MARKER* )>& aPathGenerator )
243 {
244 if( aItem->GetErrorCode() == DRCE_CREEPAGE )
245 violations.push_back( aItem );
246 } );
247
248 bds.m_DRCEngine->RunTests( EDA_UNITS::MM, true, false );
249
251
252 int ruleAreaHits = 0;
253
254 for( const std::shared_ptr<DRC_ITEM>& item : violations )
255 {
256 BOOST_TEST_MESSAGE( wxString::Format( " Violation: %s", item->GetErrorMessage( false ) ) );
257
258 for( const KIID& id : { item->GetMainItemID(), item->GetAuxItemID() } )
259 {
260 BOARD_ITEM* boardItem = m_board->ResolveItem( id, true );
261
262 if( boardItem && boardItem->Type() == PCB_ZONE_T && static_cast<ZONE*>( boardItem )->GetIsRuleArea() )
263 {
264 ++ruleAreaHits;
265 }
266 }
267 }
268
269 BOOST_CHECK_MESSAGE( ruleAreaHits == 0,
270 wxString::Format( "%d creepage violations name the rule area as a violating item. A "
271 "rule area is a boundary, not copper.",
272 ruleAreaHits ) );
273
274 BOOST_CHECK_MESSAGE( violations.empty(),
275 wxString::Format( "Expected no creepage violations, got %d.", (int) violations.size() ) );
276}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
virtual void SetNet(NETINFO_ITEM *aNetInfo)
Set a NET_INFO object for the item.
void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
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)
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
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
void InitEngine(const wxFileName &aRulePath)
Initialize the DRC engine.
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
Definition kiid.h:46
const std::filesystem::path & Path() const
Get the path to the temporary directory as a std::filesystem::path.
Definition file_utils.h:59
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
Handle the data for a net.
Definition netinfo.h:50
void SetEnd(const VECTOR2I &aEnd) override
void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
void SetStart(const VECTOR2I &aStart) override
void SetEnd(const VECTOR2I &aEnd)
Definition pcb_track.h:89
void SetStart(const VECTOR2I &aStart)
Definition pcb_track.h:92
virtual void SetWidth(int aWidth)
Definition pcb_track.h:86
Handle a list of polygons defining a copper zone.
Definition zone.h:70
void SetDoNotAllowPads(bool aEnable)
Definition zone.h:826
bool GetIsRuleArea() const
Accessors to parameters used in Rule Area zones:
Definition zone.h:807
bool AppendCorner(const VECTOR2I &aPosition, int aHoleIdx, bool aAllowDuplication=false)
Add a new corner to the zone outline (to the main outline or a hole)
Definition zone.cpp:1441
void SetIsRuleArea(bool aEnable)
Definition zone.h:808
void SetDoNotAllowTracks(bool aEnable)
Definition zone.h:825
void SetLayerSet(const LSET &aLayerSet) override
Definition zone.cpp:666
void SetDoNotAllowVias(bool aEnable)
Definition zone.h:824
void SetDoNotAllowFootprints(bool aEnable)
Definition zone.h:827
void SetDoNotAllowZoneFills(bool aEnable)
Definition zone.h:823
@ DRCE_CREEPAGE
Definition drc_item.h:42
@ DRCE_FIRST
Definition drc_item.h:36
@ DRCE_LAST
Definition drc_item.h:131
@ RECTANGLE
Use RECTANGLE instead of RECT to avoid collision in a Windows header.
Definition eda_shape.h:57
@ Edge_Cuts
Definition layer_ids.h:108
@ B_Cu
Definition layer_ids.h:61
@ F_Cu
Definition layer_ids.h:60
void LoadBoard(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< BOARD > &aBoard)
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_IGNORE
std::unique_ptr< BOARD > m_board
DRC_CREEPAGE_TEST_FIXTURE()=default
BOOST_FIXTURE_TEST_CASE(CreepageHVvsGND, DRC_CREEPAGE_TEST_FIXTURE)
BOOST_TEST_MESSAGE("Polyline has "<< chain.PointCount()<< " points")
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition typeinfo.h:100
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683