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_painter.h>
37 #include <trigo.h>
38 
39 
40 SCH_SHEET_PIN::SCH_SHEET_PIN( SCH_SHEET* parent, const wxPoint& pos, const wxString& text ) :
41  SCH_HIERLABEL( pos, text, SCH_SHEET_PIN_T ),
42  m_edge( SHEET_UNDEFINED_SIDE )
43 {
44  SetParent( parent );
45  wxASSERT( parent );
47 
48  SetTextPos( pos );
49 
50  if( parent->IsVerticalOrientation() )
52  else
54 
56  m_isDangling = true;
57  m_number = 2;
58 }
59 
60 
62 {
63  return new SCH_SHEET_PIN( *this );
64 }
65 
66 
67 void SCH_SHEET_PIN::Print( const RENDER_SETTINGS* aSettings, const wxPoint& aOffset )
68 {
69  // The icon selection is handle by the virtual method CreateGraphicShape called by ::Print
70  SCH_HIERLABEL::Print( aSettings, aOffset );
71 }
72 
73 
75 {
76  wxCHECK_RET( aItem->Type() == SCH_SHEET_PIN_T,
77  wxString::Format( wxT( "SCH_SHEET_PIN object cannot swap data with %s object." ),
78  aItem->GetClass() ) );
79 
80  SCH_SHEET_PIN* pin = ( SCH_SHEET_PIN* ) aItem;
81  SCH_TEXT::SwapData( (SCH_TEXT*) pin );
82  int tmp = pin->GetNumber();
83  pin->SetNumber( GetNumber() );
84  SetNumber( tmp );
85  SHEET_SIDE stmp = pin->GetEdge();
86  pin->SetEdge( GetEdge() );
87  SetEdge( stmp );
88 }
89 
90 
91 bool SCH_SHEET_PIN::operator==( const SCH_SHEET_PIN* aPin ) const
92 {
93  return aPin == this;
94 }
95 
96 
98 {
99  return 1;
100 }
101 
102 
103 void SCH_SHEET_PIN::SetNumber( int aNumber )
104 {
105  wxASSERT( aNumber >= 2 );
106 
107  m_number = aNumber;
108 }
109 
110 
112 {
113  SCH_SHEET* Sheet = GetParent();
114 
115  // use SHEET_UNDEFINED_SIDE to adjust text orientation without changing edge
116 
117  switch( aEdge )
118  {
119  case SHEET_LEFT_SIDE:
120  m_edge = aEdge;
121  SetTextX( Sheet->m_pos.x );
122  SetLabelSpinStyle( LABEL_SPIN_STYLE::RIGHT ); // Orientation horiz inverse
123  break;
124 
125  case SHEET_RIGHT_SIDE:
126  m_edge = aEdge;
127  SetTextX( Sheet->m_pos.x + Sheet->m_size.x );
128  SetLabelSpinStyle( LABEL_SPIN_STYLE::LEFT ); // Orientation horiz normal
129  break;
130 
131  case SHEET_TOP_SIDE:
132  m_edge = aEdge;
133  SetTextY( Sheet->m_pos.y );
134  SetLabelSpinStyle( LABEL_SPIN_STYLE::BOTTOM ); // Orientation vert BOTTOM
135  break;
136 
137  case SHEET_BOTTOM_SIDE:
138  m_edge = aEdge;
139  SetTextY( Sheet->m_pos.y + Sheet->m_size.y );
140  SetLabelSpinStyle( LABEL_SPIN_STYLE::UP ); // Orientation vert UP
141  break;
142 
143  default:
144  break;
145  }
146 }
147 
148 
150 {
151  return m_edge;
152 }
153 
154 
156 {
157  SCH_SHEET* sheet = GetParent();
158 
159  if( sheet == NULL )
160  return;
161 
162  int leftSide = sheet->m_pos.x;
163  int rightSide = sheet->m_pos.x + sheet->m_size.x;
164  int topSide = sheet->m_pos.y;
165  int botSide = sheet->m_pos.y + sheet->m_size.y;
166 
167  SHAPE_LINE_CHAIN sheetEdge;
168 
169  sheetEdge.Append( leftSide, topSide );
170  sheetEdge.Append( rightSide, topSide );
171  sheetEdge.Append( rightSide, botSide );
172  sheetEdge.Append( leftSide, botSide );
173  sheetEdge.Append( leftSide, topSide );
174 
175  switch( sheetEdge.NearestSegment( Pos ) )
176  {
177  case 0: SetEdge( SHEET_TOP_SIDE ); break;
178  case 1: SetEdge( SHEET_RIGHT_SIDE ); break;
179  case 2: SetEdge( SHEET_BOTTOM_SIDE ); break;
180  case 3: SetEdge( SHEET_LEFT_SIDE ); break;
181  default: wxASSERT( "Invalid segment number" );
182  }
183 
184  switch( GetEdge() )
185  {
186  case SHEET_RIGHT_SIDE:
187  case SHEET_LEFT_SIDE:
188  SetTextY( Pos.y );
189 
190  if( GetTextPos().y < topSide )
191  SetTextY( topSide );
192 
193  if( GetTextPos().y > botSide )
194  SetTextY( botSide );
195 
196  break;
197 
198  case SHEET_BOTTOM_SIDE:
199  case SHEET_TOP_SIDE:
200  SetTextX( Pos.x );
201 
202  if( GetTextPos().x < leftSide )
203  SetTextX( leftSide );
204 
205  if( GetTextPos().x > rightSide )
206  SetTextX( rightSide );
207 
208  break;
209 
211  wxASSERT( "Undefined sheet side" );
212  }
213 }
214 
215 
217 {
218  int p = GetTextPos().y - aCenter;
219 
220  SetTextY( aCenter - p );
221 
222  switch( m_edge )
223  {
224  case SHEET_TOP_SIDE: SetEdge( SHEET_BOTTOM_SIDE ); break;
225  case SHEET_BOTTOM_SIDE: SetEdge( SHEET_TOP_SIDE ); break;
226  default: break;
227  }
228 }
229 
230 
232 {
233  int p = GetTextPos().x - aCenter;
234 
235  SetTextX( aCenter - p );
236 
237  switch( m_edge )
238  {
239  case SHEET_LEFT_SIDE: SetEdge( SHEET_RIGHT_SIDE ); break;
240  case SHEET_RIGHT_SIDE: SetEdge( SHEET_LEFT_SIDE ); break;
241  default: break;
242  }
243 }
244 
245 
246 void SCH_SHEET_PIN::Rotate( wxPoint aPosition )
247 {
248  wxPoint pt = GetTextPos();
249  RotatePoint( &pt, aPosition, 900 );
250  ConstrainOnEdge( pt );
251 }
252 
253 
255  std::vector<wxPoint>& aPoints, const wxPoint& aPos )
256 {
257  /*
258  * These are the same icon shapes as SCH_HIERLABEL but the graphic icon is slightly
259  * different in 2 cases:
260  * for INPUT type the icon is the OUTPUT shape of SCH_HIERLABEL
261  * for OUTPUT type the icon is the INPUT shape of SCH_HIERLABEL
262  */
264 
265  switch( m_shape )
266  {
269  default: break;
270  }
271 
272  SCH_HIERLABEL::CreateGraphicShape( aRenderSettings, aPoints, aPos );
273  m_shape = tmp;
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 {
292  return add_hierar_pin_xpm;
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
SHEET_SIDE
Defines the edge of the sheet that the sheet pin is positioned SHEET_LEFT_SIDE = 0: pin on left side ...
Definition: sch_sheet.h:56
Plot settings, and plotting engines (PostScript, Gerber, HPGL and DXF)
BITMAP_DEF GetMenuImage() const override
Return a pointer to an image to be used in menus.
SCH_SHEET * GetParent() const
Get the parent sheet object of this sheet pin.
Definition: sch_sheet.h:169
PNG memory record (file in memory).
Definition: bitmap_def.h:29
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:172
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:253
wxSize m_size
Definition: sch_sheet.h:231
wxString GetClass() const override
Return the class name.
Definition: sch_sheet.h:109
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:1407
const BITMAP_OPAQUE add_hierar_pin_xpm[1]
wxPoint m_pos
Definition: sch_sheet.h:230
bool Contains(const wxPoint &aPoint) const
Definition: eda_rect.cpp:57
virtual void SetParent(EDA_ITEM *aParent)
Definition: eda_item.h:165
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
void SetTextX(int aX)
Definition: eda_text.h:256
void CreateGraphicShape(const RENDER_SETTINGS *aSettings, std::vector< wxPoint > &aPoints, const wxPoint &Pos) override
Calculate the graphic shape (a polygon) associated to the text.
Definition: sch_text.cpp:1427
#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:201
#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:1366
const EDA_RECT GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition: sch_text.cpp:1451
Define a sheet pin (label) used in sheets to create hierarchical schematics.
Definition: sch_sheet.h:85
int GetNumber() const
Get the sheet label number.
Definition: sch_sheet.h:144
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:219
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.
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.
Definition: sch_sheet.h:88
#define _(s)
Definition: 3d_actions.cpp:33
SHAPE_LINE_CHAIN.
bool IsVerticalOrientation() const
Definition: sch_sheet.cpp:349
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:149
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:222
const wxPoint & GetTextPos() const
Definition: eda_text.h:254
void Rotate(wxPoint aPosition) override
Rotate the item around aPosition 90 degrees in the clockwise direction.
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:168
Helper class used to store the state of schematic items that can be connected to other schematic item...
Definition: sch_item.h:87
void CreateGraphicShape(const RENDER_SETTINGS *aSettings, std::vector< wxPoint > &aPoints, const wxPoint &aPos) override
Calculate the graphic shape (a polygon) associated to the text.
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:196
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:133
SHEET_SIDE m_edge
Definition: sch_sheet.h:93
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:162