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 <sch_painter.h>
39 #include <trigo.h>
40 
41 
42 SCH_SHEET_PIN::SCH_SHEET_PIN( SCH_SHEET* parent, const wxPoint& pos, const wxString& text ) :
43  SCH_HIERLABEL( pos, text, SCH_SHEET_PIN_T ),
44  m_edge( SHEET_UNDEFINED_SIDE )
45 {
46  SetParent( parent );
47  wxASSERT( parent );
49 
50  SetTextPos( pos );
51 
52  if( parent->IsVerticalOrientation() )
54  else
56 
58  m_isDangling = true;
59  m_number = 2;
60 }
61 
62 
64 {
65  return new SCH_SHEET_PIN( *this );
66 }
67 
68 
69 void SCH_SHEET_PIN::Print( RENDER_SETTINGS* aSettings, const wxPoint& aOffset )
70 {
71  // The icon selection is handle by the virtual method CreateGraphicShape called by ::Print
72  SCH_HIERLABEL::Print( aSettings, aOffset );
73 }
74 
75 
77 {
78  wxCHECK_RET( aItem->Type() == SCH_SHEET_PIN_T,
79  wxString::Format( wxT( "SCH_SHEET_PIN object cannot swap data with %s object." ),
80  GetChars( aItem->GetClass() ) ) );
81 
82  SCH_SHEET_PIN* pin = ( SCH_SHEET_PIN* ) aItem;
83  SCH_TEXT::SwapData( (SCH_TEXT*) pin );
84  int tmp = pin->GetNumber();
85  pin->SetNumber( GetNumber() );
86  SetNumber( tmp );
87  SHEET_SIDE stmp = pin->GetEdge();
88  pin->SetEdge( GetEdge() );
89  SetEdge( stmp );
90 }
91 
92 
93 bool SCH_SHEET_PIN::operator==( const SCH_SHEET_PIN* aPin ) const
94 {
95  return aPin == this;
96 }
97 
98 
100 {
101  return 1;
102 }
103 
104 
105 void SCH_SHEET_PIN::SetNumber( int aNumber )
106 {
107  wxASSERT( aNumber >= 2 );
108 
109  m_number = aNumber;
110 }
111 
112 
114 {
115  SCH_SHEET* Sheet = GetParent();
116 
117  // use SHEET_UNDEFINED_SIDE to adjust text orientation without changing edge
118 
119  switch( aEdge )
120  {
121  case SHEET_LEFT_SIDE:
122  m_edge = aEdge;
123  SetTextX( Sheet->m_pos.x );
124  SetLabelSpinStyle( LABEL_SPIN_STYLE::RIGHT ); // Orientation horiz inverse
125  break;
126 
127  case SHEET_RIGHT_SIDE:
128  m_edge = aEdge;
129  SetTextX( Sheet->m_pos.x + Sheet->m_size.x );
130  SetLabelSpinStyle( LABEL_SPIN_STYLE::LEFT ); // Orientation horiz normal
131  break;
132 
133  case SHEET_TOP_SIDE:
134  m_edge = aEdge;
135  SetTextY( Sheet->m_pos.y );
136  SetLabelSpinStyle( LABEL_SPIN_STYLE::BOTTOM ); // Orientation vert BOTTOM
137  break;
138 
139  case SHEET_BOTTOM_SIDE:
140  m_edge = aEdge;
141  SetTextY( Sheet->m_pos.y + Sheet->m_size.y );
142  SetLabelSpinStyle( LABEL_SPIN_STYLE::UP ); // Orientation vert UP
143  break;
144 
145  default:
146  break;
147  }
148 }
149 
150 
152 {
153  return m_edge;
154 }
155 
156 
158 {
159  SCH_SHEET* sheet = GetParent();
160 
161  if( sheet == NULL )
162  return;
163 
164  int leftSide = sheet->m_pos.x;
165  int rightSide = sheet->m_pos.x + sheet->m_size.x;
166  int topSide = sheet->m_pos.y;
167  int botSide = sheet->m_pos.y + sheet->m_size.y;
168 
169  SHAPE_LINE_CHAIN sheetEdge;
170 
171  sheetEdge.Append( leftSide, topSide );
172  sheetEdge.Append( rightSide, topSide );
173  sheetEdge.Append( rightSide, botSide );
174  sheetEdge.Append( leftSide, botSide );
175  sheetEdge.Append( leftSide, topSide );
176 
177  switch( sheetEdge.NearestSegment( Pos ) )
178  {
179  case 0: SetEdge( SHEET_TOP_SIDE ); break;
180  case 1: SetEdge( SHEET_RIGHT_SIDE ); break;
181  case 2: SetEdge( SHEET_BOTTOM_SIDE ); break;
182  case 3: SetEdge( SHEET_LEFT_SIDE ); break;
183  default: wxASSERT( "Invalid segment number" );
184  }
185 
186  switch( GetEdge() )
187  {
188  case SHEET_RIGHT_SIDE:
189  case SHEET_LEFT_SIDE:
190  SetTextY( Pos.y );
191 
192  if( GetTextPos().y < topSide )
193  SetTextY( topSide );
194 
195  if( GetTextPos().y > botSide )
196  SetTextY( botSide );
197 
198  break;
199 
200  case SHEET_BOTTOM_SIDE:
201  case SHEET_TOP_SIDE:
202  SetTextX( Pos.x );
203 
204  if( GetTextPos().x < leftSide )
205  SetTextX( leftSide );
206 
207  if( GetTextPos().x > rightSide )
208  SetTextX( rightSide );
209 
210  break;
211 
213  wxASSERT( "Undefined sheet side" );
214  }
215 }
216 
217 
218 void SCH_SHEET_PIN::MirrorX( int aXaxis_position )
219 {
220  int p = GetTextPos().y - aXaxis_position;
221 
222  SetTextY( aXaxis_position - p );
223 
224  switch( m_edge )
225  {
226  case SHEET_TOP_SIDE: SetEdge( SHEET_BOTTOM_SIDE ); break;
227  case SHEET_BOTTOM_SIDE: SetEdge( SHEET_TOP_SIDE ); break;
228  default: break;
229  }
230 }
231 
232 
233 void SCH_SHEET_PIN::MirrorY( int aYaxis_position )
234 {
235  int p = GetTextPos().x - aYaxis_position;
236 
237  SetTextX( aYaxis_position - p );
238 
239  switch( m_edge )
240  {
241  case SHEET_LEFT_SIDE: SetEdge( SHEET_RIGHT_SIDE ); break;
242  case SHEET_RIGHT_SIDE: SetEdge( SHEET_LEFT_SIDE ); break;
243  default: break;
244  }
245 }
246 
247 
249 {
250  wxPoint pt = GetTextPos();
251  RotatePoint( &pt, aPosition, 900 );
252  ConstrainOnEdge( pt );
253 }
254 
255 
257  std::vector<wxPoint>& aPoints, const wxPoint& aPos )
258 {
259  /*
260  * These are the same icon shapes as SCH_HIERLABEL but the graphic icon is slightly
261  * different in 2 cases:
262  * for INPUT type the icon is the OUTPUT shape of SCH_HIERLABEL
263  * for OUTPUT type the icon is the INPUT shape of SCH_HIERLABEL
264  */
266 
267  switch( m_shape )
268  {
271  default: break;
272  }
273 
274  SCH_HIERLABEL::CreateGraphicShape( aRenderSettings, aPoints, aPos );
275  m_shape = tmp;
276 }
277 
278 
279 void SCH_SHEET_PIN::GetEndPoints( std::vector <DANGLING_END_ITEM>& aItemList )
280 {
282  aItemList.push_back( item );
283 }
284 
285 
287 {
288  return wxString::Format( _( "Hierarchical Sheet Pin %s" ), ShortenedShownText() );
289 }
290 
291 
293 {
294  return add_hierar_pin_xpm;
295 }
296 
297 
298 bool SCH_SHEET_PIN::HitTest( const wxPoint& aPoint, int aAccuracy ) const
299 {
300  EDA_RECT rect = GetBoundingBox();
301 
302  rect.Inflate( aAccuracy );
303 
304  return rect.Contains( aPoint );
305 }
306 
307 
308 #if defined(DEBUG)
309 
310 void SCH_SHEET_PIN::Show( int nestLevel, std::ostream& os ) const
311 {
312  // XML output:
313  NestedSpace( nestLevel, os ) << '<' << GetClass().Lower().mb_str() << ">"
314  << " pin_name=\"" << TO_UTF8( GetText() )
315  << '"' << "/>\n" << std::flush;
316 }
317 
318 #endif
EDA_UNITS
Definition: common.h:198
SCH_LAYER_ID m_Layer
Definition: sch_item.h:199
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:168
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.
RENDER_SETTINGS Contains all the knowledge about how graphical objects are drawn on any output surfac...
SCH_SHEET_PIN(SCH_SHEET *parent, const wxPoint &pos=wxPoint(0, 0), const wxString &text=wxEmptyString)
void MirrorY(int aYaxis_position) override
Mirror 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:173
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:247
wxSize m_size
Definition: sch_sheet.h:230
wxString GetClass() const override
Function GetClass returns the class name.
Definition: sch_sheet.h:108
void Print(RENDER_SETTINGS *aSettings, const wxPoint &offset) override
Print a schematic item.
Definition: sch_text.cpp:1104
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
Mirror item relative to the X axis about aXaxis_position.
void Print(RENDER_SETTINGS *aSettings, const wxPoint &aOffset) override
Print a schematic item.
wxPoint m_pos
Definition: sch_sheet.h:229
bool Contains(const wxPoint &aPoint) const
Function Contains.
virtual void SetParent(EDA_ITEM *aParent)
Definition: base_struct.h:196
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
void SetTextX(int aX)
Definition: eda_text.h:250
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:100
#define NULL
void SetEdge(SHEET_SIDE aEdge)
wxString ShortenedShownText() const
Returns a shortened version (max 15 characters) of the shown text.
Definition: eda_text.cpp:201
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:1063
const EDA_RECT GetBoundingBox() const override
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes.
Definition: sch_text.cpp:1148
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:143
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:216
int NearestSegment(const VECTOR2I &aP) const
Find the segment nearest the given point.
PINSHEETLABEL_SHAPE
Definition: sch_text.h:152
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:153
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:201
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:87
#define _(s)
Definition: 3d_actions.cpp:33
SHAPE_LINE_CHAIN.
bool IsVerticalOrientation() const
Definition: sch_sheet.cpp:330
void SwapData(SCH_ITEM *aItem) override
Swap the internal data structures aItem with the schematic item.
Definition: sch_text.cpp:270
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:159
void SwapData(SCH_ITEM *aItem) override
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:221
const wxPoint & GetTextPos() const
Definition: eda_text.h:248
void Rotate(wxPoint aPosition) override
Rotate 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...
void CreateGraphicShape(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:1124
PINSHEETLABEL_SHAPE m_shape
Definition: sch_text.h:169
Helper class used to store the state of schematic items that can be connected to other schematic item...
Definition: sch_item.h:85
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:194
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:127
void CreateGraphicShape(RENDER_SETTINGS *aSettings, std::vector< wxPoint > &aPoints, const wxPoint &aPos) override
Calculate the graphic shape (a polygon) associated to the text.
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:251
KICAD_T Type() const
Function Type()
Definition: base_struct.h:193