KiCad PCB EDA Suite
sch_sheet_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) 2006 Jean-Pierre Charras, [email protected]
5  * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include <algorithm>
26 
27 #include <bitmaps.h>
28 #include <general.h>
30 #include <string_utils.h>
31 #include <plotters/plotter.h>
32 #include <sch_draw_panel.h>
33 #include <sch_edit_frame.h>
34 #include <sch_sheet.h>
35 #include <sch_sheet_pin.h>
36 #include <sch_painter.h>
37 #include <schematic.h>
38 #include <trigo.h>
39 
40 
41 SCH_SHEET_PIN::SCH_SHEET_PIN( SCH_SHEET* parent, const wxPoint& pos, const wxString& text ) :
43  m_edge( SHEET_SIDE::UNDEFINED )
44 {
45  SetParent( parent );
46  wxASSERT( parent );
48 
49  SetTextPos( pos );
50 
51  if( parent->IsVerticalOrientation() )
53  else
55 
57  m_isDangling = true;
58  m_number = 2;
59 }
60 
61 
63 {
64  return new SCH_SHEET_PIN( *this );
65 }
66 
67 
68 void SCH_SHEET_PIN::Print( const RENDER_SETTINGS* aSettings, const wxPoint& aOffset )
69 {
70  // The icon selection is handle by the virtual method CreateGraphicShape called by ::Print
71  SCH_HIERLABEL::Print( aSettings, aOffset );
72 }
73 
74 
76 {
77  wxCHECK_RET( aItem->Type() == SCH_SHEET_PIN_T,
78  wxString::Format( wxT( "SCH_SHEET_PIN object cannot swap data with %s object." ),
79  aItem->GetClass() ) );
80 
81  SCH_SHEET_PIN* pin = ( SCH_SHEET_PIN* ) aItem;
83  int tmp = pin->GetNumber();
84  pin->SetNumber( GetNumber() );
85  SetNumber( tmp );
86  SHEET_SIDE stmp = pin->GetEdge();
87  pin->SetEdge( GetEdge() );
88  SetEdge( stmp );
89 }
90 
91 
92 bool SCH_SHEET_PIN::operator==( const SCH_SHEET_PIN* aPin ) const
93 {
94  return aPin == this;
95 }
96 
97 
99 {
100  if( Schematic() )
102 
103  return Mils2iu( DEFAULT_LINE_WIDTH_MILS );
104 }
105 
106 
107 void SCH_SHEET_PIN::SetNumber( int aNumber )
108 {
109  wxASSERT( aNumber >= 2 );
110 
111  m_number = aNumber;
112 }
113 
114 
116 {
117  SCH_SHEET* Sheet = GetParent();
118 
119  // use SHEET_UNDEFINED_SIDE to adjust text orientation without changing edge
120 
121  switch( aEdge )
122  {
123  case SHEET_SIDE::LEFT:
124  m_edge = aEdge;
125  SetTextX( Sheet->m_pos.x );
126  SetLabelSpinStyle( LABEL_SPIN_STYLE::RIGHT ); // Orientation horiz inverse
127  break;
128 
129  case SHEET_SIDE::RIGHT:
130  m_edge = aEdge;
131  SetTextX( Sheet->m_pos.x + Sheet->m_size.x );
132  SetLabelSpinStyle( LABEL_SPIN_STYLE::LEFT ); // Orientation horiz normal
133  break;
134 
135  case SHEET_SIDE::TOP:
136  m_edge = aEdge;
137  SetTextY( Sheet->m_pos.y );
138  SetLabelSpinStyle( LABEL_SPIN_STYLE::BOTTOM ); // Orientation vert BOTTOM
139  break;
140 
141  case SHEET_SIDE::BOTTOM:
142  m_edge = aEdge;
143  SetTextY( Sheet->m_pos.y + Sheet->m_size.y );
144  SetLabelSpinStyle( LABEL_SPIN_STYLE::UP ); // Orientation vert UP
145  break;
146 
147  default:
148  break;
149  }
150 }
151 
152 
154 {
155  return m_edge;
156 }
157 
158 
160 {
161  SCH_SHEET* sheet = GetParent();
162 
163  if( sheet == nullptr )
164  return;
165 
166  int leftSide = sheet->m_pos.x;
167  int rightSide = sheet->m_pos.x + sheet->m_size.x;
168  int topSide = sheet->m_pos.y;
169  int botSide = sheet->m_pos.y + sheet->m_size.y;
170 
171  SHAPE_LINE_CHAIN sheetEdge;
172 
173  sheetEdge.Append( leftSide, topSide );
174  sheetEdge.Append( rightSide, topSide );
175  sheetEdge.Append( rightSide, botSide );
176  sheetEdge.Append( leftSide, botSide );
177  sheetEdge.Append( leftSide, topSide );
178 
179  switch( sheetEdge.NearestSegment( Pos ) )
180  {
181  case 0: SetEdge( SHEET_SIDE::TOP ); break;
182  case 1: SetEdge( SHEET_SIDE::RIGHT ); break;
183  case 2: SetEdge( SHEET_SIDE::BOTTOM ); break;
184  case 3: SetEdge( SHEET_SIDE::LEFT ); break;
185  default: wxASSERT( "Invalid segment number" );
186  }
187 
188  switch( GetEdge() )
189  {
190  case SHEET_SIDE::RIGHT:
191  case SHEET_SIDE::LEFT:
192  SetTextY( Pos.y );
193 
194  if( GetTextPos().y < topSide )
195  SetTextY( topSide );
196 
197  if( GetTextPos().y > botSide )
198  SetTextY( botSide );
199 
200  break;
201 
202  case SHEET_SIDE::BOTTOM:
203  case SHEET_SIDE::TOP:
204  SetTextX( Pos.x );
205 
206  if( GetTextPos().x < leftSide )
207  SetTextX( leftSide );
208 
209  if( GetTextPos().x > rightSide )
210  SetTextX( rightSide );
211 
212  break;
213 
215  wxASSERT( "Undefined sheet side" );
216  }
217 }
218 
219 
221 {
222  int p = GetTextPos().y - aCenter;
223 
224  SetTextY( aCenter - p );
225 
226  switch( m_edge )
227  {
230  default: break;
231  }
232 }
233 
234 
236 {
237  int p = GetTextPos().x - aCenter;
238 
239  SetTextX( aCenter - p );
240 
241  switch( m_edge )
242  {
245  default: break;
246  }
247 }
248 
249 
250 void SCH_SHEET_PIN::Rotate( const wxPoint& aCenter )
251 {
252  wxPoint pt = GetTextPos();
253  RotatePoint( &pt, aCenter, 900 );
254  ConstrainOnEdge( pt );
255 }
256 
257 
259  std::vector<wxPoint>& aPoints, const wxPoint& aPos ) const
260 {
261  /*
262  * These are the same icon shapes as SCH_HIERLABEL but the graphic icon is slightly
263  * different in 2 cases:
264  * for INPUT type the icon is the OUTPUT shape of SCH_HIERLABEL
265  * for OUTPUT type the icon is the INPUT shape of SCH_HIERLABEL
266  */
268 
269  switch( shape )
270  {
273  default: break;
274  }
275 
276  SCH_HIERLABEL::CreateGraphicShape( aSettings, aPoints, aPos, shape );
277 }
278 
279 
280 void SCH_SHEET_PIN::GetEndPoints( std::vector<DANGLING_END_ITEM>& aItemList )
281 {
283  aItemList.push_back( item );
284 }
285 
286 
288 {
289  return wxString::Format( _( "Hierarchical Sheet Pin %s" ), ShortenedShownText() );
290 }
291 
292 
294 {
296 }
297 
298 
299 bool SCH_SHEET_PIN::HitTest( const wxPoint& aPoint, int aAccuracy ) const
300 {
301  EDA_RECT rect = GetBoundingBox();
302 
303  rect.Inflate( aAccuracy );
304 
305  return rect.Contains( aPoint );
306 }
307 
308 
309 #if defined(DEBUG)
310 
311 void SCH_SHEET_PIN::Show( int nestLevel, std::ostream& os ) const
312 {
313  // XML output:
314  NestedSpace( nestLevel, os ) << '<' << GetClass().Lower().mb_str() << ">"
315  << " pin_name=\"" << TO_UTF8( GetText() )
316  << '"' << "/>\n" << std::flush;
317 }
318 
319 #endif
Plot settings, and plotting engines (PostScript, Gerber, HPGL and DXF)
SCH_SHEET * GetParent() const
Get the parent sheet object of this sheet pin.
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
SCH_SHEET_PIN(SCH_SHEET *parent, const wxPoint &pos=wxPoint(0, 0), const wxString &text=wxEmptyString)
#define DEFAULT_LINE_WIDTH_MILS
The default wire width in mils. (can be changed in preference menu)
bool m_isDangling
True if not connected to another object if the object derive from SCH_TEXT supports connections.
Definition: sch_text.h:262
SCHEMATIC_SETTINGS & Settings() const
Definition: schematic.cpp:167
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:267
SCHEMATIC * Schematic() const
Searches the item hierarchy to find a SCHEMATIC.
Definition: sch_item.cpp:97
wxSize m_size
Definition: sch_sheet.h:457
wxString GetClass() const override
Return the class name.
Definition: sch_sheet_pin.h:80
BITMAPS GetMenuImage() const override
Return a pointer to an image to be used in menus.
SHEET_SIDE GetEdge() const
bool HitTest(const wxPoint &aPosition, int aAccuracy=0) const override
Test if aPosition is contained within or on the bounding box of an item.
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:229
void Print(const RENDER_SETTINGS *aSettings, const wxPoint &offset) override
Print a schematic item.
Definition: sch_text.cpp:1582
wxPoint m_pos
Definition: sch_sheet.h:456
void CreateGraphicShape(const RENDER_SETTINGS *aSettings, std::vector< wxPoint > &aPoints, const wxPoint &aPos) const override
Calculate the graphic shape (a polygon) associated to the text.
bool Contains(const wxPoint &aPoint) const
Definition: eda_rect.cpp:57
virtual void SetParent(EDA_ITEM *aParent)
Definition: eda_item.h:116
void Append(int aX, int aY, bool aAllowDuplication=false)
Append a new point at the end of the line chain.
void SetTextX(int aX)
Definition: eda_text.h:270
void Rotate(const wxPoint &aCenter) override
Rotate the item around aCenter 90 degrees in the clockwise direction.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
SCH_LAYER_ID m_layer
Definition: sch_item.h:481
void SetEdge(SHEET_SIDE aEdge)
void Print(const RENDER_SETTINGS *aSettings, const wxPoint &aOffset) override
Print a schematic item.
wxString ShortenedShownText() const
Returns a shortened version (max 15 characters) of the shown text.
Definition: eda_text.cpp:218
void SetLabelSpinStyle(LABEL_SPIN_STYLE aSpinStyle) override
Set a spin or rotation angle, along with specific horizontal and vertical justification styles with e...
Definition: sch_text.cpp:1541
#define _(s)
void CreateGraphicShape(const RENDER_SETTINGS *aSettings, std::vector< wxPoint > &aPoints, const wxPoint &aPos) const override
Calculate the graphic shape (a polygon) associated to the text.
Definition: sch_text.cpp:1602
const EDA_RECT GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition: sch_text.cpp:1634
Define a sheet pin (label) used in sheets to create hierarchical schematics.
Definition: sch_sheet_pin.h:65
int GetNumber() const
Get the sheet label number.
void MirrorVertically(int aCenter) override
Mirror item vertically about aCenter.
void ConstrainOnEdge(wxPoint Pos)
Adjust label position to edge based on proximity to vertical or horizontal edge of the parent sheet.
int GetPenWidth() const override
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:54
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
int NearestSegment(const VECTOR2I &aP) const
Find the segment nearest the given point.
void MirrorHorizontally(int aCenter) override
Mirror item horizontally about aCenter.
PINSHEETLABEL_SHAPE
Definition: sch_text.h:95
void SetNumber(int aNumber)
Set the sheet label number.
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:32
void GetEndPoints(std::vector< DANGLING_END_ITEM > &aItemList) override
Add the schematic item end points to aItemList if the item has end points.
int m_number
Label number use for saving sheet label to file.
Represent a polyline (an zero-thickness chain of connected line segments).
bool IsVerticalOrientation() const
Definition: sch_sheet.cpp:351
void SwapData(SCH_ITEM *aItem) override
Swap the internal data structures aItem with the schematic item.
Definition: sch_text.cpp:361
Handle the component boundary box.
Definition: eda_rect.h:42
bool operator==(const SCH_SHEET_PIN *aPin) const
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:100
void SwapData(SCH_ITEM *aItem) override
Swap the internal data structures aItem with the schematic item.
virtual wxString GetClass() const override
Return the class name.
Definition: sch_item.h:191
const wxPoint & GetTextPos() const
Definition: eda_text.h:268
wxString GetSelectMenuText(EDA_UNITS aUnits) const override
Return the text to display to be used in the selection clarification context menu when multiple items...
PINSHEETLABEL_SHAPE m_shape
Definition: sch_text.h:258
SHEET_SIDE
Define the edge of the sheet that the sheet pin is positioned.
Definition: sch_sheet_pin.h:45
Helper class used to store the state of schematic items that can be connected to other schematic item...
Definition: sch_item.h:79
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:182
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:154
SHEET_SIDE m_edge
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Inflate the rectangle horizontally by dx and vertically by dy.
Definition: eda_rect.cpp:364
void SetTextY(int aY)
Definition: eda_text.h:271
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:113