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 <http://www.gnu.org/licenses/>.
18 */
19
21
22#include <connection_graph.h>
23#include <lib_symbol.h>
24#include <pin_type.h>
25
26#include <sch_bus_entry.h>
27#include <sch_line.h>
28#include <sch_pin.h>
29#include <sch_symbol.h>
30#include <sch_sheet.h>
31#include <sch_sheet_path.h>
32#include <sch_screen.h>
33
34#include <algorithm>
35#include <map>
36#include <set>
37
39{
40 // Create root sheet and screen
41 SCH_SCREEN* screen = new SCH_SCREEN( nullptr );
42 SCH_SHEET* sheet = new SCH_SHEET( nullptr, VECTOR2I( 0, 0 ), VECTOR2I( 1000, 1000 ) );
43 sheet->SetScreen( screen );
44
45 SCH_SHEET_PATH sheetPath;
46 sheetPath.push_back( sheet );
47
48 // Build library symbol
49 LIB_SYMBOL lib( "TEST", nullptr );
50 lib.SetGlobalPower();
52 lib.JumperPinGroups().push_back( { wxString( "3" ), wxString( "4" ) } );
53
54 auto make_pin = [&]( const wxString& num, const wxString& name, ELECTRICAL_PINTYPE type, const VECTOR2I& pos )
55 {
56 SCH_PIN* pin = new SCH_PIN( &lib );
57 pin->SetNumber( num );
58 pin->SetName( name );
59 pin->SetType( type );
60 pin->SetPosition( pos );
61 lib.AddDrawItem( pin );
62 };
63
64 make_pin( "1", "VCC", ELECTRICAL_PINTYPE::PT_POWER_IN, VECTOR2I( 0, 0 ) );
65 make_pin( "2", "A", ELECTRICAL_PINTYPE::PT_INPUT, VECTOR2I( 1000, 0 ) );
66 make_pin( "2", "B", ELECTRICAL_PINTYPE::PT_INPUT, VECTOR2I( 2000, 0 ) );
67 make_pin( "3", "C", ELECTRICAL_PINTYPE::PT_INPUT, VECTOR2I( 3000, 0 ) );
68 make_pin( "4", "D", ELECTRICAL_PINTYPE::PT_INPUT, VECTOR2I( 4000, 0 ) );
69
70 // Create schematic symbol instance
71 SCH_SYMBOL symbol( lib, lib.GetLibId(), &sheetPath, 0, 0, VECTOR2I( 0, 0 ) );
72 symbol.SetValueFieldText( "VCC" );
73 symbol.UpdatePins();
74
75 std::vector<SCH_PIN*> pins = symbol.GetPins( &sheetPath );
76 SCH_PIN* powerPin = nullptr;
77 SCH_PIN* dupA = nullptr;
78 SCH_PIN* dupB = nullptr;
79 SCH_PIN* jumpC = nullptr;
80 SCH_PIN* jumpD = nullptr;
81
82 for( SCH_PIN* pin : pins )
83 {
84 if( pin->GetNumber() == "1" )
85 powerPin = pin;
86 else if( pin->GetNumber() == "2" )
87 {
88 if( !dupA )
89 dupA = pin;
90 else
91 dupB = pin;
92 }
93 else if( pin->GetNumber() == "3" )
94 jumpC = pin;
95 else if( pin->GetNumber() == "4" )
96 jumpD = pin;
97 }
98
99 CONNECTION_GRAPH graph;
100 std::map<VECTOR2I, std::vector<SCH_ITEM*>> connection_map;
101 graph.updateSymbolConnectivity( sheetPath, &symbol, connection_map );
102
103 // Global power pin captured
104 BOOST_REQUIRE_EQUAL( graph.m_global_power_pins.size(), 1 );
105 BOOST_CHECK( graph.m_global_power_pins[0].second == powerPin );
106 BOOST_CHECK_EQUAL( powerPin->Connection( &sheetPath )->Name( true ), "VCC" );
107
108 // Duplicate pin numbers link together
109 const SCH_ITEM_VEC& dupAConn = dupA->ConnectedItems( sheetPath );
110 BOOST_CHECK( std::find( dupAConn.begin(), dupAConn.end(), dupB ) != dupAConn.end() );
111 const SCH_ITEM_VEC& dupBConn = dupB->ConnectedItems( sheetPath );
112 BOOST_CHECK( std::find( dupBConn.begin(), dupBConn.end(), dupA ) != dupBConn.end() );
113
114 // Jumper group links pins
115 const SCH_ITEM_VEC& jumpCConn = jumpC->ConnectedItems( sheetPath );
116 BOOST_CHECK( std::find( jumpCConn.begin(), jumpCConn.end(), jumpD ) != jumpCConn.end() );
117 const SCH_ITEM_VEC& jumpDConn = jumpD->ConnectedItems( sheetPath );
118 BOOST_CHECK( std::find( jumpDConn.begin(), jumpDConn.end(), jumpC ) != jumpDConn.end() );
119
120 // Connection map contains all pins
121 std::set<SCH_PIN*> mapPins;
122 for( const auto& [pos, items] : connection_map )
123 for( SCH_ITEM* item : items )
124 mapPins.insert( static_cast<SCH_PIN*>( item ) );
125 BOOST_CHECK_EQUAL( mapPins.size(), 5 );
126
127 // Item list contains all pins
128 BOOST_CHECK_EQUAL( graph.m_items.size(), 5 );
129 for( SCH_PIN* pin : { powerPin, dupA, dupB, jumpC, jumpD } )
130 {
131 BOOST_CHECK( std::find( graph.m_items.begin(), graph.m_items.end(), pin )
132 != graph.m_items.end() );
133 }
134
135 delete sheet; // deletes screen
136}
137
138
140{
141 std::map<VECTOR2I, std::vector<SCH_ITEM*>> cmap;
142
143 // Create root sheet and screen
144 SCH_SCREEN* screen = new SCH_SCREEN( nullptr );
145 SCH_SHEET* sheet = new SCH_SHEET( nullptr, VECTOR2I( 0, 0 ), VECTOR2I( 1000, 1000 ) );
146 sheet->SetScreen( screen );
147
148 SCH_SHEET_PATH sheetPath;
149 sheetPath.push_back( sheet );
150
151 CONNECTION_GRAPH graph;
152
153 // Wire line
154 SCH_LINE wire( VECTOR2I( 0, 0 ), LAYER_WIRE );
155 wire.SetEndPoint( VECTOR2I( 100, 0 ) );
156
157 graph.updateGenericItemConnectivity( sheetPath, &wire, cmap );
158 SCH_CONNECTION* wireConn = wire.Connection( &sheetPath );
159 BOOST_REQUIRE( wireConn );
160 BOOST_CHECK( wireConn->Type() == CONNECTION_TYPE::NET );
161 BOOST_CHECK( cmap.count( wire.GetStartPoint() ) );
162 BOOST_CHECK( cmap.count( wire.GetEndPoint() ) );
163
164 // Bus line
165 cmap.clear();
166 SCH_LINE bus( VECTOR2I( 0, 0 ), LAYER_BUS );
167 bus.SetEndPoint( VECTOR2I( 0, 100 ) );
168
169 graph.updateGenericItemConnectivity( sheetPath, &bus, cmap );
170 SCH_CONNECTION* busConn = bus.Connection( &sheetPath );
171 BOOST_REQUIRE( busConn );
172 BOOST_CHECK( busConn->Type() == CONNECTION_TYPE::BUS );
173 BOOST_CHECK( cmap.count( bus.GetStartPoint() ) );
174 BOOST_CHECK( cmap.count( bus.GetEndPoint() ) );
175
176 // Bus-to-bus entry
177 cmap.clear();
178 SCH_BUS_BUS_ENTRY busEntry( VECTOR2I( 0, 0 ), false );
179 SCH_LINE dummy1( VECTOR2I( 0, 0 ), LAYER_BUS );
180 SCH_LINE dummy2( VECTOR2I( 0, 0 ), LAYER_BUS );
181 busEntry.m_connected_bus_items[0] = &dummy1;
182 busEntry.m_connected_bus_items[1] = &dummy2;
183
184 graph.updateGenericItemConnectivity( sheetPath, &busEntry, cmap );
185 SCH_CONNECTION* bbConn = busEntry.Connection( &sheetPath );
186 BOOST_REQUIRE( bbConn );
187 BOOST_CHECK( bbConn->Type() == CONNECTION_TYPE::BUS );
188 BOOST_CHECK( busEntry.m_connected_bus_items[0] == nullptr );
189 BOOST_CHECK( busEntry.m_connected_bus_items[1] == nullptr );
190 auto ptsBB = busEntry.GetConnectionPoints();
191 BOOST_CHECK( cmap.count( ptsBB[0] ) );
192 BOOST_CHECK( cmap.count( ptsBB[1] ) );
193
194 // Bus-wire entry
195 cmap.clear();
196 SCH_BUS_WIRE_ENTRY bwEntry( VECTOR2I( 0, 0 ), false );
197 SCH_LINE dummyBus( VECTOR2I( 0, 0 ), LAYER_BUS );
198 bwEntry.m_connected_bus_item = &dummyBus;
199
200 graph.updateGenericItemConnectivity( sheetPath, &bwEntry, cmap );
201 SCH_CONNECTION* bwConn = bwEntry.Connection( &sheetPath );
202 BOOST_REQUIRE( bwConn );
203 BOOST_CHECK( bwConn->Type() == CONNECTION_TYPE::NET );
204 BOOST_CHECK( bwEntry.m_connected_bus_item == nullptr );
205 auto ptsBW = bwEntry.GetConnectionPoints();
206 BOOST_CHECK( cmap.count( ptsBW[0] ) );
207 BOOST_CHECK( cmap.count( ptsBW[1] ) );
208
209 // Pin
210 cmap.clear();
211 LIB_SYMBOL libSymbol( "PWR", nullptr );
212 SCH_PIN* libPin = new SCH_PIN( &libSymbol );
213 libPin->SetNumber( "1" );
214 libPin->SetName( "P" );
216 libPin->SetPosition( VECTOR2I( 10, 10 ) );
217 libSymbol.AddDrawItem( libPin );
218 libSymbol.SetGlobalPower();
219
220 SCH_SYMBOL symbol( libSymbol, libSymbol.GetLibId(), &sheetPath, 0, 0, VECTOR2I( 0, 0 ) );
221 symbol.SetRef( &sheetPath, "U1" );
222 symbol.UpdatePins();
223 SCH_PIN* pin = symbol.GetPins( &sheetPath )[0];
224
225 graph.updateGenericItemConnectivity( sheetPath, pin, cmap );
226 SCH_CONNECTION* pinConn = pin->Connection( &sheetPath );
227 BOOST_REQUIRE( pinConn );
228 BOOST_CHECK( pinConn->Type() == CONNECTION_TYPE::NET );
229 BOOST_CHECK( graph.m_global_power_pins.size() == 1 );
230 BOOST_CHECK( cmap.count( pin->GetPosition() ) );
231}
232
233BOOST_AUTO_TEST_SUITE( UpdateItemsConnectivity )
234BOOST_AUTO_TEST_CASE( SymbolConnectivityLinksPins )
235{
237}
238BOOST_AUTO_TEST_CASE( GenericItemConnectivity )
239{
241}
242
const char * name
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
CONNECTION_GRAPH(SCHEMATIC *aSchematic=nullptr)
std::vector< SCH_ITEM * > m_items
All connectable items in the schematic.
Define a library symbol object.
Definition lib_symbol.h:85
const LIB_ID & GetLibId() const override
Definition lib_symbol.h:154
void SetGlobalPower()
void SetDuplicatePinNumbersAreJumpers(bool aEnabled)
Definition lib_symbol.h:574
std::vector< std::set< wxString > > & JumperPinGroups()
Each jumper pin group is a set of pin numbers that should be treated as internally connected.
Definition lib_symbol.h:580
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:167
const SCH_ITEM_VEC & ConnectedItems(const SCH_SHEET_PATH &aPath)
Retrieve the set of items connected to this item on the given sheet.
Definition sch_item.cpp:382
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:323
Segment description base class to describe items which have 2 end points (track, wire,...
Definition sch_line.h:42
VECTOR2I GetEndPoint() const
Definition sch_line.h:144
VECTOR2I GetStartPoint() const
Definition sch_line.h:139
void SetEndPoint(const VECTOR2I &aPosition)
Definition sch_line.h:145
void SetNumber(const wxString &aNumber)
Definition sch_pin.cpp:633
void SetName(const wxString &aName)
Definition sch_pin.cpp:418
void SetPosition(const VECTOR2I &aPos) override
Definition sch_pin.h:238
void SetType(ELECTRICAL_PINTYPE aType)
Definition sch_pin.cpp:332
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:47
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
Schematic symbol object.
Definition sch_symbol.h:75
void SetValueFieldText(const wxString &aValue)
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.
std::vector< SCH_PIN * > GetPins(const SCH_SHEET_PATH *aSheet) const
Retrieve a list of the SCH_PINs for the given sheet path.
@ LAYER_WIRE
Definition layer_ids.h:451
@ LAYER_BUS
Definition layer_ids.h:452
ELECTRICAL_PINTYPE
The symbol library pin object electrical types used in ERC tests.
Definition pin_type.h:36
@ PT_INPUT
usual pin input: must be connected
Definition pin_type.h:37
@ PT_POWER_IN
power input (GND, VCC for ICs). Must be connected to a power output.
Definition pin_type.h:46
@ BUS
This item represents a bus vector.
@ NET
This item represents a net.
std::vector< SCH_ITEM * > SCH_ITEM_VEC
Definition sch_item.h:156
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()
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:695