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 <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 ) :
44{
45 SetParent( parent );
46 wxASSERT( parent );
48
49 SetTextPos( pos );
50
51 if( parent->IsVerticalOrientation() )
53 else
55
57 m_isDangling = true;
58 m_number = 2;
59}
60
61
63{
64 return new SCH_SHEET_PIN( *this );
65}
66
67
69{
71
72 wxCHECK_RET( aItem->Type() == SCH_SHEET_PIN_T,
73 wxString::Format( "SCH_SHEET_PIN object cannot swap data with %s object.",
74 aItem->GetClass() ) );
75
76 SCH_SHEET_PIN* pin = static_cast<SCH_SHEET_PIN*>( aItem );
77
78 std::swap( m_number, pin->m_number );
79 std::swap( m_edge, pin->m_edge );
80}
81
82
84{
85 return aPin == this;
86}
87
88
90{
91 if( Schematic() )
93
94 return schIUScale.MilsToIU( DEFAULT_LINE_WIDTH_MILS );
95}
96
97
98void SCH_SHEET_PIN::SetNumber( int aNumber )
99{
100 wxASSERT( aNumber >= 2 );
101
102 m_number = aNumber;
103}
104
105
107{
108 SCH_SHEET* sheet = GetParent();
109
110 // use SHEET_UNDEFINED_SIDE to adjust text orientation without changing edge
111
112 switch( aEdge )
113 {
114 case SHEET_SIDE::LEFT:
115 m_edge = aEdge;
116 SetTextX( sheet->m_pos.x );
117 SetSpinStyle( SPIN_STYLE::RIGHT ); // Orientation horiz inverse
118 break;
119
121 m_edge = aEdge;
122 SetTextX( sheet->m_pos.x + sheet->m_size.x );
123 SetSpinStyle( SPIN_STYLE::LEFT ); // Orientation horiz normal
124 break;
125
126 case SHEET_SIDE::TOP:
127 m_edge = aEdge;
128 SetTextY( sheet->m_pos.y );
129 SetSpinStyle( SPIN_STYLE::BOTTOM ); // Orientation vert BOTTOM
130 break;
131
133 m_edge = aEdge;
134 SetTextY( sheet->m_pos.y + sheet->m_size.y );
135 SetSpinStyle( SPIN_STYLE::UP ); // Orientation vert UP
136 break;
137
138 default:
139 break;
140 }
141}
142
143
145{
146 return m_edge;
147}
148
149
150void SCH_SHEET_PIN::ConstrainOnEdge( const VECTOR2I& aPos, bool aAllowEdgeSwitch )
151{
152 SCH_SHEET* sheet = GetParent();
153
154 if( sheet == nullptr )
155 return;
156
157 int leftSide = sheet->m_pos.x;
158 int rightSide = sheet->m_pos.x + sheet->m_size.x;
159 int topSide = sheet->m_pos.y;
160 int botSide = sheet->m_pos.y + sheet->m_size.y;
161
162 SHAPE_LINE_CHAIN sheetEdge;
163
164 sheetEdge.Append( leftSide, topSide );
165 sheetEdge.Append( rightSide, topSide );
166 sheetEdge.Append( rightSide, botSide );
167 sheetEdge.Append( leftSide, botSide );
168 sheetEdge.Append( leftSide, topSide );
169
170 if( aAllowEdgeSwitch )
171 {
172 switch( sheetEdge.NearestSegment( aPos ) )
173 {
174 case 0: SetSide( SHEET_SIDE::TOP ); break;
175 case 1: SetSide( SHEET_SIDE::RIGHT ); break;
176 case 2: SetSide( SHEET_SIDE::BOTTOM ); break;
177 case 3: SetSide( SHEET_SIDE::LEFT ); break;
178 default: wxASSERT( "Invalid segment number" );
179 }
180 }
181 else
182 {
183 SetSide( GetSide() );
184 }
185
186 switch( GetSide() )
187 {
189 case SHEET_SIDE::LEFT:
190 SetTextY( aPos.y );
191
192 if( GetTextPos().y < topSide )
193 SetTextY( topSide );
194
195 if( GetTextPos().y > botSide )
196 SetTextY( botSide );
197
198 break;
199
201 case SHEET_SIDE::TOP:
202 SetTextX( aPos.x );
203
204 if( GetTextPos().x < leftSide )
205 SetTextX( leftSide );
206
207 if( GetTextPos().x > rightSide )
208 SetTextX( rightSide );
209
210 break;
211
213 wxASSERT( "Undefined sheet side" );
214 }
215}
216
217
219{
220 int p = GetTextPos().y - aCenter;
221
222 SetTextY( aCenter - p );
223
224 switch( m_edge )
225 {
228 default: break;
229 }
230}
231
232
234{
235 int p = GetTextPos().x - aCenter;
236
237 SetTextX( aCenter - p );
238
239 switch( m_edge )
240 {
243 default: break;
244 }
245}
246
247
248void SCH_SHEET_PIN::Rotate( const VECTOR2I& aCenter, bool aRotateCCW )
249{
250 VECTOR2I pt = GetTextPos();
251 VECTOR2I delta = pt - aCenter;
252
253 RotatePoint( pt, aCenter, aRotateCCW ? ANGLE_90 : ANGLE_270 );
254
255 SHEET_SIDE oldSide = GetSide();
256 ConstrainOnEdge( pt, true );
257
258 // If the new side is the same as the old side, instead mirror across the center of that side.
259 if( GetSide() == oldSide )
260 {
261 switch( GetSide() )
262 {
263 case SHEET_SIDE::TOP:
265 SetTextPos( VECTOR2I( aCenter.x - delta.x, GetTextPos().y ) );
266 break;
267
268 case SHEET_SIDE::LEFT:
270 SetTextPos( VECTOR2I( GetTextPos().x, aCenter.y - delta.y ) );
271 break;
272
273 default:
274 break;
275 }
276 }
277 // If the new side is opposite to the old side, instead mirror across the center of an adjacent
278 // side.
279 else if( GetSide() == GetOppositeSide( oldSide ) )
280 {
281 switch( GetSide() )
282 {
283 case SHEET_SIDE::TOP:
285 SetTextPos( VECTOR2I( aCenter.x + delta.x, GetTextPos().y ) );
286 break;
287
288 case SHEET_SIDE::LEFT:
290 SetTextPos( VECTOR2I( GetTextPos().x, aCenter.y + delta.y ) );
291 break;
292
293 default:
294 break;
295 }
296 }
297}
298
299
301 std::vector<VECTOR2I>& aPoints, const VECTOR2I& aPos ) const
302{
303 /*
304 * These are the same icon shapes as SCH_HIERLABEL but the graphic icon is slightly
305 * different in 2 cases:
306 * for INPUT type the icon is the OUTPUT shape of SCH_HIERLABEL
307 * for OUTPUT type the icon is the INPUT shape of SCH_HIERLABEL
308 */
310
311 switch( shape )
312 {
315 default: break;
316 }
317
318 SCH_HIERLABEL::CreateGraphicShape( aSettings, aPoints, aPos, shape );
319}
320
321
322void SCH_SHEET_PIN::GetEndPoints( std::vector<DANGLING_END_ITEM>& aItemList )
323{
325 aItemList.push_back( item );
326}
327
328
329wxString SCH_SHEET_PIN::GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const
330{
331 return wxString::Format( _( "Hierarchical Sheet Pin '%s'" ),
332 aFull ? GetShownText( false ) : KIUI::EllipsizeMenuText( GetText() ) );
333}
334
335
340
341
342bool SCH_SHEET_PIN::HitTest( const VECTOR2I& aPoint, int aAccuracy ) const
343{
344 BOX2I rect = GetBoundingBox();
345
346 rect.Inflate( aAccuracy );
347
348 return rect.Contains( aPoint );
349}
350
351
352bool SCH_SHEET_PIN::operator==( const SCH_ITEM& aOther ) const
353{
354 if( aOther.Type() != Type() )
355 return false;
356
357 const SCH_SHEET_PIN* other = static_cast<const SCH_SHEET_PIN*>( &aOther );
358
359 return m_edge == other->m_edge && m_number == other->m_number
360 && SCH_HIERLABEL::operator==( aOther );
361}
362
363
364double SCH_SHEET_PIN::Similarity( const SCH_ITEM& aOther ) const
365{
366 if( aOther.Type() != Type() )
367 return 0.0;
368
369 const SCH_SHEET_PIN* other = static_cast<const SCH_SHEET_PIN*>( &aOther );
370
371 double similarity = 1.0;
372
373 if( m_edge != other->m_edge )
374 similarity *= 0.9;
375
376 if( m_number != other->m_number )
377 similarity *= 0.9;
378
379 similarity *= SCH_HIERLABEL::Similarity( aOther );
380
381 return similarity;
382}
383
384
386 const SCH_SHEET_PATH* aInstance ) const
387{
388 // Do not compare to ourself.
389 if( aItem == this )
390 return false;
391
392 const SCH_SHEET_PIN* pin = dynamic_cast<const SCH_SHEET_PIN*>( aItem );
393
394 // Don't compare against a different SCH_ITEM.
395 wxCHECK( pin, false );
396
397 if( GetPosition() != pin->GetPosition() )
398 return true;
399
400 return GetText() != pin->GetText();
401}
402
403
404#if defined(DEBUG)
405
406void SCH_SHEET_PIN::Show( int nestLevel, std::ostream& os ) const
407{
408 // XML output:
409 NestedSpace( nestLevel, os ) << '<' << GetClass().Lower().mb_str() << ">"
410 << " pin_name=\"" << TO_UTF8( GetText() )
411 << '"' << "/>\n" << std::flush;
412}
413
414#endif
415
416
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:114
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:96
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
virtual void SetParent(EDA_ITEM *aParent)
Definition eda_item.h:113
const VECTOR2I & GetTextPos() const
Definition eda_text.h:273
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:585
void SetTextX(int aX)
Definition eda_text.cpp:591
void SetTextY(int aY)
Definition eda_text.cpp:597
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.
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
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:167
virtual void swapData(SCH_ITEM *aItem)
Swap the internal data structures aItem with the schematic item.
Definition sch_item.cpp:600
SCHEMATIC * Schematic() const
Search the item hierarchy to find a SCHEMATIC.
Definition sch_item.cpp:247
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:369
virtual bool operator==(const SCH_ITEM &aOther) const
Definition sch_item.cpp:667
wxString GetClass() const override
Return the class name.
Definition sch_item.h:177
SCH_LAYER_ID m_layer
Definition sch_item.h:772
wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const override
const BOX2I GetBoundingBox() const override
Return the bounding box of the label including its fields.
LABEL_FLAG_SHAPE m_shape
Definition sch_label.h:377
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.
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.
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.
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:676
VECTOR2I m_pos
Definition sch_sheet.h:675
bool IsVerticalOrientation() const
VECTOR2I GetPosition() const override
Definition sch_text.h:147
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:413
static constexpr EDA_ANGLE ANGLE_270
Definition eda_angle.h:416
@ LAYER_SHEETLABEL
Definition layer_ids.h:475
KICOMMON_API wxString EllipsizeMenuText(const wxString &aString)
Ellipsize text (at the end) to be no more than 36 characters.
#define TYPE_HASH(x)
Definition property.h:74
#define REGISTER_TYPE(x)
@ SHEET_LABEL_END
Definition sch_item.h:86
LABEL_FLAG_SHAPE
Definition sch_label.h:99
@ L_OUTPUT
Definition sch_label.h:101
@ L_INPUT
Definition sch_label.h:100
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:178
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695