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-2021 KiCad Developers, see change_log.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 <lib_pin.h>
23 #include <sch_symbol.h>
24 #include <sch_pin.h>
25 #include <schematic.h>
26 #include <schematic_settings.h>
27 #include <sch_sheet_path.h>
28 #include <sch_edit_frame.h>
29 
30 
31 SCH_PIN::SCH_PIN( LIB_PIN* aLibPin, SCH_SYMBOL* aParentSymbol ) :
32  SCH_ITEM( aParentSymbol, SCH_PIN_T )
33 {
34  m_alt = wxEmptyString;
35  m_number = aLibPin->GetNumber();
36  m_libPin = aLibPin;
37  SetPosition( aLibPin->GetPosition() );
38  m_isDangling = true;
39 }
40 
41 
46 SCH_PIN::SCH_PIN( SCH_SYMBOL* aParentSymbol, const wxString& aNumber, const wxString& aAlt ) :
47  SCH_ITEM( aParentSymbol, SCH_PIN_T )
48 {
49  m_alt = aAlt;
50  m_number = aNumber;
51  m_libPin = nullptr;
52  m_isDangling = true;
53 }
54 
55 
56 SCH_PIN::SCH_PIN( const SCH_PIN& aPin ) :
57  SCH_ITEM( aPin )
58 {
59  m_alt = aPin.m_alt;
60  m_number = aPin.m_number;
61  m_libPin = aPin.m_libPin;
62  m_position = aPin.m_position;
64 }
65 
66 
68 {
69  SCH_ITEM::operator=( aPin );
70 
71  m_alt = aPin.m_alt;
72  m_number = aPin.m_number;
73  m_libPin = aPin.m_libPin;
74  m_position = aPin.m_position;
76 
77  return *this;
78 }
79 
80 
81 wxString SCH_PIN::GetName() const
82 {
83  if( !m_alt.IsEmpty() )
84  return m_alt;
85 
86  return m_libPin->GetName();
87 }
88 
89 
90 wxString SCH_PIN::GetShownName() const
91 {
92  wxString name = m_libPin->GetName();
93 
94  if( !m_alt.IsEmpty() )
95  name = m_alt;
96 
97  if( name == wxT( "~" ) )
98  return wxEmptyString;
99  else
100  return name;
101 }
102 
103 
104 wxString SCH_PIN::GetShownNumber() const
105 {
106  if( m_number == wxT( "~" ) )
107  return wxEmptyString;
108  else
109  return m_number;
110 }
111 
112 
114 {
115  if( !m_alt.IsEmpty() )
116  return m_libPin->GetAlt( m_alt ).m_Type;
117 
118  return m_libPin->GetType();
119 }
120 
121 
123 {
124  if( !m_alt.IsEmpty() )
125  return m_libPin->GetAlt( m_alt ).m_Shape;
126 
127  return m_libPin->GetShape();
128 }
129 
130 
132 {
133  return m_libPin->GetOrientation();
134 }
135 
136 
138 {
139  return m_libPin->GetLength();
140 }
141 
142 
143 void SCH_PIN::ViewGetLayers( int aLayers[], int& aCount ) const
144 {
145  aCount = 3;
146  aLayers[0] = LAYER_DANGLING;
147  aLayers[1] = LAYER_DEVICE;
148  aLayers[2] = LAYER_SELECTION_SHADOWS;
149 }
150 
151 
152 bool SCH_PIN::Matches( const wxFindReplaceData& aSearchData, void* aAuxDat ) const
153 {
154  if( !( aSearchData.GetFlags() & FR_SEARCH_ALL_PINS ) )
155  return false;
156 
157  return EDA_ITEM::Matches( GetName(), aSearchData )
158  || EDA_ITEM::Matches( GetNumber(), aSearchData );
159 }
160 
161 
162 bool SCH_PIN::Replace( const wxFindReplaceData& aSearchData, void* aAuxData )
163 {
164  bool isReplaced = false;
165 
166  /* TODO: waiting on a way to override pins in the schematic...
167  isReplaced |= EDA_ITEM::Replace( aSearchData, m_name );
168  isReplaced |= EDA_ITEM::Replace( aSearchData, m_number );
169  */
170 
171  return isReplaced;
172 }
173 
174 
176 {
177  return static_cast<SCH_SYMBOL*>( GetParent() );
178 }
179 
180 
181 wxString SCH_PIN::GetSelectMenuText( EDA_UNITS aUnits ) const
182 {
183  return wxString::Format( wxT( "%s %s" ),
184  GetParentSymbol()->GetSelectMenuText( aUnits ),
185  m_libPin->GetSelectMenuText( aUnits ) );
186 }
187 
188 
189 void SCH_PIN::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
190 {
191  EDA_UNITS units = aFrame->GetUserUnits();
192  wxString msg;
193 
194  aList.emplace_back( _( "Type" ), _( "Pin" ) );
195 
196  if( m_libPin->GetUnit() == 0 )
197  msg = _( "All" );
198  else
199  msg.Printf( wxT( "%d" ), m_libPin->GetUnit() );
200 
201  aList.emplace_back( _( "Unit" ), msg );
202 
203  if( m_libPin->GetConvert() == LIB_ITEM::LIB_CONVERT::BASE )
204  msg = _( "no" );
205  else if( m_libPin->GetConvert() == LIB_ITEM::LIB_CONVERT::DEMORGAN )
206  msg = _( "yes" );
207  else
208  msg = wxT( "?" );
209 
210  aList.emplace_back( _( "Converted" ), msg );
211 
212  aList.emplace_back( _( "Name" ), GetShownName() );
213  aList.emplace_back( _( "Number" ), GetShownNumber() );
214  aList.emplace_back( _( "Type" ), ElectricalPinTypeGetText( GetType() ) );
215  aList.emplace_back( _( "Style" ), PinShapeGetText( GetShape() ) );
216 
217  aList.emplace_back( _( "Visible" ), IsVisible() ? _( "Yes" ) : _( "No" ) );
218 
219  aList.emplace_back( _( "Length" ), MessageTextFromValue( units, GetLength() ), true );
220 
221  int i = PinOrientationIndex( GetOrientation() );
222  aList.emplace_back( _( "Orientation" ), PinOrientationName( (unsigned) i ) );
223 
224  SCH_EDIT_FRAME* schframe = dynamic_cast<SCH_EDIT_FRAME*>( aFrame );
225  SCH_SHEET_PATH* currentSheet = schframe ? &schframe->GetCurrentSheet() : nullptr;
226  SCH_SYMBOL* symbol = GetParentSymbol();
227 
228  aList.emplace_back( symbol->GetRef( currentSheet ), symbol->GetValue( currentSheet, true ) );
229 
230 #if defined(DEBUG)
231  if( !IsConnectivityDirty() && dynamic_cast<SCH_EDIT_FRAME*>( aFrame ) )
232  {
233  SCH_CONNECTION* conn = Connection();
234 
235  if( conn )
236  conn->AppendInfoToMsgPanel( aList );
237  }
238 #endif
239 
240 }
241 
242 
244 {
245  std::lock_guard<std::recursive_mutex> lock( m_netmap_mutex );
246 
247  if( aPath )
248  m_net_name_map.erase( *aPath );
249  else
250  m_net_name_map.clear();
251 }
252 
253 
254 wxString SCH_PIN::GetDefaultNetName( const SCH_SHEET_PATH& aPath, bool aForceNoConnect )
255 {
256  if( m_libPin->IsPowerConnection() )
257  return m_libPin->GetName();
258 
259  std::lock_guard<std::recursive_mutex> lock( m_netmap_mutex );
260 
261  auto it = m_net_name_map.find( aPath );
262 
263  if( it != m_net_name_map.end() )
264  {
265  if( it->second.second == aForceNoConnect )
266  return it->second.first;
267  }
268 
269  wxString name = wxT( "Net-(" );
270 
271  if( aForceNoConnect || GetType() == ELECTRICAL_PINTYPE::PT_NC )
272  name = ( wxT( "unconnected-(" ) );
273 
274  name << GetParentSymbol()->GetRef( &aPath );
275 
276  bool annotated = true;
277 
278  // Add timestamp for uninitialized symbols
279  if( name.Last() == '?' )
280  {
282  annotated = false;
283  }
284 
285  name << wxT( "-Pad" ) << m_libPin->GetNumber() << wxT( ")" );
286 
287  if( annotated )
288  m_net_name_map[ aPath ] = std::make_pair( name, aForceNoConnect );
289 
290  return name;
291 }
292 
293 
295 {
298 }
299 
300 
302 {
305 
306  r.RevertYAxis();
307 
308  r = t.TransformCoordinate( r );
309  r.Offset( GetParentSymbol()->GetPosition() );
310 
311  return r;
312 }
313 
314 
315 bool SCH_PIN::HitTest( const wxPoint& aPosition, int aAccuracy ) const
316 {
317  // When looking for an "exact" hit aAccuracy will be 0 which works poorly if the pin has
318  // no pin number or name. Give it a floor.
319  if( Schematic() )
320  aAccuracy = std::max( aAccuracy, Schematic()->Settings().m_PinSymbolSize / 4 );
321 
322  EDA_RECT rect = GetBoundingBox();
323  return rect.Inflate( aAccuracy ).Contains( aPosition );
324 }
325 
326 
328 {
329  return new SCH_PIN( *this );
330 }
331 
332 
333 bool SCH_PIN::ConnectionPropagatesTo( const EDA_ITEM* aItem ) const
334 {
335  // Reciprocal checking is done in CONNECTION_GRAPH anyway
336  return !( m_libPin->GetType() == ELECTRICAL_PINTYPE::PT_NC );
337 }
wxString GetSelectMenuText(EDA_UNITS aUnits) const override
Return the text to display to be used in the selection clarification context menu when multiple items...
Definition: lib_pin.cpp:1199
wxString GetSelectMenuText(EDA_UNITS aUnits) const override
Return the text to display to be used in the selection clarification context menu when multiple items...
Definition: sch_pin.cpp:181
wxString MessageTextFromValue(EDA_UNITS aUnits, int aValue, bool aAddUnitLabel, EDA_DATA_TYPE aType)
Convert a value to a string using double notation.
Definition: base_units.cpp:104
bool Replace(const wxFindReplaceData &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:162
virtual bool Matches(const wxFindReplaceData &aSearchData, void *aAuxData) const
Compare the item against the search criteria in aSearchData.
Definition: eda_item.h:364
bool HitTest(const wxPoint &aPosition, int aAccuracy=0) const override
Test if aPosition is inside or on the boundary of this item.
Definition: sch_pin.cpp:315
SCH_SYMBOL * GetParentSymbol() const
Definition: sch_pin.cpp:175
const wxPoint GetLocalPosition() const
Definition: sch_pin.h:81
int GetLength() const
Definition: sch_pin.cpp:137
int GetOrientation() const
Definition: lib_pin.h:75
bool IsVisible() const
Definition: sch_pin.h:117
void ClearDefaultNetName(const SCH_SHEET_PATH *aPath)
Definition: sch_pin.cpp:243
wxString ElectricalPinTypeGetText(ELECTRICAL_PINTYPE aType)
Definition: pin_type.cpp:240
GRAPHIC_PINSHAPE GetShape() const
Definition: lib_pin.h:78
SCHEMATIC * Schematic() const
Searches the item hierarchy to find a SCHEMATIC.
Definition: sch_item.cpp:104
wxString GetDefaultNetName(const SCH_SHEET_PATH &aPath, bool aForceNoConnect=false)
Definition: sch_pin.cpp:254
int PinOrientationIndex(int code)
Definition: pin_type.cpp:150
wxString AsString() const
Definition: kiid.cpp:236
Schematic editor (Eeschema) main window.
wxString GetNumber() const
Definition: sch_pin.h:122
wxString PinShapeGetText(GRAPHIC_PINSHAPE aShape)
Definition: pin_type.cpp:279
ALT GetAlt(const wxString &aAlt)
Definition: lib_pin.h:134
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const
Return the reference for the given sheet path.
Definition: sch_symbol.cpp:464
const EDA_RECT GetBoundingBox() const override
Definition: lib_pin.h:176
GRAPHIC_PINSHAPE
Definition: pin_type.h:55
The base class for create windows for drawing purpose.
TRANSFORM & GetTransform()
Definition: sch_symbol.h:232
wxPoint GetPosition() const override
Definition: sch_pin.h:80
ELECTRICAL_PINTYPE m_Type
Definition: lib_pin.h:60
bool Contains(const wxPoint &aPoint) const
Definition: eda_rect.cpp:57
bool IsConnectivityDirty() const
Definition: sch_item.h:413
wxPoint TransformCoordinate(const wxPoint &aPoint) const
Calculate a new coordinate according to the mirror/rotation transform.
Definition: transform.cpp:42
wxString GetShownName() const
Definition: sch_pin.cpp:90
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:188
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:138
wxString PinOrientationName(unsigned aPinOrientationCode)
Definition: pin_type.cpp:119
int GetUnit() const
Definition: lib_item.h:266
bool m_isDangling
Definition: sch_pin.h:155
for transforming drawing coordinates for a wxDC device context.
Definition: transform.h:45
void AppendInfoToMsgPanel(std::vector< MSG_PANEL_ITEM > &aList) const
Adds information about the connection object to aList.
const wxString & GetName() const
Definition: lib_pin.h:106
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:189
wxString GetShownNumber() const
Definition: sch_pin.cpp:104
EDA_ITEM * GetParent() const
Definition: eda_item.h:114
int GetOrientation() const
Definition: sch_pin.cpp:131
#define _(s)
bool Matches(const wxFindReplaceData &aSearchData, void *aAuxData) const override
Compare the item against the search criteria in aSearchData.
Definition: sch_pin.cpp:152
GRAPHIC_PINSHAPE GetShape() const
Definition: sch_pin.cpp:122
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
const wxString GetValue(const SCH_SHEET_PATH *sheet, bool aResolve) const
Return the instance-specific value for the given sheet path.
Definition: sch_symbol.cpp:596
E_SERIE r
Definition: eserie.cpp:41
int GetConvert() const
Definition: lib_item.h:269
std::recursive_mutex m_netmap_mutex
The name that this pin connection will drive onto a net.
Definition: sch_pin.h:158
SCH_PIN & operator=(const SCH_PIN &aPin)
Definition: sch_pin.cpp:67
void SetPosition(const wxPoint &aPosition) override
Definition: sch_pin.h:82
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: sch_pin.cpp:327
const KIID m_Uuid
Definition: eda_item.h:474
wxPoint GetPosition() const override
Definition: lib_pin.h:212
wxString m_number
Definition: sch_pin.h:152
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
EDA_UNITS
Definition: eda_units.h:38
const wxString & GetNumber() const
Definition: lib_pin.h:116
ELECTRICAL_PINTYPE GetType() const
Definition: lib_pin.h:84
Schematic symbol object.
Definition: sch_symbol.h:78
const char * name
Definition: DXF_plotter.cpp:56
wxString m_alt
Definition: sch_pin.h:153
ELECTRICAL_PINTYPE
The symbol library pin object electrical types used in ERC tests.
Definition: pin_type.h:35
std::map< const SCH_SHEET_PATH, std::pair< wxString, bool > > m_net_name_map
Definition: sch_pin.h:159
int GetLength() const
Definition: lib_pin.h:81
Each graphical item can have a SCH_CONNECTION describing its logical connection (to a bus or net).
ELECTRICAL_PINTYPE GetType() const
Definition: sch_pin.cpp:113
SCH_PIN(LIB_PIN *aLibPin, SCH_SYMBOL *aParentSymbol)
Definition: sch_pin.cpp:31
Handle the component boundary box.
Definition: eda_rect.h:42
bool ConnectionPropagatesTo(const EDA_ITEM *aItem) const override
Return true if this item should propagate connection info to aItem.
Definition: sch_pin.cpp:333
wxPoint GetPosition() const override
Definition: sch_symbol.h:645
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:99
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:143
SCH_SHEET_PATH & GetCurrentSheet() const
const EDA_RECT GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition: sch_pin.cpp:301
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
not connected (must be left open)
LIB_PIN * m_libPin
Definition: sch_pin.h:150
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:182
GRAPHIC_PINSHAPE m_Shape
Definition: lib_pin.h:59
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
wxString GetName() const
Definition: sch_pin.cpp:81
SCH_ITEM & operator=(const SCH_ITEM &aPin)
Definition: sch_item.cpp:62
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Inflate the rectangle horizontally by dx and vertically by dy.
Definition: eda_rect.cpp:364
wxPoint GetTransformedPosition() const
Definition: sch_pin.cpp:294
wxPoint m_position
Definition: sch_pin.h:154