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 <sch_sheet_path.h>
26 #include <sch_edit_frame.h>
27 
28 
29 SCH_PIN::SCH_PIN( LIB_PIN* aLibPin, SCH_SYMBOL* aParentSymbol ) :
30  SCH_ITEM( aParentSymbol, SCH_PIN_T )
31 {
32  m_alt = wxEmptyString;
33  m_number = aLibPin->GetNumber();
34  m_libPin = aLibPin;
35  SetPosition( aLibPin->GetPosition() );
36  m_isDangling = true;
37 }
38 
39 
44 SCH_PIN::SCH_PIN( SCH_SYMBOL* aParentSymbol, const wxString& aNumber, const wxString& aAlt ) :
45  SCH_ITEM( aParentSymbol, SCH_PIN_T )
46 {
47  m_alt = aAlt;
48  m_number = aNumber;
49  m_libPin = nullptr;
50  m_isDangling = true;
51 }
52 
53 
54 SCH_PIN::SCH_PIN( const SCH_PIN& aPin ) :
55  SCH_ITEM( aPin )
56 {
57  m_alt = aPin.m_alt;
58  m_number = aPin.m_number;
59  m_libPin = aPin.m_libPin;
60  m_position = aPin.m_position;
62 }
63 
64 
66 {
67  SCH_ITEM::operator=( aPin );
68 
69  m_alt = aPin.m_alt;
70  m_number = aPin.m_number;
71  m_libPin = aPin.m_libPin;
72  m_position = aPin.m_position;
74 
75  return *this;
76 }
77 
78 
79 wxString SCH_PIN::GetName() const
80 {
81  if( !m_alt.IsEmpty() )
82  return m_alt;
83 
84  return m_libPin->GetName();
85 }
86 
87 
88 wxString SCH_PIN::GetShownName() const
89 {
90  wxString name = m_libPin->GetName();
91 
92  if( !m_alt.IsEmpty() )
93  name = m_alt;
94 
95  if( name == "~" )
96  return wxEmptyString;
97  else
98  return name;
99 }
100 
101 
102 wxString SCH_PIN::GetShownNumber() const
103 {
104  if( m_number == "~" )
105  return wxEmptyString;
106  else
107  return m_number;
108 }
109 
110 
112 {
113  if( !m_alt.IsEmpty() )
114  return m_libPin->GetAlt( m_alt ).m_Type;
115 
116  return m_libPin->GetType();
117 }
118 
119 
121 {
122  if( !m_alt.IsEmpty() )
123  return m_libPin->GetAlt( m_alt ).m_Shape;
124 
125  return m_libPin->GetShape();
126 }
127 
128 
130 {
131  return m_libPin->GetOrientation();
132 }
133 
134 
136 {
137  return m_libPin->GetLength();
138 }
139 
140 
141 bool SCH_PIN::Matches( const wxFindReplaceData& aSearchData, void* aAuxDat ) const
142 {
143  if( !( aSearchData.GetFlags() & FR_SEARCH_ALL_PINS ) )
144  return false;
145 
146  return EDA_ITEM::Matches( GetName(), aSearchData )
147  || EDA_ITEM::Matches( GetNumber(), aSearchData );
148 }
149 
150 
151 bool SCH_PIN::Replace( const wxFindReplaceData& aSearchData, void* aAuxData )
152 {
153  bool isReplaced = false;
154 
155  /* TODO: waiting on a way to override pins in the schematic...
156  isReplaced |= EDA_ITEM::Replace( aSearchData, m_name );
157  isReplaced |= EDA_ITEM::Replace( aSearchData, m_number );
158  */
159 
160  return isReplaced;
161 }
162 
163 
165 {
166  return static_cast<SCH_SYMBOL*>( GetParent() );
167 }
168 
169 
170 wxString SCH_PIN::GetSelectMenuText( EDA_UNITS aUnits ) const
171 {
172  return wxString::Format( "%s %s",
173  GetParentSymbol()->GetSelectMenuText( aUnits ),
174  m_libPin->GetSelectMenuText( aUnits ) );
175 }
176 
177 
179 {
180  wxString msg;
181 
182  aList.push_back( MSG_PANEL_ITEM( _( "Type" ), _( "Pin" ) ) );
183 
184  if( m_libPin->GetUnit() == 0 )
185  msg = _( "All" );
186  else
187  msg.Printf( wxT( "%d" ), m_libPin->GetUnit() );
188 
189  aList.push_back( MSG_PANEL_ITEM( _( "Unit" ), msg ) );
190 
191  if( m_libPin->GetConvert() == LIB_ITEM::LIB_CONVERT::BASE )
192  msg = _( "no" );
193  else if( m_libPin->GetConvert() == LIB_ITEM::LIB_CONVERT::DEMORGAN )
194  msg = _( "yes" );
195  else
196  msg = wxT( "?" );
197 
198  aList.push_back( MSG_PANEL_ITEM( _( "Converted" ), msg ) );
199 
200  aList.push_back( MSG_PANEL_ITEM( _( "Name" ), GetShownName() ) );
201  aList.push_back( MSG_PANEL_ITEM( _( "Number" ), GetShownNumber() ) );
202  aList.push_back( MSG_PANEL_ITEM( _( "Type" ), ElectricalPinTypeGetText( GetType() ) ) );
203 
204  msg = PinShapeGetText( GetShape() );
205  aList.push_back( MSG_PANEL_ITEM( _( "Style" ), msg ) );
206 
207  msg = IsVisible() ? _( "Yes" ) : _( "No" );
208  aList.push_back( MSG_PANEL_ITEM( _( "Visible" ), msg ) );
209 
210  // Display pin length
211  msg = StringFromValue( aFrame->GetUserUnits(), GetLength() );
212  aList.push_back( MSG_PANEL_ITEM( _( "Length" ), msg ) );
213 
214  msg = PinOrientationName( (unsigned) PinOrientationIndex( GetOrientation() ) );
215  aList.push_back( MSG_PANEL_ITEM( _( "Orientation" ), msg ) );
216 
217  msg = MessageTextFromValue( aFrame->GetUserUnits(), m_position.x );
218  aList.emplace_back( _( "Pos X" ), msg );
219 
220  msg = MessageTextFromValue( aFrame->GetUserUnits(), m_position.y );
221  aList.emplace_back( _( "Pos Y" ), msg );
222 
223  SCH_EDIT_FRAME* schframe = dynamic_cast<SCH_EDIT_FRAME*>( aFrame );
224  SCH_SHEET_PATH* currentSheet = schframe ? &schframe->GetCurrentSheet() : nullptr;
225  SCH_SYMBOL* symbol = GetParentSymbol();
226 
227  aList.emplace_back( symbol->GetRef( currentSheet ), symbol->GetValue( currentSheet, true ) );
228 
229 #if defined(DEBUG)
230 
231  SCH_EDIT_FRAME* frame = dynamic_cast<SCH_EDIT_FRAME*>( aFrame );
232 
233  if( !frame )
234  return;
235 
236  SCH_CONNECTION* conn = Connection();
237 
238  if( conn )
239  conn->AppendInfoToMsgPanel( aList );
240 
241 #endif
242 
243 }
244 
245 
247 {
248  std::lock_guard<std::recursive_mutex> lock( m_netmap_mutex );
249 
250  if( aPath )
251  m_net_name_map.erase( *aPath );
252  else
253  m_net_name_map.clear();
254 }
255 
256 
257 wxString SCH_PIN::GetDefaultNetName( const SCH_SHEET_PATH& aPath, bool aForceNoConnect )
258 {
259  if( m_libPin->IsPowerConnection() )
260  return m_libPin->GetName();
261 
262  std::lock_guard<std::recursive_mutex> lock( m_netmap_mutex );
263 
264  auto it = m_net_name_map.find( aPath );
265 
266  if( it != m_net_name_map.end() )
267  {
268  if( it->second.second == aForceNoConnect )
269  return it->second.first;
270  }
271 
272  wxString name = "Net-(";
273 
274  if( aForceNoConnect || GetType() == ELECTRICAL_PINTYPE::PT_NC )
275  name = ( "unconnected-(" );
276 
277  name << GetParentSymbol()->GetRef( &aPath );
278 
279  bool annotated = true;
280 
281  // Add timestamp for uninitialized symbols
282  if( name.Last() == '?' )
283  {
285  annotated = false;
286  }
287 
288  name << "-Pad" << m_libPin->GetNumber() << ")";
289 
290  if( annotated )
291  m_net_name_map[ aPath ] = std::make_pair( name, aForceNoConnect );
292 
293  return name;
294 }
295 
296 
298 {
301 }
302 
303 
305 {
308 
309  r.RevertYAxis();
310 
311  r = t.TransformCoordinate( r );
313 
314  return r;
315 }
316 
317 
318 bool SCH_PIN::HitTest( const wxPoint& aPosition, int aAccuracy ) const
319 {
320  // When looking for an "exact" hit aAccuracy will be 0 which works poorly if the pin has
321  // no pin number or name. Give it a floor.
322  aAccuracy = std::max( aAccuracy, GetPenWidth() );
323 
324  EDA_RECT rect = GetBoundingBox();
325  return rect.Inflate( aAccuracy ).Contains( aPosition );
326 }
327 
328 
329 bool SCH_PIN::ConnectionPropagatesTo( const EDA_ITEM* aItem ) const
330 {
331  // Reciprocal checking is done in CONNECTION_GRAPH anyway
332  return !( m_libPin->GetType() == ELECTRICAL_PINTYPE::PT_NC );
333 }
void Offset(int dx, int dy)
Definition: eda_rect.h:147
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:1176
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:170
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:151
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:318
SCH_SYMBOL * GetParentSymbol() const
Definition: sch_pin.cpp:164
const wxPoint GetLocalPosition() const
Definition: sch_pin.h:78
int GetLength() const
Definition: sch_pin.cpp:135
int GetOrientation() const
Definition: lib_pin.h:75
bool IsVisible() const
Definition: sch_pin.h:103
void ClearDefaultNetName(const SCH_SHEET_PATH *aPath)
Definition: sch_pin.cpp:246
wxString ElectricalPinTypeGetText(ELECTRICAL_PINTYPE aType)
Definition: pin_type.cpp:237
GRAPHIC_PINSHAPE GetShape() const
Definition: lib_pin.h:78
wxString GetDefaultNetName(const SCH_SHEET_PATH &aPath, bool aForceNoConnect=false)
Definition: sch_pin.cpp:257
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:108
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:432
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.
void RevertYAxis()
Mirror the rectangle from the X axis (negate Y pos and size).
Definition: eda_rect.h:198
TRANSFORM & GetTransform()
Definition: sch_symbol.h:231
wxPoint GetPosition() const override
Definition: sch_pin.h:77
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:88
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:259
bool m_isDangling
Definition: sch_pin.h:141
for transforming drawing coordinates for a wxDC device context.
Definition: transform.h:45
const wxString & GetName() const
Definition: lib_pin.h:106
wxString GetShownNumber() const
Definition: sch_pin.cpp:102
EDA_ITEM * GetParent() const
Definition: eda_item.h:115
int GetOrientation() const
Definition: sch_pin.cpp:129
#define _(s)
bool Matches(const wxFindReplaceData &aSearchData, void *aAuxData) const override
Compare the item against the search criteria in aSearchData.
Definition: sch_pin.cpp:141
GRAPHIC_PINSHAPE GetShape() const
Definition: sch_pin.cpp:120
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:564
int GetConvert() const
Definition: lib_item.h:262
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:144
SCH_PIN & operator=(const SCH_PIN &aPin)
Definition: sch_pin.cpp:65
void SetPosition(const wxPoint &aPosition) override
Definition: sch_pin.h:79
virtual int GetPenWidth() const
Definition: sch_item.h:289
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:138
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
void AppendInfoToMsgPanel(MSG_PANEL_ITEMS &aList) const
Adds information about the connection object to aList.
Schematic symbol object.
Definition: sch_symbol.h:78
const char * name
Definition: DXF_plotter.cpp:59
wxString m_alt
Definition: sch_pin.h:139
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:145
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:111
SCH_PIN(LIB_PIN *aLibPin, SCH_SYMBOL *aParentSymbol)
Definition: sch_pin.cpp:29
Handle the component boundary box.
Definition: eda_rect.h:42
void GetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, MSG_PANEL_ITEMS &aList) override
Definition: sch_pin.cpp:178
bool ConnectionPropagatesTo(const EDA_ITEM *aItem) const override
Return true if this item should propagate connection info to aItem.
Definition: sch_pin.cpp:329
wxPoint GetPosition() const override
Definition: sch_symbol.h:641
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:100
std::vector< MSG_PANEL_ITEM > MSG_PANEL_ITEMS
Definition: msgpanel.h:97
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
EDA_MSG_PANEL items for displaying messages.
Definition: msgpanel.h:53
const EDA_RECT GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition: sch_pin.cpp:304
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:136
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:197
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:79
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:297
wxPoint m_position
Definition: sch_pin.h:140