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 The 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, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <algorithm>
22
23#include <bitmaps.h>
24#include <api/api_enums.h>
25#include <api/api_utils.h>
26#include <api/schematic/schematic_types.pb.h>
27#include <general.h>
29#include <string_utils.h>
30#include <plotters/plotter.h>
31#include <sch_draw_panel.h>
32#include <sch_edit_frame.h>
33#include <sch_sheet.h>
34#include <sch_sheet_pin.h>
35#include <sch_painter.h>
36#include <schematic.h>
37#include <trigo.h>
38#include <properties/property.h>
40
41
42SCH_SHEET_PIN::SCH_SHEET_PIN( SCH_SHEET* parent, const VECTOR2I& pos, const wxString& text ) :
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
63void SCH_SHEET_PIN::Serialize( kiapi::schematic::types::SheetPin& pin ) const
64{
65 using namespace kiapi::schematic::types;
66
67
68 pin.mutable_id()->set_value( m_Uuid.AsStdString() );
69 kiapi::common::PackVector2( *pin.mutable_position(), GetPosition(), schIUScale );
70 pin.set_spin_style( ToProtoEnum<SPIN_STYLE::SPIN,
71 SchematicLabelSpinStyle>( static_cast<SPIN_STYLE::SPIN>( (int) GetSpinStyle() ) ) );
74 pin.set_locked( SCH_ITEM::IsLocked() ? kiapi::common::types::LockedState::LS_LOCKED
75 : kiapi::common::types::LockedState::LS_UNLOCKED );
76
77 EDA_TEXT::Serialize( *pin.mutable_text(), schIUScale );
78
79}
80
81void SCH_SHEET_PIN::Serialize( google::protobuf::Any& aContainer ) const
82{
83 kiapi::schematic::types::SheetPin pin;
84 Serialize( pin );
85 aContainer.PackFrom( pin );
86}
87
88
89bool SCH_SHEET_PIN::Deserialize( const kiapi::schematic::types::SheetPin& pin )
90{
91 using namespace kiapi::schematic::types;
92
93 const_cast<KIID&>( m_Uuid ) = KIID( pin.id().value() );
94
95 if( !EDA_TEXT::Deserialize( pin.text(), schIUScale ) )
96 return false;
97
102 SetLocked( pin.locked() == kiapi::common::types::LockedState::LS_LOCKED );
103 return true;
104}
105
106bool SCH_SHEET_PIN::Deserialize( const google::protobuf::Any& aContainer )
107{
108 kiapi::schematic::types::SheetPin pin;
109
110 if( !aContainer.UnpackTo( &pin ) )
111 return false;
112
113 return Deserialize( pin );
114}
115
116
118{
119 return new SCH_SHEET_PIN( *this );
120}
121
122
124{
126
127 wxCHECK_RET( aItem->Type() == SCH_SHEET_PIN_T,
128 wxString::Format( "SCH_SHEET_PIN object cannot swap data with %s object.", aItem->GetClass() ) );
129
130 SCH_SHEET_PIN* pin = static_cast<SCH_SHEET_PIN*>( aItem );
131
132 std::swap( m_number, pin->m_number );
133 std::swap( m_edge, pin->m_edge );
134}
135
136
138{
139 if( Schematic() )
141
142 return schIUScale.MilsToIU( DEFAULT_LINE_WIDTH_MILS );
143}
144
145
146void SCH_SHEET_PIN::SetNumber( int aNumber )
147{
148 wxASSERT( aNumber >= 2 );
149
150 m_number = aNumber;
151}
152
153
155{
156 SCH_SHEET* sheet = GetParent();
157
158 // use SHEET_UNDEFINED_SIDE to adjust text orientation without changing edge
159
160 switch( aEdge )
161 {
162 case SHEET_SIDE::LEFT:
163 m_edge = aEdge;
164 SetTextX( sheet->m_pos.x );
165 SetSpinStyle( SPIN_STYLE::RIGHT ); // Orientation horiz inverse
166 break;
167
169 m_edge = aEdge;
170 SetTextX( sheet->m_pos.x + sheet->m_size.x );
171 SetSpinStyle( SPIN_STYLE::LEFT ); // Orientation horiz normal
172 break;
173
174 case SHEET_SIDE::TOP:
175 m_edge = aEdge;
176 SetTextY( sheet->m_pos.y );
177 SetSpinStyle( SPIN_STYLE::BOTTOM ); // Orientation vert BOTTOM
178 break;
179
181 m_edge = aEdge;
182 SetTextY( sheet->m_pos.y + sheet->m_size.y );
183 SetSpinStyle( SPIN_STYLE::UP ); // Orientation vert UP
184 break;
185
186 default:
187 break;
188 }
189}
190
191
193{
194 return m_edge;
195}
196
197
198void SCH_SHEET_PIN::ConstrainOnEdge( const VECTOR2I& aPos, bool aAllowEdgeSwitch )
199{
200 SCH_SHEET* sheet = GetParent();
201
202 if( sheet == nullptr )
203 return;
204
205 int leftSide = sheet->m_pos.x;
206 int rightSide = sheet->m_pos.x + sheet->m_size.x;
207 int topSide = sheet->m_pos.y;
208 int botSide = sheet->m_pos.y + sheet->m_size.y;
209
210 SHAPE_LINE_CHAIN sheetEdge;
211
212 sheetEdge.Append( leftSide, topSide );
213 sheetEdge.Append( rightSide, topSide );
214 sheetEdge.Append( rightSide, botSide );
215 sheetEdge.Append( leftSide, botSide );
216 sheetEdge.Append( leftSide, topSide );
217
218 if( aAllowEdgeSwitch )
219 {
220 switch( sheetEdge.NearestSegment( aPos ) )
221 {
222 case 0: SetSide( SHEET_SIDE::TOP ); break;
223 case 1: SetSide( SHEET_SIDE::RIGHT ); break;
224 case 2: SetSide( SHEET_SIDE::BOTTOM ); break;
225 case 3: SetSide( SHEET_SIDE::LEFT ); break;
226 default: wxASSERT( "Invalid segment number" );
227 }
228 }
229 else
230 {
231 SetSide( GetSide() );
232 }
233
234 switch( GetSide() )
235 {
237 case SHEET_SIDE::LEFT:
238 SetTextY( aPos.y );
239
240 if( GetTextPos().y < topSide )
241 SetTextY( topSide );
242
243 if( GetTextPos().y > botSide )
244 SetTextY( botSide );
245
246 break;
247
249 case SHEET_SIDE::TOP:
250 SetTextX( aPos.x );
251
252 if( GetTextPos().x < leftSide )
253 SetTextX( leftSide );
254
255 if( GetTextPos().x > rightSide )
256 SetTextX( rightSide );
257
258 break;
259
261 wxASSERT( "Undefined sheet side" );
262 }
263}
264
265
267{
268 int p = GetTextPos().y - aCenter;
269
270 SetTextY( aCenter - p );
271
272 switch( m_edge )
273 {
276 default: break;
277 }
278}
279
280
282{
283 int p = GetTextPos().x - aCenter;
284
285 SetTextX( aCenter - p );
286
287 switch( m_edge )
288 {
291 default: break;
292 }
293}
294
295
296void SCH_SHEET_PIN::Rotate( const VECTOR2I& aCenter, bool aRotateCCW )
297{
298 VECTOR2I pt = GetTextPos();
299 VECTOR2I delta = pt - aCenter;
300
301 RotatePoint( pt, aCenter, aRotateCCW ? ANGLE_90 : ANGLE_270 );
302
303 SHEET_SIDE oldSide = GetSide();
304 ConstrainOnEdge( pt, true );
305
306 // If the new side is the same as the old side, instead mirror across the center of that side.
307 if( GetSide() == oldSide )
308 {
309 switch( GetSide() )
310 {
311 case SHEET_SIDE::TOP:
313 SetTextPos( VECTOR2I( aCenter.x - delta.x, GetTextPos().y ) );
314 break;
315
316 case SHEET_SIDE::LEFT:
318 SetTextPos( VECTOR2I( GetTextPos().x, aCenter.y - delta.y ) );
319 break;
320
321 default:
322 break;
323 }
324 }
325 // If the new side is opposite to the old side, instead mirror across the center of an adjacent
326 // side.
327 else if( GetSide() == GetOppositeSide( oldSide ) )
328 {
329 switch( GetSide() )
330 {
331 case SHEET_SIDE::TOP:
333 SetTextPos( VECTOR2I( aCenter.x + delta.x, GetTextPos().y ) );
334 break;
335
336 case SHEET_SIDE::LEFT:
338 SetTextPos( VECTOR2I( GetTextPos().x, aCenter.y + delta.y ) );
339 break;
340
341 default:
342 break;
343 }
344 }
345}
346
347
348void SCH_SHEET_PIN::CreateGraphicShape( const RENDER_SETTINGS* aSettings, std::vector<VECTOR2I>& aPoints,
349 const VECTOR2I& aPos ) const
350{
351 /*
352 * These are the same icon shapes as SCH_HIERLABEL but the graphic icon is slightly
353 * different in 2 cases:
354 * for INPUT type the icon is the OUTPUT shape of SCH_HIERLABEL
355 * for OUTPUT type the icon is the INPUT shape of SCH_HIERLABEL
356 */
358
359 switch( shape )
360 {
363 default: break;
364 }
365
366 SCH_HIERLABEL::CreateGraphicShape( aSettings, aPoints, aPos, shape );
367}
368
369
370void SCH_SHEET_PIN::GetEndPoints( std::vector<DANGLING_END_ITEM>& aItemList )
371{
373 aItemList.push_back( item );
374}
375
376
377wxString SCH_SHEET_PIN::GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const
378{
379 return wxString::Format( _( "Hierarchical Sheet Pin '%s'" ), aFull ? GetShownText( FOR_GUI )
381}
382
383
388
389
390bool SCH_SHEET_PIN::HitTest( const VECTOR2I& aPoint, int aAccuracy ) const
391{
392 BOX2I rect = GetBoundingBox();
393
394 rect.Inflate( aAccuracy );
395
396 return rect.Contains( aPoint );
397}
398
399
400bool SCH_SHEET_PIN::operator==( const SCH_ITEM& aOther ) const
401{
402 if( aOther.Type() != Type() )
403 return false;
404
405 const SCH_SHEET_PIN* other = static_cast<const SCH_SHEET_PIN*>( &aOther );
406
407 if( GetNumber() != other->GetNumber() )
408 return false;
409
410 if( GetSide() != other->GetSide() )
411 return false;
412
413 return SCH_HIERLABEL::operator==( *other );
414}
415
416
417double SCH_SHEET_PIN::Similarity( const SCH_ITEM& aOther ) const
418{
419 if( aOther.Type() != Type() )
420 return 0.0;
421
422 const SCH_SHEET_PIN* other = static_cast<const SCH_SHEET_PIN*>( &aOther );
423
424 double similarity = 1.0;
425
426 if( m_edge != other->m_edge )
427 similarity *= 0.9;
428
429 if( m_number != other->m_number )
430 similarity *= 0.9;
431
432 similarity *= SCH_HIERLABEL::Similarity( aOther );
433
434 return similarity;
435}
436
437
438bool SCH_SHEET_PIN::HasConnectivityChanges( const SCH_ITEM* aItem, const SCH_SHEET_PATH* aInstance ) const
439{
440 // Do not compare to ourself.
441 if( aItem == this )
442 return false;
443
444 const SCH_SHEET_PIN* pin = dynamic_cast<const SCH_SHEET_PIN*>( aItem );
445
446 // Don't compare against a different SCH_ITEM.
447 wxCHECK( pin, false );
448
449 if( GetPosition() != pin->GetPosition() )
450 return true;
451
452 return GetText() != pin->GetText();
453}
454
455
457{
458 if( SCH_SHEET* parentSheet = GetParent() )
459 {
460 if( parentSheet->IsLocked() )
461 return true;
462 }
463
464 return SCH_ITEM::IsLocked();
465}
466
467
468#if defined(DEBUG)
469
470void SCH_SHEET_PIN::Show( int nestLevel, std::ostream& os ) const
471{
472 // XML output:
473 NestedSpace( nestLevel, os ) << '<' << GetClass().Lower().mb_str() << ">"
474 << " pin_name=\"" << TO_UTF8( GetText() )
475 << '"' << "/>\n" << std::flush;
476}
477
478#endif
479
480
KICOMMON_API types::KiCadObjectType ToProtoEnum(KICAD_T aValue)
KICOMMON_API KICAD_T FromProtoEnum(types::KiCadObjectType aValue)
Definition api_enums.cpp:55
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:130
BITMAPS
A list of all bitmap identifiers.
@ add_hierar_pin
BOX2< VECTOR2I > BOX2I
Definition box2.h:914
constexpr BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition box2.h:553
constexpr bool Contains(const Vec &aPoint) const
Definition box2.h:165
Helper class used to store the state of schematic items that can be connected to other schematic item...
Definition sch_item.h:101
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
const KIID m_Uuid
Definition eda_item.h:599
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
virtual void SetParent(EDA_ITEM *aParent)
Definition eda_item.cpp:153
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
Definition eda_text.cpp:170
virtual VECTOR2I GetTextPos() const
Definition eda_text.h:313
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition eda_text.h:118
virtual void SetTextPos(const VECTOR2I &aPoint)
Definition eda_text.cpp:556
virtual void SetTextX(int aX)
Definition eda_text.cpp:563
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
Definition eda_text.cpp:200
virtual void SetTextY(int aY)
Definition eda_text.cpp:569
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
Definition kiid.h:46
Provide class metadata.
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
void Mask(TYPE_ID aDerived, TYPE_ID aBase, const wxString &aName)
Sets a base class property as masked in a derived class.
static PROPERTY_MANAGER & Instance()
void AddTypeCast(TYPE_CAST_BASE *aCast)
Register a type converter.
SCHEMATIC_SETTINGS & Settings() const
void SetSpinStyle(SPIN_STYLE aSpinStyle) override
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.
SCH_HIERLABEL(const VECTOR2I &aPos=VECTOR2I(0, 0), const wxString &aText=wxEmptyString, KICAD_T aType=SCH_HIER_LABEL_T)
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:170
void SetLocked(bool aLocked) override
Definition sch_item.h:262
virtual void swapData(SCH_ITEM *aItem)
Swap the internal data structures aItem with the schematic item.
Definition sch_item.cpp:834
SCHEMATIC * Schematic() const
Search the item hierarchy to find a SCHEMATIC.
Definition sch_item.cpp:302
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:393
bool IsLocked() const override
Definition sch_item.cpp:179
virtual bool operator==(const SCH_ITEM &aOther) const
Definition sch_item.cpp:911
wxString GetClass() const override
Return the class name.
Definition sch_item.h:180
SCH_LAYER_ID m_layer
Definition sch_item.h:843
SPIN_STYLE GetSpinStyle() const
void SetShape(LABEL_FLAG_SHAPE aShape)
Definition sch_label.h:179
LABEL_FLAG_SHAPE GetShape() const
Definition sch_label.h:178
const BOX2I GetBoundingBox() const override
Return the bounding box of the label including its fields.
wxString GetShownText(const SCH_SHEET_PATH *aPath, RESOLUTION_CONTEXT aContext, int aDepth=0) const override
LABEL_FLAG_SHAPE m_shape
Definition sch_label.h:365
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.
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
bool operator==(const SCH_ITEM &aOther) const override
void SetNumber(int aNumber)
Set the sheet label number.
bool IsLocked() const override
SCH_SHEET_PIN(SCH_SHEET *parent, const VECTOR2I &pos=VECTOR2I(0, 0), const wxString &text=wxEmptyString)
SHEET_SIDE m_edge
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
void SetPosition(const VECTOR2I &aPosition) override
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 swapData(SCH_ITEM *aItem) override
Swap the internal data structures aItem with the schematic item.
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 Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
int GetPenWidth() const override
static SHEET_SIDE GetOppositeSide(SHEET_SIDE aSide)
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.
SHEET_SIDE GetSide() const
wxString GetClass() const override
Return the class name.
void GetEndPoints(std::vector< DANGLING_END_ITEM > &aItemList) override
Add the schematic item end points to aItemList if the item has end points.
int GetNumber() const
Get the sheet label number.
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 ConstrainOnEdge(const VECTOR2I &aPos, bool aAllowEdgeSwitch)
Adjust label position to edge based on proximity to vertical or horizontal edge of the parent sheet.
void SetSide(SHEET_SIDE aEdge)
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:48
VECTOR2I m_size
Definition sch_sheet.h:713
VECTOR2I m_pos
Definition sch_sheet.h:712
bool IsVerticalOrientation() const
VECTOR2I GetPosition() const override
Definition sch_text.h:143
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.
@ FOR_GUI
Definition common.h:93
#define DEFAULT_LINE_WIDTH_MILS
The default line width in mils. (can be changed in preference menu)
#define _(s)
static constexpr EDA_ANGLE ANGLE_90
Definition eda_angle.h:450
static constexpr EDA_ANGLE ANGLE_270
Definition eda_angle.h:453
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition idf_helpers.h:37
@ LAYER_SHEETLABEL
Definition layer_ids.h:503
KICOMMON_API wxString EllipsizeMenuText(const wxString &aString)
Ellipsize text (at the end) to be no more than 36 characters.
KICOMMON_API VECTOR2I UnpackVector2(const types::Vector2 &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API void PackVector2(types::Vector2 &aOutput, const VECTOR2I &aInput, const EDA_IU_SCALE &aScale)
#define _HKI(x)
Definition page_info.cpp:40
#define TYPE_HASH(x)
Macro to generate unique identifier for a type.
Definition property.h:74
#define REGISTER_TYPE(x)
Helper macro to map type hashes to names.
@ SHEET_LABEL_END
Definition sch_item.h:91
LABEL_FLAG_SHAPE
Label and flag shapes used with text objects.
Definition sch_label.h:97
@ L_OUTPUT
Definition sch_label.h:99
@ L_INPUT
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.
KIBIS_PIN * pin
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:227
@ SCH_SHEET_PIN_T
Definition typeinfo.h:170
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708