KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 <[email protected]>
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
64
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
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 {
156 m_vector_prefix = std::move( prefix );
157
158 long i = 0;
159
160 for( const wxString& vector_member : members )
161 {
162 std::shared_ptr<SCH_CONNECTION> member = std::make_shared<SCH_CONNECTION>( m_parent, m_sheet );
163
164 member->m_type = CONNECTION_TYPE::NET;
165 member->m_prefix = m_prefix;
166 member->m_local_name = vector_member;
167 member->m_local_prefix = m_prefix;
168 member->m_vector_index = i++;
169 member->SetName( vector_member );
170 member->SetGraph( m_graph );
171 m_members.push_back( std::move( member ) );
172 }
173 }
174 else if( NET_SETTINGS::ParseBusGroup( unescaped, &prefix, &members ) )
175 {
177 m_bus_prefix = prefix;
178
179 // Named bus groups generate a net prefix, unnamed ones don't
180 if( !prefix.IsEmpty() )
181 prefix += wxT( "." );
182
183 for( const wxString& group_member : members )
184 {
185 // Handle alias inside bus group member list
186 if( auto alias = m_graph->GetBusAlias( group_member ) )
187 {
188 for( const wxString& alias_member : alias->Members() )
189 {
190 std::shared_ptr<SCH_CONNECTION> member = std::make_shared<SCH_CONNECTION>( m_parent, m_sheet );
191 member->SetPrefix( prefix );
192 member->SetGraph( m_graph );
193 member->ConfigureFromLabel( EscapeString( alias_member, CTX_NETNAME ) );
194 m_members.push_back( std::move( member ) );
195 }
196 }
197 else
198 {
199 std::shared_ptr<SCH_CONNECTION> member = std::make_shared<SCH_CONNECTION>( m_parent, m_sheet );
200 member->SetPrefix( prefix );
201 member->SetGraph( m_graph );
202 member->ConfigureFromLabel( group_member );
203 m_members.push_back( std::move( member ) );
204 }
205 }
206 }
207 else
208 {
210 }
211
212 recacheName();
213}
214
215
217{
219 m_name.Empty();
220 m_local_name.Empty();
221 m_local_prefix.Empty();
222 m_cached_name.Empty();
224 m_prefix.Empty();
225 m_bus_prefix.Empty();
226 m_suffix .Empty();
228 m_driver = nullptr;
229 m_members.clear();
230 m_dirty = true;
231 m_net_code = 0;
232 m_bus_code = 0;
233 m_subgraph_code = 0;
234 m_vector_start = 0;
235 m_vector_end = 0;
236 m_vector_index = 0;
237 m_vector_prefix.Empty();
238}
239
240
242{
243 m_graph = aOther.m_graph;
244
245 // Note: m_lastDriver is not cloned as it needs to be the last driver of *this* connection
246 m_driver = aOther.Driver();
247 m_sheet = aOther.Sheet();
248
249 // Note: m_local_sheet is not cloned
250 m_name = aOther.m_name;
251
252 CONNECTION_TYPE origType = m_type;
253 m_type = aOther.m_type;
254
255 // Note: m_local_name is not cloned if not set yet
256 if( m_local_name.IsEmpty() )
257 {
258 m_local_name = aOther.LocalName();
259 m_local_prefix = aOther.Prefix();
260 }
261
262 m_prefix = aOther.Prefix();
263
264 // m_bus_prefix is not cloned; only used for local names
265 m_suffix = aOther.Suffix();
266 m_net_code = aOther.NetCode();
267 m_bus_code = aOther.BusCode();
268 m_vector_start = aOther.VectorStart();
269 m_vector_end = aOther.VectorEnd();
270
271 // Note: m_vector_index is not cloned
272 m_vector_prefix = aOther.VectorPrefix();
273
274 // Note: subgraph code isn't cloned, it should remain with the original object
275
276 // Handle vector bus members: make sure local names are preserved where possible
277 const std::vector<std::shared_ptr<SCH_CONNECTION>>& otherMembers = aOther.Members();
278
279 auto cloneMember =
280 [&]( const SCH_CONNECTION& aSrc ) -> std::shared_ptr<SCH_CONNECTION>
281 {
282 auto copy = std::make_shared<SCH_CONNECTION>( m_parent, m_sheet );
283 copy->SetGraph( m_graph );
284 copy->Clone( aSrc );
285
286 copy->m_vector_index = aSrc.m_vector_index;
287
288 return copy;
289 };
290
291 if( origType == CONNECTION_TYPE::BUS && aOther.Type() == CONNECTION_TYPE::BUS )
292 {
293 if( m_members.empty() )
294 {
295 m_members.reserve( otherMembers.size() );
296
297 for( const std::shared_ptr<SCH_CONNECTION>& src : otherMembers )
298 m_members.push_back( cloneMember( *src ) );
299 }
300 else
301 {
302 size_t cloneLimit = std::min( m_members.size(), otherMembers.size() );
303
304 for( size_t i = 0; i < cloneLimit; ++i )
305 m_members[i]->Clone( *otherMembers[i] );
306 }
307 }
308 else if( origType == CONNECTION_TYPE::BUS_GROUP && aOther.Type() == CONNECTION_TYPE::BUS_GROUP )
309 {
310 if( m_members.empty() )
311 {
312 m_members.reserve( otherMembers.size() );
313
314 for( const std::shared_ptr<SCH_CONNECTION>& src : otherMembers )
315 m_members.push_back( cloneMember( *src ) );
316 }
317 else
318 {
319 // TODO: refactor this once we support deep nesting
320 for( std::shared_ptr<SCH_CONNECTION>& member : m_members )
321 {
322 auto it = std::find_if( otherMembers.begin(), otherMembers.end(),
323 [&]( const std::shared_ptr<SCH_CONNECTION>& aTest )
324 {
325 return aTest->LocalName() == member->LocalName();
326 } );
327
328 if( it != otherMembers.end() )
329 member->Clone( **it );
330 }
331 }
332 }
333 else if( aOther.IsBus() )
334 {
335 m_members.clear();
336 m_members.reserve( otherMembers.size() );
337
338 for( const std::shared_ptr<SCH_CONNECTION>& src : otherMembers )
339 m_members.push_back( cloneMember( *src ) );
340 }
341
342 m_type = aOther.Type();
343
344 recacheName();
345}
346
347
349{
350 wxASSERT( Parent() );
351
352 switch( Parent()->Type() )
353 {
354 case SCH_LABEL_T:
356 case SCH_HIER_LABEL_T:
357 case SCH_SHEET_PIN_T:
358 case SCH_SHEET_T:
359 return true;
360
361 case SCH_PIN_T:
362 {
363 const SCH_PIN* pin = static_cast<const SCH_PIN*>( Parent() );
364
365 if( const SCH_SYMBOL* symbol = dynamic_cast<const SCH_SYMBOL*>( pin->GetParentSymbol() ) )
366 {
367 // Only annotated symbols should drive nets.
368 return pin->IsPower() || symbol->IsAnnotated( &m_sheet );
369 }
370
371 return true;
372 }
373
374 default:
375 return false;
376 }
377}
378
379
381{
382 return m_driver != m_lastDriver;
383}
384
385
390
391
392
393wxString SCH_CONNECTION::Name( bool aIgnoreSheet ) const
394{
395 wxASSERT( !m_cached_name.IsEmpty() );
396 return aIgnoreSheet ? m_cached_name : m_cached_name_with_path;
397}
398
399
401{
402 wxString retv;
403
404 if( m_graph )
405 {
406 CONNECTION_SUBGRAPH* subgraph = m_graph->GetSubgraphForItem( m_parent );
407
408 if( subgraph )
409 retv = subgraph->GetNetName();
410 }
411
412 return retv;
413}
414
415
417{
418 m_cached_name = m_name.IsEmpty() ? wxString( wxT( "<NO NET>" ) )
419 : wxString( m_prefix ) << m_name << m_suffix;
420
421 bool prepend_path = true;
422
423 if( !Parent() || m_type == CONNECTION_TYPE::NONE )
424 prepend_path = false;
425
426 if( m_driver )
427 {
428 switch( m_driver->Type() )
429 {
431 prepend_path = false;
432 break;
433
434 case SCH_PIN_T:
435 { // Normal pins and global power pins do not need a path. But local power pins do
436 SCH_PIN* pin = static_cast<SCH_PIN*>( m_driver );
437
438 prepend_path = pin->IsLocalPower();
439 break;
440 }
441
442 default:
443 break;
444 }
445 }
446
447 // Use aEscapeSheetNames=true so that sheets with '/' in their names have the slash
448 // escaped to "{slash}". This ensures pattern matching for net classes works correctly
449 // since '/' is used as the hierarchy separator.
450 m_cached_name_with_path = prepend_path
451 ? m_sheet.PathHumanReadable( true, false, true ) << m_cached_name
453}
454
455
456void SCH_CONNECTION::SetPrefix( const wxString& aPrefix )
457{
458 m_prefix = aPrefix;
459
460 recacheName();
461
462 for( const std::shared_ptr<SCH_CONNECTION>& m : Members() )
463 m->SetPrefix( aPrefix );
464}
465
466
467void SCH_CONNECTION::SetSuffix( const wxString& aSuffix )
468{
469 m_suffix = aSuffix;
470
471 recacheName();
472
473 for( const std::shared_ptr<SCH_CONNECTION>& m : Members() )
474 m->SetSuffix( aSuffix );
475}
476
477
478void SCH_CONNECTION::AppendInfoToMsgPanel( std::vector<MSG_PANEL_ITEM>& aList ) const
479{
480 wxString msg, group_name;
481 std::vector<wxString> group_members;
482
483 aList.emplace_back( _( "Connection Name" ), UnescapeString( Name() ) );
484
485 if( IsBus() )
486 {
487 if( std::shared_ptr<BUS_ALIAS> alias = m_graph->GetBusAlias( m_name ) )
488 {
489 msg.Printf( _( "Bus Alias %s Members" ), m_name );
490 aList.emplace_back( msg, boost::algorithm::join( alias->Members(), " " ) );
491 }
492 else if( NET_SETTINGS::ParseBusGroup( m_name, &group_name, &group_members ) )
493 {
494 for( const wxString& group_member : group_members )
495 {
496 if( std::shared_ptr<BUS_ALIAS> group_alias = m_graph->GetBusAlias( group_member ) )
497 {
498 msg.Printf( _( "Bus Alias %s Members" ), group_alias->GetName() );
499 aList.emplace_back( msg, boost::algorithm::join( group_alias->Members(), " " ) );
500 }
501 }
502 }
503 }
504
505#if defined(DEBUG)
506 // These messages are not flagged as translatable, because they are only debug messages
507
508 if( IsBus() )
509 aList.emplace_back( wxT( "Bus Code" ), wxString::Format( "%d", m_bus_code ) );
510
511 aList.emplace_back( wxT( "Subgraph Code" ), wxString::Format( "%d", m_subgraph_code ) );
512
513 if( SCH_ITEM* driver = Driver() )
514 {
515 UNITS_PROVIDER unitsProvider( schIUScale, EDA_UNITS::MM );
516
517 msg.Printf( wxS( "%s at %p" ),
518 driver->GetItemDescription( &unitsProvider, false ),
519 driver );
520 aList.emplace_back( wxT( "Connection Source" ), msg );
521 }
522#endif
523}
524
525
526bool SCH_CONNECTION::IsBusLabel( const wxString& aLabel )
527{
528 const wxString& unescaped = UnescapeString( aLabel );
529
530 return NET_SETTINGS::ParseBusVector( unescaped, nullptr, nullptr )
531 || NET_SETTINGS::ParseBusGroup( unescaped, nullptr, nullptr );
532}
533
534
535bool SCH_CONNECTION::MightBeBusLabel( const wxString& aLabel )
536{
537 // Weak heuristic for performance reasons. Stronger test will be used for connectivity
538 wxString label = UnescapeString( aLabel );
539
540 return label.Contains( wxT( "[" ) ) || label.Contains( wxT( "{" ) );
541}
542
543
544const std::vector< std::shared_ptr< SCH_CONNECTION > > SCH_CONNECTION::AllMembers() const
545{
546 std::vector< std::shared_ptr< SCH_CONNECTION > > ret( m_members );
547
548 for( const std::shared_ptr<SCH_CONNECTION>& member : m_members )
549 {
550 if( member->IsBus() )
551 ret.insert( ret.end(), member->Members().begin(), member->Members().end() );
552 }
553
554 return ret;
555}
556
557
558static bool isSuperSubOverbar( wxChar c )
559{
560 return c == '_' || c == '^' || c == '~';
561};
562
563
564wxString SCH_CONNECTION::PrintBusForUI( const wxString& aGroup )
565{
566 size_t groupLen = aGroup.length();
567 size_t i = 0;
568 wxString ret;
569
570 // Parse prefix
571 //
572 for( ; i < groupLen; ++i )
573 {
574 if( isSuperSubOverbar( aGroup[i] ) && i + 1 < groupLen && aGroup[i+1] == '{' )
575 {
576 ret += aGroup[i];
577 i++;
578 continue;
579 }
580 else if( aGroup[i] == '}' )
581 {
582 continue;
583 }
584
585 // Handle backslash-escaped spaces (display without the backslash)
586 if( aGroup[i] == '\\' && i + 1 < groupLen && aGroup[i + 1] == ' ' )
587 {
588 ret += ' ';
589 i++;
590 continue;
591 }
592
593 ret += aGroup[i];
594
595 if( aGroup[i] == '{' )
596 break;
597 }
598
599 // Parse members
600 //
601 i++; // '{' character
602
603 for( ; i < groupLen; ++i )
604 {
605 if( isSuperSubOverbar( aGroup[i] ) && i + 1 < groupLen && aGroup[i+1] == '{' )
606 {
607 ret += aGroup[i];
608 i++;
609 continue;
610 }
611 else if( aGroup[i] == '}' )
612 {
613 continue;
614 }
615
616 // Handle backslash-escaped spaces (display without the backslash)
617 if( aGroup[i] == '\\' && i + 1 < groupLen && aGroup[i + 1] == ' ' )
618 {
619 ret += ' ';
620 i++;
621 continue;
622 }
623
624 ret += aGroup[i];
625
626 if( aGroup[i] == '}' )
627 break;
628 }
629
630 return ret;
631}
632
633
635{
636 if( !aOther->IsBus() )
637 return false;
638
639 if( !IsBus() )
640 {
641 for( const std::shared_ptr<SCH_CONNECTION>& otherMember : aOther->Members() )
642 {
643 if( FullLocalName() == otherMember->FullLocalName() )
644 return true;
645 }
646
647 return false;
648 }
649
650 // If both connections are buses, check if all members of this bus are in the other bus
651 for( const std::shared_ptr<SCH_CONNECTION>& member : m_members )
652 {
653 bool found = false;
654
655 for( const std::shared_ptr<SCH_CONNECTION>& otherMember : aOther->Members() )
656 {
657 if( member->FullLocalName() == otherMember->FullLocalName() )
658 {
659 found = true;
660 break;
661 }
662 }
663
664 // If one of the members is not found in the other connection, this is not a subset
665 if( !found )
666 return false;
667 }
668
669 return true;
670}
671
672
674{
675 if( !aOther->IsBus() )
676 return false;
677
678 wxString me = Name( true );
679
680 for( const std::shared_ptr<SCH_CONNECTION>& m : aOther->Members() )
681 {
682 if( m->Name( true ) == me )
683 return true;
684 }
685
686 return false;
687}
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:127
Calculate the connectivity of a schematic and generates netlists.
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)
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.
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
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...
void SetSheet(const SCH_SHEET_PATH &aSheet)
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:168
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Schematic symbol object.
Definition sch_symbol.h:76
#define _(s)
static bool isSuperSubOverbar(wxChar c)
static bool isSuperSubOverbar(wxChar c)
CONNECTION_TYPE
@ BUS
This item represents a bus vector.
@ NET
This item represents a net.
@ NONE
No connection to this item.
@ BUS_GROUP
This item represents a bus group.
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
KIBIS_PIN * pin
@ SCH_LABEL_T
Definition typeinfo.h:168
@ SCH_SHEET_T
Definition typeinfo.h:176
@ SCH_HIER_LABEL_T
Definition typeinfo.h:170
@ SCH_SHEET_PIN_T
Definition typeinfo.h:175
@ SCH_GLOBAL_LABEL_T
Definition typeinfo.h:169
@ SCH_PIN_T
Definition typeinfo.h:154