KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 The 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>
31#include <advanced_config.h>
32#include <string_utils.h>
33
34#include <sch_connection.h>
35#include <boost/algorithm/string/join.hpp>
36
66 m_sheet( aPath ),
67 m_local_sheet( aPath ),
68 m_parent( aParent ),
69 m_driver( nullptr ),
70 m_graph( nullptr )
71{
72 Reset();
73}
74
75
77 m_sheet( SCH_SHEET_PATH() ),
78 m_local_sheet( SCH_SHEET_PATH() ),
79 m_parent( nullptr ),
80 m_driver( nullptr ),
81 m_graph( aGraph )
82{
83 Reset();
84}
85
86
88 m_parent( nullptr ),
89 m_driver( nullptr )
90{
91 Reset();
92 Clone( aOther );
93}
94
95
96bool SCH_CONNECTION::operator==( const SCH_CONNECTION& aOther ) const
97{
98 // NOTE: Not comparing m_dirty or net/bus/subgraph codes
99 if( ( aOther.m_driver == m_driver ) &&
100 ( aOther.m_type == m_type ) &&
101 ( aOther.m_name == m_name ) &&
102 ( aOther.m_sheet == m_sheet ) )
103 {
104 return true;
105 }
106
107 return false;
108}
109
110
112{
113 m_driver = aItem;
114
115 recacheName();
116
117 for( const std::shared_ptr<SCH_CONNECTION>& member : m_members )
118 member->SetDriver( aItem );
119}
120
121
123{
124 m_sheet = aSheet;
125 m_local_sheet = aSheet;
126
127 recacheName();
128
129 for( const std::shared_ptr<SCH_CONNECTION>& member : m_members )
130 member->SetSheet( aSheet );
131}
132
133
135{
136 return !( aOther == *this );
137}
138
139
140void SCH_CONNECTION::ConfigureFromLabel( const wxString& aLabel )
141{
142 m_members.clear();
143
144 m_name = aLabel;
145 m_local_name = aLabel;
147
148 wxString prefix;
149 std::vector<wxString> members;
150
151 wxString unescaped = UnescapeString( aLabel );
152
153 if( NET_SETTINGS::ParseBusVector( unescaped, &prefix, &members ) )
154 {
155 m_type = CONNECTION_TYPE::BUS;
156 m_vector_prefix = prefix;
157
158 long i = 0;
159
160 for( const wxString& vector_member : members )
161 {
162 auto member = std::make_shared<SCH_CONNECTION>( m_parent, m_sheet );
163 member->m_type = CONNECTION_TYPE::NET;
164 member->m_prefix = m_prefix;
165 member->m_local_name = vector_member;
166 member->m_local_prefix = m_prefix;
167 member->m_vector_index = i++;
168 member->SetName( vector_member );
169 member->SetGraph( m_graph );
170 m_members.push_back( member );
171 }
172 }
173 else if( NET_SETTINGS::ParseBusGroup( unescaped, &prefix, &members ) )
174 {
175 m_type = CONNECTION_TYPE::BUS_GROUP;
176 m_bus_prefix = prefix;
177
178 // Named bus groups generate a net prefix, unnamed ones don't
179 if( !prefix.IsEmpty() )
180 prefix += wxT( "." );
181
182 for( const wxString& group_member : members )
183 {
184 // Handle alias inside bus group member list
185 if( auto alias = m_graph->GetBusAlias( group_member ) )
186 {
187 for( const wxString& alias_member : alias->Members() )
188 {
189 auto member = std::make_shared< SCH_CONNECTION >( m_parent, m_sheet );
190 member->SetPrefix( prefix );
191 member->SetGraph( m_graph );
192 member->ConfigureFromLabel( EscapeString( alias_member, CTX_NETNAME ) );
193 m_members.push_back( member );
194 }
195 }
196 else
197 {
198 auto member = std::make_shared< SCH_CONNECTION >( m_parent, m_sheet );
199 member->SetPrefix( prefix );
200 member->SetGraph( m_graph );
201 member->ConfigureFromLabel( group_member );
202 m_members.push_back( member );
203 }
204 }
205 }
206 else
207 {
208 m_type = CONNECTION_TYPE::NET;
209 }
210
211 recacheName();
212}
213
214
216{
217 m_type = CONNECTION_TYPE::NONE;
218 m_name.Empty();
219 m_local_name.Empty();
220 m_local_prefix.Empty();
221 m_cached_name.Empty();
223 m_prefix.Empty();
224 m_bus_prefix.Empty();
225 m_suffix .Empty();
227 m_driver = nullptr;
228 m_members.clear();
229 m_dirty = true;
230 m_net_code = 0;
231 m_bus_code = 0;
232 m_subgraph_code = 0;
233 m_vector_start = 0;
234 m_vector_end = 0;
235 m_vector_index = 0;
236 m_vector_prefix.Empty();
237}
238
239
241{
242 m_graph = aOther.m_graph;
243
244 // Note: m_lastDriver is not cloned as it needs to be the last driver of *this* connection
245 m_driver = aOther.Driver();
246 m_sheet = aOther.Sheet();
247
248 // Note: m_local_sheet is not cloned
249 m_name = aOther.m_name;
250 m_type = aOther.m_type;
251
252 // Note: m_local_name is not cloned if not set yet
253 if( m_local_name.IsEmpty() )
254 {
255 m_local_name = aOther.LocalName();
256 m_local_prefix = aOther.Prefix();
257 }
258
259 m_prefix = aOther.Prefix();
260
261 // m_bus_prefix is not cloned; only used for local names
262 m_suffix = aOther.Suffix();
263 m_net_code = aOther.NetCode();
264 m_bus_code = aOther.BusCode();
265 m_vector_start = aOther.VectorStart();
266 m_vector_end = aOther.VectorEnd();
267
268 // Note: m_vector_index is not cloned
269 m_vector_prefix = aOther.VectorPrefix();
270
271 // Note: subgraph code isn't cloned, it should remain with the original object
272
273 // Handle vector bus members: make sure local names are preserved where possible
274 const std::vector<std::shared_ptr<SCH_CONNECTION>>& otherMembers = aOther.Members();
275
276 if( m_type == CONNECTION_TYPE::BUS && aOther.Type() == CONNECTION_TYPE::BUS )
277 {
278 if( m_members.empty() )
279 {
280 m_members = otherMembers;
281 }
282 else
283 {
284 size_t cloneLimit = std::min( m_members.size(), otherMembers.size() );
285
286 for( size_t i = 0; i < cloneLimit; ++i )
287 m_members[i]->Clone( *otherMembers[i] );
288 }
289 }
290 else if( m_type == CONNECTION_TYPE::BUS_GROUP && aOther.Type() == CONNECTION_TYPE::BUS_GROUP )
291 {
292 if( m_members.empty() )
293 {
294 m_members = otherMembers;
295 }
296 else
297 {
298 // TODO: refactor this once we support deep nesting
299 for( std::shared_ptr<SCH_CONNECTION>& member : m_members )
300 {
301 auto it = std::find_if( otherMembers.begin(), otherMembers.end(),
302 [&]( const std::shared_ptr<SCH_CONNECTION>& aTest )
303 {
304 return aTest->LocalName() == member->LocalName();
305 } );
306
307 if( it != otherMembers.end() )
308 member->Clone( **it );
309 }
310 }
311 }
312
313 m_type = aOther.Type();
314
315 recacheName();
316}
317
318
320{
321 wxASSERT( Parent() );
322
323 switch( Parent()->Type() )
324 {
325 case SCH_LABEL_T:
327 case SCH_HIER_LABEL_T:
328 case SCH_SHEET_PIN_T:
329 case SCH_SHEET_T:
330 return true;
331
332 case SCH_PIN_T:
333 {
334 const SCH_PIN* pin = static_cast<const SCH_PIN*>( Parent() );
335
336 if( const SCH_SYMBOL* symbol = dynamic_cast<const SCH_SYMBOL*>( pin->GetParentSymbol() ) )
337 {
338 // Only annotated symbols should drive nets.
339 return pin->IsPower() || symbol->IsAnnotated( &m_sheet );
340 }
341
342 return true;
343 }
344
345 default:
346 return false;
347 }
348}
349
350
352{
353 return m_driver != m_lastDriver;
354}
355
356
358{
360}
361
362
363
364wxString SCH_CONNECTION::Name( bool aIgnoreSheet ) const
365{
366 wxASSERT( !m_cached_name.IsEmpty() );
367 return aIgnoreSheet ? m_cached_name : m_cached_name_with_path;
368}
369
370
372{
373 wxString retv;
374
375 if( m_graph )
376 {
378
379 if( subgraph )
380 retv = subgraph->GetNetName();
381 }
382
383 return retv;
384}
385
386
388{
389 m_cached_name = m_name.IsEmpty() ? wxString( wxT( "<NO NET>" ) )
390 : wxString( m_prefix ) << m_name << m_suffix;
391
392 bool prepend_path = true;
393
394 if( !Parent() || m_type == CONNECTION_TYPE::NONE )
395 prepend_path = false;
396
397 if( m_driver )
398 {
399 switch( m_driver->Type() )
400 {
402 prepend_path = false;
403 break;
404
405 case SCH_PIN_T:
406 { // Normal pins and global power pins do not need a path. But local power pins do
407 SCH_PIN* pin = static_cast<SCH_PIN*>( m_driver );
408
409 prepend_path = pin->IsLocalPower();
410 break;
411 }
412
413 default:
414 break;
415 }
416 }
417
420}
421
422
423void SCH_CONNECTION::SetPrefix( const wxString& aPrefix )
424{
425 m_prefix = aPrefix;
426
427 recacheName();
428
429 for( const std::shared_ptr<SCH_CONNECTION>& m : Members() )
430 m->SetPrefix( aPrefix );
431}
432
433
434void SCH_CONNECTION::SetSuffix( const wxString& aSuffix )
435{
436 m_suffix = aSuffix;
437
438 recacheName();
439
440 for( const std::shared_ptr<SCH_CONNECTION>& m : Members() )
441 m->SetSuffix( aSuffix );
442}
443
444
445void SCH_CONNECTION::AppendInfoToMsgPanel( std::vector<MSG_PANEL_ITEM>& aList ) const
446{
447 wxString msg, group_name, members;
448 std::vector<wxString> group_members;
449
450 aList.emplace_back( _( "Connection Name" ), UnescapeString( Name() ) );
451
452 if( std::shared_ptr<BUS_ALIAS> alias = m_graph->GetBusAlias( m_name ) )
453 {
454 msg.Printf( _( "Bus Alias %s Members" ), m_name );
455 aList.emplace_back( msg, boost::algorithm::join( alias->Members(), " " ) );
456 }
457 else if( NET_SETTINGS::ParseBusGroup( m_name, &group_name, &group_members ) )
458 {
459 for( const wxString& group_member : group_members )
460 {
461 if( std::shared_ptr<BUS_ALIAS> group_alias = m_graph->GetBusAlias( group_member ) )
462 {
463 msg.Printf( _( "Bus Alias %s Members" ), group_alias->GetName() );
464 aList.emplace_back( msg, boost::algorithm::join( group_alias->Members(), " " ) );
465 }
466 }
467 }
468
469#if defined(DEBUG)
470 // These messages are not flagged as translatable, because they are only debug messages
471
472 if( IsBus() )
473 aList.emplace_back( wxT( "Bus Code" ), wxString::Format( "%d", m_bus_code ) );
474
475 aList.emplace_back( wxT( "Subgraph Code" ), wxString::Format( "%d", m_subgraph_code ) );
476
477 if( SCH_ITEM* driver = Driver() )
478 {
479 UNITS_PROVIDER unitsProvider( schIUScale, EDA_UNITS::MM );
480
481 msg.Printf( wxS( "%s at %p" ),
482 driver->GetItemDescription( &unitsProvider, false ),
483 driver );
484 aList.emplace_back( wxT( "Connection Source" ), msg );
485 }
486#endif
487}
488
489
490bool SCH_CONNECTION::IsBusLabel( const wxString& aLabel )
491{
492 const wxString& unescaped = UnescapeString( aLabel );
493
494 return NET_SETTINGS::ParseBusVector( unescaped, nullptr, nullptr )
495 || NET_SETTINGS::ParseBusGroup( unescaped, nullptr, nullptr );
496}
497
498
499bool SCH_CONNECTION::MightBeBusLabel( const wxString& aLabel )
500{
501 // Weak heuristic for performance reasons. Stronger test will be used for connectivity
502 wxString label = UnescapeString( aLabel );
503
504 return label.Contains( wxT( "[" ) ) || label.Contains( wxT( "{" ) );
505}
506
507
508const std::vector< std::shared_ptr< SCH_CONNECTION > > SCH_CONNECTION::AllMembers() const
509{
510 std::vector< std::shared_ptr< SCH_CONNECTION > > ret( m_members );
511
512 for( const std::shared_ptr<SCH_CONNECTION>& member : m_members )
513 {
514 if( member->IsBus() )
515 ret.insert( ret.end(), member->Members().begin(), member->Members().end() );
516 }
517
518 return ret;
519}
520
521
522static bool isSuperSubOverbar( wxChar c )
523{
524 return c == '_' || c == '^' || c == '~';
525};
526
527
528wxString SCH_CONNECTION::PrintBusForUI( const wxString& aGroup )
529{
530 size_t groupLen = aGroup.length();
531 size_t i = 0;
532 wxString ret;
533
534 // Parse prefix
535 //
536 for( ; i < groupLen; ++i )
537 {
538 if( isSuperSubOverbar( aGroup[i] ) && i + 1 < groupLen && aGroup[i+1] == '{' )
539 {
540 i++;
541 continue;
542 }
543 else if( aGroup[i] == '}' )
544 {
545 continue;
546 }
547
548 ret += aGroup[i];
549
550 if( aGroup[i] == '{' )
551 break;
552 }
553
554 // Parse members
555 //
556 i++; // '{' character
557
558 for( ; i < groupLen; ++i )
559 {
560 if( isSuperSubOverbar( aGroup[i] ) && i + 1 < groupLen && aGroup[i+1] == '{' )
561 {
562 i++;
563 continue;
564 }
565 else if( aGroup[i] == '}' )
566 {
567 continue;
568 }
569
570 ret += aGroup[i];
571
572 if( aGroup[i] == '}' )
573 break;
574 }
575
576 return ret;
577}
578
579
581{
582 if( !aOther->IsBus() )
583 return false;
584
585 for( const std::shared_ptr<SCH_CONNECTION>& member : aOther->Members() )
586 {
587 if( member->FullLocalName() == FullLocalName() )
588 return true;
589 }
590
591 return false;
592}
593
594
596{
597 if( !aOther->IsBus() )
598 return false;
599
600 wxString me = Name( true );
601
602 for( const std::shared_ptr<SCH_CONNECTION>& m : aOther->Members() )
603 {
604 if( m->Name( true ) == me )
605 return true;
606 }
607
608 return false;
609}
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:110
Calculate the connectivity of a schematic and generates netlists.
std::shared_ptr< BUS_ALIAS > GetBusAlias(const wxString &aName)
Return a bus alias pointer for the given name if it exists (from cache)
CONNECTION_SUBGRAPH * GetSubgraphForItem(SCH_ITEM *aItem) const
A subgraph is a set of items that are electrically connected on a single sheet.
wxString GetNetName() const
Return the fully-qualified net name for this subgraph (if one exists)
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:108
static bool ParseBusGroup(const wxString &aGroup, wxString *name, std::vector< wxString > *aMemberList)
Parse a bus group label into the name and a list of components.
static bool ParseBusVector(const wxString &aBus, wxString *aName, std::vector< wxString > *aMemberList)
Parse a bus vector (e.g.
Each graphical item can have a SCH_CONNECTION describing its logical connection (to a bus or net).
long VectorEnd() const
wxString FullLocalName() const
long m_vector_start
Highest member of a vector bus.
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.
SCH_ITEM * m_parent
The SCH_ITEM this connection is owned by.
SCH_SHEET_PATH m_sheet
The hierarchical sheet this connection is on.
bool operator!=(const SCH_CONNECTION &aOther) const
void SetSuffix(const wxString &aSuffix)
long VectorStart() const
wxString m_name
Name of the connection.
SCH_ITEM * Parent() const
wxString GetNetName() const
long m_vector_end
Lowest member of a vector bus.
SCH_SHEET_PATH Sheet() const
CONNECTION_TYPE Type() const
bool HasDriverChanged() const
const std::vector< std::shared_ptr< SCH_CONNECTION > > AllMembers() const
SCH_CONNECTION(SCH_ITEM *aParent=nullptr, const SCH_SHEET_PATH &aPath=SCH_SHEET_PATH())
Buses can be defined in multiple ways.
void Reset()
Clears connectivity information.
int BusCode() const
bool operator==(const SCH_CONNECTION &aOther) const
Note: the equality operator for SCH_CONNECTION only tests the net properties, not the ownership / she...
std::vector< std::shared_ptr< SCH_CONNECTION > > m_members
For bus connections, store a list of member connections.
wxString m_local_prefix
Local prefix for group bus members (used with m_local_name)
CONNECTION_TYPE m_type
SCH_ITEM * m_driver
The SCH_ITEM that drives this connection's net.
bool IsDriver() const
Checks if the SCH_ITEM this connection is attached to can drive connections Drivers can be labels,...
void SetPrefix(const wxString &aPrefix)
wxString LocalName() const
wxString Name(bool aIgnoreSheet=false) const
bool IsSubsetOf(SCH_CONNECTION *aOther) const
Returns true if this connection is contained within aOther (but not the same as aOther)
wxString Suffix() const
void SetDriver(SCH_ITEM *aItem)
SCH_SHEET_PATH m_local_sheet
When a connection is overridden by one on a different hierarchical sheet, it will be cloned and m_she...
bool IsBus() const
wxString m_prefix
Prefix if connection is member of a labeled bus group (or "" if not)
void Clone(const SCH_CONNECTION &aOther)
Copies connectivity information (but not parent) from another connection.
void * m_lastDriver
WEAK POINTER (there is no guarantee it is still allocated) Equality comparisons are OK,...
wxString m_cached_name_with_path
Full name including sheet path (if not global)
long m_vector_index
Index of bus vector member nets.
wxString VectorPrefix() const
SCH_ITEM * Driver() const
void AppendInfoToMsgPanel(std::vector< MSG_PANEL_ITEM > &aList) const
Adds information about the connection object to aList.
static bool IsBusLabel(const wxString &aLabel)
Test if aLabel has a bus notation.
wxString m_suffix
Name suffix (used only for disambiguation)
wxString m_bus_prefix
Optional prefix of a bus group (always empty for nets and vector buses)
const std::vector< std::shared_ptr< SCH_CONNECTION > > & Members() const
void SetSheet(SCH_SHEET_PATH aSheet)
wxString m_cached_name
Full name, including prefix and suffix.
wxString Prefix() const
CONNECTION_GRAPH * m_graph
Pointer to the connection graph for the schematic this connection exists on.
static wxString PrintBusForUI(const wxString &aString)
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...
int m_subgraph_code
Groups directly-connected items.
wxString m_vector_prefix
Prefix name of the vector, if m_type == CONNECTION_BUS (or "" if not).
static bool MightBeBusLabel(const wxString &aLabel)
Test if aLabel looks like a bus notation.
int NetCode() const
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:167
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
wxString PathHumanReadable(bool aUseShortRootName=true, bool aStripTrailingSeparator=false) const
Return the sheet path in a human readable form made from the sheet names.
Schematic symbol object.
Definition: sch_symbol.h:75
#define _(s)
static bool isSuperSubOverbar(wxChar c)
static bool isSuperSubOverbar(wxChar c)
wxString UnescapeString(const wxString &aSource)
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
@ CTX_NETNAME
Definition: string_utils.h:53
@ SCH_LABEL_T
Definition: typeinfo.h:167
@ SCH_SHEET_T
Definition: typeinfo.h:175
@ SCH_HIER_LABEL_T
Definition: typeinfo.h:169
@ SCH_SHEET_PIN_T
Definition: typeinfo.h:174
@ SCH_GLOBAL_LABEL_T
Definition: typeinfo.h:168
@ SCH_PIN_T
Definition: typeinfo.h:153