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, jaen-pierre.charras@gipsa-lab.inpg.com
5  * Copyright (C) 1992-2019 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 <gr_text.h>
31 #include <kicad_string.h>
32 #include <plotter.h>
33 #include <sch_draw_panel.h>
34 #include <sch_edit_frame.h>
35 #include <sch_sheet.h>
36 #include <sch_sheet_pin.h>
37 #include <sch_painter.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_UNDEFINED_SIDE )
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  return 1;
101 }
102 
103 
104 void SCH_SHEET_PIN::SetNumber( int aNumber )
105 {
106  wxASSERT( aNumber >= 2 );
107 
108  m_number = aNumber;
109 }
110 
111 
113 {
114  SCH_SHEET* Sheet = GetParent();
115 
116  // use SHEET_UNDEFINED_SIDE to adjust text orientation without changing edge
117 
118  switch( aEdge )
119  {
120  case SHEET_LEFT_SIDE:
121  m_edge = aEdge;
122  SetTextX( Sheet->m_pos.x );
123  SetLabelSpinStyle( LABEL_SPIN_STYLE::RIGHT ); // Orientation horiz inverse
124  break;
125 
126  case SHEET_RIGHT_SIDE:
127  m_edge = aEdge;
128  SetTextX( Sheet->m_pos.x + Sheet->m_size.x );
129  SetLabelSpinStyle( LABEL_SPIN_STYLE::LEFT ); // Orientation horiz normal
130  break;
131 
132  case SHEET_TOP_SIDE:
133  m_edge = aEdge;
134  SetTextY( Sheet->m_pos.y );
135  SetLabelSpinStyle( LABEL_SPIN_STYLE::BOTTOM ); // Orientation vert BOTTOM
136  break;
137 
138  case SHEET_BOTTOM_SIDE:
139  m_edge = aEdge;
140  SetTextY( Sheet->m_pos.y + Sheet->m_size.y );
141  SetLabelSpinStyle( LABEL_SPIN_STYLE::UP ); // Orientation vert UP
142  break;
143 
144  default:
145  break;
146  }
147 }
148 
149 
151 {
152  return m_edge;
153 }
154 
155 
157 {
158  SCH_SHEET* sheet = GetParent();
159 
160  if( sheet == NULL )
161  return;
162 
163  int leftSide = sheet->m_pos.x;
164  int rightSide = sheet->m_pos.x + sheet->m_size.x;
165  int topSide = sheet->m_pos.y;
166  int botSide = sheet->m_pos.y + sheet->m_size.y;
167 
168  SHAPE_LINE_CHAIN sheetEdge;
169 
170  sheetEdge.Append( leftSide, topSide );
171  sheetEdge.Append( rightSide, topSide );
172  sheetEdge.Append( rightSide, botSide );
173  sheetEdge.Append( leftSide, botSide );
174  sheetEdge.Append( leftSide, topSide );
175 
176  switch( sheetEdge.NearestSegment( Pos ) )
177  {
178  case 0: SetEdge( SHEET_TOP_SIDE ); break;
179  case 1: SetEdge( SHEET_RIGHT_SIDE ); break;
180  case 2: SetEdge( SHEET_BOTTOM_SIDE ); break;
181  case 3: SetEdge( SHEET_LEFT_SIDE ); break;
182  default: wxASSERT( "Invalid segment number" );
183  }
184 
185  switch( GetEdge() )
186  {
187  case SHEET_RIGHT_SIDE:
188  case SHEET_LEFT_SIDE:
189  SetTextY( Pos.y );
190 
191  if( GetTextPos().y < topSide )
192  SetTextY( topSide );
193 
194  if( GetTextPos().y > botSide )
195  SetTextY( botSide );
196 
197  break;
198 
199  case SHEET_BOTTOM_SIDE:
200  case SHEET_TOP_SIDE:
201  SetTextX( Pos.x );
202 
203  if( GetTextPos().x < leftSide )
204  SetTextX( leftSide );
205 
206  if( GetTextPos().x > rightSide )
207  SetTextX( rightSide );
208 
209  break;
210 
212  wxASSERT( "Undefined sheet side" );
213  }
214 }
215 
216 
218 {
219  int p = GetTextPos().y - aCenter;
220 
221  SetTextY( aCenter - p );
222 
223  switch( m_edge )
224  {
225  case SHEET_TOP_SIDE: SetEdge( SHEET_BOTTOM_SIDE ); break;
226  case SHEET_BOTTOM_SIDE: SetEdge( SHEET_TOP_SIDE ); break;
227  default: break;
228  }
229 }
230 
231 
233 {
234  int p = GetTextPos().x - aCenter;
235 
236  SetTextX( aCenter - p );
237 
238  switch( m_edge )
239  {
240  case SHEET_LEFT_SIDE: SetEdge( SHEET_RIGHT_SIDE ); break;
241  case SHEET_RIGHT_SIDE: SetEdge( SHEET_LEFT_SIDE ); break;
242  default: break;
243  }
244 }
245 
246 
247 void SCH_SHEET_PIN::Rotate( wxPoint aCenter )
248 {
249  wxPoint pt = GetTextPos();
250  RotatePoint( &pt, aCenter, 900 );
251  ConstrainOnEdge( pt );
252 }
253 
254 
256  std::vector<wxPoint>& aPoints, const wxPoint& aPos ) const
257 {
258  /*
259  * These are the same icon shapes as SCH_HIERLABEL but the graphic icon is slightly
260  * different in 2 cases:
261  * for INPUT type the icon is the OUTPUT shape of SCH_HIERLABEL
262  * for OUTPUT type the icon is the INPUT shape of SCH_HIERLABEL
263  */
265 
266  switch( shape )
267  {
270  default: break;
271  }
272 
273  SCH_HIERLABEL::CreateGraphicShape( aSettings, aPoints, aPos, shape );
274 }
275 
276 
277 void SCH_SHEET_PIN::GetEndPoints( std::vector<DANGLING_END_ITEM>& aItemList )
278 {
280  aItemList.push_back( item );
281 }
282 
283 
285 {
286  return wxString::Format( _( "Hierarchical Sheet Pin %s" ), ShortenedShownText() );
287 }
288 
289 
291 {
293 }
294 
295 
296 bool SCH_SHEET_PIN::HitTest( const wxPoint& aPoint, int aAccuracy ) const
297 {
298  EDA_RECT rect = GetBoundingBox();
299 
300  rect.Inflate( aAccuracy );
301 
302  return rect.Contains( aPoint );
303 }
304 
305 
306 #if defined(DEBUG)
307 
308 void SCH_SHEET_PIN::Show( int nestLevel, std::ostream& os ) const
309 {
310  // XML output:
311  NestedSpace( nestLevel, os ) << '<' << GetClass().Lower().mb_str() << ">"
312  << " pin_name=\"" << TO_UTF8( GetText() )
313  << '"' << "/>\n" << std::flush;
314 }
315 
316 #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)
bool m_isDangling
True if not connected to another object if the object derive from SCH_TEXT supports connections.
Definition: sch_text.h:316
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:253
wxSize m_size
Definition: sch_sheet.h:455
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:228
void Print(const RENDER_SETTINGS *aSettings, const wxPoint &offset) override
Print a schematic item.
Definition: sch_text.cpp:1438
wxPoint m_pos
Definition: sch_sheet.h:454
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:166
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
void SetTextX(int aX)
Definition: eda_text.h:256
#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:496
#define NULL
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:202
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:1397
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:1458
const EDA_RECT GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition: sch_text.cpp:1489
void Rotate(wxPoint aCenter) override
Rotate the item around aCenter 90 degrees in the clockwise direction.
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:151
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.
#define _(s)
Definition: 3d_actions.cpp:33
SHAPE_LINE_CHAIN.
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:289
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:150
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:206
const wxPoint & GetTextPos() const
Definition: eda_text.h:254
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:312
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:87
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:197
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:133
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:363
void SetTextY(int aY)
Definition: eda_text.h:257
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:163