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( google::protobuf::Any& aContainer ) const
64{
65 using namespace kiapi::schematic::types;
66
67 SheetPin pin;
68
69 pin.mutable_id()->set_value( m_Uuid.AsStdString() );
70 kiapi::common::PackVector2( *pin.mutable_position(), GetPosition(), schIUScale );
71 pin.set_spin_style(
73 static_cast<SPIN_STYLE::SPIN>( static_cast<int>( GetSpinStyle() ) ) ) );
76 pin.set_locked( SCH_ITEM::IsLocked() ? kiapi::common::types::LockedState::LS_LOCKED
77 : kiapi::common::types::LockedState::LS_UNLOCKED );
78
79 google::protobuf::Any any;
81 any.UnpackTo( pin.mutable_text() );
82
83 aContainer.PackFrom( pin );
84}
85
86
87bool SCH_SHEET_PIN::Deserialize( const google::protobuf::Any& aContainer )
88{
89 using namespace kiapi::schematic::types;
90
91 SheetPin pin;
92
93 if( !aContainer.UnpackTo( &pin ) )
94 return false;
95
96 const_cast<KIID&>( m_Uuid ) = KIID( pin.id().value() );
97
98 google::protobuf::Any any;
99 any.PackFrom( pin.text() );
100
102 return false;
103
108 SetLocked( pin.locked() == kiapi::common::types::LockedState::LS_LOCKED );
109 return true;
110}
111
112
114{
115 return new SCH_SHEET_PIN( *this );
116}
117
118
120{
122
123 wxCHECK_RET( aItem->Type() == SCH_SHEET_PIN_T,
124 wxString::Format( "SCH_SHEET_PIN object cannot swap data with %s object.",
125 aItem->GetClass() ) );
126
127 SCH_SHEET_PIN* pin = static_cast<SCH_SHEET_PIN*>( aItem );
128
129 std::swap( m_number, pin->m_number );
130 std::swap( m_edge, pin->m_edge );
131}
132
133
135{
136 return aPin == this;
137}
138
139
141{
142 if( Schematic() )
144
145 return schIUScale.MilsToIU( DEFAULT_LINE_WIDTH_MILS );
146}
147
148
149void SCH_SHEET_PIN::SetNumber( int aNumber )
150{
151 wxASSERT( aNumber >= 2 );
152
153 m_number = aNumber;
154}
155
156
158{
159 SCH_SHEET* sheet = GetParent();
160
161 // use SHEET_UNDEFINED_SIDE to adjust text orientation without changing edge
162
163 switch( aEdge )
164 {
165 case SHEET_SIDE::LEFT:
166 m_edge = aEdge;
167 SetTextX( sheet->m_pos.x );
168 SetSpinStyle( SPIN_STYLE::RIGHT ); // Orientation horiz inverse
169 break;
170
172 m_edge = aEdge;
173 SetTextX( sheet->m_pos.x + sheet->m_size.x );
174 SetSpinStyle( SPIN_STYLE::LEFT ); // Orientation horiz normal
175 break;
176
177 case SHEET_SIDE::TOP:
178 m_edge = aEdge;
179 SetTextY( sheet->m_pos.y );
180 SetSpinStyle( SPIN_STYLE::BOTTOM ); // Orientation vert BOTTOM
181 break;
182
184 m_edge = aEdge;
185 SetTextY( sheet->m_pos.y + sheet->m_size.y );
186 SetSpinStyle( SPIN_STYLE::UP ); // Orientation vert UP
187 break;
188
189 default:
190 break;
191 }
192}
193
194
196{
197 return m_edge;
198}
199
200
201void SCH_SHEET_PIN::ConstrainOnEdge( const VECTOR2I& aPos, bool aAllowEdgeSwitch )
202{
203 SCH_SHEET* sheet = GetParent();
204
205 if( sheet == nullptr )
206 return;
207
208 int leftSide = sheet->m_pos.x;
209 int rightSide = sheet->m_pos.x + sheet->m_size.x;
210 int topSide = sheet->m_pos.y;
211 int botSide = sheet->m_pos.y + sheet->m_size.y;
212
213 SHAPE_LINE_CHAIN sheetEdge;
214
215 sheetEdge.Append( leftSide, topSide );
216 sheetEdge.Append( rightSide, topSide );
217 sheetEdge.Append( rightSide, botSide );
218 sheetEdge.Append( leftSide, botSide );
219 sheetEdge.Append( leftSide, topSide );
220
221 if( aAllowEdgeSwitch )
222 {
223 switch( sheetEdge.NearestSegment( aPos ) )
224 {
225 case 0: SetSide( SHEET_SIDE::TOP ); break;
226 case 1: SetSide( SHEET_SIDE::RIGHT ); break;
227 case 2: SetSide( SHEET_SIDE::BOTTOM ); break;
228 case 3: SetSide( SHEET_SIDE::LEFT ); break;
229 default: wxASSERT( "Invalid segment number" );
230 }
231 }
232 else
233 {
234 SetSide( GetSide() );
235 }
236
237 switch( GetSide() )
238 {
240 case SHEET_SIDE::LEFT:
241 SetTextY( aPos.y );
242
243 if( GetTextPos().y < topSide )
244 SetTextY( topSide );
245
246 if( GetTextPos().y > botSide )
247 SetTextY( botSide );
248
249 break;
250
252 case SHEET_SIDE::TOP:
253 SetTextX( aPos.x );
254
255 if( GetTextPos().x < leftSide )
256 SetTextX( leftSide );
257
258 if( GetTextPos().x > rightSide )
259 SetTextX( rightSide );
260
261 break;
262
264 wxASSERT( "Undefined sheet side" );
265 }
266}
267
268
270{
271 int p = GetTextPos().y - aCenter;
272
273 SetTextY( aCenter - p );
274
275 switch( m_edge )
276 {
279 default: break;
280 }
281}
282
283
285{
286 int p = GetTextPos().x - aCenter;
287
288 SetTextX( aCenter - p );
289
290 switch( m_edge )
291 {
294 default: break;
295 }
296}
297
298
299void SCH_SHEET_PIN::Rotate( const VECTOR2I& aCenter, bool aRotateCCW )
300{
301 VECTOR2I pt = GetTextPos();
302 VECTOR2I delta = pt - aCenter;
303
304 RotatePoint( pt, aCenter, aRotateCCW ? ANGLE_90 : ANGLE_270 );
305
306 SHEET_SIDE oldSide = GetSide();
307 ConstrainOnEdge( pt, true );
308
309 // If the new side is the same as the old side, instead mirror across the center of that side.
310 if( GetSide() == oldSide )
311 {
312 switch( GetSide() )
313 {
314 case SHEET_SIDE::TOP:
316 SetTextPos( VECTOR2I( aCenter.x - delta.x, GetTextPos().y ) );
317 break;
318
319 case SHEET_SIDE::LEFT:
321 SetTextPos( VECTOR2I( GetTextPos().x, aCenter.y - delta.y ) );
322 break;
323
324 default:
325 break;
326 }
327 }
328 // If the new side is opposite to the old side, instead mirror across the center of an adjacent
329 // side.
330 else if( GetSide() == GetOppositeSide( oldSide ) )
331 {
332 switch( GetSide() )
333 {
334 case SHEET_SIDE::TOP:
336 SetTextPos( VECTOR2I( aCenter.x + delta.x, GetTextPos().y ) );
337 break;
338
339 case SHEET_SIDE::LEFT:
341 SetTextPos( VECTOR2I( GetTextPos().x, aCenter.y + delta.y ) );
342 break;
343
344 default:
345 break;
346 }
347 }
348}
349
350
352 std::vector<VECTOR2I>& aPoints, const VECTOR2I& aPos ) const
353{
354 /*
355 * These are the same icon shapes as SCH_HIERLABEL but the graphic icon is slightly
356 * different in 2 cases:
357 * for INPUT type the icon is the OUTPUT shape of SCH_HIERLABEL
358 * for OUTPUT type the icon is the INPUT shape of SCH_HIERLABEL
359 */
361
362 switch( shape )
363 {
366 default: break;
367 }
368
369 SCH_HIERLABEL::CreateGraphicShape( aSettings, aPoints, aPos, shape );
370}
371
372
373void SCH_SHEET_PIN::GetEndPoints( std::vector<DANGLING_END_ITEM>& aItemList )
374{
376 aItemList.push_back( item );
377}
378
379
380wxString SCH_SHEET_PIN::GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const
381{
382 return wxString::Format( _( "Hierarchical Sheet Pin '%s'" ),
383 aFull ? GetShownText( false ) : KIUI::EllipsizeMenuText( GetText() ) );
384}
385
386
391
392
393bool SCH_SHEET_PIN::HitTest( const VECTOR2I& aPoint, int aAccuracy ) const
394{
395 BOX2I rect = GetBoundingBox();
396
397 rect.Inflate( aAccuracy );
398
399 return rect.Contains( aPoint );
400}
401
402
403bool SCH_SHEET_PIN::operator==( const SCH_ITEM& aOther ) const
404{
405 if( aOther.Type() != Type() )
406 return false;
407
408 const SCH_SHEET_PIN* other = static_cast<const SCH_SHEET_PIN*>( &aOther );
409
410 return m_edge == other->m_edge && m_number == other->m_number
411 && SCH_HIERLABEL::operator==( aOther );
412}
413
414
415double SCH_SHEET_PIN::Similarity( const SCH_ITEM& aOther ) const
416{
417 if( aOther.Type() != Type() )
418 return 0.0;
419
420 const SCH_SHEET_PIN* other = static_cast<const SCH_SHEET_PIN*>( &aOther );
421
422 double similarity = 1.0;
423
424 if( m_edge != other->m_edge )
425 similarity *= 0.9;
426
427 if( m_number != other->m_number )
428 similarity *= 0.9;
429
430 similarity *= SCH_HIERLABEL::Similarity( aOther );
431
432 return similarity;
433}
434
435
437 const SCH_SHEET_PATH* aInstance ) const
438{
439 // Do not compare to ourself.
440 if( aItem == this )
441 return false;
442
443 const SCH_SHEET_PIN* pin = dynamic_cast<const SCH_SHEET_PIN*>( aItem );
444
445 // Don't compare against a different SCH_ITEM.
446 wxCHECK( pin, false );
447
448 if( GetPosition() != pin->GetPosition() )
449 return true;
450
451 return GetText() != pin->GetText();
452}
453
454
456{
457 if( SCH_SHEET* parentSheet = GetParent() )
458 {
459 if( parentSheet->IsLocked() )
460 return true;
461 }
462
463 return SCH_ITEM::IsLocked();
464}
465
466
467#if defined(DEBUG)
468
469void SCH_SHEET_PIN::Show( int nestLevel, std::ostream& os ) const
470{
471 // XML output:
472 NestedSpace( nestLevel, os ) << '<' << GetClass().Lower().mb_str() << ">"
473 << " pin_name=\"" << TO_UTF8( GetText() )
474 << '"' << "/>\n" << std::flush;
475}
476
477#endif
478
479
types::KiCadObjectType ToProtoEnum(KICAD_T aValue)
KICAD_T FromProtoEnum(types::KiCadObjectType aValue)
Definition api_enums.cpp:47
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:123
BITMAPS
A list of all bitmap identifiers.
@ add_hierar_pin
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
constexpr BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition box2.h:554
constexpr bool Contains(const Vec &aPoint) const
Definition box2.h:164
Helper class used to store the state of schematic items that can be connected to other schematic item...
Definition sch_item.h:93
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:96
const KIID m_Uuid
Definition eda_item.h:531
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:108
virtual void SetParent(EDA_ITEM *aParent)
Definition eda_item.cpp:89
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
Definition eda_text.cpp:165
virtual VECTOR2I GetTextPos() const
Definition eda_text.h:294
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition eda_text.h:110
virtual void SetTextPos(const VECTOR2I &aPoint)
Definition eda_text.cpp:576
virtual void SetTextX(int aX)
Definition eda_text.cpp:583
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
Definition eda_text.cpp:208
virtual void SetTextY(int aY)
Definition eda_text.cpp:589
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
Definition kiid.h:44
Provide class metadata.Helper macro to map type hashes to names.
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:162
void SetLocked(bool aLocked) override
Definition sch_item.h:251
virtual void swapData(SCH_ITEM *aItem)
Swap the internal data structures aItem with the schematic item.
Definition sch_item.cpp:626
SCHEMATIC * Schematic() const
Search the item hierarchy to find a SCHEMATIC.
Definition sch_item.cpp:268
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:367
bool IsLocked() const override
Definition sch_item.cpp:148
virtual bool operator==(const SCH_ITEM &aOther) const
Definition sch_item.cpp:696
wxString GetClass() const override
Return the class name.
Definition sch_item.h:172
SCH_LAYER_ID m_layer
Definition sch_item.h:775
wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const override
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.
LABEL_FLAG_SHAPE m_shape
Definition sch_label.h:375
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.
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.
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 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:44
VECTOR2I m_size
Definition sch_sheet.h:670
VECTOR2I m_pos
Definition sch_sheet.h:669
bool IsVerticalOrientation() const
VECTOR2I GetPosition() const override
Definition sch_text.h:146
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.
A type-safe container of any type.
Definition ki_any.h:92
#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:413
static constexpr EDA_ANGLE ANGLE_270
Definition eda_angle.h:416
@ LAYER_SHEETLABEL
Definition layer_ids.h:473
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)
Definition property.h:74
#define REGISTER_TYPE(x)
@ SHEET_LABEL_END
Definition sch_item.h:83
LABEL_FLAG_SHAPE
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.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
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:225
@ SCH_SHEET_PIN_T
Definition typeinfo.h:171
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683