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 <common.h>
28 #include <erc_settings.h>
29 #include <sch_connection.h>
30 #include <sch_item.h>
31 
32 
33 #ifdef DEBUG
34 // Uncomment this line to enable connectivity debugging features
35 // #define CONNECTIVITY_DEBUG
36 #endif
37 
38 
39 class SCH_EDIT_FRAME;
40 class SCH_HIERLABEL;
41 class SCH_PIN;
42 class SCH_SHEET_PIN;
43 
44 
59 {
60 public:
61  enum PRIORITY {
69  };
70 
71  explicit CONNECTION_SUBGRAPH( SCH_EDIT_FRAME* aFrame ) :
72  m_dirty( false ), m_absorbed( false ), m_absorbed_by( nullptr ), m_code( -1 ),
73  m_multiple_drivers( false ), m_strong_driver( false ), m_local_driver( false ),
74  m_no_connect( nullptr ), m_bus_entry( nullptr ), m_driver( nullptr ), m_frame( aFrame ),
75  m_driver_connection( nullptr )
76  {}
86  bool ResolveDrivers( bool aCreateMarkers = false );
87 
91  wxString GetNetName() const;
92 
94  std::vector<SCH_ITEM*> GetBusLabels() const;
95 
97  wxString GetNameForDriver( SCH_ITEM* aItem ) const;
98 
100  void Absorb( CONNECTION_SUBGRAPH* aOther );
101 
103  void AddItem( SCH_ITEM* aItem );
104 
106  void UpdateItemConnections();
107 
122  static PRIORITY GetDriverPriority( SCH_ITEM* aDriver );
123 
124  bool m_dirty;
125 
128 
131 
132  long m_code;
133 
140 
143 
146 
149 
152 
153  std::vector<SCH_ITEM*> m_items;
154 
155  std::vector<SCH_ITEM*> m_drivers;
156 
158 
160 
161  // Needed for m_userUnits for now; maybe refactor later
163 
166 
175  std::unordered_map< std::shared_ptr<SCH_CONNECTION>,
176  std::unordered_set<CONNECTION_SUBGRAPH*> > m_bus_neighbors;
177 
183  std::unordered_map< std::shared_ptr<SCH_CONNECTION>,
184  std::unordered_set<CONNECTION_SUBGRAPH*> > m_bus_parents;
185 
186  // Cache for lookup of any hierarchical (sheet) pins on this subgraph (for referring down)
187  std::vector<SCH_SHEET_PIN*> m_hier_pins;
188 
189  // Cache for lookup of any hierarchical ports on this subgraph (for referring up)
190  std::vector<SCH_HIERLABEL*> m_hier_ports;
191 };
192 
193 
198 {
199 public:
201  m_frame( aFrame )
202  {}
203 
204  void Reset();
205 
212  void Recalculate( SCH_SHEET_LIST aSheetList, bool aUnconditional = false );
213 
220  std::shared_ptr<BUS_ALIAS> GetBusAlias( const wxString& aName );
221 
229  std::vector<const CONNECTION_SUBGRAPH*> GetBusesNeedingMigration();
230 
240  int RunERC( const ERC_SETTINGS& aSettings, bool aCreateMarkers = true );
241 
242  // TODO(JE) Remove this when pressure valve is removed
243  static bool m_allowRealTime;
244 
245  // TODO(JE) firm up API and move to private
246  std::map<int, std::vector<CONNECTION_SUBGRAPH*> > m_net_code_to_subgraphs_map;
247 
248 private:
249 
250  std::unordered_set<SCH_ITEM*> m_items;
251 
252  // The owner of all CONNECTION_SUBGRAPH objects
253  std::vector<CONNECTION_SUBGRAPH*> m_subgraphs;
254 
255  // Cache of a subset of m_subgraphs
256  std::vector<CONNECTION_SUBGRAPH*> m_driver_subgraphs;
257 
258  // Cache to lookup subgraphs in m_driver_subgraphs by sheet path
259  std::unordered_map<SCH_SHEET_PATH,
260  std::vector<CONNECTION_SUBGRAPH*>> m_sheet_to_subgraphs_map;
261 
262  std::vector<std::pair<SCH_SHEET_PATH, SCH_PIN*>> m_invisible_power_pins;
263 
264  std::unordered_map< wxString, std::shared_ptr<BUS_ALIAS> > m_bus_alias_cache;
265 
266  std::map<wxString, int> m_net_name_to_code_map;
267 
268  std::map<wxString, int> m_bus_name_to_code_map;
269 
270  std::map<wxString, std::vector<const CONNECTION_SUBGRAPH*>> m_global_label_cache;
271 
272  std::map< std::pair<SCH_SHEET_PATH, wxString>,
273  std::vector<const CONNECTION_SUBGRAPH*> > m_local_label_cache;
274 
275  std::unordered_map<wxString,
276  std::vector<CONNECTION_SUBGRAPH*>> m_net_name_to_subgraphs_map;
277 
279 
281 
283 
284  std::mutex m_item_mutex;
285 
286  // Needed for m_userUnits for now; maybe refactor later
288 
317  std::vector<SCH_ITEM*> aItemList );
318 
332  void buildConnectionGraph();
333 
339  int assignNewNetCode( SCH_CONNECTION& aConnection );
340 
345  void assignNetCodesToBus( SCH_CONNECTION* aConnection );
346 
353  void propagateToNeighbors( CONNECTION_SUBGRAPH* aSubgraph );
354 
365  static SCH_CONNECTION* matchBusMember( SCH_CONNECTION* aBusConnection,
366  SCH_CONNECTION* aSearch );
367 
368  void recacheSubgraphName( CONNECTION_SUBGRAPH* aSubgraph, const wxString& aOldName );
369 
379  bool ercCheckBusToNetConflicts( const CONNECTION_SUBGRAPH* aSubgraph,
380  bool aCreateMarkers );
381 
393  bool ercCheckBusToBusConflicts( const CONNECTION_SUBGRAPH* aSubgraph,
394  bool aCreateMarkers );
395 
410  bool aCreateMarkers );
411 
422  bool ercCheckNoConnects( const CONNECTION_SUBGRAPH* aSubgraph,
423  bool aCreateMarkers );
424 
435  bool ercCheckLabels( const CONNECTION_SUBGRAPH* aSubgraph, bool aCreateMarkers,
436  bool aCheckGlobalLabels );
437 
438 };
439 
440 #endif
CONNECTION_SUBGRAPH(SCH_EDIT_FRAME *aFrame)
SCH_SHEET_PATH m_sheet
Class SCH_SHEET_LIST.
SCH_CONNECTION * m_driver_connection
Cache for driver connection.
void propagateToNeighbors(CONNECTION_SUBGRAPH *aSubgraph)
Updates all neighbors of a subgraph with this one's connectivity info.
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
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...
int RunERC(const ERC_SETTINGS &aSettings, bool aCreateMarkers=true)
Runs electrical rule checks on the connectivity graph.
SCH_ITEM * m_bus_entry
Bus entry in graph, if any.
bool ercCheckLabels(const CONNECTION_SUBGRAPH *aSubgraph, bool aCreateMarkers, bool aCheckGlobalLabels)
Checks one subgraph for proper connection of labels.
SCH_EDIT_FRAME * m_frame
Calculates the connectivity of a schematic and generates netlists.
void Absorb(CONNECTION_SUBGRAPH *aOther)
Combines another subgraph on the same sheet into this one.
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
static PRIORITY GetDriverPriority(SCH_ITEM *aDriver)
Returns the priority (higher is more important) of a candidate driver.
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
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.
std::mutex m_item_mutex
Schematic editor (Eeschema) main window.
std::map< int, std::vector< CONNECTION_SUBGRAPH * > > m_net_code_to_subgraphs_map
CONNECTION_GRAPH(SCH_EDIT_FRAME *aFrame)
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.
bool ResolveDrivers(bool aCreateMarkers=false)
Determines which potential driver should drive the subgraph.
bool ercCheckNoConnects(const CONNECTION_SUBGRAPH *aSubgraph, bool aCreateMarkers)
Checks one subgraph for proper presence or absence of no-connect symbols.
bool m_multiple_drivers
True if this subgraph contains more than one driver that should be shorted together in the netlist.
void Recalculate(SCH_SHEET_LIST aSheetList, bool aUnconditional=false)
Updates the connection graph for the given list of sheets.
void recacheSubgraphName(CONNECTION_SUBGRAPH *aSubgraph, const wxString &aOldName)
int assignNewNetCode(SCH_CONNECTION &aConnection)
Helper to assign a new net code to a connection.
bool ercCheckBusToBusConflicts(const CONNECTION_SUBGRAPH *aSubgraph, bool aCreateMarkers)
Checks one subgraph for conflicting connections between two bus items.
std::vector< SCH_ITEM * > m_items
SCH_EDIT_FRAME * m_frame
std::vector< CONNECTION_SUBGRAPH * > m_driver_subgraphs
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:75
Container for ERC settings.
Definition: erc_settings.h:30
Class SCH_SHEET_PATH.
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.
bool ercCheckBusToNetConflicts(const CONNECTION_SUBGRAPH *aSubgraph, bool aCreateMarkers)
Checks one subgraph for conflicting connections between net and bus labels.
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.
Each graphical item can have a SCH_CONNECTION describing its logical connection (to a bus or net).
bool ercCheckBusToBusEntryConflicts(const CONNECTION_SUBGRAPH *aSubgraph, bool aCreateMarkers)
Checks one subgraph for conflicting bus entry to bus connections.
std::vector< SCH_ITEM * > GetBusLabels() const
Returns all the bus labels attached to this subgraph (if any)
void updateItemConnectivity(SCH_SHEET_PATH aSheet, std::vector< SCH_ITEM * > aItemList)
Updates the graphical connectivity between items (i.e.
std::map< wxString, int > m_bus_name_to_code_map
The common library.
std::map< wxString, int > m_net_name_to_code_map
std::unordered_set< SCH_ITEM * > m_items
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.
Class SCH_ITEM is a base class for any item which can be embedded within the SCHEMATIC container clas...
Definition: sch_item.h:114
wxString GetNetName() const
Returns the fully-qualified net name for this subgraph (if one exists)
static SCH_CONNECTION * matchBusMember(SCH_CONNECTION *aBusConnection, SCH_CONNECTION *aSearch)
Search for a matching bus member inside a bus connection.
wxString GetNameForDriver(SCH_ITEM *aItem) const
Returns the candidate net name for a driver.
void UpdateItemConnections()
Updates all items to match the driver connection.