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
41SCH_SHEET_PIN::SCH_SHEET_PIN( SCH_SHEET* parent, const VECTOR2I& 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
68void SCH_SHEET_PIN::Print( const RENDER_SETTINGS* aSettings, const VECTOR2I& 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( "SCH_SHEET_PIN object cannot swap data with %s object.",
79 aItem->GetClass() ) );
80
81 SCH_SHEET_PIN* pin = static_cast<SCH_SHEET_PIN*>( aItem );
82
84 std::swap( m_number, pin->m_number );
85 std::swap( m_edge, pin->m_edge );
86}
87
88
90{
91 return aPin == this;
92}
93
94
96{
97 if( Schematic() )
99
101}
102
103
104void 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_SIDE::LEFT:
121 m_edge = aEdge;
122 SetTextX( Sheet->m_pos.x );
123 SetTextSpinStyle( TEXT_SPIN_STYLE::RIGHT ); // Orientation horiz inverse
124 break;
125
127 m_edge = aEdge;
128 SetTextX( Sheet->m_pos.x + Sheet->m_size.x );
129 SetTextSpinStyle( TEXT_SPIN_STYLE::LEFT ); // Orientation horiz normal
130 break;
131
132 case SHEET_SIDE::TOP:
133 m_edge = aEdge;
134 SetTextY( Sheet->m_pos.y );
135 SetTextSpinStyle( TEXT_SPIN_STYLE::BOTTOM ); // Orientation vert BOTTOM
136 break;
137
139 m_edge = aEdge;
140 SetTextY( Sheet->m_pos.y + Sheet->m_size.y );
141 SetTextSpinStyle( TEXT_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 == nullptr )
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: SetSide( SHEET_SIDE::TOP ); break;
179 case 1: SetSide( SHEET_SIDE::RIGHT ); break;
180 case 2: SetSide( SHEET_SIDE::BOTTOM ); break;
181 case 3: SetSide( SHEET_SIDE::LEFT ); break;
182 default: wxASSERT( "Invalid segment number" );
183 }
184
185 switch( GetSide() )
186 {
188 case SHEET_SIDE::LEFT:
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
200 case SHEET_SIDE::TOP:
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 {
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 {
242 default: break;
243 }
244}
245
246
247void SCH_SHEET_PIN::Rotate( const VECTOR2I& aCenter )
248{
249 VECTOR2I pt = GetTextPos();
250 VECTOR2I delta = pt - aCenter;
251
252 RotatePoint( pt, aCenter, ANGLE_90 );
253
254 SHEET_SIDE oldSide = GetSide();
255 ConstrainOnEdge( pt );
256
257 // If the new side is the same as the old side, instead mirror across the center of that side.
258 if( GetSide() == oldSide )
259 {
260 switch( GetSide() )
261 {
262 case SHEET_SIDE::TOP:
264 SetTextPos( VECTOR2I( aCenter.x - delta.x, GetTextPos().y ) );
265 break;
266
267 case SHEET_SIDE::LEFT:
269 SetTextPos( VECTOR2I( GetTextPos().x, aCenter.y - delta.y ) );
270 break;
271
272 default:
273 break;
274 }
275 }
276 // If the new side is opposite to the old side, instead mirror across the center of an adjacent
277 // side.
278 else if( GetSide() == GetOppositeSide( oldSide ) )
279 {
280 switch( GetSide() )
281 {
282 case SHEET_SIDE::TOP:
284 SetTextPos( VECTOR2I( aCenter.x + delta.x, GetTextPos().y ) );
285 break;
286
287 case SHEET_SIDE::LEFT:
289 SetTextPos( VECTOR2I( GetTextPos().x, aCenter.y + delta.y ) );
290 break;
291
292 default:
293 break;
294 }
295 }
296}
297
298
300 std::vector<VECTOR2I>& aPoints, const VECTOR2I& aPos ) const
301{
302 /*
303 * These are the same icon shapes as SCH_HIERLABEL but the graphic icon is slightly
304 * different in 2 cases:
305 * for INPUT type the icon is the OUTPUT shape of SCH_HIERLABEL
306 * for OUTPUT type the icon is the INPUT shape of SCH_HIERLABEL
307 */
309
310 switch( shape )
311 {
314 default: break;
315 }
316
317 SCH_HIERLABEL::CreateGraphicShape( aSettings, aPoints, aPos, shape );
318}
319
320
321void SCH_SHEET_PIN::GetEndPoints( std::vector<DANGLING_END_ITEM>& aItemList )
322{
324 aItemList.push_back( item );
325}
326
327
328wxString SCH_SHEET_PIN::GetItemDescription( UNITS_PROVIDER* aUnitsProvider ) const
329{
330 return wxString::Format( _( "Hierarchical Sheet Pin %s" ),
332}
333
334
336{
338}
339
340
341bool SCH_SHEET_PIN::HitTest( const VECTOR2I& aPoint, int aAccuracy ) const
342{
343 BOX2I rect = GetBoundingBox();
344
345 rect.Inflate( aAccuracy );
346
347 return rect.Contains( aPoint );
348}
349
350
351#if defined(DEBUG)
352
353void SCH_SHEET_PIN::Show( int nestLevel, std::ostream& os ) const
354{
355 // XML output:
356 NestedSpace( nestLevel, os ) << '<' << GetClass().Lower().mb_str() << ">"
357 << " pin_name=\"" << TO_UTF8( GetText() )
358 << '"' << "/>\n" << std::flush;
359}
360
361#endif
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:111
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:33
@ add_hierar_pin
bool Contains(const Vec &aPoint) const
Definition: box2.h:141
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:506
Helper class used to store the state of schematic items that can be connected to other schematic item...
Definition: sch_item.h:82
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:97
virtual void SetParent(EDA_ITEM *aParent)
Definition: eda_item.h:100
const VECTOR2I & GetTextPos() const
Definition: eda_text.h:208
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:87
void SetTextPos(const VECTOR2I &aPoint)
Definition: eda_text.cpp:373
void SetTextX(int aX)
Definition: eda_text.cpp:379
void SetTextY(int aY)
Definition: eda_text.cpp:385
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
SCHEMATIC_SETTINGS & Settings() const
Definition: schematic.cpp:205
void CreateGraphicShape(const RENDER_SETTINGS *aSettings, std::vector< VECTOR2I > &aPoints, const VECTOR2I &aPos) const override
Calculate the graphic shape (a polygon) associated to the text.
Definition: sch_label.cpp:1572
void SetTextSpinStyle(TEXT_SPIN_STYLE aSpinStyle) override
Set a spin or rotation angle, along with specific horizontal and vertical justification styles with e...
Definition: sch_label.cpp:1565
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:147
virtual wxString GetClass() const override
Return the class name.
Definition: sch_item.h:157
SCHEMATIC * Schematic() const
Searches the item hierarchy to find a SCHEMATIC.
Definition: sch_item.cpp:112
SCH_LAYER_ID m_layer
Definition: sch_item.h:491
bool m_isDangling
Definition: sch_label.h:224
wxString GetShownText(int aDepth=0, bool aAllowExtraText=true) const override
Return the string actually shown after processing of the base text.
Definition: sch_label.cpp:598
void SwapData(SCH_ITEM *aItem) override
Swap the internal data structures aItem with the schematic item.
Definition: sch_label.cpp:241
const BOX2I GetBoundingBox() const override
Return the bounding box of the label including its fields.
Definition: sch_label.cpp:727
void Print(const RENDER_SETTINGS *aSettings, const VECTOR2I &offset) override
Print a schematic item.
Definition: sch_label.cpp:1017
LABEL_FLAG_SHAPE m_shape
Definition: sch_label.h:221
Define a sheet pin (label) used in sheets to create hierarchical schematics.
Definition: sch_sheet_pin.h:66
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
void SetNumber(int aNumber)
Set the sheet label number.
void Print(const RENDER_SETTINGS *aSettings, const VECTOR2I &aOffset) override
Print a schematic item.
SCH_SHEET_PIN(SCH_SHEET *parent, const VECTOR2I &pos=VECTOR2I(0, 0), const wxString &text=wxEmptyString)
SHEET_SIDE m_edge
int m_number
Label number use for saving sheet label to file.
void MirrorHorizontally(int aCenter) override
Mirror item horizontally about aCenter.
void MirrorVertically(int aCenter) override
Mirror item vertically about aCenter.
void ConstrainOnEdge(VECTOR2I Pos)
Adjust label position to edge based on proximity to vertical or horizontal edge of the parent sheet.
wxString GetItemDescription(UNITS_PROVIDER *aUnitsProvider) const override
Return a user-visible description string of this item.
int GetPenWidth() const override
static SHEET_SIDE GetOppositeSide(SHEET_SIDE aSide)
Definition: sch_sheet_pin.h:87
void CreateGraphicShape(const RENDER_SETTINGS *aSettings, std::vector< VECTOR2I > &aPoints, const VECTOR2I &aPos) const override
Calculate the graphic shape (a polygon) associated to the text.
void SwapData(SCH_ITEM *aItem) override
Swap the internal data structures aItem with the schematic item.
SHEET_SIDE GetSide() const
wxString GetClass() const override
Return the class name.
Definition: sch_sheet_pin.h:80
void GetEndPoints(std::vector< DANGLING_END_ITEM > &aItemList) override
Add the schematic item end points to aItemList if the item has end points.
void Rotate(const VECTOR2I &aCenter) override
Rotate the item around aCenter 90 degrees in the clockwise direction.
bool operator==(const SCH_SHEET_PIN *aPin) const
BITMAPS GetMenuImage() const override
Return a pointer to an image to be used in menus.
bool HitTest(const VECTOR2I &aPosition, int aAccuracy=0) const override
Test if aPosition is inside or on the boundary of this item.
SCH_SHEET * GetParent() const
Get the parent sheet object of this sheet pin.
void SetSide(SHEET_SIDE aEdge)
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:57
VECTOR2I m_size
Definition: sch_sheet.h:501
VECTOR2I m_pos
Definition: sch_sheet.h:500
bool IsVerticalOrientation() const
Definition: sch_sheet.cpp:421
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
int NearestSegment(const VECTOR2I &aP) const
Find the segment nearest the given point.
void Append(int aX, int aY, bool aAllowDuplication=false)
Append a new point at the end of the line chain.
#define DEFAULT_LINE_WIDTH_MILS
The default wire width in mils. (can be changed in preference menu)
#define _(s)
static constexpr EDA_ANGLE & ANGLE_90
Definition: eda_angle.h:431
@ LAYER_SHEETLABEL
Definition: layer_ids.h:366
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
wxString EllipsizeMenuText(const wxString &aString)
Ellipsize text (at the end) to be no more than 36 characters.
Definition: ui_common.cpp:215
Plot settings, and plotting engines (PostScript, Gerber, HPGL and DXF)
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
@ SHEET_LABEL_END
Definition: sch_item.h:72
SHEET_SIDE
Define the edge of the sheet that the sheet pin is positioned.
Definition: sch_sheet_pin.h:46
LABEL_FLAG_SHAPE
Definition: sch_text.h:96
@ L_OUTPUT
Definition: sch_text.h:98
@ L_INPUT
Definition: sch_text.h:97
constexpr int MilsToIU(int mils) const
Definition: base_units.h:94
constexpr int delta
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Definition: trigo.cpp:183
@ SCH_SHEET_PIN_T
Definition: typeinfo.h:157
VECTOR2< int > VECTOR2I
Definition: vector2d.h:590