KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_connectivity_revision.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 modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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
22
23#include <advanced_config.h>
26#include <lib_symbol.h>
27#include <sch_commit.h>
28#include <sch_pin.h>
29#include <sch_screen.h>
30#include <sch_sheet.h>
31#include <sch_symbol.h>
32#include <schematic.h>
34#include <tool/tool_manager.h>
35#include <scoped_set_reset.h>
36
37
39{
41 std::unique_ptr<SCHEMATIC> schematic;
42};
43
44
45BOOST_FIXTURE_TEST_SUITE( ConnectivityRevision, CONNECTIVITY_REVISION_FIXTURE )
46
47BOOST_AUTO_TEST_CASE( ScreenIndexTracksSymbolPinReplacementAndUndo )
48{
49 auto& config = const_cast<ADVANCED_CFG&>( ADVANCED_CFG::GetCfg() );
50 SCOPED_SET_RESET restoreEngine( config.m_ConnectivityEngine, config.m_ConnectivityEngine );
51 SCOPED_SET_RESET restoreIncremental( config.m_IncrementalConnectivity, config.m_IncrementalConnectivity );
52 config.m_ConnectivityEngine = true;
53 config.m_IncrementalConnectivity = true;
54
55 KI_TEST::LoadSchematic( settings, "issue7203", schematic );
56 SCH_SCREEN* screen = schematic->GetTopLevelSheets().front()->GetScreen();
57 SCH_SYMBOL* symbol = nullptr;
58
59 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
60 {
61 auto* candidate = static_cast<SCH_SYMBOL*>( item );
62
63 if( candidate->GetLibSymbolRef() && candidate->GetPins().size() >= 2 )
64 {
65 symbol = candidate;
66 break;
67 }
68 }
69
70 BOOST_REQUIRE( symbol );
71 TOOL_MANAGER manager;
72 manager.SetEnvironment( schematic.get(), nullptr, nullptr, nullptr, nullptr );
73 schematic->RebuildConnectivity();
75 const auto verify = [&]()
76 {
77 const std::string incremental = SCH_CONNECTIVITY::Dump( *schematic, schematic->Connectivity() );
78 rebuilt.Update( *schematic, true );
79 BOOST_CHECK_EQUAL( incremental, SCH_CONNECTIVITY::Dump( *schematic, rebuilt ) );
80 };
81 verify();
82
83 SCH_PIN* pin = symbol->GetPins().front();
84 const KIID id = pin->m_Uuid;
85 const wxString number = pin->GetNumber();
86 const KIID_PATH path = schematic->Hierarchy().front().Path();
87 BOOST_REQUIRE( schematic->Connectivity().Connection( id, path ) );
88 BOOST_REQUIRE( screen->GetConnectivityItem( id ) == pin );
89 std::unique_ptr<SCH_ITEM> undo( static_cast<SCH_ITEM*>( symbol->Clone() ) );
90 auto updated = symbol->GetLibSymbolRef()->Flatten();
91
92 for( SCH_PIN* libPin : updated->GetPinsByNumber( number ) )
93 updated->RemoveDrawItem( libPin );
94
95 SCH_COMMIT replacement( &manager );
96 replacement.Modify( symbol, screen );
97 symbol->SetLibSymbol( updated.release() );
98 BOOST_CHECK( !screen->GetConnectivityItem( id ) );
99 replacement.Push( "Replace native symbol pin set", SKIP_UNDO );
100 BOOST_CHECK( !schematic->Connectivity().Connection( id, path ) );
101 verify();
102
103 SCH_COMMIT restoration( &manager );
104 restoration.Modify( symbol, screen );
105 symbol->SwapItemData( undo.get() );
106 SCH_ITEM* restored = screen->GetConnectivityItem( id );
107 BOOST_REQUIRE( restored );
108 BOOST_CHECK( restored->GetParent() == symbol );
109 BOOST_CHECK( restored == symbol->GetPin( number ) );
110 restoration.Push( "Restore native symbol pin set", SKIP_UNDO );
111 BOOST_REQUIRE( schematic->Connectivity().Connection( id, path ) );
112 verify();
113}
114
115
116BOOST_AUTO_TEST_CASE( AssigningSymbolPreservesAlternatePinDefinitions )
117{
118 KI_TEST::LoadSchematic( settings, "issue22286/bugtest", schematic );
119 SCH_SCREEN* screen = schematic->GetTopLevelSheets().front()->GetScreen();
120 SCH_SYMBOL* symbol = nullptr;
121 SCH_PIN* pin = nullptr;
122
123 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
124 {
125 auto* candidate = static_cast<SCH_SYMBOL*>( item );
126
127 for( SCH_PIN* candidatePin : candidate->GetPins() )
128 {
129 if( !candidatePin->GetAlt().IsEmpty() )
130 {
131 symbol = candidate;
132 pin = candidatePin;
133 break;
134 }
135 }
136
137 if( symbol )
138 break;
139 }
140
141 BOOST_REQUIRE( symbol );
142 const KIID id = pin->m_Uuid;
143 const wxString alternate = pin->GetAlt();
144 const auto type = pin->GetType();
145 BOOST_REQUIRE( screen->GetConnectivityItem( id ) == pin );
146 auto copy = std::make_unique<SCH_SYMBOL>( *symbol );
147 *symbol = *copy;
148 auto* updated = dynamic_cast<SCH_PIN*>( screen->GetConnectivityItem( id ) );
149 BOOST_REQUIRE( updated );
150 BOOST_REQUIRE( updated->GetLibPin() );
151 BOOST_CHECK_EQUAL( updated->GetAlt(), alternate );
152 BOOST_CHECK( updated->GetType() == type );
153 BOOST_CHECK( updated->GetLibPin()->GetParent() == symbol->GetLibSymbolRef().get() );
154}
155
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Modify a given item in the model.
Definition commit.h:102
EDA_ITEM * GetParent() const
Definition eda_item.h:112
EE_TYPE OfType(KICAD_T aType) const
Definition sch_rtree.h:248
Definition kiid.h:46
void RemoveDrawItem(SCH_ITEM *aItem)
Remove draw aItem from list.
std::unique_ptr< LIB_SYMBOL > Flatten() const
Return a flattened symbol inheritance to the caller.
Commit for the schematic and symbol editors.
Definition sch_commit.h:54
virtual void Push(const wxString &aMessage=wxT("A commit"), int aCommitFlags=0) override
Execute the changes.
Model-owned source resolver.
void Update(SCHEMATIC &aSchematic, bool aRebuild=false)
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:170
void SwapItemData(SCH_ITEM *aImage)
Swap data between aItem and aImage.
Definition sch_item.cpp:841
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:122
SCH_ITEM * GetConnectivityItem(const KIID &aId) const
Resolve a drawing item or a connectable child on this screen; ambiguous IDs return null.
Schematic symbol object.
Definition sch_symbol.h:74
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
std::vector< const SCH_PIN * > GetPins(const SCH_SHEET_PATH *aSheet) const
Retrieve a list of the SCH_PINs for the given sheet path.
SCH_PIN * GetPin(const wxString &number) const
Find a symbol pin by number.
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition sch_symbol.h:182
void SetLibSymbol(LIB_SYMBOL *aLibSymbol)
Set this schematic symbol library symbol reference to aLibSymbol.
RAII class that sets an value at construction and resets it to the original value at destruction.
Master controller class:
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).
void LoadSchematic(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< SCHEMATIC > &aSchematic)
std::string Dump(SCHEMATIC &aSchematic)
Canonical, pointer-free connectivity rows for migration and rebuild comparisons.
#define SKIP_UNDO
Definition sch_commit.h:38
std::unique_ptr< SCHEMATIC > schematic
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_CASE(ScreenIndexTracksSymbolPinReplacementAndUndo)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
std::string path
KIBIS_PIN * pin
BOOST_CHECK_EQUAL(result, "25.4")
@ SCH_SYMBOL_T
Definition typeinfo.h:168