KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_netchain.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) 2023 KiCad Developers
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#ifndef SCH_NETCHAIN_H
21#define SCH_NETCHAIN_H
22
23#include <cstdint>
24#include <set>
25#include <type_traits>
26#include <utility>
27#include <wx/string.h>
28#include <gal/color4d.h>
29#include <kiid.h>
30
36{
37public:
40 static constexpr wxStringCharType SYNTHETIC_NET_PREFIX[] = wxS( "__SG_" );
41
42 static wxString MakeKey( const wxString& aName, uint32_t aComponent );
43
45 static bool IsPersistableNet( const wxString& aNet )
46 {
47 return !aNet.IsEmpty() && !aNet.StartsWith( SYNTHETIC_NET_PREFIX );
48 }
49
51
52 friend void swap( SCH_NETCHAIN& aLeft, SCH_NETCHAIN& aRight ) noexcept
53 {
54 const auto swapMember =
55 []<typename T>( T& a, T& b ) noexcept
56 {
57 static_assert( std::is_nothrow_swappable_v<T> );
58 using std::swap;
59 swap( a, b );
60 };
61
62 swapMember( aLeft.m_name, aRight.m_name );
63 swapMember( aLeft.m_nets, aRight.m_nets );
64 swapMember( aLeft.m_symbols, aRight.m_symbols );
65 swapMember( aLeft.m_terminalPins, aRight.m_terminalPins );
66 swapMember( aLeft.m_terminalPaths, aRight.m_terminalPaths );
67 swapMember( aLeft.m_terminalRefDes, aRight.m_terminalRefDes );
68 swapMember( aLeft.m_terminalPinNum, aRight.m_terminalPinNum );
69 swapMember( aLeft.m_netClass, aRight.m_netClass );
70 swapMember( aLeft.m_color, aRight.m_color );
71 }
72
73 void SetName( const wxString& aName ) { m_name = aName; }
74 const wxString& GetName() const { return m_name; }
75
76 static bool IsValidName( const wxString& aName )
77 {
78 if( aName.IsEmpty() )
79 return false;
80
81 for( wxUniChar c : aName )
82 if( c == '"' || c == '\'' || c == '(' || c == ')' || c == ' ' )
83 return false;
84
85 return true;
86 }
87
88 void AddNet( const wxString& aNet )
89 {
90 m_nets.insert( aNet );
91 }
92
93 void RemoveNet( const wxString& aNet )
94 {
95 m_nets.erase( aNet );
96 }
97
98 void ReplaceNets( const std::set<wxString>& aNew )
99 {
100 m_nets = aNew;
101 }
102
103 // Track a symbol that participates in this chain (2-pin passthrough component).
104 void AddSymbol( class SCH_SYMBOL* aSymbol )
105 {
106 m_symbols.insert( aSymbol );
107 }
108
109 const std::set<class SCH_SYMBOL*>& GetSymbols() const { return m_symbols; }
110
111 void AbsorbSymbolsFrom( const SCH_NETCHAIN& aOther )
112 {
113 m_symbols.insert( aOther.m_symbols.begin(), aOther.m_symbols.end() );
114 }
115
116 // The symbol set holds non-owning raw pointers to schematic items. Callers must invoke
117 // this before any pass that may free those items.
119 {
120 m_symbols.clear();
121 }
122
123 const std::set<wxString>& GetNets() const { return m_nets; }
124
125 void SetTerminalPins( const KIID& aPinA, const KIID& aPinB )
126 {
127 m_terminalPins[0] = aPinA;
128 m_terminalPins[1] = aPinB;
129 }
130
131 const KIID& GetTerminalPinA() const { return m_terminalPins[0]; }
132 const KIID& GetTerminalPinB() const { return m_terminalPins[1]; }
133
134 const KIID_PATH& GetTerminalPath( int aIdx ) const { return m_terminalPaths[aIdx]; }
135
136 void SetTerminalPaths( const KIID_PATH& aPathA, const KIID_PATH& aPathB )
137 {
138 m_terminalPaths[0] = aPathA;
139 m_terminalPaths[1] = aPathB;
140 }
141
142 bool IsTerminal( const KIID& aPin, const KIID_PATH& aSheet ) const
143 {
144 return ( m_terminalPins[0] == aPin && m_terminalPaths[0] == aSheet )
145 || ( m_terminalPins[1] == aPin && m_terminalPaths[1] == aSheet );
146 }
147
153 void SetNetClass( const wxString& aNetClass ) { m_netClass = aNetClass; }
154 const wxString& GetNetClass() const { return m_netClass; }
155
162 void SetColor( const KIGFX::COLOR4D& aColor ) { m_color = aColor; }
163 const KIGFX::COLOR4D& GetColor() const { return m_color; }
164
165 void SetTerminalRefs( const wxString& aRefDesA, const wxString& aPinNumA, const wxString& aRefDesB,
166 const wxString& aPinNumB )
167 {
168 m_terminalRefDes[0] = aRefDesA;
169 m_terminalPinNum[0] = aPinNumA;
170 m_terminalRefDes[1] = aRefDesB;
171 m_terminalPinNum[1] = aPinNumB;
172 }
173
174 const wxString& GetTerminalRefDes( int aIdx ) const
175 {
176 wxASSERT( aIdx >= 0 && aIdx < 2 );
177 return m_terminalRefDes[aIdx];
178 }
179
180 const wxString& GetTerminalPinNum( int aIdx ) const
181 {
182 wxASSERT( aIdx >= 0 && aIdx < 2 );
183 return m_terminalPinNum[aIdx];
184 }
185
186private:
187 wxString m_name;
188 std::set<wxString> m_nets;
189 std::set<class SCH_SYMBOL*> m_symbols;
192 wxString m_terminalRefDes[2];
193 wxString m_terminalPinNum[2];
194 wxString m_netClass;
196};
197
198#endif
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition color4d.h:400
Definition kiid.h:46
const KIID & GetTerminalPinB() const
static wxString MakeKey(const wxString &aName, uint32_t aComponent)
const std::set< wxString > & GetNets() const
void AddSymbol(class SCH_SYMBOL *aSymbol)
std::set< wxString > m_nets
const wxString & GetNetClass() const
void SetTerminalRefs(const wxString &aRefDesA, const wxString &aPinNumA, const wxString &aRefDesB, const wxString &aPinNumB)
const KIGFX::COLOR4D & GetColor() const
const wxString & GetName() const
KIID_PATH m_terminalPaths[2]
static bool IsValidName(const wxString &aName)
KIID m_terminalPins[2]
wxString m_name
wxString m_terminalPinNum[2]
std::set< class SCH_SYMBOL * > m_symbols
KIGFX::COLOR4D m_color
const wxString & GetTerminalRefDes(int aIdx) const
void SetTerminalPins(const KIID &aPinA, const KIID &aPinB)
void ClearSymbols()
wxString m_terminalRefDes[2]
friend void swap(SCH_NETCHAIN &aLeft, SCH_NETCHAIN &aRight) noexcept
void RemoveNet(const wxString &aNet)
wxString m_netClass
void ReplaceNets(const std::set< wxString > &aNew)
const KIID_PATH & GetTerminalPath(int aIdx) const
const std::set< class SCH_SYMBOL * > & GetSymbols() const
void SetNetClass(const wxString &aNetClass)
Net chains may override the netclass applied to every member net.
static constexpr wxStringCharType SYNTHETIC_NET_PREFIX[]
Prefix used when synthesising net names for unnamed subgraphs.
void AbsorbSymbolsFrom(const SCH_NETCHAIN &aOther)
void SetTerminalPaths(const KIID_PATH &aPathA, const KIID_PATH &aPathB)
static bool IsPersistableNet(const wxString &aNet)
Synthetic keys do not survive a reload, so only named nets are written out.
const wxString & GetTerminalPinNum(int aIdx) const
void SetColor(const KIGFX::COLOR4D &aColor)
Optional display color for the chain.
const KIID & GetTerminalPinA() const
bool IsTerminal(const KIID &aPin, const KIID_PATH &aSheet) const
void AddNet(const wxString &aNet)
void SetName(const wxString &aName)
Schematic symbol object.
Definition sch_symbol.h:76