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-2024 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 SCH_RENDER_SETTINGS* aSettings, int aUnit, int aBodyStyle,
69 const VECTOR2I& aOffset, bool aForceNoFill, bool aDimmed )
70{
71 // The icon selection is handle by the virtual method CreateGraphicShape called by ::Print
72 SCH_HIERLABEL::Print( aSettings, aUnit, aBodyStyle, aOffset, aForceNoFill, aDimmed );
73}
74
75
77{
79
80 wxCHECK_RET( aItem->Type() == SCH_SHEET_PIN_T,
81 wxString::Format( "SCH_SHEET_PIN object cannot swap data with %s object.",
82 aItem->GetClass() ) );
83
84 SCH_SHEET_PIN* pin = static_cast<SCH_SHEET_PIN*>( aItem );
85
86 std::swap( m_number, pin->m_number );
87 std::swap( m_edge, pin->m_edge );
88}
89
90
92{
93 return aPin == this;
94}
95
96
98{
99 if( Schematic() )
101
103}
104
105
106void SCH_SHEET_PIN::SetNumber( int aNumber )
107{
108 wxASSERT( aNumber >= 2 );
109
110 m_number = aNumber;
111}
112
113
115{
116 SCH_SHEET* Sheet = GetParent();
117
118 // use SHEET_UNDEFINED_SIDE to adjust text orientation without changing edge
119
120 switch( aEdge )
121 {
122 case SHEET_SIDE::LEFT:
123 m_edge = aEdge;
124 SetTextX( Sheet->m_pos.x );
125 SetSpinStyle( SPIN_STYLE::RIGHT ); // Orientation horiz inverse
126 break;
127
128 case SHEET_SIDE::RIGHT:
129 m_edge = aEdge;
130 SetTextX( Sheet->m_pos.x + Sheet->m_size.x );
131 SetSpinStyle( SPIN_STYLE::LEFT ); // Orientation horiz normal
132 break;
133
134 case SHEET_SIDE::TOP:
135 m_edge = aEdge;
136 SetTextY( Sheet->m_pos.y );
137 SetSpinStyle( SPIN_STYLE::BOTTOM ); // Orientation vert BOTTOM
138 break;
139
140 case SHEET_SIDE::BOTTOM:
141 m_edge = aEdge;
142 SetTextY( Sheet->m_pos.y + Sheet->m_size.y );
143 SetSpinStyle( SPIN_STYLE::UP ); // Orientation vert UP
144 break;
145
146 default:
147 break;
148 }
149}
150
151
153{
154 return m_edge;
155}
156
157
158void SCH_SHEET_PIN::ConstrainOnEdge( VECTOR2I aPos, bool aAllowEdgeSwitch )
159{
160 SCH_SHEET* sheet = GetParent();
161
162 if( sheet == nullptr )
163 return;
164
165 int leftSide = sheet->m_pos.x;
166 int rightSide = sheet->m_pos.x + sheet->m_size.x;
167 int topSide = sheet->m_pos.y;
168 int botSide = sheet->m_pos.y + sheet->m_size.y;
169
170 SHAPE_LINE_CHAIN sheetEdge;
171
172 sheetEdge.Append( leftSide, topSide );
173 sheetEdge.Append( rightSide, topSide );
174 sheetEdge.Append( rightSide, botSide );
175 sheetEdge.Append( leftSide, botSide );
176 sheetEdge.Append( leftSide, topSide );
177
178 if( aAllowEdgeSwitch )
179 {
180 switch( sheetEdge.NearestSegment( aPos ) )
181 {
182 case 0: SetSide( SHEET_SIDE::TOP ); break;
183 case 1: SetSide( SHEET_SIDE::RIGHT ); break;
184 case 2: SetSide( SHEET_SIDE::BOTTOM ); break;
185 case 3: SetSide( SHEET_SIDE::LEFT ); break;
186 default: wxASSERT( "Invalid segment number" );
187 }
188 }
189 else
190 {
191 SetSide( GetSide() );
192 }
193
194 switch( GetSide() )
195 {
196 case SHEET_SIDE::RIGHT:
197 case SHEET_SIDE::LEFT:
198 SetTextY( aPos.y );
199
200 if( GetTextPos().y < topSide )
201 SetTextY( topSide );
202
203 if( GetTextPos().y > botSide )
204 SetTextY( botSide );
205
206 break;
207
208 case SHEET_SIDE::BOTTOM:
209 case SHEET_SIDE::TOP:
210 SetTextX( aPos.x );
211
212 if( GetTextPos().x < leftSide )
213 SetTextX( leftSide );
214
215 if( GetTextPos().x > rightSide )
216 SetTextX( rightSide );
217
218 break;
219
220 case SHEET_SIDE::UNDEFINED:
221 wxASSERT( "Undefined sheet side" );
222 }
223}
224
225
227{
228 int p = GetTextPos().y - aCenter;
229
230 SetTextY( aCenter - p );
231
232 switch( m_edge )
233 {
234 case SHEET_SIDE::TOP: SetSide( SHEET_SIDE::BOTTOM ); break;
235 case SHEET_SIDE::BOTTOM: SetSide( SHEET_SIDE::TOP ); break;
236 default: break;
237 }
238}
239
240
242{
243 int p = GetTextPos().x - aCenter;
244
245 SetTextX( aCenter - p );
246
247 switch( m_edge )
248 {
249 case SHEET_SIDE::LEFT: SetSide( SHEET_SIDE::RIGHT ); break;
250 case SHEET_SIDE::RIGHT: SetSide( SHEET_SIDE::LEFT ); break;
251 default: break;
252 }
253}
254
255
256void SCH_SHEET_PIN::Rotate( const VECTOR2I& aCenter, bool aRotateCCW )
257{
258 VECTOR2I pt = GetTextPos();
259 VECTOR2I delta = pt - aCenter;
260
261 RotatePoint( pt, aCenter, aRotateCCW ? ANGLE_90 : ANGLE_270 );
262
263 SHEET_SIDE oldSide = GetSide();
264 ConstrainOnEdge( pt, true );
265
266 // If the new side is the same as the old side, instead mirror across the center of that side.
267 if( GetSide() == oldSide )
268 {
269 switch( GetSide() )
270 {
271 case SHEET_SIDE::TOP:
272 case SHEET_SIDE::BOTTOM:
273 SetTextPos( VECTOR2I( aCenter.x - delta.x, GetTextPos().y ) );
274 break;
275
276 case SHEET_SIDE::LEFT:
277 case SHEET_SIDE::RIGHT:
278 SetTextPos( VECTOR2I( GetTextPos().x, aCenter.y - delta.y ) );
279 break;
280
281 default:
282 break;
283 }
284 }
285 // If the new side is opposite to the old side, instead mirror across the center of an adjacent
286 // side.
287 else if( GetSide() == GetOppositeSide( oldSide ) )
288 {
289 switch( GetSide() )
290 {
291 case SHEET_SIDE::TOP:
292 case SHEET_SIDE::BOTTOM:
293 SetTextPos( VECTOR2I( aCenter.x + delta.x, GetTextPos().y ) );
294 break;
295
296 case SHEET_SIDE::LEFT:
297 case SHEET_SIDE::RIGHT:
298 SetTextPos( VECTOR2I( GetTextPos().x, aCenter.y + delta.y ) );
299 break;
300
301 default:
302 break;
303 }
304 }
305}
306
307
309 std::vector<VECTOR2I>& aPoints, const VECTOR2I& aPos ) const
310{
311 /*
312 * These are the same icon shapes as SCH_HIERLABEL but the graphic icon is slightly
313 * different in 2 cases:
314 * for INPUT type the icon is the OUTPUT shape of SCH_HIERLABEL
315 * for OUTPUT type the icon is the INPUT shape of SCH_HIERLABEL
316 */
318
319 switch( shape )
320 {
321 case LABEL_FLAG_SHAPE::L_INPUT: shape = LABEL_FLAG_SHAPE::L_OUTPUT; break;
322 case LABEL_FLAG_SHAPE::L_OUTPUT: shape = LABEL_FLAG_SHAPE::L_INPUT; break;
323 default: break;
324 }
325
326 SCH_HIERLABEL::CreateGraphicShape( aSettings, aPoints, aPos, shape );
327}
328
329
330void SCH_SHEET_PIN::GetEndPoints( std::vector<DANGLING_END_ITEM>& aItemList )
331{
333 aItemList.push_back( item );
334}
335
336
337wxString SCH_SHEET_PIN::GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const
338{
339 return wxString::Format( _( "Hierarchical Sheet Pin %s" ),
340 aFull ? GetShownText( false ) : KIUI::EllipsizeMenuText( GetText() ) );
341}
342
343
345{
346 return BITMAPS::add_hierar_pin;
347}
348
349
350bool SCH_SHEET_PIN::HitTest( const VECTOR2I& aPoint, int aAccuracy ) const
351{
352 BOX2I rect = GetBoundingBox();
353
354 rect.Inflate( aAccuracy );
355
356 return rect.Contains( aPoint );
357}
358
359
360bool SCH_SHEET_PIN::operator==( const SCH_ITEM& aOther ) const
361{
362 if( aOther.Type() != Type() )
363 return false;
364
365 const SCH_SHEET_PIN* other = static_cast<const SCH_SHEET_PIN*>( &aOther );
366
367 return m_edge == other->m_edge && m_number == other->m_number
368 && SCH_HIERLABEL::operator==( aOther );
369}
370
371
372double SCH_SHEET_PIN::Similarity( const SCH_ITEM& aOther ) const
373{
374 if( aOther.Type() != Type() )
375 return 0.0;
376
377 const SCH_SHEET_PIN* other = static_cast<const SCH_SHEET_PIN*>( &aOther );
378
379 double similarity = 1.0;
380
381 if( m_edge != other->m_edge )
382 similarity *= 0.9;
383
384 if( m_number != other->m_number )
385 similarity *= 0.9;
386
387 similarity *= SCH_HIERLABEL::Similarity( aOther );
388
389 return similarity;
390}
391
392
394 const SCH_SHEET_PATH* aInstance ) const
395{
396 // Do not compare to ourself.
397 if( aItem == this )
398 return false;
399
400 const SCH_SHEET_PIN* pin = dynamic_cast<const SCH_SHEET_PIN*>( aItem );
401
402 // Don't compare against a different SCH_ITEM.
403 wxCHECK( pin, false );
404
405 if( GetPosition() != pin->GetPosition() )
406 return true;
407
408 return GetText() != pin->GetText();
409}
410
411
412#if defined(DEBUG)
413
414void SCH_SHEET_PIN::Show( int nestLevel, std::ostream& os ) const
415{
416 // XML output:
417 NestedSpace( nestLevel, os ) << '<' << GetClass().Lower().mb_str() << ">"
418 << " pin_name=\"" << TO_UTF8( GetText() )
419 << '"' << "/>\n" << std::flush;
420}
421
422#endif
423
424
426{
428 {
435
437 }
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:110
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:33
bool Contains(const Vec &aPoint) const
Definition: box2.h:158
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:541
Helper class used to store the state of schematic items that can be connected to other schematic item...
Definition: sch_item.h:95
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:88
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:100
virtual void SetParent(EDA_ITEM *aParent)
Definition: eda_item.h:103
const VECTOR2I & GetTextPos() const
Definition: eda_text.h:234
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:98
void SetTextPos(const VECTOR2I &aPoint)
Definition: eda_text.cpp:417
void SetTextX(int aX)
Definition: eda_text.cpp:423
void SetTextY(int aY)
Definition: eda_text.cpp:429
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:85
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:87
void AddTypeCast(TYPE_CAST_BASE *aCast)
Register a type converter.
SCHEMATIC_SETTINGS & Settings() const
Definition: schematic.cpp:294
void SetSpinStyle(SPIN_STYLE aSpinStyle) override
Definition: sch_label.cpp:2060
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:2067
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:166
virtual void Print(const SCH_RENDER_SETTINGS *aSettings, int aUnit, int aBodyStyle, const VECTOR2I &aOffset, bool aForceNoFill, bool aDimmed)
Print an item.
Definition: sch_item.h:594
SCHEMATIC * Schematic() const
Searches the item hierarchy to find a SCHEMATIC.
Definition: sch_item.cpp:150
virtual double Similarity(const SCH_ITEM &aItem) const
Return a measure of how likely the other object is to represent the same object.
Definition: sch_item.h:306
virtual bool operator==(const SCH_ITEM &aOther) const
Definition: sch_item.cpp:395
virtual void SwapData(SCH_ITEM *aItem)
Swap the internal data structures aItem with the schematic item.
Definition: sch_item.cpp:348
wxString GetClass() const override
Return the class name.
Definition: sch_item.h:176
SCH_LAYER_ID m_layer
Definition: sch_item.h:723
wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const override
Definition: sch_label.cpp:867
bool m_isDangling
Definition: sch_label.h:370
const BOX2I GetBoundingBox() const override
Return the bounding box of the label including its fields.
Definition: sch_label.cpp:997
LABEL_FLAG_SHAPE m_shape
Definition: sch_label.h:367
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
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.
SCH_SHEET_PIN(SCH_SHEET *parent, const VECTOR2I &pos=VECTOR2I(0, 0), const wxString &text=wxEmptyString)
void Print(const SCH_RENDER_SETTINGS *aSettings, int aUnit, int aBodyStyle, const VECTOR2I &aOffset, bool aForceNoFill, bool aDimmed) override
Print an item.
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.
bool HasConnectivityChanges(const SCH_ITEM *aItem, const SCH_SHEET_PATH *aInstance=nullptr) const override
Check if aItem has connectivity changes against this object.
void MirrorVertically(int aCenter) override
Mirror item vertically about aCenter.
double Similarity(const SCH_ITEM &aOther) const override
Return a measure of how likely the other object is to represent the same object.
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:88
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.
bool operator==(const SCH_SHEET_PIN *aPin) const
wxString GetItemDescription(UNITS_PROVIDER *aUnitsProvider, bool aFull) const override
Return a user-visible description string of this item.
BITMAPS GetMenuImage() const override
Return a pointer to an image to be used in menus.
void Rotate(const VECTOR2I &aCenter, bool aRotateCCW) override
Rotate the item around aCenter 90 degrees in the clockwise direction.
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:544
VECTOR2I m_pos
Definition: sch_sheet.h:543
bool IsVerticalOrientation() const
Definition: sch_sheet.cpp:478
VECTOR2I GetPosition() const override
Definition: sch_text.h:141
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:403
static constexpr EDA_ANGLE ANGLE_270
Definition: eda_angle.h:406
@ LAYER_SHEETLABEL
Definition: layer_ids.h:380
KICOMMON_API wxString EllipsizeMenuText(const wxString &aString)
Ellipsize text (at the end) to be no more than 36 characters.
Definition: ui_common.cpp:213
#define TYPE_HASH(x)
Definition: property.h:71
#define REGISTER_TYPE(x)
Definition: property_mgr.h:371
@ SHEET_LABEL_END
Definition: sch_item.h:85
LABEL_FLAG_SHAPE
Definition: sch_label.h:98
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:391
constexpr int MilsToIU(int mils) const
Definition: base_units.h:93
constexpr int delta
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition: trigo.cpp:228
@ SCH_SHEET_PIN_T
Definition: typeinfo.h:173
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:673