KiCad PCB EDA Suite
sch_connection.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 (C) 2018 CERN
5  * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * @author Jon Evans <jon@craftyjon.com>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include <regex>
24 #include <wx/tokenzr.h>
25 
26 #include <connection_graph.h>
27 #include <sch_symbol.h>
28 #include <sch_pin.h>
29 #include <sch_screen.h>
30 #include <project/net_settings.h>
31 #include <advanced_config.h>
32 #include <kicad_string.h>
33 
34 #include <sch_connection.h>
35 
65  m_sheet( aPath ),
66  m_parent( aParent ),
67  m_driver( nullptr ),
68  m_graph( nullptr )
69 {
70  Reset();
71 }
72 
73 
75  m_sheet( SCH_SHEET_PATH() ),
76  m_parent( nullptr ),
77  m_driver( nullptr ),
78  m_graph( aGraph )
79 {
80  Reset();
81 }
82 
83 
84 bool SCH_CONNECTION::operator==( const SCH_CONNECTION& aOther ) const
85 {
86  // NOTE: Not comparing m_dirty or net/bus/subgraph codes
87  if( ( aOther.m_driver == m_driver ) &&
88  ( aOther.m_type == m_type ) &&
89  ( aOther.m_name == m_name ) &&
90  ( aOther.m_sheet == m_sheet ) )
91  {
92  return true;
93  }
94 
95  return false;
96 }
97 
98 
100 {
101  m_driver = aItem;
102 
103  recacheName();
104 
105  for( const std::shared_ptr<SCH_CONNECTION>& member : m_members )
106  member->SetDriver( aItem );
107 }
108 
109 
111 {
112  m_sheet = aSheet;
113 
114  recacheName();
115 
116  for( const std::shared_ptr<SCH_CONNECTION>& member : m_members )
117  member->SetSheet( aSheet );
118 }
119 
120 
121 bool SCH_CONNECTION::operator!=( const SCH_CONNECTION& aOther ) const
122 {
123  return !( aOther == *this );
124 }
125 
126 
127 void SCH_CONNECTION::ConfigureFromLabel( const wxString& aLabel )
128 {
129  m_members.clear();
130 
131  m_name = aLabel;
132  m_local_name = aLabel;
133 
134  wxString prefix;
135  std::vector<wxString> members;
136 
137  wxString unescaped = UnescapeString( aLabel );
138 
139  if( NET_SETTINGS::ParseBusVector( unescaped, &prefix, &members ) )
140  {
142  m_vector_prefix = prefix;
143 
144  long i = 0;
145 
146  for( const wxString& vector_member : members )
147  {
148  auto member = std::make_shared<SCH_CONNECTION>( m_parent, m_sheet );
149  member->m_type = CONNECTION_TYPE::NET;
150  member->m_prefix = m_prefix;
151  member->m_local_name = vector_member;
152  member->m_vector_index = i++;
153  member->SetName( vector_member );
154  member->SetGraph( m_graph );
155  m_members.push_back( member );
156  }
157  }
158  else if( NET_SETTINGS::ParseBusGroup( unescaped, &prefix, &members ) )
159  {
161  m_bus_prefix = prefix;
162 
163  // Named bus groups generate a net prefix, unnamed ones don't
164  if( !prefix.IsEmpty() )
165  prefix += wxT( "." );
166 
167  for( const wxString& group_member : members )
168  {
169  // Handle alias inside bus group member list
170  if( auto alias = m_graph->GetBusAlias( group_member ) )
171  {
172  for( const wxString& alias_member : alias->Members() )
173  {
174  auto member = std::make_shared< SCH_CONNECTION >( m_parent, m_sheet );
175  member->SetPrefix( prefix );
176  member->SetGraph( m_graph );
177  member->ConfigureFromLabel( alias_member );
178  m_members.push_back( member );
179  }
180  }
181  else
182  {
183  auto member = std::make_shared< SCH_CONNECTION >( m_parent, m_sheet );
184  member->SetPrefix( prefix );
185  member->SetGraph( m_graph );
186  member->ConfigureFromLabel( group_member );
187  m_members.push_back( member );
188  }
189  }
190  }
191  else
192  {
194  }
195 
196  recacheName();
197 }
198 
199 
201 {
203  m_name.Empty();
204  m_local_name.Empty();
205  m_cached_name.Empty();
206  m_cached_name_with_path.Empty();
207  m_prefix.Empty();
208  m_bus_prefix.Empty();
209  m_suffix .Empty();
211  m_driver = nullptr;
212  m_members.clear();
213  m_dirty = true;
214  m_net_code = 0;
215  m_bus_code = 0;
216  m_subgraph_code = 0;
217  m_vector_start = 0;
218  m_vector_end = 0;
219  m_vector_index = 0;
220  m_vector_prefix.Empty();
221 }
222 
223 
225 {
226  m_graph = aOther.m_graph;
227  // Note: m_lastDriver is not cloned as it needs to be the last driver of *this* connection
228  m_driver = aOther.Driver();
229  m_sheet = aOther.Sheet();
230  m_name = aOther.m_name;
231  // Note: m_local_name is not cloned if not set yet
232  if( m_local_name.IsEmpty() )
233  m_local_name = aOther.LocalName();
234 
235  m_prefix = aOther.Prefix();
236  m_bus_prefix = aOther.BusPrefix();
237  m_suffix = aOther.Suffix();
238  m_net_code = aOther.NetCode();
239  m_bus_code = aOther.BusCode();
240  m_vector_start = aOther.VectorStart();
241  m_vector_end = aOther.VectorEnd();
242  // Note: m_vector_index is not cloned
243  m_vector_prefix = aOther.VectorPrefix();
244 
245  // Note: subgraph code isn't cloned, it should remain with the original object
246 
247  // Handle vector bus members: make sure local names are preserved where possible
248  std::vector<std::shared_ptr<SCH_CONNECTION>>& otherMembers = aOther.Members();
249 
250  if( m_type == CONNECTION_TYPE::BUS && aOther.Type() == CONNECTION_TYPE::BUS &&
251  m_members.size() == otherMembers.size() )
252  {
253  for( size_t i = 0; i < m_members.size(); ++i )
254  {
255  m_members[i]->Clone( *otherMembers[i] );
256  }
257  }
258  else
259  {
260  m_members = otherMembers;
261  }
262 
263  m_type = aOther.Type();
264 
265  recacheName();
266 }
267 
268 
270 {
271  wxASSERT( Parent() );
272 
273  switch( Parent()->Type() )
274  {
275  case SCH_LABEL_T:
276  case SCH_GLOBAL_LABEL_T:
277  case SCH_HIER_LABEL_T:
278  case SCH_SHEET_PIN_T:
279  case SCH_SHEET_T:
280  case LIB_PIN_T:
281  return true;
282 
283  case SCH_PIN_T:
284  {
285  auto pin = static_cast<SCH_PIN*>( Parent() );
286 
287  // Only annotated components should drive nets
288  return pin->IsPowerConnection() || pin->GetParentSymbol()->IsAnnotated( &m_sheet );
289  }
290 
291  default:
292  return false;
293  }
294 }
295 
296 
298 {
299  return m_driver != m_lastDriver;
300 }
301 
302 
304 {
306 }
307 
308 
309 
310 wxString SCH_CONNECTION::Name( bool aIgnoreSheet ) const
311 {
312  wxASSERT( !m_cached_name.IsEmpty() );
313  return aIgnoreSheet ? m_cached_name : m_cached_name_with_path;
314 }
315 
316 
318 {
319  m_cached_name = m_name.IsEmpty() ? "<NO NET>" : m_prefix + m_name + m_suffix;
320 
321  bool prepend_path = true;
322 
323  if( !Parent() || m_type == CONNECTION_TYPE::NONE )
324  prepend_path = false;
325 
326  if( m_driver )
327  {
328  switch( m_driver->Type() )
329  {
330  case SCH_GLOBAL_LABEL_T:
331  case SCH_PIN_T:
332  // Pins are either power connections or belong to a uniquely-annotated
333  // component, so they don't need a path if they are driving the subgraph
334  prepend_path = false;
335  break;
336 
337  default:
338  break;
339  }
340  }
341 
344 }
345 
346 
347 void SCH_CONNECTION::SetPrefix( const wxString& aPrefix )
348 {
349  m_prefix = aPrefix;
350 
351  recacheName();
352 
353  for( const auto& m : Members() )
354  m->SetPrefix( aPrefix );
355 }
356 
357 
358 void SCH_CONNECTION::SetSuffix( const wxString& aSuffix )
359 {
360  m_suffix = aSuffix;
361 
362  recacheName();
363 
364  for( const auto& m : Members() )
365  m->SetSuffix( aSuffix );
366 }
367 
368 
370 {
371  wxString msg, group_name;
372  std::vector<wxString> group_members;
373 
374  aList.push_back( MSG_PANEL_ITEM( _( "Connection Name" ), UnescapeString( Name() ) ) );
375 
376  // NOTE(JE) Disabling this for now, because net codes are generated in the netlist exporter
377  // in order to avoid sort costs. It may make sense to just tear out net codes from the
378  // CONNECTION_GRAPH entirely in the future, as they are mostly only useful for netlist exports.
379 #if 0
380  if( !IsBus() )
381  {
382  msg.Printf( "%d", m_net_code );
383  aList.push_back( MSG_PANEL_ITEM( _( "Net Code" ), msg ) );
384  }
385 #endif
386 
387  if( auto alias = m_graph->GetBusAlias( m_name ) )
388  {
389  msg.Printf( _( "Bus Alias %s Members" ), m_name );
390 
391  wxString members;
392 
393  for( const auto& member : alias->Members() )
394  members << member << " ";
395 
396  aList.push_back( MSG_PANEL_ITEM( msg, members ) );
397  }
398  else if( NET_SETTINGS::ParseBusGroup( m_name, &group_name, &group_members ) )
399  {
400  for( const auto& group_member : group_members )
401  {
402  if( auto group_alias = m_graph->GetBusAlias( group_member ) )
403  {
404  msg.Printf( _( "Bus Alias %s Members" ), group_alias->GetName() );
405 
406  wxString members;
407 
408  for( const auto& member : group_alias->Members() )
409  members << member << " ";
410 
411  aList.push_back( MSG_PANEL_ITEM( msg, members ) );
412  }
413  }
414  }
415 
416 #if defined(DEBUG)
417  // These messages are not flagged as translatable, because they are only debug messages
418 
419  if( !ADVANCED_CFG::GetCfg().m_RealTimeConnectivity || !CONNECTION_GRAPH::m_allowRealTime )
420  return;
421 
422  if( IsBus() )
423  {
424  msg.Printf( "%d", m_bus_code );
425  aList.push_back( MSG_PANEL_ITEM( "Bus Code", msg ) );
426  }
427 
428  msg.Printf( "%d", m_subgraph_code );
429  aList.push_back( MSG_PANEL_ITEM( "Subgraph Code", msg ) );
430 
431  if( auto driver = Driver() )
432  {
433  msg.Printf( "%s at %p", driver->GetSelectMenuText( EDA_UNITS::MILLIMETRES ), driver );
434  aList.push_back( MSG_PANEL_ITEM( "Connection Source", msg ) );
435  }
436 #endif
437 }
438 
439 
440 bool SCH_CONNECTION::IsBusLabel( const wxString& aLabel )
441 {
442  const wxString& unescaped = UnescapeString( aLabel );
443 
444  return NET_SETTINGS::ParseBusVector( unescaped, nullptr, nullptr )
445  || NET_SETTINGS::ParseBusGroup( unescaped, nullptr, nullptr );
446 }
447 
448 
449 bool SCH_CONNECTION::MightBeBusLabel( const wxString& aLabel )
450 {
451  // Weak heuristic for performance reasons. Stronger test will be used for connectivity
452  wxString label = UnescapeString( aLabel );
453 
454  return label.Contains( wxT( "[" ) ) || label.Contains( wxT( "{" ) );
455 }
456 
457 
458 const std::vector< std::shared_ptr< SCH_CONNECTION > > SCH_CONNECTION::AllMembers() const
459 {
460  std::vector< std::shared_ptr< SCH_CONNECTION > > ret( m_members );
461 
462  for( const auto& member : m_members )
463  if( member->IsBus() )
464  ret.insert( ret.end(), member->Members().begin(), member->Members().end() );
465 
466  return ret;
467 }
468 
469 
470 static bool isSuperSub( wxChar c )
471 {
472  return c == '_' || c == '^';
473 };
474 
475 
476 wxString SCH_CONNECTION::PrintBusForUI( const wxString& aGroup )
477 {
478  size_t groupLen = aGroup.length();
479  size_t i = 0;
480  wxString ret;
481  int braceNesting = 0;
482  int tildeNesting = 0;
483 
484  // Parse prefix
485  //
486  for( ; i < groupLen; ++i )
487  {
488  if( isSuperSub( aGroup[i] ) && i + 1 < groupLen && aGroup[i+1] == '{' )
489  {
490  braceNesting++;
491  i++;
492  continue;
493  }
494  else if( aGroup[i] == '~' )
495  {
496  if( tildeNesting )
497  {
498  tildeNesting = 0;
499  continue;
500  }
501  else
502  {
503  tildeNesting++;
504  }
505  }
506  else if( aGroup[i] == '}' )
507  {
508  braceNesting--;
509  continue;
510  }
511 
512  ret += aGroup[i];
513 
514  if( aGroup[i] == '{' )
515  break;
516  }
517 
518  // Parse members
519  //
520  i++; // '{' character
521 
522  for( ; i < groupLen; ++i )
523  {
524  if( isSuperSub( aGroup[i] ) && i + 1 < groupLen && aGroup[i+1] == '{' )
525  {
526  braceNesting++;
527  i++;
528  continue;
529  }
530  else if( aGroup[i] == '~' )
531  {
532  if( tildeNesting )
533  {
534  tildeNesting = 0;
535  continue;
536  }
537  else
538  {
539  tildeNesting++;
540  }
541  }
542  else if( aGroup[i] == '}' )
543  {
544  braceNesting--;
545  continue;
546  }
547 
548  ret += aGroup[i];
549 
550  if( aGroup[i] == '}' )
551  break;
552  }
553 
554  return ret;
555 }
556 
557 
559 {
560  if( !aOther->IsBus() )
561  return false;
562 
563  for( const auto& member : aOther->Members() )
564  {
565  if( member->FullLocalName() == FullLocalName() )
566  return true;
567  }
568 
569  return false;
570 }
571 
572 
574 {
575  if( !aOther->IsBus() )
576  return false;
577 
578  auto me = Name( true );
579 
580  for( const auto& m : aOther->Members() )
581  if( m->Name( true ) == me )
582  return true;
583 
584  return false;
585 }
static wxString PrintBusForUI(const wxString &aString)
int m_subgraph_code
Groups directly-connected items.
wxString Prefix() const
static bool MightBeBusLabel(const wxString &aLabel)
Test if aLabel looks like a bus notation.
wxString m_cached_name
Full name, including prefix and suffix.
std::vector< std::shared_ptr< SCH_CONNECTION > > m_members
For bus connections, store a list of member connections.
std::shared_ptr< BUS_ALIAS > GetBusAlias(const wxString &aName)
Returns a bus alias pointer for the given name if it exists (from cache)
Calculates the connectivity of a schematic and generates netlists.
wxString m_name
Name of the connection.
SCH_ITEM * m_driver
The SCH_ITEM that drives this connection's net.
static bool IsBusLabel(const wxString &aLabel)
Test if aLabel has a bus notation.
This item represents a bus group.
wxString m_bus_prefix
Optional prefix of a bux group (always empty for nets and vector buses)
wxString PathHumanReadable(bool aUseShortRootName=true) const
Return the sheet path in a human readable form made from the sheet names.
SCH_CONNECTION(SCH_ITEM *aParent=nullptr, SCH_SHEET_PATH aPath=SCH_SHEET_PATH())
Buses can be defined in multiple ways.
bool IsMemberOfBus(SCH_CONNECTION *aOther) const
Returns true if this connection is a member of bus connection aOther.
void ConfigureFromLabel(const wxString &aLabel)
Configures the connection given a label.
void SetDriver(SCH_ITEM *aItem)
void SetSheet(SCH_SHEET_PATH aSheet)
wxString m_cached_name_with_path
Full name including sheet path (if not global)
wxString LocalName() const
bool HasDriverChanged() const
int BusCode() const
CONNECTION_TYPE m_type
No connection to this item.
int NetCode() const
static bool ParseBusGroup(wxString aGroup, wxString *name, std::vector< wxString > *aMemberList)
Parses a bus group label into the name and a list of components.
wxString Suffix() const
wxString Name(bool aIgnoreSheet=false) const
const std::vector< std::shared_ptr< SCH_CONNECTION > > AllMembers() const
long VectorEnd() const
void Reset()
Clears connectivity information.
SCH_ITEM * Driver() const
wxString m_prefix
Prefix if connection is member of a labeled bus group (or "" if not)
CONNECTION_GRAPH * m_graph
Pointer to the connection graph for the schematic this connection exists on.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
wxString FullLocalName() const
void Clone(SCH_CONNECTION &aOther)
Copies connectivity information (but not parent) from another connection.
bool IsDriver() const
Checks if the SCH_ITEM this connection is attached to can drive connections Drivers can be labels,...
SCH_SHEET_PATH m_sheet
The hierarchical sheet this connection is on.
This item represents a net.
wxString m_suffix
Name suffix (used only for disambiguation)
static bool m_allowRealTime
wxString m_local_name
For bus members, we want to keep track of the "local" name of a member, that is, the name it takes on...
SCH_SHEET_PATH Sheet() const
void AppendInfoToMsgPanel(MSG_PANEL_ITEMS &aList) const
Adds information about the connection object to aList.
long m_vector_start
Highest member of a vector bus.
wxString VectorPrefix() const
Each graphical item can have a SCH_CONNECTION describing its logical connection (to a bus or net).
void SetSuffix(const wxString &aSuffix)
#define _(s)
Definition: 3d_actions.cpp:33
wxString UnescapeString(const wxString &aSource)
Definition: string.cpp:150
void SetPrefix(const wxString &aPrefix)
static bool ParseBusVector(const wxString &aBus, wxString *aName, std::vector< wxString > *aMemberList)
Parses a bus vector (e.g.
bool operator!=(const SCH_CONNECTION &aOther) const
wxString BusPrefix() const
SCH_ITEM * Parent() const
std::vector< MSG_PANEL_ITEM > MSG_PANEL_ITEMS
Definition: msgpanel.h:97
long VectorStart() const
std::vector< std::shared_ptr< SCH_CONNECTION > > & Members()
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
long m_vector_index
Index of bus vector member nets.
static bool isSuperSub(wxChar c)
long m_vector_end
Lowest member of a vector bus.
EDA_MSG_ITEM is used EDA_MSG_PANEL as the item type for displaying messages.
Definition: msgpanel.h:54
bool IsBus() const
bool IsSubsetOf(SCH_CONNECTION *aOther) const
Returns true if this connection is contained within aOther (but not the same as aOther)
wxString m_vector_prefix
void * m_lastDriver
WEAK POINTER (there is no guarantee it is still allocated) Equality comparisons are OK,...
CONNECTION_TYPE Type() const
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:196
SCH_ITEM * m_parent
The SCH_ITEM this connection is owned by.
bool operator==(const SCH_CONNECTION &aOther) const
Note: the equality operator for SCH_CONNECTION only tests the net properties, not the ownership / she...
This item represents a bus vector.
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:162