KiCad PCB EDA Suite
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  * @author Jon Evans <jon@craftyjon.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef _CONNECTION_GRAPH_H
22 #define _CONNECTION_GRAPH_H
23 
24 #include <mutex>
25 #include <vector>
26 
27 #include <erc_settings.h>
28 #include <sch_connection.h>
29 #include <sch_item.h>
30 
31 
32 #ifdef DEBUG
33 // Uncomment this line to enable connectivity debugging features
34 // #define CONNECTIVITY_DEBUG
35 #endif
36 
37 
38 class CONNECTION_GRAPH;
39 class SCHEMATIC;
40 class SCH_EDIT_FRAME;
41 class SCH_HIERLABEL;
42 class SCH_PIN;
43 class SCH_SHEET_PIN;
44 
45 
60 {
61 public:
62  enum class PRIORITY
63  {
64  INVALID = -1,
65  NONE = 0,
66  PIN,
67  SHEET_PIN,
68  HIER_LABEL,
70  POWER_PIN,
71  GLOBAL
72  };
73 
74  explicit CONNECTION_SUBGRAPH( CONNECTION_GRAPH* aGraph ) :
75  m_graph( aGraph ),
76  m_dirty( false ),
77  m_absorbed( false ),
78  m_absorbed_by( nullptr ),
79  m_code( -1 ),
80  m_multiple_drivers( false ),
81  m_strong_driver( false ),
82  m_local_driver( false ),
83  m_no_connect( nullptr ),
84  m_bus_entry( nullptr ),
85  m_driver( nullptr ),
86  m_driver_connection( nullptr ),
87  m_hier_parent( nullptr ),
88  m_first_driver( nullptr ),
89  m_second_driver( nullptr )
90  {}
91 
92  ~CONNECTION_SUBGRAPH() = default;
93 
103  bool ResolveDrivers( bool aCheckMultipleDrivers = false );
104 
108  wxString GetNetName() const;
109 
111  std::vector<SCH_ITEM*> GetBusLabels() const;
112 
114  const wxString& GetNameForDriver( SCH_ITEM* aItem );
115 
116  const wxString GetNameForDriver( SCH_ITEM* aItem ) const;
117 
119  void Absorb( CONNECTION_SUBGRAPH* aOther );
120 
122  void AddItem( SCH_ITEM* aItem );
123 
125  void UpdateItemConnections();
126 
141  static PRIORITY GetDriverPriority( SCH_ITEM* aDriver );
142 
144  {
145  if( m_driver )
146  return GetDriverPriority( m_driver );
147  else
148  return PRIORITY::NONE;
149  }
150 
152 
153  bool m_dirty;
154 
157 
160 
161  long m_code;
162 
169 
172 
175 
178 
181 
182  std::vector<SCH_ITEM*> m_items;
183 
184  std::vector<SCH_ITEM*> m_drivers;
185 
187 
189 
192 
201  std::unordered_map< std::shared_ptr<SCH_CONNECTION>,
202  std::unordered_set<CONNECTION_SUBGRAPH*> > m_bus_neighbors;
203 
209  std::unordered_map< std::shared_ptr<SCH_CONNECTION>,
210  std::unordered_set<CONNECTION_SUBGRAPH*> > m_bus_parents;
211 
212  // Cache for lookup of any hierarchical (sheet) pins on this subgraph (for referring down)
213  std::vector<SCH_SHEET_PIN*> m_hier_pins;
214 
215  // Cache for lookup of any hierarchical ports on this subgraph (for referring up)
216  std::vector<SCH_HIERLABEL*> m_hier_ports;
217 
218  // If not null, this indicates the subgraph on a higher level sheet that is linked to this one
220 
222  std::unordered_map<SCH_ITEM*, wxString> m_driver_name_cache;
223 
229 
232 
233 private:
234 
235  wxString driverName( SCH_ITEM* aItem ) const;
236 };
237 
239 typedef std::pair<wxString, int> NET_NAME_CODE;
240 
242 typedef std::map<NET_NAME_CODE, std::vector<CONNECTION_SUBGRAPH*>> NET_MAP;
243 
248 {
249 public:
250  CONNECTION_GRAPH( SCHEMATIC* aSchematic = nullptr ) :
251  m_last_net_code( 1 ),
252  m_last_bus_code( 1 ),
254  m_schematic( aSchematic )
255  {}
256 
258  {
259  Reset();
260  }
261 
262  void Reset();
263 
264  void SetSchematic( SCHEMATIC* aSchematic )
265  {
266  m_schematic = aSchematic;
267  }
268 
276  void Recalculate( const SCH_SHEET_LIST& aSheetList, bool aUnconditional = false,
277  std::function<void( SCH_ITEM* )>* aChangedItemHandler = nullptr );
278 
285  std::shared_ptr<BUS_ALIAS> GetBusAlias( const wxString& aName );
286 
294  std::vector<const CONNECTION_SUBGRAPH*> GetBusesNeedingMigration();
295 
303  int RunERC();
304 
305  const NET_MAP& GetNetMap() const { return m_net_code_to_subgraphs_map; }
306 
313  CONNECTION_SUBGRAPH* FindSubgraphByName( const wxString& aNetName,
314  const SCH_SHEET_PATH& aPath );
315 
322  CONNECTION_SUBGRAPH* FindFirstSubgraphByName( const wxString& aNetName );
323 
325 
326  // TODO(JE) Remove this when pressure valve is removed
327  static bool m_allowRealTime;
328 
329 private:
330  // All the sheets in the schematic (as long as we don't have partial updates)
332 
333  // All connectable items in the schematic
334  std::vector<SCH_ITEM*> m_items;
335 
336  // The owner of all CONNECTION_SUBGRAPH objects
337  std::vector<CONNECTION_SUBGRAPH*> m_subgraphs;
338 
339  // Cache of a subset of m_subgraphs
340  std::vector<CONNECTION_SUBGRAPH*> m_driver_subgraphs;
341 
342  // Cache to lookup subgraphs in m_driver_subgraphs by sheet path
343  std::unordered_map<SCH_SHEET_PATH, std::vector<CONNECTION_SUBGRAPH*>> m_sheet_to_subgraphs_map;
344 
345  std::vector<std::pair<SCH_SHEET_PATH, SCH_PIN*>> m_invisible_power_pins;
346 
347  std::unordered_map< wxString, std::shared_ptr<BUS_ALIAS> > m_bus_alias_cache;
348 
349  std::map<wxString, int> m_net_name_to_code_map;
350 
351  std::map<wxString, int> m_bus_name_to_code_map;
352 
353  std::map<wxString, std::vector<const CONNECTION_SUBGRAPH*>> m_global_label_cache;
354 
355  std::map< std::pair<SCH_SHEET_PATH, wxString>,
356  std::vector<const CONNECTION_SUBGRAPH*> > m_local_label_cache;
357 
358  std::unordered_map<wxString, std::vector<CONNECTION_SUBGRAPH*>> m_net_name_to_subgraphs_map;
359 
360  std::map<SCH_ITEM*, CONNECTION_SUBGRAPH*> m_item_to_subgraph_map;
361 
363 
365 
367 
369 
371 
399  void updateItemConnectivity( const SCH_SHEET_PATH& aSheet,
400  const std::vector<SCH_ITEM*>& aItemList );
401 
415  void buildConnectionGraph();
416 
422  int assignNewNetCode( SCH_CONNECTION& aConnection );
423 
428  void assignNetCodesToBus( SCH_CONNECTION* aConnection );
429 
436  void propagateToNeighbors( CONNECTION_SUBGRAPH* aSubgraph );
437 
448  static SCH_CONNECTION* matchBusMember( SCH_CONNECTION* aBusConnection,
449  SCH_CONNECTION* aSearch );
450 
459  std::shared_ptr<SCH_CONNECTION> getDefaultConnection( SCH_ITEM* aItem,
460  CONNECTION_SUBGRAPH* aSubgraph );
461 
462  void recacheSubgraphName( CONNECTION_SUBGRAPH* aSubgraph, const wxString& aOldName );
463 
471  bool ercCheckMultipleDrivers( const CONNECTION_SUBGRAPH* aSubgraph );
472 
481  bool ercCheckBusToNetConflicts( const CONNECTION_SUBGRAPH* aSubgraph );
482 
493  bool ercCheckBusToBusConflicts( const CONNECTION_SUBGRAPH* aSubgraph );
494 
507  bool ercCheckBusToBusEntryConflicts( const CONNECTION_SUBGRAPH* aSubgraph );
508 
518  bool ercCheckNoConnects( const CONNECTION_SUBGRAPH* aSubgraph );
519 
528  bool ercCheckFloatingWires( const CONNECTION_SUBGRAPH* aSubgraph );
529 
539  bool ercCheckLabels( const CONNECTION_SUBGRAPH* aSubgraph );
540 
548  int ercCheckHierSheets();
549 
550 };
551 
552 #endif
CONNECTION_SUBGRAPH * FindSubgraphByName(const wxString &aNetName, const SCH_SHEET_PATH &aPath)
Returns the subgraph for a given net name on a given sheet.
bool ercCheckMultipleDrivers(const CONNECTION_SUBGRAPH *aSubgraph)
If the subgraph has multiple drivers of equal priority that are graphically connected,...
SCH_SHEET_PATH m_sheet
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
SCH_CONNECTION * m_driver_connection
Cache for driver connection.
void SetSchematic(SCHEMATIC *aSchematic)
void propagateToNeighbors(CONNECTION_SUBGRAPH *aSubgraph)
Updates all neighbors of a subgraph with this one's connectivity info.
bool ercCheckBusToBusEntryConflicts(const CONNECTION_SUBGRAPH *aSubgraph)
Checks one subgraph for conflicting bus entry to bus connections.
void buildConnectionGraph()
Generates the connection graph (after all item connectivity has been updated)
std::unordered_map< wxString, std::shared_ptr< BUS_ALIAS > > m_bus_alias_cache
Holds all the data relating to one schematic A schematic may consist of one or more sheets (and one r...
Definition: schematic.h:44
std::shared_ptr< BUS_ALIAS > GetBusAlias(const wxString &aName)
Returns a bus alias pointer for the given name if it exists (from cache)
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...
SCH_ITEM * m_bus_entry
Bus entry in graph, if any.
CONNECTION_SUBGRAPH(CONNECTION_GRAPH *aGraph)
Calculates the connectivity of a schematic and generates netlists.
void Absorb(CONNECTION_SUBGRAPH *aOther)
Combines another subgraph on the same sheet into this one.
SCH_ITEM * m_second_driver
Used for multiple drivers ERC message; stores the second possible driver (or nullptr)
bool m_absorbed
True if this subgraph has been absorbed into another. No pointers here are safe if so!
std::unordered_map< SCH_SHEET_PATH, std::vector< CONNECTION_SUBGRAPH * > > m_sheet_to_subgraphs_map
std::pair< wxString, int > NET_NAME_CODE
Associates a net code with the final name of a net.
bool ercCheckFloatingWires(const CONNECTION_SUBGRAPH *aSubgraph)
Checks one subgraph for floating wires.
std::vector< SCH_SHEET_PIN * > m_hier_pins
bool m_local_driver
True if the driver is a local (i.e. non-global) type.
std::vector< CONNECTION_SUBGRAPH * > m_subgraphs
~CONNECTION_SUBGRAPH()=default
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.
NET_MAP m_net_code_to_subgraphs_map
Schematic editor (Eeschema) main window.
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.
std::vector< SCH_ITEM * > m_items
SCHEMATIC * m_schematic
The schematic this graph represents.
std::unordered_map< wxString, std::vector< CONNECTION_SUBGRAPH * > > m_net_name_to_subgraphs_map
A subgraph is a set of items that are electrically connected on a single sheet.
void updateItemConnectivity(const SCH_SHEET_PATH &aSheet, const std::vector< SCH_ITEM * > &aItemList)
Updates the graphical connectivity between items (i.e.
int ercCheckHierSheets()
Checks that a hierarchical sheet has at least one matching label inside the sheet for each port on th...
bool m_multiple_drivers
True if this subgraph contains more than one driver that should be shorted together in the netlist.
void recacheSubgraphName(CONNECTION_SUBGRAPH *aSubgraph, const wxString &aOldName)
bool ercCheckBusToNetConflicts(const CONNECTION_SUBGRAPH *aSubgraph)
Checks one subgraph for conflicting connections between net and bus labels.
CONNECTION_GRAPH * m_graph
const wxString & GetNameForDriver(SCH_ITEM *aItem)
Returns the candidate net name for a driver.
int assignNewNetCode(SCH_CONNECTION &aConnection)
Helper to assign a new net code to a connection.
CONNECTION_GRAPH(SCHEMATIC *aSchematic=nullptr)
bool ercCheckNoConnects(const CONNECTION_SUBGRAPH *aSubgraph)
Checks one subgraph for proper presence or absence of no-connect symbols.
std::vector< SCH_ITEM * > m_items
bool ResolveDrivers(bool aCheckMultipleDrivers=false)
Determines which potential driver should drive the subgraph.
std::vector< CONNECTION_SUBGRAPH * > m_driver_subgraphs
const NET_MAP & GetNetMap() const
void AddItem(SCH_ITEM *aItem)
Adds a new item to the subgraph.
bool m_strong_driver
True if the driver is "strong": a label or power object.
std::vector< SCH_ITEM * > m_drivers
Define a sheet pin (label) used in sheets to create hierarchical schematics.
Definition: sch_sheet.h:85
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
CONNECTION_SUBGRAPH * FindFirstSubgraphByName(const wxString &aNetName)
Retrieves a subgraph for the given net name, if one exists.
std::vector< SCH_HIERLABEL * > m_hier_ports
std::map< wxString, std::vector< const CONNECTION_SUBGRAPH * > > m_global_label_cache
void assignNetCodesToBus(SCH_CONNECTION *aConnection)
Ensures all members of the bus connection have a valid net code assigned.
SCH_ITEM * m_first_driver
Stores the primary driver for the multiple drivers ERC check.
std::vector< std::pair< SCH_SHEET_PATH, SCH_PIN * > > m_invisible_power_pins
static bool m_allowRealTime
CONNECTION_SUBGRAPH * m_absorbed_by
If this subgraph is absorbed, points to the absorbing (and valid) subgraph.
std::map< NET_NAME_CODE, std::vector< CONNECTION_SUBGRAPH * > > NET_MAP
Associates a NET_CODE_NAME with all the subgraphs in that net.
Each graphical item can have a SCH_CONNECTION describing its logical connection (to a bus or net).
SCH_SHEET_LIST m_sheetList
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< SCH_ITEM * > GetBusLabels() const
Returns all the bus labels attached to this subgraph (if any)
std::map< wxString, int > m_bus_name_to_code_map
wxString driverName(SCH_ITEM *aItem) const
std::map< wxString, int > m_net_name_to_code_map
CONNECTION_SUBGRAPH * m_hier_parent
CONNECTION_SUBGRAPH * GetSubgraphForItem(SCH_ITEM *aItem)
SCH_ITEM * m_no_connect
No-connect item in graph, if any.
std::map< std::pair< SCH_SHEET_PATH, wxString >, std::vector< const CONNECTION_SUBGRAPH * > > m_local_label_cache
std::vector< const CONNECTION_SUBGRAPH * > GetBusesNeedingMigration()
Determines which subgraphs have more than one conflicting bus label.
bool ercCheckLabels(const CONNECTION_SUBGRAPH *aSubgraph)
Checks one subgraph for proper connection of labels.
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:194
std::map< SCH_ITEM *, CONNECTION_SUBGRAPH * > m_item_to_subgraph_map
int RunERC()
Runs electrical rule checks on the connectivity graph.
wxString GetNetName() const
Returns the fully-qualified net name for this subgraph (if one exists)
PRIORITY GetDriverPriority()
static SCH_CONNECTION * matchBusMember(SCH_CONNECTION *aBusConnection, SCH_CONNECTION *aSearch)
Search for a matching bus member inside a bus connection.
bool ercCheckBusToBusConflicts(const CONNECTION_SUBGRAPH *aSubgraph)
Checks one subgraph for conflicting connections between two bus items.
std::unordered_map< SCH_ITEM *, wxString > m_driver_name_cache
A cache of escaped netnames from schematic items.
void UpdateItemConnections()
Updates all items to match the driver connection.