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 <jon@craftyjon.com>
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 == "~" )
98  return wxEmptyString;
99  else
100  return name;
101 }
102 
103 
104 wxString SCH_PIN::GetShownNumber() const
105 {
106  if( m_number == "~" )
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 bool SCH_PIN::Matches( const wxFindReplaceData& aSearchData, void* aAuxDat ) const
144 {
145  if( !( aSearchData.GetFlags() & FR_SEARCH_ALL_PINS ) )
146  return false;
147 
148  return EDA_ITEM::Matches( GetName(), aSearchData )
149  || EDA_ITEM::Matches( GetNumber(), aSearchData );
150 }
151 
152 
153 bool SCH_PIN::Replace( const wxFindReplaceData& aSearchData, void* aAuxData )
154 {
155  bool isReplaced = false;
156 
157  /* TODO: waiting on a way to override pins in the schematic...
158  isReplaced |= EDA_ITEM::Replace( aSearchData, m_name );
159  isReplaced |= EDA_ITEM::Replace( aSearchData, m_number );
160  */
161 
162  return isReplaced;
163 }
164 
165 
167 {
168  return static_cast<SCH_SYMBOL*>( GetParent() );
169 }
170 
171 
172 wxString SCH_PIN::GetSelectMenuText( EDA_UNITS aUnits ) const
173 {
174  return wxString::Format( "%s %s",
175  GetParentSymbol()->GetSelectMenuText( aUnits ),
176  m_libPin->GetSelectMenuText( aUnits ) );
177 }
178 
179 
180 void SCH_PIN::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
181 {
182  wxString msg;
183 
184  aList.emplace_back( _( "Type" ), _( "Pin" ) );
185 
186  if( m_libPin->GetUnit() == 0 )
187  msg = _( "All" );
188  else
189  msg.Printf( wxT( "%d" ), m_libPin->GetUnit() );
190 
191  aList.emplace_back( _( "Unit" ), msg );
192 
193  if( m_libPin->GetConvert() == LIB_ITEM::LIB_CONVERT::BASE )
194  msg = _( "no" );
195  else if( m_libPin->GetConvert() == LIB_ITEM::LIB_CONVERT::DEMORGAN )
196  msg = _( "yes" );
197  else
198  msg = wxT( "?" );
199 
200  aList.emplace_back( _( "Converted" ), msg );
201 
202  aList.emplace_back( _( "Name" ), GetShownName() );
203  aList.emplace_back( _( "Number" ), GetShownNumber() );
204  aList.emplace_back( _( "Type" ), ElectricalPinTypeGetText( GetType() ) );
205  aList.emplace_back( _( "Style" ), PinShapeGetText( GetShape() ) );
206 
207  aList.emplace_back( _( "Visible" ), IsVisible() ? _( "Yes" ) : _( "No" ) );
208 
209  aList.emplace_back( _( "Length" ), StringFromValue( aFrame->GetUserUnits(), GetLength() ) );
210 
211  int i = PinOrientationIndex( GetOrientation() );
212  aList.emplace_back( _( "Orientation" ), PinOrientationName( (unsigned) i ) );
213 
214  SCH_EDIT_FRAME* schframe = dynamic_cast<SCH_EDIT_FRAME*>( aFrame );
215  SCH_SHEET_PATH* currentSheet = schframe ? &schframe->GetCurrentSheet() : nullptr;
216  SCH_SYMBOL* symbol = GetParentSymbol();
217 
218  aList.emplace_back( symbol->GetRef( currentSheet ), symbol->GetValue( currentSheet, true ) );
219 
220 #if defined(DEBUG)
221  if( dynamic_cast<SCH_EDIT_FRAME*>( aFrame ) )
222  {
223  SCH_CONNECTION* conn = Connection();
224 
225  if( conn )
226  conn->AppendInfoToMsgPanel( aList );
227  }
228 #endif
229 
230 }
231 
232 
234 {
235  std::lock_guard<std::recursive_mutex> lock( m_netmap_mutex );
236 
237  if( aPath )
238  m_net_name_map.erase( *aPath );
239  else
240  m_net_name_map.clear();
241 }
242 
243 
244 wxString SCH_PIN::GetDefaultNetName( const SCH_SHEET_PATH& aPath, bool aForceNoConnect )
245 {
246  if( m_libPin->IsPowerConnection() )
247  return m_libPin->GetName();
248 
249  std::lock_guard<std::recursive_mutex> lock( m_netmap_mutex );
250 
251  auto it = m_net_name_map.find( aPath );
252 
253  if( it != m_net_name_map.end() )
254  {
255  if( it->second.second == aForceNoConnect )
256  return it->second.first;
257  }
258 
259  wxString name = "Net-(";
260 
261  if( aForceNoConnect || GetType() == ELECTRICAL_PINTYPE::PT_NC )
262  name = ( "unconnected-(" );
263 
264  name << GetParentSymbol()->GetRef( &aPath );
265 
266  bool annotated = true;
267 
268  // Add timestamp for uninitialized symbols
269  if( name.Last() == '?' )
270  {
272  annotated = false;
273  }
274 
275  name << "-Pad" << m_libPin->GetNumber() << ")";
276 
277  if( annotated )
278  m_net_name_map[ aPath ] = std::make_pair( name, aForceNoConnect );
279 
280  return name;
281 }
282 
283 
285 {
288 }
289 
290 
292 {
295 
296  r.RevertYAxis();
297 
298  r = t.TransformCoordinate( r );
299  r.Offset( GetParentSymbol()->GetPosition() );
300 
301  return r;
302 }
303 
304 
305 bool SCH_PIN::HitTest( const wxPoint& aPosition, int aAccuracy ) const
306 {
307  // When looking for an "exact" hit aAccuracy will be 0 which works poorly if the pin has
308  // no pin number or name. Give it a floor.
309  if( Schematic() )
310  aAccuracy = std::max( aAccuracy, Schematic()->Settings().m_PinSymbolSize / 4 );
311 
312  EDA_RECT rect = GetBoundingBox();
313  return rect.Inflate( aAccuracy ).Contains( aPosition );
314 }
315 
316 
317 bool SCH_PIN::ConnectionPropagatesTo( const EDA_ITEM* aItem ) const
318 {
319  // Reciprocal checking is done in CONNECTION_GRAPH anyway
320  return !( m_libPin->GetType() == ELECTRICAL_PINTYPE::PT_NC );
321 }
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:1184
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:172
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:153
virtual bool Matches(const wxFindReplaceData &aSearchData, void *aAuxData) const
Compare the item against the search criteria in aSearchData.
Definition: eda_item.h:365
bool HitTest(const wxPoint &aPosition, int aAccuracy=0) const override
Test if aPosition is contained within or on the bounding box of an item.
Definition: sch_pin.cpp:305
SCH_SYMBOL * GetParentSymbol() const
Definition: sch_pin.cpp:166
const wxPoint GetLocalPosition() const
Definition: sch_pin.h:79
int GetLength() const
Definition: sch_pin.cpp:137
int GetOrientation() const
Definition: lib_pin.h:75
bool IsVisible() const
Definition: sch_pin.h:104
void ClearDefaultNetName(const SCH_SHEET_PATH *aPath)
Definition: sch_pin.cpp:233
wxString ElectricalPinTypeGetText(ELECTRICAL_PINTYPE aType)
Definition: pin_type.cpp:237
GRAPHIC_PINSHAPE GetShape() const
Definition: lib_pin.h:78
SCHEMATIC * Schematic() const
Searches the item hierarchy to find a SCHEMATIC.
Definition: sch_item.cpp:97
wxString GetDefaultNetName(const SCH_SHEET_PATH &aPath, bool aForceNoConnect=false)
Definition: sch_pin.cpp:244
int PinOrientationIndex(int code)
Definition: pin_type.cpp:147
wxString AsString() const
Definition: kiid.cpp:218
Schematic editor (Eeschema) main window.
wxString GetNumber() const
Definition: sch_pin.h:109
wxString PinShapeGetText(GRAPHIC_PINSHAPE aShape)
Definition: pin_type.cpp:258
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:441
const EDA_RECT GetBoundingBox() const override
Definition: lib_pin.h:174
GRAPHIC_PINSHAPE
Definition: pin_type.h:55
The base class for create windows for drawing purpose.
TRANSFORM & GetTransform()
Definition: sch_symbol.h:231
wxPoint GetPosition() const override
Definition: sch_pin.h:78
ELECTRICAL_PINTYPE m_Type
Definition: lib_pin.h:60
bool Contains(const wxPoint &aPoint) const
Definition: eda_rect.cpp:57
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:186
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:131
wxString PinOrientationName(unsigned aPinOrientationCode)
Definition: pin_type.cpp:116
int GetUnit() const
Definition: lib_item.h:258
bool m_isDangling
Definition: sch_pin.h:142
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:180
wxString GetShownNumber() const
Definition: sch_pin.cpp:104
EDA_ITEM * GetParent() const
Definition: eda_item.h:115
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:143
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:573
E_SERIE r
Definition: eserie.cpp:41
int GetConvert() const
Definition: lib_item.h:261
EDA_ITEM & operator=(const EDA_ITEM &aItem)
Assign the members of aItem to another object.
Definition: eda_item.cpp:180
std::recursive_mutex m_netmap_mutex
The name that this pin connection will drive onto a net.
Definition: sch_pin.h:145
SCH_PIN & operator=(const SCH_PIN &aPin)
Definition: sch_pin.cpp:67
void SetPosition(const wxPoint &aPosition) override
Definition: sch_pin.h:80
const KIID m_Uuid
Definition: eda_item.h:475
wxPoint GetPosition() const override
Definition: lib_pin.h:210
wxString m_number
Definition: sch_pin.h:139
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:140
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:146
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:317
wxPoint GetPosition() const override
Definition: sch_symbol.h:644
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:100
wxString StringFromValue(EDA_UNITS aUnits, double aValue, bool aAddUnitSymbol, EDA_DATA_TYPE aType)
Convert a value to a string using double notation.
Definition: base_units.cpp:204
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:291
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:137
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
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:284
wxPoint m_position
Definition: sch_pin.h:141