KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_update_items_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 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
21
22#include <connection_graph.h>
23#include <jumper_group.h>
24#include <lib_symbol.h>
25#include <pin_type.h>
26
27#include <sch_bus_entry.h>
28#include <sch_line.h>
29#include <sch_pin.h>
30#include <sch_symbol.h>
31#include <sch_sheet.h>
32#include <sch_sheet_path.h>
33#include <sch_screen.h>
34#include <schematic.h>
35
36#include <algorithm>
37#include <map>
38#include <set>
39
41{
42 // Create schematic
43 SCHEMATIC schematic( nullptr );
44
45 // Create root sheet and screen
46 SCH_SCREEN* screen = new SCH_SCREEN( nullptr );
47 SCH_SHEET* sheet = new SCH_SHEET( nullptr, VECTOR2I( 0, 0 ), VECTOR2I( 1000, 1000 ) );
48 sheet->SetScreen( screen );
49 sheet->SetParent( &schematic );
50
51 SCH_SHEET_PATH sheetPath;
52 sheetPath.push_back( sheet );
53
54 // Build library symbol
55 LIB_SYMBOL lib( "TEST", nullptr );
56 lib.SetGlobalPower();
58 lib.JumperPinGroups().Add( JUMPER_GROUP::Make( { wxString( "3" ), wxString( "4" ) } ).value() );
59
60 auto make_pin = [&]( const wxString& num, const wxString& name, ELECTRICAL_PINTYPE type, const VECTOR2I& pos )
61 {
62 SCH_PIN* pin = new SCH_PIN( &lib );
63 pin->SetNumber( num );
64 pin->SetName( name );
65 pin->SetType( type );
66 pin->SetPosition( pos );
67 lib.AddDrawItem( pin );
68 };
69
70 make_pin( "1", "VCC", ELECTRICAL_PINTYPE::PT_POWER_IN, VECTOR2I( 0, 0 ) );
71 make_pin( "2", "A", ELECTRICAL_PINTYPE::PT_INPUT, VECTOR2I( 1000, 0 ) );
72 make_pin( "2", "B", ELECTRICAL_PINTYPE::PT_INPUT, VECTOR2I( 2000, 0 ) );
73 make_pin( "3", "C", ELECTRICAL_PINTYPE::PT_INPUT, VECTOR2I( 3000, 0 ) );
74 make_pin( "4", "D", ELECTRICAL_PINTYPE::PT_INPUT, VECTOR2I( 4000, 0 ) );
75
76 // Create schematic symbol instance
77 SCH_SYMBOL symbol( lib, lib.GetLibId(), &sheetPath, 0, 0, VECTOR2I( 0, 0 ) );
78 symbol.SetValueFieldText( "VCC" );
79 symbol.UpdatePins();
80
81 std::vector<SCH_PIN*> pins = symbol.GetPins( &sheetPath );
82 SCH_PIN* powerPin = nullptr;
83 SCH_PIN* dupA = nullptr;
84 SCH_PIN* dupB = nullptr;
85 SCH_PIN* jumpC = nullptr;
86 SCH_PIN* jumpD = nullptr;
87
88 for( SCH_PIN* pin : pins )
89 {
90 if( pin->GetNumber() == "1" )
91 powerPin = pin;
92 else if( pin->GetNumber() == "2" )
93 {
94 if( !dupA )
95 dupA = pin;
96 else
97 dupB = pin;
98 }
99 else if( pin->GetNumber() == "3" )
100 jumpC = pin;
101 else if( pin->GetNumber() == "4" )
102 jumpD = pin;
103 }
104
105 CONNECTION_GRAPH graph;
106 std::map<VECTOR2I, std::vector<SCH_ITEM*>> connection_map;
107 graph.updateSymbolConnectivity( sheetPath, &symbol, connection_map );
108
109 // Global power pin captured
110 BOOST_REQUIRE_EQUAL( graph.m_global_power_pins.size(), 1 );
111 BOOST_CHECK( graph.m_global_power_pins[0].second == powerPin );
112 BOOST_CHECK_EQUAL( powerPin->Connection( &sheetPath )->Name( true ), "VCC" );
113
114 // Duplicate pin numbers link together
115 const std::vector<SCH_ITEM*>& dupAConn = dupA->ConnectedItems( sheetPath );
116 BOOST_CHECK( std::find( dupAConn.begin(), dupAConn.end(), dupB ) != dupAConn.end() );
117 const std::vector<SCH_ITEM*>& dupBConn = dupB->ConnectedItems( sheetPath );
118 BOOST_CHECK( std::find( dupBConn.begin(), dupBConn.end(), dupA ) != dupBConn.end() );
119
120 // Jumper group links pins
121 const std::vector<SCH_ITEM*>& jumpCConn = jumpC->ConnectedItems( sheetPath );
122 BOOST_CHECK( std::find( jumpCConn.begin(), jumpCConn.end(), jumpD ) != jumpCConn.end() );
123 const std::vector<SCH_ITEM*>& jumpDConn = jumpD->ConnectedItems( sheetPath );
124 BOOST_CHECK( std::find( jumpDConn.begin(), jumpDConn.end(), jumpC ) != jumpDConn.end() );
125
126 // Connection map contains all pins
127 std::set<SCH_PIN*> mapPins;
128 for( const auto& [pos, items] : connection_map )
129 for( SCH_ITEM* item : items )
130 mapPins.insert( static_cast<SCH_PIN*>( item ) );
131 BOOST_CHECK_EQUAL( mapPins.size(), 5 );
132
133 // Item list contains all pins
134 BOOST_CHECK_EQUAL( graph.m_items.size(), 5 );
135 for( SCH_PIN* pin : { powerPin, dupA, dupB, jumpC, jumpD } )
136 {
137 BOOST_CHECK( std::find( graph.m_items.begin(), graph.m_items.end(), pin )
138 != graph.m_items.end() );
139 }
140
141 delete sheet; // deletes screen
142}
143
144
146{
147 std::map<VECTOR2I, std::vector<SCH_ITEM*>> cmap;
148
149 // Create schematic
150 SCHEMATIC schematic( nullptr );
151
152 // Create root sheet and screen
153 SCH_SCREEN* screen = new SCH_SCREEN( nullptr );
154 SCH_SHEET* sheet = new SCH_SHEET( nullptr, VECTOR2I( 0, 0 ), VECTOR2I( 1000, 1000 ) );
155 sheet->SetScreen( screen );
156 sheet->SetParent( &schematic );
157
158 SCH_SHEET_PATH sheetPath;
159 sheetPath.push_back( sheet );
160
161 CONNECTION_GRAPH graph;
162
163 // Wire line
164 SCH_LINE wire( VECTOR2I( 0, 0 ), LAYER_WIRE );
165 wire.SetEndPoint( VECTOR2I( 100, 0 ) );
166
167 graph.updateGenericItemConnectivity( sheetPath, &wire, cmap );
168 SCH_CONNECTION* wireConn = wire.Connection( &sheetPath );
169 BOOST_REQUIRE( wireConn );
170 BOOST_CHECK( wireConn->Type() == CONNECTION_TYPE::NET );
171 BOOST_CHECK( cmap.count( wire.GetStartPoint() ) );
172 BOOST_CHECK( cmap.count( wire.GetEndPoint() ) );
173
174 // Bus line
175 cmap.clear();
176 SCH_LINE bus( VECTOR2I( 0, 0 ), LAYER_BUS );
177 bus.SetEndPoint( VECTOR2I( 0, 100 ) );
178
179 graph.updateGenericItemConnectivity( sheetPath, &bus, cmap );
180 SCH_CONNECTION* busConn = bus.Connection( &sheetPath );
181 BOOST_REQUIRE( busConn );
182 BOOST_CHECK( busConn->Type() == CONNECTION_TYPE::BUS );
183 BOOST_CHECK( cmap.count( bus.GetStartPoint() ) );
184 BOOST_CHECK( cmap.count( bus.GetEndPoint() ) );
185
186 // Bus-to-bus entry
187 cmap.clear();
188 SCH_BUS_BUS_ENTRY busEntry( VECTOR2I( 0, 0 ), false );
189 SCH_LINE dummy1( VECTOR2I( 0, 0 ), LAYER_BUS );
190 SCH_LINE dummy2( VECTOR2I( 0, 0 ), LAYER_BUS );
191 busEntry.m_connected_bus_items[0] = &dummy1;
192 busEntry.m_connected_bus_items[1] = &dummy2;
193
194 graph.updateGenericItemConnectivity( sheetPath, &busEntry, cmap );
195 SCH_CONNECTION* bbConn = busEntry.Connection( &sheetPath );
196 BOOST_REQUIRE( bbConn );
197 BOOST_CHECK( bbConn->Type() == CONNECTION_TYPE::BUS );
198 BOOST_CHECK( busEntry.m_connected_bus_items[0] == nullptr );
199 BOOST_CHECK( busEntry.m_connected_bus_items[1] == nullptr );
200 auto ptsBB = busEntry.GetConnectionPoints();
201 BOOST_CHECK( cmap.count( ptsBB[0] ) );
202 BOOST_CHECK( cmap.count( ptsBB[1] ) );
203
204 // Bus-wire entry
205 cmap.clear();
206 SCH_BUS_WIRE_ENTRY bwEntry( VECTOR2I( 0, 0 ), false );
207 SCH_LINE dummyBus( VECTOR2I( 0, 0 ), LAYER_BUS );
208 bwEntry.m_connected_bus_item = &dummyBus;
209
210 graph.updateGenericItemConnectivity( sheetPath, &bwEntry, cmap );
211 SCH_CONNECTION* bwConn = bwEntry.Connection( &sheetPath );
212 BOOST_REQUIRE( bwConn );
213 BOOST_CHECK( bwConn->Type() == CONNECTION_TYPE::NET );
214 BOOST_CHECK( bwEntry.m_connected_bus_item == nullptr );
215 auto ptsBW = bwEntry.GetConnectionPoints();
216 BOOST_CHECK( cmap.count( ptsBW[0] ) );
217 BOOST_CHECK( cmap.count( ptsBW[1] ) );
218
219 // Pin
220 cmap.clear();
221 LIB_SYMBOL libSymbol( "PWR", nullptr );
222 SCH_PIN* libPin = new SCH_PIN( &libSymbol );
223 libPin->SetNumber( "1" );
224 libPin->SetName( "P" );
226 libPin->SetPosition( VECTOR2I( 10, 10 ) );
227 libSymbol.AddDrawItem( libPin );
228 libSymbol.SetGlobalPower();
229
230 SCH_SYMBOL symbol( libSymbol, libSymbol.GetLibId(), &sheetPath, 0, 0, VECTOR2I( 0, 0 ) );
231 symbol.SetRef( &sheetPath, "U1" );
232 symbol.UpdatePins();
233 SCH_PIN* pin = symbol.GetPins( &sheetPath )[0];
234
235 graph.updateGenericItemConnectivity( sheetPath, pin, cmap );
236 SCH_CONNECTION* pinConn = pin->Connection( &sheetPath );
237 BOOST_REQUIRE( pinConn );
238 BOOST_CHECK( pinConn->Type() == CONNECTION_TYPE::NET );
239 BOOST_CHECK( graph.m_global_power_pins.size() == 1 );
240 BOOST_CHECK( cmap.count( pin->GetPosition() ) );
241}
242
243BOOST_AUTO_TEST_SUITE( UpdateItemsConnectivity )
244BOOST_AUTO_TEST_CASE( SymbolConnectivityLinksPins )
245{
247}
248BOOST_AUTO_TEST_CASE( GenericItemConnectivity )
249{
251}
252
const char * name
CONNECTION_GRAPH(SCHEMATIC *aSchematic=nullptr, SCH_CONNECTIVITY::NETCHAIN_MANAGER *aNetChains=nullptr)
friend class SCHEMATIC
void updateGenericItemConnectivity(const SCH_SHEET_PATH &aSheet, SCH_ITEM *aItem, std::map< VECTOR2I, std::vector< SCH_ITEM * > > &aConnectionMap)
Update the connectivity of items that are not pins or symbols.
void updateSymbolConnectivity(const SCH_SHEET_PATH &aSheet, SCH_SYMBOL *aSymbol, std::map< VECTOR2I, std::vector< SCH_ITEM * > > &aConnectionMap)
Update the connectivity of a symbol and its pins.
std::vector< std::pair< SCH_SHEET_PATH, SCH_PIN * > > m_global_power_pins
std::vector< SCH_ITEM * > m_items
All connectable items in the schematic.
virtual void SetParent(EDA_ITEM *aParent)
Definition eda_item.cpp:153
void Add(JUMPER_GROUP aGroup)
Add a JUMPER_GROUP to the set.
static std::optional< JUMPER_GROUP > Make(std::set< wxString > aNames)
Factory method to create a JUMPER_GROUP from a set of names.
Define a library symbol object.
Definition lib_symbol.h:114
const LIB_ID & GetLibId() const override
Definition lib_symbol.h:190
void SetGlobalPower()
void SetDuplicatePinNumbersAreJumpers(bool aEnabled)
Definition lib_symbol.h:885
JUMPER_GROUP_SET & JumperPinGroups()
Each jumper pin group is a set of pin numbers that should be treated as internally connected.
Definition lib_symbol.h:891
void AddDrawItem(SCH_ITEM *aItem, bool aSort=true)
Add a new draw aItem to the draw object list and sort according to aSort.
Class for a bus to bus entry.
SCH_ITEM * m_connected_bus_items[2]
Pointer to the bus items (usually bus wires) connected to this bus-bus entry (either or both may be n...
std::vector< VECTOR2I > GetConnectionPoints() const override
Add all the connection points for this item to aPoints.
Class for a wire to bus entry.
SCH_ITEM * m_connected_bus_item
Pointer to the bus item (usually a bus wire) connected to this bus-wire entry, if it is connected to ...
Each graphical item can have a SCH_CONNECTION describing its logical connection (to a bus or net).
CONNECTION_TYPE Type() const
wxString Name(bool aIgnoreSheet=false) const
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:170
const std::vector< SCH_ITEM * > & ConnectedItems(const SCH_SHEET_PATH &aPath) const
Retrieve the set of items connected to this item on the given sheet.
Definition sch_item.cpp:766
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
VECTOR2I GetEndPoint() const
Definition sch_line.h:145
VECTOR2I GetStartPoint() const
Definition sch_line.h:136
void SetEndPoint(const VECTOR2I &aPosition)
Definition sch_line.h:146
void SetNumber(const wxString &aNumber)
Definition sch_pin.cpp:842
void SetName(const wxString &aName)
Definition sch_pin.cpp:545
void SetPosition(const VECTOR2I &aPos) override
Definition sch_pin.h:318
void SetType(ELECTRICAL_PINTYPE aType)
Definition sch_pin.cpp:455
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
void push_back(SCH_SHEET *aSheet)
Forwarded method from std::vector.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:48
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
Schematic symbol object.
Definition sch_symbol.h:73
std::vector< const SCH_PIN * > GetPins(const SCH_SHEET_PATH *aSheet) const
Retrieve a list of the SCH_PINs for the given sheet path.
void UpdatePins()
Updates the cache of SCH_PIN objects for each pin.
void SetRef(const SCH_SHEET_PATH *aSheet, const wxString &aReference)
Set the reference for the given sheet path for this symbol.
void SetValueFieldText(const wxString &aValue, const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString)
@ LAYER_WIRE
Definition layer_ids.h:474
@ LAYER_BUS
Definition layer_ids.h:475
ELECTRICAL_PINTYPE
The symbol library pin object electrical types used in ERC tests.
Definition pin_type.h:32
@ PT_INPUT
usual pin input: must be connected
Definition pin_type.h:33
@ PT_POWER_IN
power input (GND, VCC for ICs). Must be connected to a power output.
Definition pin_type.h:42
@ BUS
This item represents a bus vector.
@ NET
This item represents a net.
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
KIBIS_PIN * pin
BOOST_CHECK_EQUAL(result, "25.4")
void boost_test_update_generic_connectivity()
void boost_test_update_symbol_connectivity()
BOOST_AUTO_TEST_CASE(SymbolConnectivityLinksPins)
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708