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-2022 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 = 3;
156 aLayers[0] = LAYER_DANGLING;
157 aLayers[1] = LAYER_DEVICE;
158 aLayers[2] = LAYER_SELECTION_SHADOWS;
159}
160
161
162bool SCH_PIN::Matches( const EDA_SEARCH_DATA& aSearchData, void* aAuxDat ) const
163{
164 const SCH_SEARCH_DATA& schSearchData =
165 dynamic_cast<const SCH_SEARCH_DATA&>( aSearchData );
166
167 if( !schSearchData.searchAllPins )
168 return false;
169
170 return EDA_ITEM::Matches( GetName(), aSearchData )
171 || EDA_ITEM::Matches( GetNumber(), aSearchData );
172}
173
174
175bool SCH_PIN::Replace( const EDA_SEARCH_DATA& aSearchData, void* aAuxData )
176{
177 bool isReplaced = false;
178
179 /* TODO: waiting on a way to override pins in the schematic...
180 isReplaced |= EDA_ITEM::Replace( aSearchData, m_name );
181 isReplaced |= EDA_ITEM::Replace( aSearchData, m_number );
182 */
183
184 return isReplaced;
185}
186
187
189{
190 return static_cast<SCH_SYMBOL*>( GetParent() );
191}
192
193
194wxString SCH_PIN::GetSelectMenuText( UNITS_PROVIDER* aUnitsProvider ) const
195{
196 return wxString::Format( "Symbol %s %s",
197 GetParentSymbol()->GetField( REFERENCE_FIELD )->GetShownText(),
198 m_libPin->GetSelectMenuText( aUnitsProvider ) );
199}
200
201
202void SCH_PIN::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
203{
204 wxString msg;
205
206 aList.emplace_back( _( "Type" ), _( "Pin" ) );
207
208 if( m_libPin->GetConvert() == LIB_ITEM::LIB_CONVERT::BASE )
209 msg = _( "no" );
210 else if( m_libPin->GetConvert() == LIB_ITEM::LIB_CONVERT::DEMORGAN )
211 msg = _( "yes" );
212 else
213 msg = wxT( "?" );
214
215 aList.emplace_back( _( "Converted" ), msg );
216
217 aList.emplace_back( _( "Name" ), GetShownName() );
218 aList.emplace_back( _( "Number" ), GetShownNumber() );
219 aList.emplace_back( _( "Type" ), ElectricalPinTypeGetText( GetType() ) );
220 aList.emplace_back( _( "Style" ), PinShapeGetText( GetShape() ) );
221
222 aList.emplace_back( _( "Visible" ), IsVisible() ? _( "Yes" ) : _( "No" ) );
223
224 aList.emplace_back( _( "Length" ), aFrame->MessageTextFromValue( GetLength() ), true );
225
227 aList.emplace_back( _( "Orientation" ), PinOrientationName( (unsigned) i ) );
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 aList.emplace_back( symbol->GetRef( currentSheet ), symbol->GetValueFieldText( true ) );
234
235#if defined(DEBUG)
236 if( !IsConnectivityDirty() && dynamic_cast<SCH_EDIT_FRAME*>( aFrame ) )
237 {
238 SCH_CONNECTION* conn = Connection();
239
240 if( conn )
241 conn->AppendInfoToMsgPanel( aList );
242 }
243#endif
244
245}
246
247
248bool SCH_PIN::IsStacked( const SCH_PIN* aPin ) const
249{
250 return m_parent == aPin->GetParent()
252 && GetType() == aPin->GetType();
253}
254
255
257{
258 std::lock_guard<std::recursive_mutex> lock( m_netmap_mutex );
259
260 if( aPath )
261 m_net_name_map.erase( *aPath );
262 else
263 m_net_name_map.clear();
264}
265
266
267wxString SCH_PIN::GetDefaultNetName( const SCH_SHEET_PATH& aPath, bool aForceNoConnect )
268{
271
272 std::lock_guard<std::recursive_mutex> lock( m_netmap_mutex );
273
274 auto it = m_net_name_map.find( aPath );
275
276 if( it != m_net_name_map.end() )
277 {
278 if( it->second.second == aForceNoConnect )
279 return it->second.first;
280 }
281
282 wxString name = "Net-(";
283
284 if( aForceNoConnect || GetType() == ELECTRICAL_PINTYPE::PT_NC )
285 name = ( "unconnected-(" );
286
287 bool annotated = true;
288
289 // Use timestamp for unannotated symbols
290 if( GetParentSymbol()->GetRef( &aPath, false ).Last() == '?' )
291 {
293 name << "-Pad" << m_libPin->GetNumber() << ")";
294 annotated = false;
295 }
296 else if( !m_libPin->GetShownName().IsEmpty()
298 {
299 // Pin names might not be unique between different units so we must have the
300 // unit token in the reference designator
301 name << GetParentSymbol()->GetRef( &aPath, true );
302 name << "-" << EscapeString( m_libPin->GetShownName(), CTX_NETNAME ) << ")";
303 }
304 else
305 {
306 // Pin numbers are unique, so we skip the unit token
307 name << GetParentSymbol()->GetRef( &aPath, false );
308 name << "-Pad" << EscapeString( m_libPin->GetShownNumber(), CTX_NETNAME ) << ")";
309 }
310
311 if( annotated )
312 m_net_name_map[ aPath ] = std::make_pair( name, aForceNoConnect );
313
314 return name;
315}
316
317
319{
322}
323
324
325const BOX2I SCH_PIN::GetBoundingBox( bool aIncludeInvisiblePins, bool aIncludeNameAndNumber,
326 bool aIncludeElectricalType ) const
327{
329 BOX2I r = m_libPin->GetBoundingBox( aIncludeInvisiblePins, aIncludeNameAndNumber,
330 aIncludeElectricalType );
331
332 r.RevertYAxis();
333
334 r = t.TransformCoordinate( r );
335 r.Offset( GetParentSymbol()->GetPosition() );
336
337 return r;
338}
339
340
341bool SCH_PIN::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
342{
343 // When looking for an "exact" hit aAccuracy will be 0 which works poorly if the pin has
344 // no pin number or name. Give it a floor.
345 if( Schematic() )
346 aAccuracy = std::max( aAccuracy, Schematic()->Settings().m_PinSymbolSize / 4 );
347
348 BOX2I rect = GetBoundingBox( false, true, m_flags & SHOW_ELEC_TYPE );
349 return rect.Inflate( aAccuracy ).Contains( aPosition );
350}
351
352
353bool SCH_PIN::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
354{
355 BOX2I sel = aRect;
356
357 if( aAccuracy )
358 sel.Inflate( aAccuracy );
359
360 if( aContained )
361 return sel.Contains( GetBoundingBox( false, false, false ) );
362
363 return sel.Intersects( GetBoundingBox( false, true, m_flags & SHOW_ELEC_TYPE ) );
364}
365
366
368{
369 return new SCH_PIN( *this );
370}
371
372
374{
375 // Reciprocal checking is done in CONNECTION_GRAPH anyway
377}
const char * name
Definition: DXF_plotter.cpp:56
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:494
EDA_ITEM_FLAGS m_flags
Definition: eda_item.h:500
virtual bool Matches(const EDA_SEARCH_DATA &aSearchData, void *aAuxData) const
Compare the item against the search criteria in aSearchData.
Definition: eda_item.h:384
EDA_ITEM * GetParent() const
Definition: eda_item.h:99
EDA_ITEM * m_parent
Linked list: Link (parent struct)
Definition: eda_item.h:498
wxString AsString() const
Definition: kiid.cpp:251
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:184
wxString GetShownNumber() const
Definition: lib_pin.h:123
wxString GetShownName() const
Definition: lib_pin.cpp:175
VECTOR2I GetPosition() const override
Definition: lib_pin.h:222
wxString GetSelectMenuText(UNITS_PROVIDER *aUnitsProvider) const override
Return the text to display to be used in the selection clarification context menu when multiple items...
Definition: lib_pin.cpp:1325
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
bool IsPowerConnection() const
Return whether this pin forms an implicit power connection: i.e., is hidden and of type POWER_IN.
Definition: lib_pin.h:197
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
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:162
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:202
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:177
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:373
wxString GetSelectMenuText(UNITS_PROVIDER *aUnitsProvider) const override
Return the text to display to be used in the selection clarification context menu when multiple items...
Definition: sch_pin.cpp:194
wxString GetName() const
Definition: sch_pin.cpp:85
bool m_isDangling
Definition: sch_pin.h:173
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:176
VECTOR2I GetPosition() const override
Definition: sch_pin.h:82
LIB_PIN * m_libPin
Definition: sch_pin.h:168
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:175
VECTOR2I m_position
Definition: sch_pin.h:172
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:341
wxString m_alt
Definition: sch_pin.h:171
VECTOR2I GetTransformedPosition() const
Definition: sch_pin.cpp:318
void ClearDefaultNetName(const SCH_SHEET_PATH *aPath)
Definition: sch_pin.cpp:256
bool IsStacked(const SCH_PIN *aPin) const
Definition: sch_pin.cpp:248
wxString m_number
Definition: sch_pin.h:170
SCH_SYMBOL * GetParentSymbol() const
Definition: sch_pin.cpp:188
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: sch_pin.cpp:367
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:267
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:80
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const
Return the reference for the given sheet path.
Definition: sch_symbol.cpp:624
VECTOR2I GetPosition() const override
Definition: sch_symbol.h:697
const wxString GetValueFieldText(bool aResolve) const
Definition: sch_symbol.cpp:758
TRANSFORM & GetTransform()
Definition: sch_symbol.h:277
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:41
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.
E_SERIE r
Definition: eserie.cpp:41
@ LAYER_DANGLING
Definition: layer_ids.h:368
@ LAYER_DEVICE
Definition: layer_ids.h:357
@ LAYER_PIN
Definition: layer_ids.h:361
@ 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)
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