KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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() )
52 SetSide( SHEET_SIDE::TOP );
53 else
54 SetSide( SHEET_SIDE::LEFT );
55
56 m_shape = LABEL_FLAG_SHAPE::L_INPUT;
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{
78
79 wxCHECK_RET( aItem->Type() == SCH_SHEET_PIN_T,
80 wxString::Format( "SCH_SHEET_PIN object cannot swap data with %s object.",
81 aItem->GetClass() ) );
82
83 SCH_SHEET_PIN* pin = static_cast<SCH_SHEET_PIN*>( aItem );
84
85 std::swap( m_number, pin->m_number );
86 std::swap( m_edge, pin->m_edge );
87}
88
89
91{
92 return aPin == this;
93}
94
95
97{
98 if( Schematic() )
100
102}
103
104
105void 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_SIDE::LEFT:
122 m_edge = aEdge;
123 SetTextX( Sheet->m_pos.x );
124 SetSpinStyle( SPIN_STYLE::RIGHT ); // Orientation horiz inverse
125 break;
126
127 case SHEET_SIDE::RIGHT:
128 m_edge = aEdge;
129 SetTextX( Sheet->m_pos.x + Sheet->m_size.x );
130 SetSpinStyle( SPIN_STYLE::LEFT ); // Orientation horiz normal
131 break;
132
133 case SHEET_SIDE::TOP:
134 m_edge = aEdge;
135 SetTextY( Sheet->m_pos.y );
136 SetSpinStyle( SPIN_STYLE::BOTTOM ); // Orientation vert BOTTOM
137 break;
138
139 case SHEET_SIDE::BOTTOM:
140 m_edge = aEdge;
141 SetTextY( Sheet->m_pos.y + Sheet->m_size.y );
142 SetSpinStyle( 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
157void SCH_SHEET_PIN::ConstrainOnEdge( VECTOR2I aPos, bool aAllowEdgeSwitch )
158{
159 SCH_SHEET* sheet = GetParent();
160
161 if( sheet == nullptr )
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 if( aAllowEdgeSwitch )
178 {
179 switch( sheetEdge.NearestSegment( aPos ) )
180 {
181 case 0: SetSide( SHEET_SIDE::TOP ); break;
182 case 1: SetSide( SHEET_SIDE::RIGHT ); break;
183 case 2: SetSide( SHEET_SIDE::BOTTOM ); break;
184 case 3: SetSide( SHEET_SIDE::LEFT ); break;
185 default: wxASSERT( "Invalid segment number" );
186 }
187 }
188 else
189 {
190 SetSide( GetSide() );
191 }
192
193 switch( GetSide() )
194 {
195 case SHEET_SIDE::RIGHT:
196 case SHEET_SIDE::LEFT:
197 SetTextY( aPos.y );
198
199 if( GetTextPos().y < topSide )
200 SetTextY( topSide );
201
202 if( GetTextPos().y > botSide )
203 SetTextY( botSide );
204
205 break;
206
207 case SHEET_SIDE::BOTTOM:
208 case SHEET_SIDE::TOP:
209 SetTextX( aPos.x );
210
211 if( GetTextPos().x < leftSide )
212 SetTextX( leftSide );
213
214 if( GetTextPos().x > rightSide )
215 SetTextX( rightSide );
216
217 break;
218
219 case SHEET_SIDE::UNDEFINED:
220 wxASSERT( "Undefined sheet side" );
221 }
222}
223
224
226{
227 int p = GetTextPos().y - aCenter;
228
229 SetTextY( aCenter - p );
230
231 switch( m_edge )
232 {
233 case SHEET_SIDE::TOP: SetSide( SHEET_SIDE::BOTTOM ); break;
234 case SHEET_SIDE::BOTTOM: SetSide( SHEET_SIDE::TOP ); break;
235 default: break;
236 }
237}
238
239
241{
242 int p = GetTextPos().x - aCenter;
243
244 SetTextX( aCenter - p );
245
246 switch( m_edge )
247 {
248 case SHEET_SIDE::LEFT: SetSide( SHEET_SIDE::RIGHT ); break;
249 case SHEET_SIDE::RIGHT: SetSide( SHEET_SIDE::LEFT ); break;
250 default: break;
251 }
252}
253
254
255void SCH_SHEET_PIN::Rotate( const VECTOR2I& aCenter )
256{
257 VECTOR2I pt = GetTextPos();
258 VECTOR2I delta = pt - aCenter;
259
260 RotatePoint( pt, aCenter, ANGLE_90 );
261
262 SHEET_SIDE oldSide = GetSide();
263 ConstrainOnEdge( pt, true );
264
265 // If the new side is the same as the old side, instead mirror across the center of that side.
266 if( GetSide() == oldSide )
267 {
268 switch( GetSide() )
269 {
270 case SHEET_SIDE::TOP:
271 case SHEET_SIDE::BOTTOM:
272 SetTextPos( VECTOR2I( aCenter.x - delta.x, GetTextPos().y ) );
273 break;
274
275 case SHEET_SIDE::LEFT:
276 case SHEET_SIDE::RIGHT:
277 SetTextPos( VECTOR2I( GetTextPos().x, aCenter.y - delta.y ) );
278 break;
279
280 default:
281 break;
282 }
283 }
284 // If the new side is opposite to the old side, instead mirror across the center of an adjacent
285 // side.
286 else if( GetSide() == GetOppositeSide( oldSide ) )
287 {
288 switch( GetSide() )
289 {
290 case SHEET_SIDE::TOP:
291 case SHEET_SIDE::BOTTOM:
292 SetTextPos( VECTOR2I( aCenter.x + delta.x, GetTextPos().y ) );
293 break;
294
295 case SHEET_SIDE::LEFT:
296 case SHEET_SIDE::RIGHT:
297 SetTextPos( VECTOR2I( GetTextPos().x, aCenter.y + delta.y ) );
298 break;
299
300 default:
301 break;
302 }
303 }
304}
305
306
308 std::vector<VECTOR2I>& aPoints, const VECTOR2I& aPos ) const
309{
310 /*
311 * These are the same icon shapes as SCH_HIERLABEL but the graphic icon is slightly
312 * different in 2 cases:
313 * for INPUT type the icon is the OUTPUT shape of SCH_HIERLABEL
314 * for OUTPUT type the icon is the INPUT shape of SCH_HIERLABEL
315 */
317
318 switch( shape )
319 {
320 case LABEL_FLAG_SHAPE::L_INPUT: shape = LABEL_FLAG_SHAPE::L_OUTPUT; break;
321 case LABEL_FLAG_SHAPE::L_OUTPUT: shape = LABEL_FLAG_SHAPE::L_INPUT; break;
322 default: break;
323 }
324
325 SCH_HIERLABEL::CreateGraphicShape( aSettings, aPoints, aPos, shape );
326}
327
328
329void SCH_SHEET_PIN::GetEndPoints( std::vector<DANGLING_END_ITEM>& aItemList )
330{
332 aItemList.push_back( item );
333}
334
335
336wxString SCH_SHEET_PIN::GetItemDescription( UNITS_PROVIDER* aUnitsProvider ) const
337{
338 return wxString::Format( _( "Hierarchical Sheet Pin %s" ),
340}
341
342
344{
345 return BITMAPS::add_hierar_pin;
346}
347
348
349bool SCH_SHEET_PIN::HitTest( const VECTOR2I& aPoint, int aAccuracy ) const
350{
351 BOX2I rect = GetBoundingBox();
352
353 rect.Inflate( aAccuracy );
354
355 return rect.Contains( aPoint );
356}
357
358
359#if defined(DEBUG)
360
361void SCH_SHEET_PIN::Show( int nestLevel, std::ostream& os ) const
362{
363 // XML output:
364 NestedSpace( nestLevel, os ) << '<' << GetClass().Lower().mb_str() << ">"
365 << " pin_name=\"" << TO_UTF8( GetText() )
366 << '"' << "/>\n" << std::flush;
367}
368
369#endif
370
371
373{
375 {
382
384 }
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:111
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:33
bool Contains(const Vec &aPoint) const
Definition: box2.h:142
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:507
Helper class used to store the state of schematic items that can be connected to other schematic item...
Definition: sch_item.h:85
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:219
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:95
void SetTextPos(const VECTOR2I &aPoint)
Definition: eda_text.cpp:398
void SetTextX(int aX)
Definition: eda_text.cpp:404
void SetTextY(int aY)
Definition: eda_text.cpp:410
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:74
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:76
void AddTypeCast(TYPE_CAST_BASE *aCast)
Register a type converter.
SCHEMATIC_SETTINGS & Settings() const
Definition: schematic.cpp:209
void SetSpinStyle(SPIN_STYLE aSpinStyle) override
Definition: sch_label.cpp:1855
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:1862
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:150
virtual void Print(const RENDER_SETTINGS *aSettings, const VECTOR2I &aOffset)=0
Print a schematic item.
virtual wxString GetClass() const override
Return the class name.
Definition: sch_item.h:160
SCHEMATIC * Schematic() const
Searches the item hierarchy to find a SCHEMATIC.
Definition: sch_item.cpp:113
virtual void SwapData(SCH_ITEM *aItem)
Swap the internal data structures aItem with the schematic item.
Definition: sch_item.cpp:261
SCH_LAYER_ID m_layer
Definition: sch_item.h:510
bool m_isDangling
Definition: sch_label.h:323
const BOX2I GetBoundingBox() const override
Return the bounding box of the label including its fields.
Definition: sch_label.cpp:961
LABEL_FLAG_SHAPE m_shape
Definition: sch_label.h:320
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.
wxString GetItemDescription(UNITS_PROVIDER *aUnitsProvider) const override
Return a user-visible description string of this item.
void ConstrainOnEdge(VECTOR2I aPos, bool aAllowEdgeSwitch)
Adjust label position to edge based on proximity to vertical or horizontal edge of the parent sheet.
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:511
VECTOR2I m_pos
Definition: sch_sheet.h:510
bool IsVerticalOrientation() const
Definition: sch_sheet.cpp:420
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:439
@ LAYER_SHEETLABEL
Definition: layer_ids.h:371
wxString EllipsizeMenuText(const wxString &aString)
Ellipsize text (at the end) to be no more than 36 characters.
Definition: ui_common.cpp:219
#define TYPE_HASH(x)
Definition: property.h:64
#define REGISTER_TYPE(x)
Definition: property_mgr.h:356
@ SHEET_LABEL_END
Definition: sch_item.h:75
LABEL_FLAG_SHAPE
Definition: sch_label.h:91
static struct SCH_SHEET_PIN_DESC _SCH_SHEET_PIN_DESC
SHEET_SIDE
Define the edge of the sheet that the sheet pin is positioned.
Definition: sch_sheet_pin.h:46
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: string_utils.h:378
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:156
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588