KiCad PCB EDA Suite
Loading...
Searching...
No Matches
connection_graph.h
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 (C) 2018 CERN
5 * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Jon Evans <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#ifndef _CONNECTION_GRAPH_H
23#define _CONNECTION_GRAPH_H
24
25#include <mutex>
26#include <vector>
27
28#include <erc_settings.h>
29#include <sch_connection.h>
30#include <sch_item.h>
31#include <wx/treectrl.h>
32
33
34#ifdef DEBUG
35// Uncomment this line to enable connectivity debugging features
36// #define CONNECTIVITY_DEBUG
37#endif
38
39
41class SCHEMATIC;
42class SCH_EDIT_FRAME;
43class SCH_HIERLABEL;
44class SCH_PIN;
45class SCH_SHEET_PIN;
46
47
62{
63public:
64 enum class PRIORITY
65 {
66 INVALID = -1,
67 NONE = 0,
68 PIN,
73 GLOBAL
74 };
75
77 m_graph( aGraph ),
78 m_dirty( false ),
79 m_absorbed( false ),
80 m_is_bus_member( false ),
81 m_absorbed_by( nullptr ),
82 m_code( -1 ),
83 m_multiple_drivers( false ),
84 m_strong_driver( false ),
85 m_local_driver( false ),
86 m_bus_entry( nullptr ),
87 m_hier_parent( nullptr ),
88 m_driver( nullptr ),
89 m_no_connect( nullptr ),
90 m_driver_connection( nullptr )
91 {}
92
94
95 friend class CONNECTION_GRAPH;
96
106 bool ResolveDrivers( bool aCheckMultipleDrivers = false );
107
111 wxString GetNetName() const;
112
114 std::vector<SCH_ITEM*> GetVectorBusLabels() const;
115
117 std::vector<SCH_ITEM*> GetAllBusLabels() const;
118
120 const wxString& GetNameForDriver( SCH_ITEM* aItem ) const;
121
122 const wxString GetNetclassForDriver( SCH_ITEM* aItem ) const;
123
125 void Absorb( CONNECTION_SUBGRAPH* aOther );
126
128 void AddItem( SCH_ITEM* aItem );
129
132
134 const std::set<SCH_ITEM*>& GetItems() const
135 {
136 return m_items;
137 }
138
140 void getAllConnectedItems( std::set<std::pair<SCH_SHEET_PATH, SCH_ITEM*>>& aItems,
141 std::set<CONNECTION_SUBGRAPH*>& aSubgraphs );
142
157 static PRIORITY GetDriverPriority( SCH_ITEM* aDriver );
158
160 {
161 if( m_driver )
162 return GetDriverPriority( m_driver );
163 else
164 return PRIORITY::NONE;
165 }
166
171 const SCH_ITEM* GetDriver() const
172 {
173 return m_driver;
174 }
175
180 {
181 return m_driver_connection;
182 }
183
187 const SCH_ITEM* GetNoConnect() const
188 {
189 return m_no_connect;
190 }
191
193 {
194 return m_sheet;
195 }
196
197 void RemoveItem( SCH_ITEM* aItem );
198
199private:
200 wxString driverName( SCH_ITEM* aItem ) const;
201
203
205
208
214
217
219 std::set<CONNECTION_SUBGRAPH*> m_absorbed_subgraphs;
220
221 long m_code;
222
229
232
235
238
239 std::set<SCH_ITEM*> m_drivers;
240
249 std::unordered_map< std::shared_ptr<SCH_CONNECTION>,
250 std::unordered_set<CONNECTION_SUBGRAPH*> > m_bus_neighbors;
251
257 std::unordered_map< std::shared_ptr<SCH_CONNECTION>,
258 std::unordered_set<CONNECTION_SUBGRAPH*> > m_bus_parents;
259
260 // Cache for lookup of any hierarchical (sheet) pins on this subgraph (for referring down)
261 std::set<SCH_SHEET_PIN*> m_hier_pins;
262
263 // Cache for lookup of any hierarchical ports on this subgraph (for referring up)
264 std::set<SCH_HIERLABEL*> m_hier_ports;
265
266 // If not null, this indicates the subgraph on a higher level sheet that is linked to this one
268
269 // If not null, this indicates the subgraph(s) on a lower level sheet that are linked to this one
270 std::unordered_set<CONNECTION_SUBGRAPH*> m_hier_children;
271
273 mutable std::unordered_map<SCH_ITEM*, wxString> m_driver_name_cache;
274
277
279 std::set<SCH_ITEM*> m_items;
280
283
286
289};
290
292{
293 wxString Name;
295
296 bool operator==(const NET_NAME_CODE_CACHE_KEY& other) const
297 {
298 return Name == other.Name && Netcode == other.Netcode;
299 }
300};
301
302namespace std
303{
304 template <>
306 {
307 std::size_t operator()( const NET_NAME_CODE_CACHE_KEY& k ) const
308 {
309 const std::size_t prime = 19937;
310
311 return hash<wxString>()( k.Name ) ^ ( hash<int>()( k.Netcode ) * prime );
312 }
313 };
314}
315
317typedef std::unordered_map<NET_NAME_CODE_CACHE_KEY, std::vector<CONNECTION_SUBGRAPH*>> NET_MAP;
318
323{
324public:
325 CONNECTION_GRAPH( SCHEMATIC* aSchematic = nullptr ) :
326 m_last_net_code( 1 ),
327 m_last_bus_code( 1 ),
329 m_schematic( aSchematic )
330 {}
331
333 {
334 Reset();
335 }
336
337 void Reset();
338
339 void SetSchematic( SCHEMATIC* aSchematic )
340 {
341 m_schematic = aSchematic;
342 }
343
344 void SetLastCodes( const CONNECTION_GRAPH* aOther )
345 {
349 }
350
358 void Recalculate( const SCH_SHEET_LIST& aSheetList, bool aUnconditional = false,
359 std::function<void( SCH_ITEM* )>* aChangedItemHandler = nullptr );
360
367 std::shared_ptr<BUS_ALIAS> GetBusAlias( const wxString& aName );
368
376 std::vector<const CONNECTION_SUBGRAPH*> GetBusesNeedingMigration();
377
385 int RunERC();
386
388
395 CONNECTION_SUBGRAPH* FindSubgraphByName( const wxString& aNetName,
396 const SCH_SHEET_PATH& aPath );
397
404 CONNECTION_SUBGRAPH* FindFirstSubgraphByName( const wxString& aNetName );
405
407
408 const std::vector<CONNECTION_SUBGRAPH*> GetAllSubgraphs( const wxString& aNetName ) const;
409
415 wxString GetResolvedSubgraphName( const CONNECTION_SUBGRAPH* aSubGraph ) const;
416
424 std::set<std::pair<SCH_SHEET_PATH, SCH_ITEM*>> ExtractAffectedItems(
425 const std::set<SCH_ITEM*> &aItems );
426
433 void Merge( CONNECTION_GRAPH& aGraph );
434
435private:
463 void updateItemConnectivity( const SCH_SHEET_PATH& aSheet,
464 const std::vector<SCH_ITEM*>& aItemList );
465
479 void buildConnectionGraph( std::function<void( SCH_ITEM* )>* aChangedItemHandler );
480
484 void buildItemSubGraphs();
485
490 void resolveAllDrivers();
491
496
502
507
511 void processSubGraphs();
512
518 int assignNewNetCode( SCH_CONNECTION& aConnection );
519
525 int getOrCreateNetCode( const wxString& aNetName );
526
531 void assignNetCodesToBus( SCH_CONNECTION* aConnection );
532
542 void propagateToNeighbors( CONNECTION_SUBGRAPH* aSubgraph, bool aForce );
543
550 void removeSubgraphs( std::set<CONNECTION_SUBGRAPH*>& aSubgraphs );
551
562 static SCH_CONNECTION* matchBusMember( SCH_CONNECTION* aBusConnection,
563 SCH_CONNECTION* aSearch );
564
573 std::shared_ptr<SCH_CONNECTION> getDefaultConnection( SCH_ITEM* aItem,
574 CONNECTION_SUBGRAPH* aSubgraph );
575
576 void recacheSubgraphName( CONNECTION_SUBGRAPH* aSubgraph, const wxString& aOldName );
577
585 bool ercCheckMultipleDrivers( const CONNECTION_SUBGRAPH* aSubgraph );
586
587 bool ercCheckNetclassConflicts( const std::vector<CONNECTION_SUBGRAPH*>& subgraphs );
588
597 bool ercCheckBusToNetConflicts( const CONNECTION_SUBGRAPH* aSubgraph );
598
609 bool ercCheckBusToBusConflicts( const CONNECTION_SUBGRAPH* aSubgraph );
610
624
634 bool ercCheckNoConnects( const CONNECTION_SUBGRAPH* aSubgraph );
635
644 bool ercCheckFloatingWires( const CONNECTION_SUBGRAPH* aSubgraph );
645
655 bool ercCheckLabels( const CONNECTION_SUBGRAPH* aSubgraph );
656
664 int ercCheckHierSheets();
665
671 size_t hasPins( const CONNECTION_SUBGRAPH* aLocSubgraph );
672
673
674private:
675 // All the sheets in the schematic (as long as we don't have partial updates)
677
678 // All connectable items in the schematic
679 std::vector<SCH_ITEM*> m_items;
680
681 // The owner of all CONNECTION_SUBGRAPH objects
682 std::vector<CONNECTION_SUBGRAPH*> m_subgraphs;
683
684 // Cache of a subset of m_subgraphs
685 std::vector<CONNECTION_SUBGRAPH*> m_driver_subgraphs;
686
687 // Cache to lookup subgraphs in m_driver_subgraphs by sheet path
688 std::unordered_map<SCH_SHEET_PATH, std::vector<CONNECTION_SUBGRAPH*>> m_sheet_to_subgraphs_map;
689
690 std::vector<std::pair<SCH_SHEET_PATH, SCH_PIN*>> m_global_power_pins;
691
692 std::unordered_map<wxString, std::shared_ptr<BUS_ALIAS>> m_bus_alias_cache;
693
694 std::unordered_map<wxString, int> m_net_name_to_code_map;
695
696 std::unordered_map<wxString, int> m_bus_name_to_code_map;
697
698 std::unordered_map<wxString, std::vector<const CONNECTION_SUBGRAPH*>> m_global_label_cache;
699
700 std::map< std::pair<SCH_SHEET_PATH, wxString>,
701 std::vector<const CONNECTION_SUBGRAPH*> > m_local_label_cache;
702
703 std::unordered_map<wxString, std::vector<CONNECTION_SUBGRAPH*>> m_net_name_to_subgraphs_map;
704
705 std::unordered_map<SCH_ITEM*, CONNECTION_SUBGRAPH*> m_item_to_subgraph_map;
706
708
710
712
714
716};
717
718#endif
Calculates the connectivity of a schematic and generates netlists.
int RunERC()
Runs electrical rule checks on the connectivity graph.
void SetSchematic(SCHEMATIC *aSchematic)
bool ercCheckBusToBusConflicts(const CONNECTION_SUBGRAPH *aSubgraph)
Checks one subgraph for conflicting connections between two bus items.
void processSubGraphs()
Process all subgraphs to assign netcodes and merge subgraphs based on labels.
bool ercCheckNetclassConflicts(const std::vector< CONNECTION_SUBGRAPH * > &subgraphs)
bool ercCheckLabels(const CONNECTION_SUBGRAPH *aSubgraph)
Checks one subgraph for proper connection of labels.
void collectAllDriverValues()
Maps the driver values for each subgraph.
CONNECTION_SUBGRAPH * FindSubgraphByName(const wxString &aNetName, const SCH_SHEET_PATH &aPath)
Returns the subgraph for a given net name on a given sheet.
void recacheSubgraphName(CONNECTION_SUBGRAPH *aSubgraph, const wxString &aOldName)
const NET_MAP & GetNetMap() const
static SCH_CONNECTION * matchBusMember(SCH_CONNECTION *aBusConnection, SCH_CONNECTION *aSearch)
Search for a matching bus member inside a bus connection.
std::unordered_map< wxString, std::shared_ptr< BUS_ALIAS > > m_bus_alias_cache
SCHEMATIC * m_schematic
The schematic this graph represents.
std::unordered_map< SCH_SHEET_PATH, std::vector< CONNECTION_SUBGRAPH * > > m_sheet_to_subgraphs_map
CONNECTION_SUBGRAPH * FindFirstSubgraphByName(const wxString &aNetName)
Retrieves a subgraph for the given net name, if one exists.
void propagateToNeighbors(CONNECTION_SUBGRAPH *aSubgraph, bool aForce)
Updates all neighbors of a subgraph with this one's connectivity info.
void buildItemSubGraphs()
Generates individual item subgraphs on a per-sheet basis.
bool ercCheckMultipleDrivers(const CONNECTION_SUBGRAPH *aSubgraph)
If the subgraph has multiple drivers of equal priority that are graphically connected,...
CONNECTION_SUBGRAPH * GetSubgraphForItem(SCH_ITEM *aItem)
SCH_SHEET_LIST m_sheetList
void generateGlobalPowerPinSubGraphs()
Iterate through the global power pins to collect the global labels as drivers.
const std::vector< CONNECTION_SUBGRAPH * > GetAllSubgraphs(const wxString &aNetName) const
std::unordered_map< wxString, int > m_net_name_to_code_map
void buildConnectionGraph(std::function< void(SCH_ITEM *)> *aChangedItemHandler)
Generates the connection graph (after all item connectivity has been updated)
int ercCheckHierSheets()
Checks that a hierarchical sheet has at least one matching label inside the sheet for each port on th...
bool ercCheckBusToNetConflicts(const CONNECTION_SUBGRAPH *aSubgraph)
Checks one subgraph for conflicting connections between net and bus labels.
std::shared_ptr< SCH_CONNECTION > getDefaultConnection(SCH_ITEM *aItem, CONNECTION_SUBGRAPH *aSubgraph)
Builds a new default connection for the given item based on its properties.
std::vector< const CONNECTION_SUBGRAPH * > GetBusesNeedingMigration()
Determines which subgraphs have more than one conflicting bus label.
int assignNewNetCode(SCH_CONNECTION &aConnection)
Helper to assign a new net code to a connection.
std::map< std::pair< SCH_SHEET_PATH, wxString >, std::vector< const CONNECTION_SUBGRAPH * > > m_local_label_cache
int getOrCreateNetCode(const wxString &aNetName)
void assignNetCodesToBus(SCH_CONNECTION *aConnection)
Ensures all members of the bus connection have a valid net code assigned.
std::unordered_map< wxString, int > m_bus_name_to_code_map
std::unordered_map< wxString, std::vector< const CONNECTION_SUBGRAPH * > > m_global_label_cache
std::vector< CONNECTION_SUBGRAPH * > m_subgraphs
std::vector< std::pair< SCH_SHEET_PATH, SCH_PIN * > > m_global_power_pins
CONNECTION_GRAPH(SCHEMATIC *aSchematic=nullptr)
bool ercCheckNoConnects(const CONNECTION_SUBGRAPH *aSubgraph)
Checks one subgraph for proper presence or absence of no-connect symbols.
size_t hasPins(const CONNECTION_SUBGRAPH *aLocSubgraph)
Get the number of pins in a given subgraph.
std::vector< SCH_ITEM * > m_items
std::unordered_map< wxString, std::vector< CONNECTION_SUBGRAPH * > > m_net_name_to_subgraphs_map
std::shared_ptr< BUS_ALIAS > GetBusAlias(const wxString &aName)
Returns a bus alias pointer for the given name if it exists (from cache)
void removeSubgraphs(std::set< CONNECTION_SUBGRAPH * > &aSubgraphs)
Removes references to the given subgraphs from all structures in the connection graph.
std::unordered_map< SCH_ITEM *, CONNECTION_SUBGRAPH * > m_item_to_subgraph_map
std::set< std::pair< SCH_SHEET_PATH, SCH_ITEM * > > ExtractAffectedItems(const std::set< SCH_ITEM * > &aItems)
For a set of items, this will remove the connected items and their associated data including subgraph...
wxString GetResolvedSubgraphName(const CONNECTION_SUBGRAPH *aSubGraph) const
Returns the fully-resolved netname for a given subgraph.
bool ercCheckBusToBusEntryConflicts(const CONNECTION_SUBGRAPH *aSubgraph)
Checks one subgraph for conflicting bus entry to bus connections.
std::vector< CONNECTION_SUBGRAPH * > m_driver_subgraphs
NET_MAP m_net_code_to_subgraphs_map
bool ercCheckFloatingWires(const CONNECTION_SUBGRAPH *aSubgraph)
Checks one subgraph for floating wires.
void SetLastCodes(const CONNECTION_GRAPH *aOther)
void Merge(CONNECTION_GRAPH &aGraph)
Combines the input graph contents into the current graph.
void resolveAllDrivers()
Finds all subgraphs in the connection graph and calls ResolveDrivers() in parallel.
void updateItemConnectivity(const SCH_SHEET_PATH &aSheet, const std::vector< SCH_ITEM * > &aItemList)
Updates the graphical connectivity between items (i.e.
void Recalculate(const SCH_SHEET_LIST &aSheetList, bool aUnconditional=false, std::function< void(SCH_ITEM *)> *aChangedItemHandler=nullptr)
Updates the connection graph for the given list of sheets.
void generateBusAliasMembers()
Iterate through labels to create placeholders for bus elements.
A subgraph is a set of items that are electrically connected on a single sheet.
wxString driverName(SCH_ITEM *aItem) const
PRIORITY GetDriverPriority()
const std::set< SCH_ITEM * > & GetItems() const
Provides a read-only reference to the items in the subgraph.
const SCH_ITEM * GetNoConnect() const
bool m_strong_driver
True if the driver is "strong": a label or power object.
SCH_ITEM * m_no_connect
No-connect item in graph, if any.
std::set< CONNECTION_SUBGRAPH * > m_absorbed_subgraphs
Set of subgraphs that have been absorbed by this subgraph.
const SCH_ITEM * GetDriver() const
SCH_ITEM * m_bus_entry
Bus entry in graph, if any.
SCH_SHEET_PATH m_sheet
On which logical sheet is the subgraph contained.
void UpdateItemConnections()
Updates all items to match the driver connection.
const wxString GetNetclassForDriver(SCH_ITEM *aItem) const
std::set< SCH_SHEET_PIN * > m_hier_pins
std::unordered_map< std::shared_ptr< SCH_CONNECTION >, std::unordered_set< CONNECTION_SUBGRAPH * > > m_bus_neighbors
If a subgraph is a bus, this map contains links between the bus members and any local sheet neighbors...
CONNECTION_GRAPH * m_graph
std::vector< SCH_ITEM * > GetAllBusLabels() const
Returns all the all bus labels attached to this subgraph (if any)
std::unordered_map< SCH_ITEM *, wxString > m_driver_name_cache
A cache of escaped netnames from schematic items.
const wxString & GetNameForDriver(SCH_ITEM *aItem) const
Returns the candidate net name for a driver.
wxString GetNetName() const
Returns the fully-qualified net name for this subgraph (if one exists)
std::vector< SCH_ITEM * > GetVectorBusLabels() const
Returns all the vector-based bus labels attached to this subgraph (if any)
const SCH_SHEET_PATH & GetSheet() const
bool m_multiple_drivers
True if this subgraph contains more than one driver that should be shorted together in the netlist.
bool ResolveDrivers(bool aCheckMultipleDrivers=false)
Determines which potential driver should drive the subgraph.
std::set< SCH_ITEM * > m_drivers
const SCH_CONNECTION * GetDriverConnection() const
bool m_absorbed
True if this subgraph has been absorbed into another. No pointers here are safe if so!
SCH_CONNECTION * m_driver_connection
Cache for driver connection.
CONNECTION_SUBGRAPH * m_absorbed_by
If this subgraph is absorbed, points to the absorbing (and valid) subgraph.
std::unordered_set< CONNECTION_SUBGRAPH * > m_hier_children
void AddItem(SCH_ITEM *aItem)
Adds a new item to the subgraph.
void Absorb(CONNECTION_SUBGRAPH *aOther)
Combines another subgraph on the same sheet into this one.
std::set< SCH_ITEM * > m_items
Contents of the subgraph.
std::unordered_map< std::shared_ptr< SCH_CONNECTION >, std::unordered_set< CONNECTION_SUBGRAPH * > > m_bus_parents
If this is a net, this vector contains links to any same-sheet buses that contain it.
SCH_ITEM * m_driver
Fully-resolved driver for the subgraph (might not exist in this subgraph)
CONNECTION_SUBGRAPH(CONNECTION_GRAPH *aGraph)
~CONNECTION_SUBGRAPH()=default
bool m_is_bus_member
True if the subgraph is not actually part of a net.
CONNECTION_SUBGRAPH * m_hier_parent
void RemoveItem(SCH_ITEM *aItem)
bool m_local_driver
True if the driver is a local (i.e. non-global) type.
std::set< SCH_HIERLABEL * > m_hier_ports
void getAllConnectedItems(std::set< std::pair< SCH_SHEET_PATH, SCH_ITEM * > > &aItems, std::set< CONNECTION_SUBGRAPH * > &aSubgraphs)
Finds all items in the subgraph as well as child subgraphs recursively.
Holds all the data relating to one schematic.
Definition: schematic.h:75
Each graphical item can have a SCH_CONNECTION describing its logical connection (to a bus or net).
Schematic editor (Eeschema) main window.
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:150
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.
Definition: sch_sheet_pin.h:66
std::unordered_map< NET_NAME_CODE_CACHE_KEY, std::vector< CONNECTION_SUBGRAPH * > > NET_MAP
Associates a NET_CODE_NAME with all the subgraphs in that net.
STL namespace.
bool operator==(const NET_NAME_CODE_CACHE_KEY &other) const
std::size_t operator()(const NET_NAME_CODE_CACHE_KEY &k) const