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 <fctsys.h>
29 #include <general.h>
31 #include <gr_basic.h>
32 #include <gr_text.h>
33 #include <kicad_string.h>
34 #include <plotter.h>
35 #include <sch_draw_panel.h>
36 #include <sch_edit_frame.h>
37 #include <sch_sheet.h>
38 #include <trigo.h>
39 
40 
41 SCH_SHEET_PIN::SCH_SHEET_PIN( SCH_SHEET* parent, const wxPoint& pos, const wxString& text ) :
42  SCH_HIERLABEL( pos, text, SCH_SHEET_PIN_T ),
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( wxDC* aDC, const wxPoint& aOffset )
69 {
70  // The icon selection is handle by the virtual method CreateGraphicShape called by ::Print
71  SCH_HIERLABEL::Print( aDC, 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  GetChars( aItem->GetClass() ) ) );
80 
81  SCH_SHEET_PIN* pin = ( SCH_SHEET_PIN* ) aItem;
82  SCH_TEXT::SwapData( (SCH_TEXT*) pin );
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 GetDefaultLineThickness();
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 
217 void SCH_SHEET_PIN::MirrorX( int aXaxis_position )
218 {
219  int p = GetTextPos().y - aXaxis_position;
220 
221  SetTextY( aXaxis_position - 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 
232 void SCH_SHEET_PIN::MirrorY( int aYaxis_position )
233 {
234  int p = GetTextPos().x - aYaxis_position;
235 
236  SetTextX( aYaxis_position - 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 
248 {
249  wxPoint pt = GetTextPos();
250  RotatePoint( &pt, aPosition, 900 );
251  ConstrainOnEdge( pt );
252 }
253 
254 
255 void SCH_SHEET_PIN::CreateGraphicShape( 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( 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
EDA_UNITS
Definition: common.h:184
SCH_LAYER_ID m_Layer
Definition: sch_item.h:152
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:55
BITMAP_DEF GetMenuImage() const override
Function GetMenuImage returns 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:167
PNG memory record (file in memory).
Definition: bitmap_def.h:29
EDA_ITEM * Clone() const override
Function Clone creates a duplicate of this item with linked list members set to NULL.
SCH_SHEET_PIN(SCH_SHEET *parent, const wxPoint &pos=wxPoint(0, 0), const wxString &text=wxEmptyString)
void MirrorY(int aYaxis_position) override
Function MirrorY mirrors item relative to the Y axis about aYaxis_position.
bool m_isDangling
True if not connected to another object if the object derive from SCH_TEXT supports connections.
Definition: sch_text.h:184
int GetDefaultLineThickness()
Default line thickness used to draw/plot items having a default thickness line value (i....
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:231
wxSize m_size
Definition: sch_sheet.h:230
wxString GetClass() const override
Function GetClass returns the class name.
Definition: sch_sheet.h:108
SHEET_SIDE GetEdge() const
bool HitTest(const wxPoint &aPosition, int aAccuracy=0) const override
Function HitTest tests if aPosition is contained within or on the bounding box of an item.
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:208
const BITMAP_OPAQUE add_hierar_pin_xpm[1]
void MirrorX(int aXaxis_position) override
Function MirrorX mirrors item relative to the X axis about aXaxis_position.
wxPoint m_pos
Definition: sch_sheet.h:229
void Print(wxDC *aDC, const wxPoint &aOffset) override
Function Print Print a schematic item.
void CreateGraphicShape(std::vector< wxPoint > &aPoints, const wxPoint &aPos) override
Calculate the graphic shape (a polygon) associated to the text.
bool Contains(const wxPoint &aPoint) const
Function Contains.
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
void SetTextX(int aX)
Definition: eda_text.h:234
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:48
#define NULL
void SetParent(EDA_ITEM *aParent)
Definition: base_struct.h:218
void SetEdge(SHEET_SIDE aEdge)
wxString ShortenedShownText() const
Returns a shortened version (max 15 characters) of the shown text.
Definition: eda_text.cpp:163
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:1041
const EDA_RECT GetBoundingBox() const override
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes.
Definition: sch_text.cpp:1127
Define a sheet pin (label) used in sheets to create hierarchical schematics.
Definition: sch_sheet.h:84
int GetNumber() const
Get the sheet label number.
Definition: sch_sheet.h:142
void ConstrainOnEdge(wxPoint Pos)
Adjust label position to edge based on proximity to vertical or horizontal edge of the parent sheet.
void Print(wxDC *DC, const wxPoint &offset) override
Function Print Print a schematic item.
Definition: sch_text.cpp:1082
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:215
void CreateGraphicShape(std::vector< wxPoint > &aPoints, const wxPoint &Pos) override
Calculate the graphic shape (a polygon) associated to the text.
Definition: sch_text.cpp:1105
int NearestSegment(const VECTOR2I &aP) const
Find the segment nearest the given point.
PINSHEETLABEL_SHAPE
Definition: sch_text.h:163
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:101
void SetNumber(int aNumber)
Set the sheet label number.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
void GetEndPoints(std::vector< DANGLING_END_ITEM > &aItemList) override
Function GetEndPoints adds 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:87
#define _(s)
Definition: 3d_actions.cpp:33
SHAPE_LINE_CHAIN.
bool IsVerticalOrientation() const
Definition: sch_sheet.cpp:284
void SwapData(SCH_ITEM *aItem) override
Function SwapData swap the internal data structures aItem with the schematic item.
Definition: sch_text.cpp:247
EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
bool operator==(const SCH_SHEET_PIN *aPin) const
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:166
void SwapData(SCH_ITEM *aItem) override
Function SwapData swap the internal data structures aItem with the schematic item.
virtual wxString GetClass() const override
Function GetClass returns the class name.
Definition: sch_item.h:174
const wxPoint & GetTextPos() const
Definition: eda_text.h:232
void Rotate(wxPoint aPosition) override
Function Rotate rotates the item around aPosition 90 degrees in the clockwise direction.
wxString GetSelectMenuText(EDA_UNITS aUnits) const override
Function GetSelectMenuText returns the text to display to be used in the selection clarification cont...
PINSHEETLABEL_SHAPE m_shape
Definition: sch_text.h:180
DANGLING_END_ITEM is a helper class used to store the state of schematic items that can be connected ...
Definition: sch_item.h:76
SCH_ITEM is a base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:147
int GetPenSize() const override
Function GetPenSize virtual pure.
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:123
SHEET_SIDE m_edge
Definition: sch_sheet.h:92
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
void SetTextY(int aY)
Definition: eda_text.h:235
KICAD_T Type() const
Function Type()
Definition: base_struct.h:212