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, 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 <api/api_enums.h>
29#include <api/api_utils.h>
30#include <api/schematic/schematic_types.pb.h>
31#include <general.h>
33#include <string_utils.h>
34#include <plotters/plotter.h>
35#include <sch_draw_panel.h>
36#include <sch_edit_frame.h>
37#include <sch_sheet.h>
38#include <sch_sheet_pin.h>
39#include <sch_painter.h>
40#include <schematic.h>
41#include <trigo.h>
42#include <properties/property.h>
44
45
46SCH_SHEET_PIN::SCH_SHEET_PIN( SCH_SHEET* parent, const VECTOR2I& pos, const wxString& text ) :
49{
50 SetParent( parent );
51 wxASSERT( parent );
53
54 SetTextPos( pos );
55
56 if( parent->IsVerticalOrientation() )
58 else
60
62 m_isDangling = true;
63 m_number = 2;
64}
65
66
67void SCH_SHEET_PIN::Serialize( google::protobuf::Any& aContainer ) const
68{
69 using namespace kiapi::schematic::types;
70
71 SheetPin pin;
72
73 pin.mutable_id()->set_value( m_Uuid.AsStdString() );
74 kiapi::common::PackVector2( *pin.mutable_position(), GetPosition(), schIUScale );
75 pin.set_spin_style(
77 static_cast<SPIN_STYLE::SPIN>( static_cast<int>( GetSpinStyle() ) ) ) );
80 pin.set_locked( SCH_ITEM::IsLocked() ? kiapi::common::types::LockedState::LS_LOCKED
81 : kiapi::common::types::LockedState::LS_UNLOCKED );
82
83 google::protobuf::Any any;
85 any.UnpackTo( pin.mutable_text() );
86
87 aContainer.PackFrom( pin );
88}
89
90
91bool SCH_SHEET_PIN::Deserialize( const google::protobuf::Any& aContainer )
92{
93 using namespace kiapi::schematic::types;
94
95 SheetPin pin;
96
97 if( !aContainer.UnpackTo( &pin ) )
98 return false;
99
100 const_cast<KIID&>( m_Uuid ) = KIID( pin.id().value() );
101
102 google::protobuf::Any any;
103 any.PackFrom( pin.text() );
104
106 return false;
107
112 SetLocked( pin.locked() == kiapi::common::types::LockedState::LS_LOCKED );
113 return true;
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.",
129 aItem->GetClass() ) );
130
131 SCH_SHEET_PIN* pin = static_cast<SCH_SHEET_PIN*>( aItem );
132
133 std::swap( m_number, pin->m_number );
134 std::swap( m_edge, pin->m_edge );
135}
136
137
139{
140 return aPin == this;
141}
142
143
145{
146 if( Schematic() )
148
149 return schIUScale.MilsToIU( DEFAULT_LINE_WIDTH_MILS );
150}
151
152
153void SCH_SHEET_PIN::SetNumber( int aNumber )
154{
155 wxASSERT( aNumber >= 2 );
156
157 m_number = aNumber;
158}
159
160
162{
163 SCH_SHEET* sheet = GetParent();
164
165 // use SHEET_UNDEFINED_SIDE to adjust text orientation without changing edge
166
167 switch( aEdge )
168 {
169 case SHEET_SIDE::LEFT:
170 m_edge = aEdge;
171 SetTextX( sheet->m_pos.x );
172 SetSpinStyle( SPIN_STYLE::RIGHT ); // Orientation horiz inverse
173 break;
174
176 m_edge = aEdge;
177 SetTextX( sheet->m_pos.x + sheet->m_size.x );
178 SetSpinStyle( SPIN_STYLE::LEFT ); // Orientation horiz normal
179 break;
180
181 case SHEET_SIDE::TOP:
182 m_edge = aEdge;
183 SetTextY( sheet->m_pos.y );
184 SetSpinStyle( SPIN_STYLE::BOTTOM ); // Orientation vert BOTTOM
185 break;
186
188 m_edge = aEdge;
189 SetTextY( sheet->m_pos.y + sheet->m_size.y );
190 SetSpinStyle( SPIN_STYLE::UP ); // Orientation vert UP
191 break;
192
193 default:
194 break;
195 }
196}
197
198
200{
201 return m_edge;
202}
203
204
205void SCH_SHEET_PIN::ConstrainOnEdge( const VECTOR2I& aPos, bool aAllowEdgeSwitch )
206{
207 SCH_SHEET* sheet = GetParent();
208
209 if( sheet == nullptr )
210 return;
211
212 int leftSide = sheet->m_pos.x;
213 int rightSide = sheet->m_pos.x + sheet->m_size.x;
214 int topSide = sheet->m_pos.y;
215 int botSide = sheet->m_pos.y + sheet->m_size.y;
216
217 SHAPE_LINE_CHAIN sheetEdge;
218
219 sheetEdge.Append( leftSide, topSide );
220 sheetEdge.Append( rightSide, topSide );
221 sheetEdge.Append( rightSide, botSide );
222 sheetEdge.Append( leftSide, botSide );
223 sheetEdge.Append( leftSide, topSide );
224
225 if( aAllowEdgeSwitch )
226 {
227 switch( sheetEdge.NearestSegment( aPos ) )
228 {
229 case 0: SetSide( SHEET_SIDE::TOP ); break;
230 case 1: SetSide( SHEET_SIDE::RIGHT ); break;
231 case 2: SetSide( SHEET_SIDE::BOTTOM ); break;
232 case 3: SetSide( SHEET_SIDE::LEFT ); break;
233 default: wxASSERT( "Invalid segment number" );
234 }
235 }
236 else
237 {
238 SetSide( GetSide() );
239 }
240
241 switch( GetSide() )
242 {
244 case SHEET_SIDE::LEFT:
245 SetTextY( aPos.y );
246
247 if( GetTextPos().y < topSide )
248 SetTextY( topSide );
249
250 if( GetTextPos().y > botSide )
251 SetTextY( botSide );
252
253 break;
254
256 case SHEET_SIDE::TOP:
257 SetTextX( aPos.x );
258
259 if( GetTextPos().x < leftSide )
260 SetTextX( leftSide );
261
262 if( GetTextPos().x > rightSide )
263 SetTextX( rightSide );
264
265 break;
266
268 wxASSERT( "Undefined sheet side" );
269 }
270}
271
272
274{
275 int p = GetTextPos().y - aCenter;
276
277 SetTextY( aCenter - p );
278
279 switch( m_edge )
280 {
283 default: break;
284 }
285}
286
287
289{
290 int p = GetTextPos().x - aCenter;
291
292 SetTextX( aCenter - p );
293
294 switch( m_edge )
295 {
298 default: break;
299 }
300}
301
302
303void SCH_SHEET_PIN::Rotate( const VECTOR2I& aCenter, bool aRotateCCW )
304{
305 VECTOR2I pt = GetTextPos();
306 VECTOR2I delta = pt - aCenter;
307
308 RotatePoint( pt, aCenter, aRotateCCW ? ANGLE_90 : ANGLE_270 );
309
310 SHEET_SIDE oldSide = GetSide();
311 ConstrainOnEdge( pt, true );
312
313 // If the new side is the same as the old side, instead mirror across the center of that side.
314 if( GetSide() == oldSide )
315 {
316 switch( GetSide() )
317 {
318 case SHEET_SIDE::TOP:
320 SetTextPos( VECTOR2I( aCenter.x - delta.x, GetTextPos().y ) );
321 break;
322
323 case SHEET_SIDE::LEFT:
325 SetTextPos( VECTOR2I( GetTextPos().x, aCenter.y - delta.y ) );
326 break;
327
328 default:
329 break;
330 }
331 }
332 // If the new side is opposite to the old side, instead mirror across the center of an adjacent
333 // side.
334 else if( GetSide() == GetOppositeSide( oldSide ) )
335 {
336 switch( GetSide() )
337 {
338 case SHEET_SIDE::TOP:
340 SetTextPos( VECTOR2I( aCenter.x + delta.x, GetTextPos().y ) );
341 break;
342
343 case SHEET_SIDE::LEFT:
345 SetTextPos( VECTOR2I( GetTextPos().x, aCenter.y + delta.y ) );
346 break;
347
348 default:
349 break;
350 }
351 }
352}
353
354
356 std::vector<VECTOR2I>& aPoints, const VECTOR2I& aPos ) const
357{
358 /*
359 * These are the same icon shapes as SCH_HIERLABEL but the graphic icon is slightly
360 * different in 2 cases:
361 * for INPUT type the icon is the OUTPUT shape of SCH_HIERLABEL
362 * for OUTPUT type the icon is the INPUT shape of SCH_HIERLABEL
363 */
365
366 switch( shape )
367 {
370 default: break;
371 }
372
373 SCH_HIERLABEL::CreateGraphicShape( aSettings, aPoints, aPos, shape );
374}
375
376
377void SCH_SHEET_PIN::GetEndPoints( std::vector<DANGLING_END_ITEM>& aItemList )
378{
380 aItemList.push_back( item );
381}
382
383
384wxString SCH_SHEET_PIN::GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const
385{
386 return wxString::Format( _( "Hierarchical Sheet Pin '%s'" ),
387 aFull ? GetShownText( false ) : KIUI::EllipsizeMenuText( GetText() ) );
388}
389
390
395
396
397bool SCH_SHEET_PIN::HitTest( const VECTOR2I& aPoint, int aAccuracy ) const
398{
399 BOX2I rect = GetBoundingBox();
400
401 rect.Inflate( aAccuracy );
402
403 return rect.Contains( aPoint );
404}
405
406
407bool SCH_SHEET_PIN::operator==( const SCH_ITEM& aOther ) const
408{
409 if( aOther.Type() != Type() )
410 return false;
411
412 const SCH_SHEET_PIN* other = static_cast<const SCH_SHEET_PIN*>( &aOther );
413
414 return m_edge == other->m_edge && m_number == other->m_number
415 && SCH_HIERLABEL::operator==( aOther );
416}
417
418
419double SCH_SHEET_PIN::Similarity( const SCH_ITEM& aOther ) const
420{
421 if( aOther.Type() != Type() )
422 return 0.0;
423
424 const SCH_SHEET_PIN* other = static_cast<const SCH_SHEET_PIN*>( &aOther );
425
426 double similarity = 1.0;
427
428 if( m_edge != other->m_edge )
429 similarity *= 0.9;
430
431 if( m_number != other->m_number )
432 similarity *= 0.9;
433
434 similarity *= SCH_HIERLABEL::Similarity( aOther );
435
436 return similarity;
437}
438
439
441 const SCH_SHEET_PATH* aInstance ) const
442{
443 // Do not compare to ourself.
444 if( aItem == this )
445 return false;
446
447 const SCH_SHEET_PIN* pin = dynamic_cast<const SCH_SHEET_PIN*>( aItem );
448
449 // Don't compare against a different SCH_ITEM.
450 wxCHECK( pin, false );
451
452 if( GetPosition() != pin->GetPosition() )
453 return true;
454
455 return GetText() != pin->GetText();
456}
457
458
460{
461 if( SCH_SHEET* parentSheet = GetParent() )
462 {
463 if( parentSheet->IsLocked() )
464 return true;
465 }
466
467 return SCH_ITEM::IsLocked();
468}
469
470
471#if defined(DEBUG)
472
473void SCH_SHEET_PIN::Show( int nestLevel, std::ostream& os ) const
474{
475 // XML output:
476 NestedSpace( nestLevel, os ) << '<' << GetClass().Lower().mb_str() << ">"
477 << " pin_name=\"" << TO_UTF8( GetText() )
478 << '"' << "/>\n" << std::flush;
479}
480
481#endif
482
483
types::KiCadObjectType ToProtoEnum(KICAD_T aValue)
KICAD_T FromProtoEnum(types::KiCadObjectType aValue)
Definition api_enums.cpp:44
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:127
BITMAPS
A list of all bitmap identifiers.
@ add_hierar_pin
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
constexpr BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition box2.h:558
constexpr bool Contains(const Vec &aPoint) const
Definition box2.h:168
Helper class used to store the state of schematic items that can be connected to other schematic item...
Definition sch_item.h:97
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:100
const KIID m_Uuid
Definition eda_item.h:528
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:112
virtual void SetParent(EDA_ITEM *aParent)
Definition eda_item.cpp:93
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
Definition eda_text.cpp:169
const VECTOR2I & GetTextPos() const
Definition eda_text.h:298
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition eda_text.h:114
void SetTextPos(const VECTOR2I &aPoint)
Definition eda_text.cpp:576
void SetTextX(int aX)
Definition eda_text.cpp:582
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
Definition eda_text.cpp:212
void SetTextY(int aY)
Definition eda_text.cpp:588
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
Definition kiid.h:48
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:168
void SetLocked(bool aLocked) override
Definition sch_item.h:257
virtual void swapData(SCH_ITEM *aItem)
Swap the internal data structures aItem with the schematic item.
Definition sch_item.cpp:630
SCHEMATIC * Schematic() const
Search the item hierarchy to find a SCHEMATIC.
Definition sch_item.cpp:272
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:373
bool IsLocked() const override
Definition sch_item.cpp:152
virtual bool operator==(const SCH_ITEM &aOther) const
Definition sch_item.cpp:698
wxString GetClass() const override
Return the class name.
Definition sch_item.h:178
SCH_LAYER_ID m_layer
Definition sch_item.h:781
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:183
LABEL_FLAG_SHAPE GetShape() const
Definition sch_label.h:182
const BOX2I GetBoundingBox() const override
Return the bounding box of the label including its fields.
LABEL_FLAG_SHAPE m_shape
Definition sch_label.h:379
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:48
VECTOR2I m_size
Definition sch_sheet.h:674
VECTOR2I m_pos
Definition sch_sheet.h:673
bool IsVerticalOrientation() const
VECTOR2I GetPosition() const override
Definition sch_text.h:150
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:93
#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:477
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:44
#define TYPE_HASH(x)
Definition property.h:74
#define REGISTER_TYPE(x)
@ SHEET_LABEL_END
Definition sch_item.h:87
LABEL_FLAG_SHAPE
Definition sch_label.h:101
@ L_OUTPUT
Definition sch_label.h:103
@ L_INPUT
Definition sch_label.h:102
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:229
@ SCH_SHEET_PIN_T
Definition typeinfo.h:175
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687