KiCad PCB EDA Suite
sch_pin.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) 2019-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Jon Evans <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include <base_units.h>
23#include <lib_pin.h>
24#include <sch_symbol.h>
25#include <sch_pin.h>
26#include <schematic.h>
27#include <schematic_settings.h>
28#include <sch_sheet_path.h>
29#include <sch_edit_frame.h>
30#include "string_utils.h"
31
32SCH_PIN::SCH_PIN( LIB_PIN* aLibPin, SCH_SYMBOL* aParentSymbol ) :
33 SCH_ITEM( aParentSymbol, SCH_PIN_T )
34{
36 m_alt = wxEmptyString;
37 m_number = aLibPin->GetNumber();
38 m_libPin = aLibPin;
39 SetPosition( aLibPin->GetPosition() );
40 m_isDangling = true;
41}
42
43
48SCH_PIN::SCH_PIN( SCH_SYMBOL* aParentSymbol, const wxString& aNumber, const wxString& aAlt ) :
49 SCH_ITEM( aParentSymbol, SCH_PIN_T )
50{
52 m_alt = aAlt;
53 m_number = aNumber;
54 m_libPin = nullptr;
55 m_isDangling = true;
56}
57
58
59SCH_PIN::SCH_PIN( const SCH_PIN& aPin ) :
60 SCH_ITEM( aPin )
61{
62 m_layer = aPin.m_layer;
63 m_alt = aPin.m_alt;
64 m_number = aPin.m_number;
65 m_libPin = aPin.m_libPin;
68}
69
70
72{
73 SCH_ITEM::operator=( aPin );
74
75 m_alt = aPin.m_alt;
76 m_number = aPin.m_number;
77 m_libPin = aPin.m_libPin;
80
81 return *this;
82}
83
84
85wxString SCH_PIN::GetName() const
86{
87 if( !m_alt.IsEmpty() )
88 return m_alt;
89
90 return m_libPin->GetName();
91}
92
93
94wxString SCH_PIN::GetShownName() const
95{
96 wxString name = m_libPin->GetName();
97
98 if( !m_alt.IsEmpty() )
99 name = m_alt;
100
101 if( name == wxS( "~" ) )
102 return wxEmptyString;
103 else
104 return name;
105}
106
107
109{
110 if( m_number == wxS( "~" ) )
111 return wxEmptyString;
112 else
113 return m_number;
114}
115
116
118{
119 if( !m_alt.IsEmpty() )
120 return m_libPin->GetAlt( m_alt ).m_Type;
121
122 return m_libPin->GetType();
123}
124
125
127{
128 if( !m_alt.IsEmpty() )
129 return m_libPin->GetAlt( m_alt ).m_Shape;
130
131 return m_libPin->GetShape();
132}
133
134
136{
137 return m_libPin->GetOrientation();
138}
139
140
142{
143 return m_libPin->GetLength();
144}
145
146
148{
149 return GetBoundingBox( false, true, true );
150}
151
152
153void SCH_PIN::ViewGetLayers( int aLayers[], int& aCount ) const
154{
155 aCount = 4;
156 aLayers[0] = LAYER_DANGLING;
157 aLayers[1] = LAYER_DEVICE;
158 aLayers[2] = LAYER_SELECTION_SHADOWS;
159 aLayers[3] = LAYER_OP_CURRENTS;
160}
161
162
163bool SCH_PIN::Matches( const EDA_SEARCH_DATA& aSearchData, void* aAuxDat ) const
164{
165 const SCH_SEARCH_DATA& schSearchData =
166 dynamic_cast<const SCH_SEARCH_DATA&>( aSearchData );
167
168 if( !schSearchData.searchAllPins )
169 return false;
170
171 return EDA_ITEM::Matches( GetName(), aSearchData )
172 || EDA_ITEM::Matches( GetNumber(), aSearchData );
173}
174
175
176bool SCH_PIN::Replace( const EDA_SEARCH_DATA& aSearchData, void* aAuxData )
177{
178 bool isReplaced = false;
179
180 /* TODO: waiting on a way to override pins in the schematic...
181 isReplaced |= EDA_ITEM::Replace( aSearchData, m_name );
182 isReplaced |= EDA_ITEM::Replace( aSearchData, m_number );
183 */
184
185 return isReplaced;
186}
187
188
190{
191 return static_cast<SCH_SYMBOL*>( GetParent() );
192}
193
194
195wxString SCH_PIN::GetItemDescription( UNITS_PROVIDER* aUnitsProvider ) const
196{
197 return wxString::Format( "Symbol %s %s",
198 GetParentSymbol()->GetField( REFERENCE_FIELD )->GetShownText(),
199 m_libPin->GetItemDescription( aUnitsProvider ) );
200}
201
202
203void SCH_PIN::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
204{
205 wxString msg;
206
207 aList.emplace_back( _( "Type" ), _( "Pin" ) );
208
209 if( m_libPin->GetConvert() == LIB_ITEM::LIB_CONVERT::BASE )
210 msg = _( "no" );
211 else if( m_libPin->GetConvert() == LIB_ITEM::LIB_CONVERT::DEMORGAN )
212 msg = _( "yes" );
213 else
214 msg = wxT( "?" );
215
216 aList.emplace_back( _( "Converted" ), msg );
217
218 aList.emplace_back( _( "Name" ), GetShownName() );
219 aList.emplace_back( _( "Number" ), GetShownNumber() );
220 aList.emplace_back( _( "Type" ), ElectricalPinTypeGetText( GetType() ) );
221 aList.emplace_back( _( "Style" ), PinShapeGetText( GetShape() ) );
222
223 aList.emplace_back( _( "Visible" ), IsVisible() ? _( "Yes" ) : _( "No" ) );
224
225 aList.emplace_back( _( "Length" ), aFrame->MessageTextFromValue( GetLength() ), true );
226
228 aList.emplace_back( _( "Orientation" ), PinOrientationName( (unsigned) i ) );
229
230 SCH_EDIT_FRAME* schframe = dynamic_cast<SCH_EDIT_FRAME*>( aFrame );
231 SCH_SHEET_PATH* currentSheet = schframe ? &schframe->GetCurrentSheet() : nullptr;
232 SCH_SYMBOL* symbol = GetParentSymbol();
233
234 aList.emplace_back( symbol->GetRef( currentSheet ), symbol->GetValueFieldText( true ) );
235
236#if defined(DEBUG)
237 if( !IsConnectivityDirty() && dynamic_cast<SCH_EDIT_FRAME*>( aFrame ) )
238 {
239 SCH_CONNECTION* conn = Connection();
240
241 if( conn )
242 conn->AppendInfoToMsgPanel( aList );
243 }
244#endif
245
246}
247
248
249bool SCH_PIN::IsStacked( const SCH_PIN* aPin ) const
250{
251 return m_parent == aPin->GetParent()
253 && ( ( GetType() == aPin->GetType() )
255 || ( aPin->GetType() == ELECTRICAL_PINTYPE::PT_PASSIVE ) );
256}
257
258
260{
261 std::lock_guard<std::recursive_mutex> lock( m_netmap_mutex );
262
263 if( aPath )
264 m_net_name_map.erase( *aPath );
265 else
266 m_net_name_map.clear();
267}
268
269
270wxString SCH_PIN::GetDefaultNetName( const SCH_SHEET_PATH& aPath, bool aForceNoConnect )
271{
272 // Need to check for parent as power symbol to make sure we aren't dealing
273 // with legacy global power pins on non-power symbols
274 if( IsGlobalPower() )
275 {
276 if( GetLibPin()->GetParent()->IsPower() )
277 return EscapeString( GetParentSymbol()->GetValueFieldText( true ), CTX_NETNAME );
278 else
280 }
281
282 std::lock_guard<std::recursive_mutex> lock( m_netmap_mutex );
283
284 auto it = m_net_name_map.find( aPath );
285
286 if( it != m_net_name_map.end() )
287 {
288 if( it->second.second == aForceNoConnect )
289 return it->second.first;
290 }
291
292 wxString name = "Net-(";
293 bool unconnected = false;
294
295 if( aForceNoConnect || GetType() == ELECTRICAL_PINTYPE::PT_NC )
296 {
297 unconnected = true;
298 name = ( "unconnected-(" );
299 }
300
301 bool annotated = true;
302
303 std::vector<SCH_PIN*> pins = GetParentSymbol()->GetPins( &aPath );
304 bool has_multiple = false;
305
306 for( SCH_PIN* pin : pins )
307 {
308 if( pin->GetShownName() == GetShownName()
309 && pin->GetShownNumber() != GetShownNumber()
310 && unconnected == ( pin->GetType() == ELECTRICAL_PINTYPE::PT_NC ) )
311 {
312 has_multiple = true;
313 break;
314 }
315 }
316
317 // Use timestamp for unannotated symbols
318 if( GetParentSymbol()->GetRef( &aPath, false ).Last() == '?' )
319 {
321 name << "-Pad" << m_libPin->GetNumber() << ")";
322 annotated = false;
323 }
324 else if( !m_libPin->GetShownName().IsEmpty()
326 {
327 // Pin names might not be unique between different units so we must have the
328 // unit token in the reference designator
329 name << GetParentSymbol()->GetRef( &aPath, true );
331
332 if( unconnected || has_multiple )
334
335 name << ")";
336 }
337 else
338 {
339 // Pin numbers are unique, so we skip the unit token
340 name << GetParentSymbol()->GetRef( &aPath, false );
341 name << "-Pad" << EscapeString( m_libPin->GetShownNumber(), CTX_NETNAME ) << ")";
342 }
343
344 if( annotated )
345 m_net_name_map[ aPath ] = std::make_pair( name, aForceNoConnect );
346
347 return name;
348}
349
350
352{
355}
356
357
358const BOX2I SCH_PIN::GetBoundingBox( bool aIncludeInvisiblePins, bool aIncludeNameAndNumber,
359 bool aIncludeElectricalType ) const
360{
362 BOX2I r = m_libPin->GetBoundingBox( aIncludeInvisiblePins, aIncludeNameAndNumber,
363 aIncludeElectricalType );
364
365 r.RevertYAxis();
366
367 r = t.TransformCoordinate( r );
369
370 return r;
371}
372
373
374bool SCH_PIN::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
375{
376 // When looking for an "exact" hit aAccuracy will be 0 which works poorly if the pin has
377 // no pin number or name. Give it a floor.
378 if( Schematic() )
379 aAccuracy = std::max( aAccuracy, Schematic()->Settings().m_PinSymbolSize / 4 );
380
381 BOX2I rect = GetBoundingBox( false, true, m_flags & SHOW_ELEC_TYPE );
382 return rect.Inflate( aAccuracy ).Contains( aPosition );
383}
384
385
386bool SCH_PIN::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
387{
388 BOX2I sel = aRect;
389
390 if( aAccuracy )
391 sel.Inflate( aAccuracy );
392
393 if( aContained )
394 return sel.Contains( GetBoundingBox( false, false, false ) );
395
396 return sel.Intersects( GetBoundingBox( false, true, m_flags & SHOW_ELEC_TYPE ) );
397}
398
399
401{
402 return new SCH_PIN( *this );
403}
404
405
407{
408 // Reciprocal checking is done in CONNECTION_GRAPH anyway
410}
const char * name
Definition: DXF_plotter.cpp:56
void Offset(coord_type dx, coord_type dy)
Definition: box2.h:224
void RevertYAxis()
Mirror the rectangle from the X axis (negate Y pos and size).
Definition: box2.h:689
bool Intersects(const BOX2< Vec > &aRect) const
Definition: box2.h:269
bool Contains(const Vec &aPoint) const
Definition: box2.h:141
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:506
The base class for create windows for drawing purpose.
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
const KIID m_Uuid
Definition: eda_item.h:492
EDA_ITEM_FLAGS m_flags
Definition: eda_item.h:498
virtual bool Matches(const EDA_SEARCH_DATA &aSearchData, void *aAuxData) const
Compare the item against the search criteria in aSearchData.
Definition: eda_item.h:382
EDA_ITEM * GetParent() const
Definition: eda_item.h:99
EDA_ITEM * m_parent
Linked list: Link (parent struct)
Definition: eda_item.h:496
wxString AsString() const
Definition: kiid.cpp:257
int GetConvert() const
Definition: lib_item.h:276
int GetLength() const
Definition: lib_pin.h:80
ELECTRICAL_PINTYPE GetType() const
Definition: lib_pin.h:90
int GetOrientation() const
Definition: lib_pin.h:74
const BOX2I GetBoundingBox() const override
Definition: lib_pin.h:189
wxString GetShownNumber() const
Definition: lib_pin.h:123
wxString GetItemDescription(UNITS_PROVIDER *aUnitsProvider) const override
Return a user-visible description string of this item.
Definition: lib_pin.cpp:1367
wxString GetShownName() const
Definition: lib_pin.cpp:175
VECTOR2I GetPosition() const override
Definition: lib_pin.h:228
const wxString & GetNumber() const
Definition: lib_pin.h:122
GRAPHIC_PINSHAPE GetShape() const
Definition: lib_pin.h:77
ALT GetAlt(const wxString &aAlt)
Definition: lib_pin.h:140
const wxString & GetName() const
Definition: lib_pin.h:112
Each graphical item can have a SCH_CONNECTION describing its logical connection (to a bus or net).
void AppendInfoToMsgPanel(std::vector< MSG_PANEL_ITEM > &aList) const
Adds information about the connection object to aList.
Schematic editor (Eeschema) main window.
SCH_SHEET_PATH & GetCurrentSheet() const
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:147
SCH_ITEM & operator=(const SCH_ITEM &aPin)
Definition: sch_item.cpp:70
SCHEMATIC * Schematic() const
Searches the item hierarchy to find a SCHEMATIC.
Definition: sch_item.cpp:112
bool IsConnectivityDirty() const
Definition: sch_item.h:413
SCH_CONNECTION * Connection(const SCH_SHEET_PATH *aSheet=nullptr) const
Retrieve the connection associated with this object in the given sheet.
Definition: sch_item.cpp:146
SCH_LAYER_ID m_layer
Definition: sch_item.h:491
wxString GetItemDescription(UNITS_PROVIDER *aUnitsProvider) const override
Return a user-visible description string of this item.
Definition: sch_pin.cpp:195
int GetLength() const
Definition: sch_pin.cpp:141
bool Matches(const EDA_SEARCH_DATA &aSearchData, void *aAuxData) const override
Compare the item against the search criteria in aSearchData.
Definition: sch_pin.cpp:163
void GetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, std::vector< MSG_PANEL_ITEM > &aList) override
Populate aList of MSG_PANEL_ITEM objects with it's internal state for display purposes.
Definition: sch_pin.cpp:203
const BOX2I ViewBBox() const override
Return the bounding box of the item covering all its layers.
Definition: sch_pin.cpp:147
void SetPosition(const VECTOR2I &aPosition) override
Definition: sch_pin.h:84
SCH_PIN & operator=(const SCH_PIN &aPin)
Definition: sch_pin.cpp:71
wxString GetShownNumber() const
Definition: sch_pin.cpp:108
std::map< const SCH_SHEET_PATH, std::pair< wxString, bool > > m_net_name_map
Definition: sch_pin.h:179
bool IsGlobalPower() const
Definition: sch_pin.h:158
bool IsVisible() const
Definition: sch_pin.h:135
bool ConnectionPropagatesTo(const EDA_ITEM *aItem) const override
Return true if this item should propagate connection info to aItem.
Definition: sch_pin.cpp:406
wxString GetName() const
Definition: sch_pin.cpp:85
LIB_PIN * GetLibPin() const
Definition: sch_pin.h:59
bool m_isDangling
Definition: sch_pin.h:175
void ViewGetLayers(int aLayers[], int &aCount) const override
Return the layers the item is drawn on (which may be more than its "home" layer)
Definition: sch_pin.cpp:153
wxString GetNumber() const
Definition: sch_pin.h:140
std::recursive_mutex m_netmap_mutex
The name that this pin connection will drive onto a net.
Definition: sch_pin.h:178
VECTOR2I GetPosition() const override
Definition: sch_pin.h:82
LIB_PIN * m_libPin
Definition: sch_pin.h:170
SCH_PIN(LIB_PIN *aLibPin, SCH_SYMBOL *aParentSymbol)
Definition: sch_pin.cpp:32
bool Replace(const EDA_SEARCH_DATA &aSearchData, void *aAuxData) override
Perform a text replace using the find and replace criteria in aSearchData on items that support text ...
Definition: sch_pin.cpp:176
VECTOR2I m_position
Definition: sch_pin.h:174
const VECTOR2I GetLocalPosition() const
Definition: sch_pin.h:83
wxString GetShownName() const
Definition: sch_pin.cpp:94
bool HitTest(const VECTOR2I &aPosition, int aAccuracy=0) const override
Test if aPosition is inside or on the boundary of this item.
Definition: sch_pin.cpp:374
wxString m_alt
Definition: sch_pin.h:173
VECTOR2I GetTransformedPosition() const
Definition: sch_pin.cpp:351
void ClearDefaultNetName(const SCH_SHEET_PATH *aPath)
Definition: sch_pin.cpp:259
bool IsStacked(const SCH_PIN *aPin) const
Definition: sch_pin.cpp:249
wxString m_number
Definition: sch_pin.h:172
SCH_SYMBOL * GetParentSymbol() const
Definition: sch_pin.cpp:189
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: sch_pin.cpp:400
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition: sch_pin.h:87
int GetOrientation() const
Definition: sch_pin.cpp:135
wxString GetDefaultNetName(const SCH_SHEET_PATH &aPath, bool aForceNoConnect=false)
Definition: sch_pin.cpp:270
GRAPHIC_PINSHAPE GetShape() const
Definition: sch_pin.cpp:126
ELECTRICAL_PINTYPE GetType() const
Definition: sch_pin.cpp:117
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:81
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const
Return the reference for the given sheet path.
Definition: sch_symbol.cpp:674
std::vector< SCH_PIN * > GetPins(const SCH_SHEET_PATH *aSheet=nullptr) const
Retrieve a list of the SCH_PINs for the given sheet path.
VECTOR2I GetPosition() const override
Definition: sch_symbol.h:712
const wxString GetValueFieldText(bool aResolve) const
Definition: sch_symbol.cpp:835
TRANSFORM & GetTransform()
Definition: sch_symbol.h:283
for transforming drawing coordinates for a wxDC device context.
Definition: transform.h:47
VECTOR2I TransformCoordinate(const VECTOR2I &aPoint) const
Calculate a new coordinate according to the mirror/rotation transform.
Definition: transform.cpp:46
wxString MessageTextFromValue(double aValue, bool aAddUnitLabel=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
A lower-precision version of StringFromValue().
#define _(s)
#define SHOW_ELEC_TYPE
Show pin electrical type. Shared with IS_ROLLOVER.
@ LAYER_DANGLING
Definition: layer_ids.h:368
@ LAYER_DEVICE
Definition: layer_ids.h:357
@ LAYER_PIN
Definition: layer_ids.h:361
@ LAYER_OP_CURRENTS
Definition: layer_ids.h:388
@ LAYER_SELECTION_SHADOWS
Definition: layer_ids.h:381
wxString PinOrientationName(unsigned aPinOrientationCode)
Definition: pin_type.cpp:119
int PinOrientationIndex(int code)
Definition: pin_type.cpp:150
wxString ElectricalPinTypeGetText(ELECTRICAL_PINTYPE aType)
Definition: pin_type.cpp:240
wxString PinShapeGetText(GRAPHIC_PINSHAPE aShape)
Definition: pin_type.cpp:278
ELECTRICAL_PINTYPE
The symbol library pin object electrical types used in ERC tests.
Definition: pin_type.h:36
@ PT_NC
not connected (must be left open)
@ PT_PASSIVE
pin for passive symbols: must be connected, and can be connected to any pin
GRAPHIC_PINSHAPE
Definition: pin_type.h:56
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
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:54
GRAPHIC_PINSHAPE m_Shape
Definition: lib_pin.h:58
ELECTRICAL_PINTYPE m_Type
Definition: lib_pin.h:59
@ REFERENCE_FIELD
Field Reference of part, i.e. "IC21".
@ SCH_PIN_T
Definition: typeinfo.h:159