KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 UnescapeString( GetParentSymbol()->GetField( REFERENCE_FIELD )->GetText() ),
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
210 msg = _( "no" );
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
227 aList.emplace_back( _( "Orientation" ), PinOrientationName( GetOrientation() ) );
228
229 SCH_EDIT_FRAME* schframe = dynamic_cast<SCH_EDIT_FRAME*>( aFrame );
230 SCH_SHEET_PATH* currentSheet = schframe ? &schframe->GetCurrentSheet() : nullptr;
231 SCH_SYMBOL* symbol = GetParentSymbol();
232
233 // Don't use GetShownText(); we want to see the variable references here
234 aList.emplace_back( symbol->GetRef( currentSheet ),
235 UnescapeString( symbol->GetField( VALUE_FIELD )->GetText() ) );
236
237#if defined(DEBUG)
238 if( !IsConnectivityDirty() && dynamic_cast<SCH_EDIT_FRAME*>( aFrame ) )
239 {
240 SCH_CONNECTION* conn = Connection();
241
242 if( conn )
243 conn->AppendInfoToMsgPanel( aList );
244 }
245#endif
246
247}
248
249
250bool SCH_PIN::IsStacked( const SCH_PIN* aPin ) const
251{
252 return m_parent == aPin->GetParent()
254 && GetName() == aPin->GetName()
255 && ( ( GetType() == aPin->GetType() ) || ( GetType() == ELECTRICAL_PINTYPE::PT_PASSIVE )
256 || ( aPin->GetType() == ELECTRICAL_PINTYPE::PT_PASSIVE ) );
257}
258
259
261{
262 std::lock_guard<std::recursive_mutex> lock( m_netmap_mutex );
263
264 if( aPath )
265 m_net_name_map.erase( *aPath );
266 else
267 m_net_name_map.clear();
268}
269
270
271wxString SCH_PIN::GetDefaultNetName( const SCH_SHEET_PATH& aPath, bool aForceNoConnect )
272{
273 // Need to check for parent as power symbol to make sure we aren't dealing
274 // with legacy global power pins on non-power symbols
275 if( IsGlobalPower() )
276 {
277 if( GetLibPin()->GetParent()->IsPower() )
278 {
279 return EscapeString( GetParentSymbol()->GetValueFieldText( true, &aPath, false ),
280 CTX_NETNAME );
281 }
282 else
283 {
285 }
286 }
287
288 std::lock_guard<std::recursive_mutex> lock( m_netmap_mutex );
289
290 auto it = m_net_name_map.find( aPath );
291
292 if( it != m_net_name_map.end() )
293 {
294 if( it->second.second == aForceNoConnect )
295 return it->second.first;
296 }
297
298 wxString name = "Net-(";
299 bool unconnected = false;
300
301 if( aForceNoConnect || GetType() == ELECTRICAL_PINTYPE::PT_NC )
302 {
303 unconnected = true;
304 name = ( "unconnected-(" );
305 }
306
307 bool annotated = true;
308
309 std::vector<SCH_PIN*> pins = GetParentSymbol()->GetPins( &aPath );
310 bool has_multiple = false;
311
312 for( SCH_PIN* pin : pins )
313 {
314 if( pin->GetShownName() == GetShownName()
315 && pin->GetShownNumber() != GetShownNumber()
316 && unconnected == ( pin->GetType() == ELECTRICAL_PINTYPE::PT_NC ) )
317 {
318 has_multiple = true;
319 break;
320 }
321 }
322
323 // Use timestamp for unannotated symbols
324 if( GetParentSymbol()->GetRef( &aPath, false ).Last() == '?' )
325 {
327 name << "-Pad" << m_libPin->GetNumber() << ")";
328 annotated = false;
329 }
330 else if( !m_libPin->GetShownName().IsEmpty()
332 {
333 // Pin names might not be unique between different units so we must have the
334 // unit token in the reference designator
335 name << GetParentSymbol()->GetRef( &aPath, true );
337
338 if( unconnected || has_multiple )
340
341 name << ")";
342 }
343 else
344 {
345 // Pin numbers are unique, so we skip the unit token
346 name << GetParentSymbol()->GetRef( &aPath, false );
347 name << "-Pad" << EscapeString( m_libPin->GetShownNumber(), CTX_NETNAME ) << ")";
348 }
349
350 if( annotated )
351 m_net_name_map[ aPath ] = std::make_pair( name, aForceNoConnect );
352
353 return name;
354}
355
356
358{
361}
362
363
364const BOX2I SCH_PIN::GetBoundingBox( bool aIncludeInvisiblePins, bool aIncludeNameAndNumber,
365 bool aIncludeElectricalType ) const
366{
368 BOX2I r = m_libPin->GetBoundingBox( aIncludeInvisiblePins, aIncludeNameAndNumber,
369 aIncludeElectricalType );
370
371 r.RevertYAxis();
372
373 r = t.TransformCoordinate( r );
375
376 return r;
377}
378
379
380bool SCH_PIN::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
381{
382 // When looking for an "exact" hit aAccuracy will be 0 which works poorly if the pin has
383 // no pin number or name. Give it a floor.
384 if( Schematic() )
385 aAccuracy = std::max( aAccuracy, Schematic()->Settings().m_PinSymbolSize / 4 );
386
387 BOX2I rect = GetBoundingBox( false, true, m_flags & SHOW_ELEC_TYPE );
388 return rect.Inflate( aAccuracy ).Contains( aPosition );
389}
390
391
392bool SCH_PIN::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
393{
394 BOX2I sel = aRect;
395
396 if( aAccuracy )
397 sel.Inflate( aAccuracy );
398
399 if( aContained )
400 return sel.Contains( GetBoundingBox( false, false, false ) );
401
402 return sel.Intersects( GetBoundingBox( false, true, m_flags & SHOW_ELEC_TYPE ) );
403}
404
405
407{
408 return new SCH_PIN( *this );
409}
410
411
413{
414 // Reciprocal checking is done in CONNECTION_GRAPH anyway
415 return !( m_libPin->GetType() == ELECTRICAL_PINTYPE::PT_NC );
416}
417
418
419static struct SCH_PIN_DESC
420{
422 {
426
427 propMgr.AddProperty( new PROPERTY<SCH_PIN, wxString>( _HKI( "Pin Name" ),
428 NO_SETTER( SCH_PIN, wxString ), &SCH_PIN::GetName ) );
429
430 propMgr.AddProperty( new PROPERTY<SCH_PIN, wxString>( _HKI( "Pin Number" ),
431 NO_SETTER( SCH_PIN, wxString ), &SCH_PIN::GetNumber ) );
432
433 propMgr.AddProperty( new PROPERTY<SCH_PIN, int>( _HKI( "Length" ),
434 NO_SETTER( SCH_PIN, int ), &SCH_PIN::GetLength, PROPERTY_DISPLAY::PT_SIZE ) );
435 }
const char * name
Definition: DXF_plotter.cpp:57
void Offset(coord_type dx, coord_type dy)
Definition: box2.h:225
void RevertYAxis()
Mirror the rectangle from the X axis (negate Y pos and size).
Definition: box2.h:690
bool Intersects(const BOX2< Vec > &aRect) const
Definition: box2.h:270
bool Contains(const Vec &aPoint) const
Definition: box2.h:142
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:507
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:482
EDA_ITEM_FLAGS m_flags
Definition: eda_item.h:487
virtual bool Matches(const EDA_SEARCH_DATA &aSearchData, void *aAuxData) const
Compare the item against the search criteria in aSearchData.
Definition: eda_item.h:372
EDA_ITEM * GetParent() const
Definition: eda_item.h:99
EDA_ITEM * m_parent
Linked list: Link (parent struct)
Definition: eda_item.h:485
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:95
wxString AsString() const
Definition: kiid.cpp:257
@ BASE
Definition: lib_item.h:77
@ DEMORGAN
Definition: lib_item.h:77
int GetConvert() const
Definition: lib_item.h:310
int GetLength() const
Definition: lib_pin.h:74
ELECTRICAL_PINTYPE GetType() const
Definition: lib_pin.h:84
const BOX2I GetBoundingBox() const override
Definition: lib_pin.h:193
PIN_ORIENTATION GetOrientation() const
Definition: lib_pin.h:68
wxString GetShownNumber() const
Definition: lib_pin.h:118
wxString GetItemDescription(UNITS_PROVIDER *aUnitsProvider) const override
Return a user-visible description string of this item.
Definition: lib_pin.cpp:1408
wxString GetShownName() const
Definition: lib_pin.cpp:176
VECTOR2I GetPosition() const override
Definition: lib_pin.h:232
const wxString & GetNumber() const
Definition: lib_pin.h:117
GRAPHIC_PINSHAPE GetShape() const
Definition: lib_pin.h:71
ALT GetAlt(const wxString &aAlt)
Definition: lib_pin.h:144
const wxString & GetName() const
Definition: lib_pin.h:106
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:74
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:76
PROPERTY_BASE & AddProperty(PROPERTY_BASE *aProperty, const wxString &aGroup=wxEmptyString)
Register a property.
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:150
SCH_ITEM & operator=(const SCH_ITEM &aPin)
Definition: sch_item.cpp:71
SCHEMATIC * Schematic() const
Searches the item hierarchy to find a SCHEMATIC.
Definition: sch_item.cpp:113
bool IsConnectivityDirty() const
Definition: sch_item.h:426
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:147
SCH_LAYER_ID m_layer
Definition: sch_item.h:510
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:412
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
PIN_ORIENTATION GetOrientation() const
Definition: sch_pin.cpp:135
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:380
wxString m_alt
Definition: sch_pin.h:173
VECTOR2I GetTransformedPosition() const
Definition: sch_pin.cpp:357
void ClearDefaultNetName(const SCH_SHEET_PATH *aPath)
Definition: sch_pin.cpp:260
bool IsStacked(const SCH_PIN *aPin) const
Definition: sch_pin.cpp:250
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:406
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition: sch_pin.h:87
wxString GetDefaultNetName(const SCH_SHEET_PATH &aPath, bool aForceNoConnect=false)
Definition: sch_pin.cpp:271
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:665
SCH_FIELD * GetField(MANDATORY_FIELD_T aFieldType)
Return a mandatory field in this symbol.
Definition: sch_symbol.cpp:858
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:703
TRANSFORM & GetTransform()
Definition: sch_symbol.h:281
for transforming drawing coordinates for a wxDC device context.
Definition: transform.h:46
VECTOR2I TransformCoordinate(const VECTOR2I &aPoint) const
Calculate a new coordinate according to the mirror/rotation transform.
Definition: transform.cpp:44
wxString MessageTextFromValue(double aValue, bool aAddUnitLabel=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
A lower-precision version of StringFromValue().
#define _HKI(x)
#define _(s)
#define SHOW_ELEC_TYPE
Show pin electrical type. Shared with IS_ROLLOVER.
@ LAYER_DANGLING
Definition: layer_ids.h:373
@ LAYER_DEVICE
Definition: layer_ids.h:362
@ LAYER_PIN
Definition: layer_ids.h:366
@ LAYER_OP_CURRENTS
Definition: layer_ids.h:394
@ LAYER_SELECTION_SHADOWS
Definition: layer_ids.h:387
wxString ElectricalPinTypeGetText(ELECTRICAL_PINTYPE aType)
Definition: pin_type.cpp:207
wxString PinShapeGetText(GRAPHIC_PINSHAPE aShape)
Definition: pin_type.cpp:245
wxString PinOrientationName(PIN_ORIENTATION aOrientation)
Definition: pin_type.cpp:265
ELECTRICAL_PINTYPE
The symbol library pin object electrical types used in ERC tests.
Definition: pin_type.h:36
PIN_ORIENTATION
The symbol library pin object orientations.
Definition: pin_type.h:75
GRAPHIC_PINSHAPE
Definition: pin_type.h:56
#define TYPE_HASH(x)
Definition: property.h:64
#define NO_SETTER(owner, type)
Definition: property.h:744
#define REGISTER_TYPE(x)
Definition: property_mgr.h:356
static struct SCH_PIN_DESC _SCH_PIN_DESC
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
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
GRAPHIC_PINSHAPE m_Shape
Definition: lib_pin.h:47
ELECTRICAL_PINTYPE m_Type
Definition: lib_pin.h:48
@ VALUE_FIELD
Field Value of part, i.e. "3.3K".
@ REFERENCE_FIELD
Field Reference of part, i.e. "IC21".
@ SCH_PIN_T
Definition: typeinfo.h:158