KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_label_bus_connectivity.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
29
31
32#include <advanced_config.h>
33#include <connection_graph.h>
35#include <sch_commit.h>
36#include <schematic.h>
37#include <sch_label.h>
38#include <sch_line.h>
39#include <sch_screen.h>
40#include <sch_sheet.h>
41#include <sch_sheet_pin.h>
42#include <scoped_set_reset.h>
44#include <tool/tool_manager.h>
45
46
47BOOST_AUTO_TEST_SUITE( LabelBusConnectivity )
48
49
51{
53 m_mgr()
54 {
55 m_mgr.LoadProject( "" );
56 m_schematic = std::make_unique<SCHEMATIC>( &m_mgr.Prj() );
57 m_schematic->Reset();
58 SCH_SHEET* defaultSheet = m_schematic->GetTopLevelSheet( 0 );
59
60 m_screen = new SCH_SCREEN( m_schematic.get() );
61 m_sheet = new SCH_SHEET( m_schematic.get() );
62 m_sheet->SetScreen( m_screen );
63 m_schematic->AddTopLevelSheet( m_sheet );
64 m_schematic->RemoveTopLevelSheet( defaultSheet );
65 delete defaultSheet;
66 }
67
69 std::unique_ptr<SCHEMATIC> m_schematic;
72};
73
74
84{
85 // Create a bus wire
86 SCH_LINE* busWire = new SCH_LINE( VECTOR2I( 0, 0 ), LAYER_BUS );
87 busWire->SetEndPoint( VECTOR2I( 5000000, 0 ) );
88 m_screen->Append( busWire );
89
90 // Create a label with a non-bus name, positioned on the bus wire
91 SCH_LABEL* label = new SCH_LABEL( VECTOR2I( 2500000, 0 ), wxT( "test" ) );
92 m_screen->Append( label );
93
94 // Build initial connectivity
95 SCH_SHEET_LIST sheets = m_schematic->BuildSheetListSortedByPageNumbers();
96 m_schematic->ConnectionGraph()->Recalculate( sheets, true );
97
98 SCH_SHEET_PATH path = sheets[0];
99
100 // Verify initial state: bus wire should have BUS connection type but no members
101 // (because "test" is not a bus name)
102 SCH_CONNECTION* busConn = busWire->Connection( &path );
103 BOOST_REQUIRE( busConn != nullptr );
104 BOOST_CHECK( busConn->IsBus() );
105 BOOST_CHECK( busConn->Members().empty() );
106
107 TOOL_MANAGER manager;
108 manager.SetEnvironment( m_schematic.get(), nullptr, nullptr, nullptr, nullptr );
109 SCH_COMMIT commit( &manager );
110 commit.Modify( label, m_screen );
111 label->SetText( wxT( "test[0..7]" ) );
112 commit.Push( "Change label to a bus", SKIP_UNDO );
113
114 // The push updates only the active engine, and the new engine publishes through the facade
115 if( ADVANCED_CFG::GetCfg().m_ConnectivityEngine )
116 {
117 const auto view = m_schematic->Connectivity().Connection( busWire->m_Uuid, path.Path() );
118 BOOST_REQUIRE( view );
119 BOOST_CHECK( view->IsBus() );
120 BOOST_CHECK_EQUAL( view->Members().leaves.size(), 8 );
121 return;
122 }
123
124 // Verify: bus wire should now have members from the bus label
125 busConn = busWire->Connection( &path );
126 BOOST_REQUIRE( busConn != nullptr );
127 BOOST_CHECK( busConn->IsBus() );
128 BOOST_CHECK_MESSAGE( !busConn->Members().empty(),
129 "Bus wire should have members after label changed to bus name" );
130
131 // Verify the correct number of members (test[0..7] = 8 members)
132 if( !busConn->Members().empty() )
133 {
134 BOOST_CHECK_EQUAL( busConn->Members().size(), 8 );
135 }
136}
137
138
144{
145 // Create a bus wire
146 SCH_LINE* busWire = new SCH_LINE( VECTOR2I( 0, 0 ), LAYER_BUS );
147 busWire->SetEndPoint( VECTOR2I( 5000000, 0 ) );
148 m_screen->Append( busWire );
149
150 // Create a label with a bus name, positioned on the bus wire
151 SCH_LABEL* label = new SCH_LABEL( VECTOR2I( 2500000, 0 ), wxT( "DATA[0..3]" ) );
152 m_screen->Append( label );
153
154 // Build connectivity
155 SCH_SHEET_LIST sheets = m_schematic->BuildSheetListSortedByPageNumbers();
156 m_schematic->ConnectionGraph()->Recalculate( sheets, true );
157
158 SCH_SHEET_PATH path = sheets[0];
159
160 // Verify: bus wire should have members from the bus label
161 SCH_CONNECTION* busConn = busWire->Connection( &path );
162 BOOST_REQUIRE( busConn != nullptr );
163 BOOST_CHECK( busConn->IsBus() );
164 BOOST_CHECK_MESSAGE( !busConn->Members().empty(),
165 "Bus wire should have members from bus label" );
166
167 // Verify the correct number of members (DATA[0..3] = 4 members)
168 if( !busConn->Members().empty() )
169 {
170 BOOST_CHECK_EQUAL( busConn->Members().size(), 4 );
171 }
172}
173
174
181BOOST_FIXTURE_TEST_CASE( SheetPinBusConnectionSurvivesMove, LABEL_BUS_CONNECTIVITY_FIXTURE )
182{
183 const wxString busName( wxT( "bus{A B}" ) );
184 const int sheetLeft = 10000000;
185
186 SCH_SCREEN* childScreen = new SCH_SCREEN( m_schematic.get() );
187 SCH_SHEET* childSheet = new SCH_SHEET( m_schematic.get(), VECTOR2I( sheetLeft, 0 ),
188 VECTOR2I( 20000000, 20000000 ) );
189
190 childScreen->SetFileName( wxT( "child.kicad_sch" ) );
191 childSheet->SetScreen( childScreen );
192 childSheet->SetName( wxT( "child" ) );
193 childSheet->SetFileName( wxT( "child.kicad_sch" ) );
194 m_screen->Append( childSheet );
195
196 childScreen->Append( new SCH_HIERLABEL( VECTOR2I( 0, 0 ), busName ) );
197
198 // The pin rides the sheet's left edge, so its x has to track the sheet origin
199 SCH_SHEET_PIN* pin = new SCH_SHEET_PIN( childSheet, VECTOR2I( sheetLeft, 5000000 ), busName );
200 childSheet->AddPin( pin );
201
202 SCH_LINE* busWire = new SCH_LINE( VECTOR2I( 5000000, 5000000 ), LAYER_BUS );
203 busWire->SetEndPoint( pin->GetTextPos() );
204 m_screen->Append( busWire );
205
206 m_schematic->RefreshHierarchy();
207
208 SCH_SHEET_LIST sheets = m_schematic->Hierarchy();
209 m_schematic->ConnectionGraph()->Recalculate( sheets, true );
210
211 SCH_SHEET_PATH rootPath = sheets[0];
212 SCH_CONNECTION* pinConn = pin->Connection( &rootPath );
213
214 BOOST_REQUIRE_MESSAGE( pinConn != nullptr, "Sheet pin has no connection before the move" );
215 BOOST_REQUIRE_MESSAGE( pinConn->IsBus(), "Sheet pin is not a bus before the move" );
216
217 // Simulate dragging the pin along the sheet edge with its bus wire attached
218 const VECTOR2I newPos( sheetLeft, 8000000 );
219
220 pin->SetPosition( newPos );
221 busWire->SetEndPoint( pin->GetTextPos() );
222 pin->SetConnectivityDirty( true );
223 busWire->SetConnectivityDirty( true );
224
225 // A schematic this small is a minor graph, so SCHEMATIC::RecalculateConnections() rebuilds
226 // unconditionally after the move
227 BOOST_REQUIRE( m_schematic->ConnectionGraph()->IsMinor() );
228 m_schematic->ConnectionGraph()->Recalculate( sheets, true );
229
230 // SCH_PAINTER ignores the resolved connection while this flag is set, which is what drops
231 // the bus colour
232 BOOST_CHECK_MESSAGE( !pin->IsConnectivityDirty(),
233 "Sheet pin connectivity still dirty after recalculation" );
234
235 pinConn = pin->Connection( &rootPath );
236
237 BOOST_REQUIRE_MESSAGE( pinConn != nullptr, "Sheet pin lost its connection after the move" );
238 BOOST_CHECK_MESSAGE( pinConn->IsBus(), "Sheet pin lost its bus connection after the move" );
239}
240
241
242// A superset bus label drives even when the subset's name sorts first
244{
245 auto& enabled = const_cast<ADVANCED_CFG&>( ADVANCED_CFG::GetCfg() ).m_ConnectivityEngine;
246 SCOPED_SET_RESET restore( enabled, true );
247
248 SCH_LINE* busWire = new SCH_LINE( VECTOR2I( 0, 0 ), LAYER_BUS );
249 busWire->SetEndPoint( VECTOR2I( 5000000, 0 ) );
250 m_screen->Append( busWire );
251 m_screen->Append( new SCH_LABEL( VECTOR2I( 1000000, 0 ), wxS( "BUS{B}" ) ) );
252 m_screen->Append( new SCH_LABEL( VECTOR2I( 4000000, 0 ), wxS( "BUS{Z B}" ) ) );
253
254 SCH_SHEET_LIST sheets = m_schematic->BuildSheetListSortedByPageNumbers();
255 m_schematic->ConnectionGraph()->Recalculate( sheets, true );
256
257 const auto view = m_schematic->Connectivity().Connection( busWire->m_Uuid, sheets[0].Path() );
258 BOOST_REQUIRE( view );
259 BOOST_CHECK_EQUAL( view->Name( true ), wxS( "BUS{Z B}" ) );
260 BOOST_CHECK_EQUAL( view->Members().leaves.size(), 2 );
261}
262
263
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
const KIID m_Uuid
Definition eda_item.h:599
virtual void SetText(const wxString &aText)
Definition eda_text.cpp:237
Commit for the schematic and symbol editors.
Definition sch_commit.h:54
void Push(const wxString &aMessage=wxEmptyString, int aCommitFlags=0) override
Execute the changes.
Each graphical item can have a SCH_CONNECTION describing its logical connection (to a bus or net).
bool IsBus() const
const std::vector< std::shared_ptr< SCH_CONNECTION > > & Members() const
void SetConnectivityDirty(bool aDirty=true)
Set the dirty flag.
Definition sch_item.cpp:314
SCH_CONNECTION * Connection(const SCH_SHEET_PATH *aSheet=nullptr) const
Retrieve the connection associated with this object in the given sheet.
Definition sch_item.cpp:580
Segment description base class to describe items which have 2 end points (track, wire,...
Definition sch_line.h:39
void SetEndPoint(const VECTOR2I &aPosition)
Definition sch_line.h:146
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Define a sheet pin (label) used in sheets to create hierarchical schematics.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:50
void SetFileName(const wxString &aFilename)
Definition sch_sheet.h:434
void AddPin(SCH_SHEET_PIN *aSheetPin)
Add aSheetPin to the sheet.
void SetName(const wxString &aName)
Definition sch_sheet.h:185
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
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).
@ LAYER_BUS
Definition layer_ids.h:484
#define SKIP_UNDO
Definition sch_commit.h:38
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
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()
std::string path
KIBIS_PIN * pin
BOOST_FIXTURE_TEST_CASE(LabelNetToBusConnectivity, LABEL_BUS_CONNECTIVITY_FIXTURE)
Test that changing a label from a net name to a bus name properly updates the connected bus wire's co...
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708