KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_board_netlist_updater_nested_groups.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 3
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/*
21 * Regression test for https://gitlab.com/kicad/code/kicad/-/work_items/24766
22 *
23 * A nested schematic group must stay nested on the board after Update PCB from Schematic.
24 *
25 * nested_groups.net was exported by KiCad from qa/data/eeschema/nested_groups.kicad_sch:
26 * kicad-cli sch export netlist --format kicadsexpr
27 */
28
29#include <board.h>
30#include <footprint.h>
31#include <pcb_group.h>
35#include <lib_id.h>
36#include <project.h>
38#include <tool/tool_manager.h>
42
43
44namespace
45{
46
47FOOTPRINT* addFootprint( BOARD* aBoard, const wxString& aRef, const LIB_ID& aFpid )
48{
49 FOOTPRINT* fp = new FOOTPRINT( aBoard );
50 fp->SetReference( aRef );
51 fp->SetFPID( aFpid );
52 aBoard->Add( fp );
53 return fp;
54}
55
56
57KIID addComponent( NETLIST& aNetlist, const wxString& aRef, const LIB_ID& aFpid )
58{
59 KIID kiid;
60 aNetlist.AddComponent( new COMPONENT( aFpid, aRef, aRef, KIID_PATH(), std::vector<KIID>{ kiid } ) );
61 return kiid;
62}
63
64
65PCB_GROUP* findGroup( BOARD* aBoard, const KIID& aUuid )
66{
67 for( PCB_GROUP* group : aBoard->Groups() )
68 {
69 if( group->m_Uuid == aUuid )
70 return group;
71 }
72
73 return nullptr;
74}
75
76
77KIID_PATH pathOf( const KIID& aUuid )
78{
80 path.push_back( aUuid );
81 return path;
82}
83
84
85PCB_GROUP* findGroupByName( BOARD* aBoard, const wxString& aName )
86{
87 for( PCB_GROUP* group : aBoard->Groups() )
88 {
89 if( group->GetName() == aName )
90 return group;
91 }
92
93 return nullptr;
94}
95
96
97void loadNetlist( NETLIST& aNetlist, const wxString& aFileName )
98{
99 try
100 {
101 std::unique_ptr<NETLIST_READER> netlistReader(
102 NETLIST_READER::GetNetlistReader( &aNetlist, aFileName, wxEmptyString ) );
103
104 BOOST_REQUIRE_MESSAGE( netlistReader, "no netlist reader for " + aFileName );
105 netlistReader->LoadNetlist();
106 }
107 catch( const IO_ERROR& ioe )
108 {
109 BOOST_FAIL( "netlist load failed: " + ioe.What().ToStdString() );
110 }
111}
112
113} // namespace
114
115
116BOOST_AUTO_TEST_SUITE( BoardNetlistUpdaterNestedGroups )
117
118
119BOOST_AUTO_TEST_CASE( NestedSchematicGroupNestsOnBoard )
120{
121 // The updater dereferences the board's project, so the board needs one.
122 SETTINGS_MANAGER settingsManager;
123 settingsManager.LoadProject( "" );
124
125 std::unique_ptr<BOARD> board = std::make_unique<BOARD>();
126 board->SetProject( &settingsManager.Prj() );
127
128 LIB_ID fpid;
129 BOOST_REQUIRE_EQUAL( fpid.Parse( wxS( "TestLib:R" ) ), -1 );
130
131 addFootprint( board.get(), wxS( "R1" ), fpid );
132 addFootprint( board.get(), wxS( "R2" ), fpid );
133 addFootprint( board.get(), wxS( "X3" ), fpid );
134 addFootprint( board.get(), wxS( "X4" ), fpid );
135 addFootprint( board.get(), wxS( "X5" ), fpid );
136
138
139 KIID r1 = addComponent( netlist, wxS( "R1" ), fpid );
140 KIID r2 = addComponent( netlist, wxS( "R2" ), fpid );
141 KIID x3 = addComponent( netlist, wxS( "X3" ), fpid );
142 KIID x4 = addComponent( netlist, wxS( "X4" ), fpid );
143 KIID x5 = addComponent( netlist, wxS( "X5" ), fpid );
144
145 KIID childUuid;
146 KIID parentUuid;
147
148 netlist.AddGroup( new NETLIST_GROUP( wxS( "RPI" ), childUuid.AsString(), LIB_ID(),
149 std::vector<KIID_PATH>{ pathOf( r1 ), pathOf( r2 ) } ) );
150
151 // RPI1 nests RPI: its members include the path RPI is referred to by.
152 netlist.AddGroup( new NETLIST_GROUP(
153 wxS( "RPI1" ), parentUuid.AsString(), LIB_ID(),
154 std::vector<KIID_PATH>{ pathOf( x3 ), pathOf( x4 ), pathOf( x5 ), pathOf( childUuid ) } ) );
155
156 netlist.ApplyGroupMembership();
157
158 TOOL_MANAGER toolMgr;
159 toolMgr.SetEnvironment( board.get(), nullptr, nullptr, nullptr, nullptr );
160 toolMgr.RegisterTool( new KI_TEST::DUMMY_TOOL() );
161
162 BOARD_NETLIST_UPDATER updater( &toolMgr, board.get() );
163 updater.SetIsDryRun( false );
164 updater.SetWillDrag( false );
165 updater.SetTransferGroups( true );
166 updater.SetReplaceFootprints( false );
167 updater.SetDeleteUnusedFootprints( false );
168
169 BOOST_REQUIRE( updater.UpdateNetlist( netlist ) );
170
171 PCB_GROUP* parentGroup = findGroup( board.get(), parentUuid );
172 PCB_GROUP* childGroup = findGroup( board.get(), childUuid );
173
174 BOOST_REQUIRE_MESSAGE( parentGroup, "parent group RPI1 was not created on the board" );
175 BOOST_REQUIRE_MESSAGE( childGroup, "child group RPI was not created on the board" );
176
177 BOOST_CHECK_MESSAGE( childGroup->GetParentGroup() == static_cast<EDA_GROUP*>( parentGroup ),
178 "child group RPI is not nested inside parent group RPI1 "
179 "(nested group membership was lost on update)" );
180
181 BOOST_CHECK_MESSAGE( parentGroup->GetItems().count( childGroup ) == 1,
182 "parent group RPI1 does not list child group RPI as a member" );
183}
184
185
186BOOST_AUTO_TEST_CASE( RootSheetNestedGroupNestsOnBoard )
187{
188 SETTINGS_MANAGER settingsManager;
189 settingsManager.LoadProject( "" );
190
191 std::unique_ptr<BOARD> board = std::make_unique<BOARD>();
192 board->SetProject( &settingsManager.Prj() );
193
194 LIB_ID fpid;
195 BOOST_REQUIRE_EQUAL( fpid.Parse( wxS( "Resistor_SMD:R_0805_2012Metric" ) ), -1 );
196
197 addFootprint( board.get(), wxS( "R1" ), fpid );
198 addFootprint( board.get(), wxS( "R2" ), fpid );
199 addFootprint( board.get(), wxS( "R3" ), fpid );
200
202 loadNetlist( netlist, wxString::FromUTF8( KI_TEST::GetPcbnewTestDataDir() ) + wxS( "netlists/nested_groups.net" ) );
203
204 BOOST_REQUIRE_EQUAL( netlist.GetCount(), 3 );
205
206 TOOL_MANAGER toolMgr;
207 toolMgr.SetEnvironment( board.get(), nullptr, nullptr, nullptr, nullptr );
208 toolMgr.RegisterTool( new KI_TEST::DUMMY_TOOL() );
209
210 BOARD_NETLIST_UPDATER updater( &toolMgr, board.get() );
211 updater.SetIsDryRun( false );
212 updater.SetWillDrag( false );
213 updater.SetTransferGroups( true );
214 updater.SetReplaceFootprints( false );
215 updater.SetDeleteUnusedFootprints( false );
216
217 BOOST_REQUIRE( updater.UpdateNetlist( netlist ) );
218
219 BOOST_CHECK_EQUAL( board->Groups().size(), 2u );
220
221 PCB_GROUP* outer = findGroupByName( board.get(), wxS( "OUTER" ) );
222 PCB_GROUP* inner = findGroupByName( board.get(), wxS( "INNER" ) );
223
224 BOOST_REQUIRE_MESSAGE( outer, "group OUTER was not created on the board" );
225 BOOST_REQUIRE_MESSAGE( inner, "group INNER was not created on the board" );
226
227 BOOST_CHECK_MESSAGE( inner->GetParentGroup() == static_cast<EDA_GROUP*>( outer ),
228 "group INNER is not nested inside group OUTER" );
229
230 BOOST_CHECK_MESSAGE( outer->GetItems().count( inner ) == 1, "group OUTER does not list group INNER as a member" );
231}
232
233
234BOOST_AUTO_TEST_CASE( UnnestedSchematicGroupUnnestsOnBoard )
235{
236 SETTINGS_MANAGER settingsManager;
237 settingsManager.LoadProject( "" );
238
239 std::unique_ptr<BOARD> board = std::make_unique<BOARD>();
240 board->SetProject( &settingsManager.Prj() );
241
242 LIB_ID fpid;
243 BOOST_REQUIRE_EQUAL( fpid.Parse( wxS( "TestLib:R" ) ), -1 );
244
245 addFootprint( board.get(), wxS( "R1" ), fpid );
246 addFootprint( board.get(), wxS( "R2" ), fpid );
247 addFootprint( board.get(), wxS( "R3" ), fpid );
248
250
251 KIID r1 = addComponent( netlist, wxS( "R1" ), fpid );
252 KIID r2 = addComponent( netlist, wxS( "R2" ), fpid );
253 KIID r3 = addComponent( netlist, wxS( "R3" ), fpid );
254
255 KIID innerUuid;
256 KIID outerUuid;
257
258 netlist.AddGroup( new NETLIST_GROUP( wxS( "INNER" ), innerUuid.AsString(), LIB_ID(),
259 std::vector<KIID_PATH>{ pathOf( r1 ), pathOf( r2 ) } ) );
260 netlist.AddGroup( new NETLIST_GROUP( wxS( "OUTER" ), outerUuid.AsString(), LIB_ID(),
261 std::vector<KIID_PATH>{ pathOf( r3 ), pathOf( innerUuid ) } ) );
262 netlist.ApplyGroupMembership();
263
264 TOOL_MANAGER toolMgr;
265 toolMgr.SetEnvironment( board.get(), nullptr, nullptr, nullptr, nullptr );
266 toolMgr.RegisterTool( new KI_TEST::DUMMY_TOOL() );
267
268 auto update = [&]()
269 {
270 BOARD_NETLIST_UPDATER updater( &toolMgr, board.get() );
271 updater.SetIsDryRun( false );
272 updater.SetWillDrag( false );
273 updater.SetTransferGroups( true );
274 updater.SetReplaceFootprints( false );
275 updater.SetDeleteUnusedFootprints( false );
276
277 BOOST_REQUIRE( updater.UpdateNetlist( netlist ) );
278 };
279
280 update();
281
282 PCB_GROUP* outer = findGroup( board.get(), outerUuid );
283 PCB_GROUP* inner = findGroup( board.get(), innerUuid );
284
285 BOOST_REQUIRE_MESSAGE( outer, "group OUTER was not created on the board" );
286 BOOST_REQUIRE_MESSAGE( inner, "group INNER was not created on the board" );
287 BOOST_REQUIRE_MESSAGE( inner->GetParentGroup() == static_cast<EDA_GROUP*>( outer ),
288 "the first update did not nest INNER inside OUTER" );
289
290 // INNER is un-nested in the schematic, so the next netlist no longer lists it in OUTER.
291 NETLIST_GROUP* outerNetlistGroup = netlist.GetGroupByUuid( outerUuid );
292
293 BOOST_REQUIRE( outerNetlistGroup );
294 outerNetlistGroup->members = { pathOf( r3 ) };
295
296 update();
297
298 outer = findGroup( board.get(), outerUuid );
299 inner = findGroup( board.get(), innerUuid );
300
301 BOOST_REQUIRE_MESSAGE( outer && inner, "the second update dropped one of the groups" );
302
303 BOOST_CHECK_MESSAGE( outer->GetItems().count( inner ) == 0, "group OUTER still lists group INNER as a member" );
304
305 BOOST_CHECK_MESSAGE( inner->GetParentGroup() == nullptr, "group INNER is still nested inside group OUTER" );
306}
307
308
309BOOST_AUTO_TEST_CASE( SchematicParentHoldingOnlyGroupsIsCreatedOnBoard )
310{
311 SETTINGS_MANAGER settingsManager;
312 settingsManager.LoadProject( "" );
313
314 std::unique_ptr<BOARD> board = std::make_unique<BOARD>();
315 board->SetProject( &settingsManager.Prj() );
316
317 LIB_ID fpid;
318 BOOST_REQUIRE_EQUAL( fpid.Parse( wxS( "TestLib:R" ) ), -1 );
319
320 addFootprint( board.get(), wxS( "R1" ), fpid );
321
323
324 KIID r1 = addComponent( netlist, wxS( "R1" ), fpid );
325
326 KIID innerUuid;
327 KIID outerUuid;
328 KIID topUuid;
329
330 // Only INNER holds symbols. OUTER and TOP hold nothing but the group below them, so the
331 // footprint update has nothing to create them from.
332 netlist.AddGroup( new NETLIST_GROUP( wxS( "INNER" ), innerUuid.AsString(), LIB_ID(),
333 std::vector<KIID_PATH>{ pathOf( r1 ) } ) );
334 netlist.AddGroup( new NETLIST_GROUP( wxS( "OUTER" ), outerUuid.AsString(), LIB_ID(),
335 std::vector<KIID_PATH>{ pathOf( innerUuid ) } ) );
336 netlist.AddGroup( new NETLIST_GROUP( wxS( "TOP" ), topUuid.AsString(), LIB_ID(),
337 std::vector<KIID_PATH>{ pathOf( outerUuid ) } ) );
338 netlist.ApplyGroupMembership();
339
340 TOOL_MANAGER toolMgr;
341 toolMgr.SetEnvironment( board.get(), nullptr, nullptr, nullptr, nullptr );
342 toolMgr.RegisterTool( new KI_TEST::DUMMY_TOOL() );
343
344 auto update = [&]()
345 {
346 BOARD_NETLIST_UPDATER updater( &toolMgr, board.get() );
347 updater.SetIsDryRun( false );
348 updater.SetWillDrag( false );
349 updater.SetTransferGroups( true );
350 updater.SetReplaceFootprints( false );
351 updater.SetDeleteUnusedFootprints( false );
352
353 BOOST_REQUIRE( updater.UpdateNetlist( netlist ) );
354 };
355
356 update();
357
358 PCB_GROUP* inner = findGroup( board.get(), innerUuid );
359 PCB_GROUP* outer = findGroup( board.get(), outerUuid );
360 PCB_GROUP* top = findGroup( board.get(), topUuid );
361
362 BOOST_REQUIRE_MESSAGE( inner, "group INNER was not created on the board" );
363 BOOST_REQUIRE_MESSAGE( outer, "group OUTER was not created on the board" );
364 BOOST_REQUIRE_MESSAGE( top, "group TOP was not created on the board" );
365
366 BOOST_CHECK_MESSAGE( inner->GetParentGroup() == static_cast<EDA_GROUP*>( outer ),
367 "group INNER is not nested inside group OUTER" );
368
369 BOOST_CHECK_MESSAGE( outer->GetParentGroup() == static_cast<EDA_GROUP*>( top ),
370 "group OUTER is not nested inside group TOP" );
371
372 // A second update of an unchanged schematic must not create the parents again.
373 update();
374
375 BOOST_CHECK_EQUAL( board->Groups().size(), 3u );
376}
377
378
379BOOST_AUTO_TEST_CASE( DryRunDoesNotCreateTheMissingParent )
380{
381 SETTINGS_MANAGER settingsManager;
382 settingsManager.LoadProject( "" );
383
384 std::unique_ptr<BOARD> board = std::make_unique<BOARD>();
385 board->SetProject( &settingsManager.Prj() );
386
387 LIB_ID fpid;
388 BOOST_REQUIRE_EQUAL( fpid.Parse( wxS( "TestLib:R" ) ), -1 );
389
390 addFootprint( board.get(), wxS( "R1" ), fpid );
391 addFootprint( board.get(), wxS( "R2" ), fpid );
392
394
395 KIID r1 = addComponent( netlist, wxS( "R1" ), fpid );
396 KIID r2 = addComponent( netlist, wxS( "R2" ), fpid );
397
398 KIID innerUuid;
399 KIID outerUuid;
400
401 netlist.AddGroup( new NETLIST_GROUP( wxS( "INNER" ), innerUuid.AsString(), LIB_ID(),
402 std::vector<KIID_PATH>{ pathOf( r1 ), pathOf( r2 ) } ) );
403 netlist.ApplyGroupMembership();
404
405 TOOL_MANAGER toolMgr;
406 toolMgr.SetEnvironment( board.get(), nullptr, nullptr, nullptr, nullptr );
407 toolMgr.RegisterTool( new KI_TEST::DUMMY_TOOL() );
408
409 auto update = [&]( bool aDryRun )
410 {
411 BOARD_NETLIST_UPDATER updater( &toolMgr, board.get() );
412 updater.SetIsDryRun( aDryRun );
413 updater.SetWillDrag( false );
414 updater.SetTransferGroups( true );
415 updater.SetReplaceFootprints( false );
416 updater.SetDeleteUnusedFootprints( false );
417
418 BOOST_REQUIRE( updater.UpdateNetlist( netlist ) );
419 };
420
421 update( false );
422
423 BOOST_REQUIRE_EQUAL( board->Groups().size(), 1u );
424
425 // The schematic now wraps INNER, so a real update would create OUTER. A dry run must not.
426 netlist.AddGroup( new NETLIST_GROUP( wxS( "OUTER" ), outerUuid.AsString(), LIB_ID(),
427 std::vector<KIID_PATH>{ pathOf( innerUuid ) } ) );
428
429 update( true );
430
431 BOOST_CHECK_MESSAGE( board->Groups().size() == 1, "a dry run created a group on the board" );
432 BOOST_CHECK_MESSAGE( findGroup( board.get(), outerUuid ) == nullptr, "a dry run created group OUTER on the board" );
433
434 update( false );
435
436 BOOST_CHECK_EQUAL( board->Groups().size(), 2u );
437}
438
439
General utilities for PCB file IO for QA programs.
Update the BOARD with a new netlist.
bool UpdateNetlist(NETLIST &aNetlist)
Update the board's components according to the new netlist.
void SetWillDrag(bool aWillDrag)
void SetIsDryRun(bool aEnabled)
Enable dry run mode (just report, no changes to PCB).
void SetDeleteUnusedFootprints(bool aEnabled)
void SetReplaceFootprints(bool aEnabled)
void SetTransferGroups(bool aEnabled)
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition board.cpp:1548
const GROUPS & Groups() const
The groups must maintain the following invariants.
Definition board.h:518
A set of EDA_ITEMs (i.e., without duplicates).
Definition eda_group.h:43
std::unordered_set< EDA_ITEM * > & GetItems()
Definition eda_group.h:78
virtual EDA_GROUP * GetParentGroup() const
Definition eda_item.h:116
void SetFPID(const LIB_ID &aFPID)
Definition footprint.h:418
void SetReference(const wxString &aReference)
Definition footprint.h:799
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
Definition kiid.h:46
wxString AsString() const
Definition kiid.cpp:264
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition lib_id.cpp:65
static NETLIST_READER * GetNetlistReader(NETLIST *aNetlist, const wxString &aNetlistFileName, const wxString &aCompFootprintFileName=wxEmptyString)
Attempt to determine the net list file type of aNetlistFileName and return the appropriate NETLIST_RE...
Store information read from a netlist along with the flags used to update the NETLIST in the BOARD.
void AddComponent(COMPONENT *aComponent)
Add aComponent to the NETLIST.
A set of BOARD_ITEMs (i.e., without duplicates).
Definition pcb_group.h:51
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
bool LoadProject(const wxString &aFullPath, bool aSetActive=true, bool aOverrideLock=false)
Load a project or sets up a new project with a specified path.
Master controller class:
void RegisterTool(TOOL_BASE *aTool)
Add a tool to the manager set and sets it up.
void SetEnvironment(EDA_ITEM *aModel, KIGFX::VIEW *aView, KIGFX::VIEW_CONTROLS *aViewControls, APP_SETTINGS_BASE *aSettings, TOOLS_HOLDER *aFrame)
Set the work environment (model, view, view controls and the parent window).
std::string GetPcbnewTestDataDir()
Utility which returns a path to the data directory where the test board files are stored.
Class to handle a set of BOARD_ITEMs.
std::vector< KIID > members
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_CASE(NestedSchematicGroupNestsOnBoard)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE_EQUAL(rows.size(), 1)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_REQUIRE_MESSAGE(dc.CacheTableInfo(table, { "part_id", "val" }), dc.GetLastError())
BOOST_REQUIRE(res.columns()==1)
BOOST_AUTO_TEST_SUITE_END()
static FOOTPRINT * addFootprint(BOARD &aBoard, const wxString &aReference)
std::string netlist
std::string path
KIBIS top(path, &reporter)