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  * @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 #include <regex>
22 #include <wx/tokenzr.h>
23 
24 #include <advanced_config.h>
25 #include <connection_graph.h>
26 #include <sch_component.h>
27 #include <sch_pin.h>
28 #include <sch_screen.h>
29 
30 #include <sch_connection.h>
31 
32 
60 static std::regex bus_label_re( "^([^[:space:]]+)(\\[[\\d]+\\.+[\\d]+\\])(~?)$" );
61 
62 static std::regex bus_group_label_re( "^([^[:space:]]+)?\\{((?:[^[:space:]]+(?:\\[[\\d]+\\.+[\\d]+\\])? ?)+)\\}$" );
63 
64 
66  m_sheet( aPath ),
67  m_parent( aParent )
68 {
69  Reset();
70 }
71 
72 
73 bool SCH_CONNECTION::operator==( const SCH_CONNECTION& aOther ) const
74 {
75  // NOTE: Not comparing m_dirty or net/bus/subgraph codes
76  if( ( aOther.m_driver == m_driver ) &&
77  ( aOther.m_type == m_type ) &&
78  ( aOther.m_name == m_name ) &&
79  ( aOther.m_sheet == m_sheet ) )
80  {
81  return true;
82  }
83 
84  return false;
85 }
86 
87 
89 {
90  m_driver = aItem;
91 
92  for( auto member : m_members )
93  member->SetDriver( aItem );
94 }
95 
96 
98 {
99  m_sheet = aSheet;
100 
101  for( auto member : m_members )
102  member->SetSheet( aSheet );
103 }
104 
105 
106 bool SCH_CONNECTION::operator!=( const SCH_CONNECTION& aOther ) const
107 {
108  return !( aOther == *this );
109 }
110 
111 
112 void SCH_CONNECTION::ConfigureFromLabel( wxString aLabel )
113 {
114  m_members.clear();
115 
116  if( IsBusVectorLabel( aLabel ) )
117  {
118  m_name = aLabel;
120 
121  std::vector<wxString> members;
122 
123  ParseBusVector( aLabel, &m_vector_prefix, members );
124  long i = 0;
125 
126  for( const auto& vector_member : members )
127  {
128  auto member = std::make_shared< SCH_CONNECTION >( m_parent, m_sheet );
129  member->m_type = CONNECTION_NET;
130  member->m_prefix = m_prefix;
131  member->m_name = vector_member;
132  member->m_vector_index = i++;
133  m_members.push_back( member );
134  }
135  }
136  else if( IsBusGroupLabel( aLabel ) )
137  {
139  m_name = aLabel;
140 
141  std::vector<wxString> members;
142  wxString group_name;
143 
144  if( ParseBusGroup( aLabel, &group_name, members ) )
145  {
146  // Named bus groups generate a net prefix, unnamed ones don't
147  wxString prefix = group_name != wxT( "" ) ? ( group_name + wxT( "." ) ) : wxT( "" );
148 
149  for( const auto& group_member : members )
150  {
151  // Handle alias inside bus group member list
152  if( auto alias = g_ConnectionGraph->GetBusAlias( group_member ) )
153  {
154  for( const auto& alias_member : alias->Members() )
155  {
156  auto member = std::make_shared< SCH_CONNECTION >( m_parent, m_sheet );
157  member->SetPrefix( prefix );
158  member->ConfigureFromLabel( alias_member );
159  m_members.push_back( member );
160  }
161  }
162  else
163  {
164  auto member = std::make_shared< SCH_CONNECTION >( m_parent, m_sheet );
165  member->SetPrefix( prefix );
166  member->ConfigureFromLabel( group_member );
167  m_members.push_back( member );
168  }
169  }
170  }
171  }
172  else
173  {
174  m_name = aLabel;
176  }
177 }
178 
179 
181 {
183  m_name.Empty();
184  m_prefix.Empty();
185  m_suffix .Empty();
186  m_driver = nullptr;
187  m_members.clear();
188  m_dirty = true;
189  m_net_code = 0;
190  m_bus_code = 0;
191  m_subgraph_code = 0;
192  m_vector_start = 0;
193  m_vector_end = 0;
194  m_vector_index = 0;
195  m_vector_prefix.Empty();
196 }
197 
198 
200 {
201  m_type = aOther.Type();
202  m_driver = aOther.Driver();
203  m_sheet = aOther.Sheet();
204  m_name = aOther.m_name;
205  m_prefix = aOther.Prefix();
206  m_suffix = aOther.Suffix();
207  m_members = aOther.Members();
208  m_net_code = aOther.NetCode();
209  m_bus_code = aOther.BusCode();
210  m_vector_start = aOther.VectorStart();
211  m_vector_end = aOther.VectorEnd();
212  m_vector_index = aOther.VectorIndex();
213  m_vector_prefix = aOther.VectorPrefix();
214 
215  // Note: subgraph code isn't cloned, it should remain with the original object
216 }
217 
218 
220 {
221  wxASSERT( Parent() );
222 
223  switch( Parent()->Type() )
224  {
225  case SCH_LABEL_T:
226  case SCH_GLOBAL_LABEL_T:
227  case SCH_HIER_LABEL_T:
228  case SCH_SHEET_PIN_T:
229  case SCH_SHEET_T:
230  case LIB_PIN_T:
231  return true;
232 
233  case SCH_PIN_T:
234  {
235  auto pin = static_cast<SCH_PIN*>( Parent() );
236 
237  // Only annotated components should drive nets
238  return pin->GetParentComponent()->IsAnnotated( &m_sheet );
239  }
240 
241  default:
242  return false;
243  }
244 }
245 
246 
247 wxString SCH_CONNECTION::Name( bool aIgnoreSheet ) const
248 {
249  wxString ret = m_prefix + m_name + m_suffix;
250 
251  if( m_name.IsEmpty() )
252  ret = "<NO NET>";
253 
254  if( !Parent() || m_type == CONNECTION_NONE )
255  return ret;
256 
257  if( !aIgnoreSheet )
258  {
259  bool prepend_path = true;
260 
261  if( m_driver )
262  {
263  switch( m_driver->Type() )
264  {
265  case SCH_PIN_T:
266  // Pins are either power connections or belong to a uniquely-annotated
267  // component, so they don't need a path if they are driving the subgraph
268  prepend_path = false;
269  break;
270 
271  case SCH_GLOBAL_LABEL_T:
272  prepend_path = false;
273  break;
274 
275  default:
276  break;
277  }
278  }
279 
280  if( prepend_path )
281  ret = m_sheet.PathHumanReadable() + ret;
282  }
283 
284  return ret;
285 }
286 
287 
288 void SCH_CONNECTION::SetPrefix( const wxString& aPrefix )
289 {
290  m_prefix = aPrefix;
291 
292  for( auto m : Members() )
293  m->SetPrefix( aPrefix );
294 }
295 
296 
297 void SCH_CONNECTION::SetSuffix( const wxString& aSuffix )
298 {
299  m_suffix = aSuffix;
300 
301  for( auto m : Members() )
302  m->SetSuffix( aSuffix );
303 }
304 
305 
307 {
308  if( !ADVANCED_CFG::GetCfg().m_realTimeConnectivity || !CONNECTION_GRAPH::m_allowRealTime )
309  return;
310 
311  wxString msg, group_name;
312  std::vector<wxString> group_members;
313 
314  aList.push_back( MSG_PANEL_ITEM( _( "Connection Name" ), Name(), BROWN ) );
315 
316  if( !IsBus() )
317  {
318  msg.Printf( "%d", m_net_code );
319  aList.push_back( MSG_PANEL_ITEM( _( "Net Code" ), msg, BROWN ) );
320  }
321 
322  if( auto alias = g_ConnectionGraph->GetBusAlias( m_name ) )
323  {
324  msg.Printf( _( "Bus Alias %s Members" ), m_name );
325 
326  wxString members;
327 
328  for( auto member : alias->Members() )
329  members << member << " ";
330 
331  aList.push_back( MSG_PANEL_ITEM( msg, members, RED ) );
332  }
333  else if( ParseBusGroup( m_name, &group_name, group_members ) )
334  {
335  for( auto group_member : group_members )
336  {
337  if( auto group_alias = g_ConnectionGraph->GetBusAlias( group_member ) )
338  {
339  msg.Printf( _( "Bus Alias %s Members" ), group_alias->GetName() );
340 
341  wxString members;
342 
343  for( auto member : group_alias->Members() )
344  members << member << " ";
345 
346  aList.push_back( MSG_PANEL_ITEM( msg, members, RED ) );
347  }
348  }
349  }
350 }
351 
352 
354 {
355  if( !ADVANCED_CFG::GetCfg().m_realTimeConnectivity || !CONNECTION_GRAPH::m_allowRealTime )
356  return;
357 
358  // These messages are not flagged as translatable, because they are only debug messges
359  wxString msg;
360 
361  AppendInfoToMsgPanel( aList );
362 
363  if( IsBus() )
364  {
365  msg.Printf( "%d", m_bus_code );
366  aList.push_back( MSG_PANEL_ITEM( "Bus Code", msg, BROWN ) );
367  }
368 
369  msg.Printf( "%d", m_subgraph_code );
370  aList.push_back( MSG_PANEL_ITEM( "Subgraph Code", msg, BROWN ) );
371 
372  if( auto driver = Driver() )
373  {
374  msg.Printf( "%s at %p", driver->GetSelectMenuText( MILLIMETRES ), driver );
375  aList.push_back( MSG_PANEL_ITEM( "Connection Source", msg, RED ) );
376  }
377 }
378 
379 
380 bool SCH_CONNECTION::IsBusLabel( const wxString& aLabel )
381 {
382  return IsBusVectorLabel( aLabel ) || IsBusGroupLabel( aLabel );
383 }
384 
385 
386 bool SCH_CONNECTION::IsBusVectorLabel( const wxString& aLabel )
387 {
388  try
389  {
390  return std::regex_match( std::string( aLabel.mb_str() ), bus_label_re );
391  }
392  catch( ... )
393  {
394  return false;
395  }
396 }
397 
398 
399 bool SCH_CONNECTION::IsBusGroupLabel( const wxString& aLabel )
400 {
401  try
402  {
403  return std::regex_match( std::string( aLabel.mb_str() ), bus_group_label_re );
404  }
405  catch( ... )
406  {
407  return false;
408  }
409 }
410 
411 
412 bool SCH_CONNECTION::ParseBusVector( wxString aBus, wxString* aName,
413  std::vector<wxString>& aMemberList ) const
414 {
415  auto ss_vector = std::string( aBus.mb_str() );
416  std::smatch matches;
417 
418  try
419  {
420  if( !std::regex_match( ss_vector, matches, bus_label_re ) )
421  return false;
422  }
423  catch( ... )
424  {
425  return false;
426  }
427 
428  long begin = 0, end = 0;
429  *aName = wxString( matches[1] );
430  wxString numberString( matches[2] );
431 
432  // If we have three match groups, it means there was a tilde at the end of the vector
433  bool append_tilde = wxString( matches[3] ).IsSameAs( wxT( "~" ) );
434 
435  // numberString will include the brackets, e.g. [5..0] so skip the first one
436  size_t i = 1, len = numberString.Len();
437  wxString tmp;
438 
439  while( i < len && numberString[i] != '.' )
440  {
441  tmp.Append( numberString[i] );
442  i++;
443  }
444 
445  tmp.ToLong( &begin );
446 
447  while( i < len && numberString[i] == '.' )
448  i++;
449 
450  tmp.Empty();
451 
452  while( i < len && numberString[i] != ']' )
453  {
454  tmp.Append( numberString[i] );
455  i++;
456  }
457 
458  tmp.ToLong( &end );
459 
460  if( begin < 0 )
461  begin = 0;
462 
463  if( end < 0 )
464  end = 0;
465 
466  if( begin > end )
467  std::swap( begin, end );
468 
469  for( long idx = begin; idx <= end; ++idx )
470  {
471  wxString str = *aName;
472  str << idx;
473 
474  if( append_tilde )
475  str << '~';
476 
477  aMemberList.emplace_back( str );
478  }
479 
480  return true;
481 }
482 
483 
484 bool SCH_CONNECTION::ParseBusGroup( wxString aGroup, wxString* aName,
485  std::vector<wxString>& aMemberList ) const
486 {
487  auto ss_group = std::string( aGroup.mb_str() );
488  std::smatch matches;
489 
490  try
491  {
492  if( !std::regex_match( ss_group, matches, bus_group_label_re ) )
493  {
494  return false;
495  }
496  }
497  catch( ... )
498  {
499  return false;
500  }
501 
502  *aName = wxString( matches[1] );
503 
504  wxStringTokenizer tokenizer( wxString( matches[2] ), " " );
505  while( tokenizer.HasMoreTokens() )
506  {
507  aMemberList.push_back( tokenizer.GetNextToken() );
508  }
509 
510  return true;
511 }
512 
513 
515 {
516  if( aOther->IsNet() )
517  return IsNet() ? ( aOther->Name( true ) == Name( true ) ) : false;
518 
519  if( !IsBus() )
520  return false;
521 
522  std::vector<wxString> mine, theirs;
523 
524  for( auto m : Members() )
525  mine.push_back( m->Name( true ) );
526 
527  for( auto m : aOther->Members() )
528  theirs.push_back( m->Name( true ) );
529 
530  std::set<wxString> subset;
531 
532  std::set_intersection( mine.begin(), mine.end(),
533  theirs.begin(), theirs.end(),
534  std::inserter(subset, subset.begin() ) );
535 
536  return ( !subset.empty() );
537 }
538 
539 
541 {
542  if( !aOther->IsBus() )
543  return false;
544 
545  auto me = Name( true );
546 
547  for( auto m : aOther->Members() )
548  if( m->Name( true ) == me )
549  return true;
550 
551  return false;
552 }
int m_subgraph_code
Groups directly-connected items.
static std::regex bus_label_re("^([^[:space:]]+)(\\[[\\d]+\\.+[\\d]+\\])(~?)$")
Buses can be defined in multiple ways.
wxString Prefix() const
No connection to this item.
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)
wxString m_name
Name of the bus.
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.
SCH_CONNECTION(SCH_ITEM *aParent=nullptr, SCH_SHEET_PATH aPath=SCH_SHEET_PATH())
bool IsMemberOfBus(SCH_CONNECTION *aOther) const
Returns true if this connection is a member of bus connection aOther.
void SetDriver(SCH_ITEM *aItem)
void SetSheet(SCH_SHEET_PATH aSheet)
CONNECTION_GRAPH * g_ConnectionGraph
This also wants to live in the eventual SCHEMATIC object.
const string & str
Definition: json11.cpp:596
int BusCode() const
CONNECTION_TYPE m_type
int NetCode() const
wxString Suffix() const
wxString Name(bool aIgnoreSheet=false) const
long VectorEnd() const
void Reset()
Clears connectivity information.
This item represents a net.
SCH_ITEM * Driver() const
void ConfigureFromLabel(wxString aLabel)
Configures the connection given a label.
static bool IsBusVectorLabel(const wxString &aLabel)
Test if aLabel has a bus vector notation (simple bus, e.g.
Definition: colors.h:60
Class SCH_SHEET_PATH.
bool ParseBusGroup(wxString aGroup, wxString *name, std::vector< wxString > &aMemberList) const
Parses a bus group label into the name and a list of components.
long VectorIndex() 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,...
#define _(s)
SCH_SHEET_PATH m_sheet
The hierarchical sheet this connection is on.
void AppendDebugInfoToMsgPanel(MSG_PANEL_ITEMS &aList) const
Adds extended debug information about the connection object to aList.
wxString m_suffix
Name suffix (used only for disambiguation)
static bool m_allowRealTime
SCH_SHEET_PATH Sheet() const
void AppendInfoToMsgPanel(MSG_PANEL_ITEMS &aList) const
Adds information about the connection object to aList.
bool IsNet() const
long m_vector_start
Highest member of a vector bus.
static std::regex bus_group_label_re("^([^[:space:]]+)?\\{((?:[^[:space:]]+(?:\\[[\\d]+\\.+[\\d]+\\])? ?)+)\\}$")
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)
void SetPrefix(const wxString &aPrefix)
bool ParseBusVector(wxString aBus, wxString *aName, std::vector< wxString > &aMemberList) const
Parses a bus vector (e.g.
size_t i
Definition: json11.cpp:597
bool operator!=(const SCH_CONNECTION &aOther) const
SCH_ITEM * Parent() const
std::vector< MSG_PANEL_ITEM > MSG_PANEL_ITEMS
Definition: msgpanel.h:102
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 of advanced config.
long m_vector_index
Index of bus vector member nets.
long m_vector_end
Lowest member of a vector bus.
static bool IsBusGroupLabel(const wxString &aLabel)
Test if aLabel has a bus group notation.
This item represents a bus group.
Class EDA_MSG_ITEM is used EDA_MSG_PANEL as the item type for displaying messages.
Definition: msgpanel.h:53
wxString PathHumanReadable() const
Function PathHumanReadable returns the sheet path in a human readable form, i.e.
bool IsBus() const
bool IsSubsetOf(SCH_CONNECTION *aOther) const
Returns true if aOther is a subset of this connection or vice versa.
wxString m_vector_prefix
CONNECTION_TYPE Type() const
Class SCH_ITEM is a base class for any item which can be embedded within the SCHEMATIC container clas...
Definition: sch_item.h:114
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...
KICAD_T Type() const
Function Type()
Definition: base_struct.h:210
This item represents a bus vector.
Definition: colors.h:62