KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_junction.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) 2009 Jean-Pierre Charras, jp.charras at wanadoo.fr
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
23#include <netclass.h>
24#include <sch_draw_panel.h>
25#include <trigo.h>
26#include <common.h>
27#include <plotters/plotter.h>
28#include <bitmaps.h>
29#include <core/mirror.h>
30#include <geometry/shape_rect.h>
32#include <sch_painter.h>
33#include <sch_junction.h>
34#include <sch_edit_frame.h>
35#include <sch_connection.h>
36#include <schematic.h>
38#include <connection_graph.h>
39#include <string_utils.h>
40#include <api/api_utils.h>
41#include <api/schematic/schematic_types.pb.h>
42#include <properties/property.h>
44
45
46SCH_JUNCTION::SCH_JUNCTION( const VECTOR2I& aPosition, int aDiameter, SCH_LAYER_ID aLayer ) :
47 SCH_ITEM( nullptr, SCH_JUNCTION_T )
48{
49 m_pos = aPosition;
51 m_diameter = aDiameter;
52 m_layer = aLayer;
53
56}
57
58
60{
61 return new SCH_JUNCTION( *this );
62}
63
64
65void SCH_JUNCTION::Serialize( google::protobuf::Any& aContainer ) const
66{
67 using namespace kiapi::common;
68
69 kiapi::schematic::types::Junction junction;
70
71 junction.mutable_id()->set_value( m_Uuid.AsStdString() );
72 PackVector2( *junction.mutable_position(), m_pos, schIUScale );
73 PackDistance( *junction.mutable_diameter(), m_diameter, schIUScale );
74
76 PackColor( *junction.mutable_color(), m_color );
77
78 junction.set_locked( IsLocked() ? types::LockedState::LS_LOCKED
79 : types::LockedState::LS_UNLOCKED );
80
81 kiapi::common::PackCustomProperties( junction.mutable_custom_properties(), *this );
82
83 if( !aContainer.PackFrom( junction ) )
84 wxFAIL_MSG( wxT( "Failed to pack SCH_JUNCTION" ) );
85}
86
87
88bool SCH_JUNCTION::Deserialize( const google::protobuf::Any& aContainer )
89{
90 using namespace kiapi::common;
91
92 kiapi::schematic::types::Junction junction;
93
94 if( !aContainer.UnpackTo( &junction ) )
95 return false;
96
97 const_cast<KIID&>( m_Uuid ) = KIID( junction.id().value() );
98 m_pos = UnpackVector2( junction.position(), schIUScale );
99 m_diameter = UnpackDistance( junction.diameter(), schIUScale );
100
101 if( junction.has_color() )
102 m_color = UnpackColor( junction.color() );
103 else
105
106 SetLocked( junction.locked() == types::LockedState::LS_LOCKED );
107 kiapi::common::UnpackCustomProperties( junction.custom_properties(), *this );
108 return true;
109}
110
111
113{
114 wxCHECK_RET( ( aItem != nullptr ) && ( aItem->Type() == SCH_JUNCTION_T ),
115 wxT( "Cannot swap junction data with invalid item." ) );
116
117 SCH_JUNCTION* item = (SCH_JUNCTION*) aItem;
118 std::swap( m_pos, item->m_pos );
119 std::swap( m_diameter, item->m_diameter );
120 std::swap( m_color, item->m_color );
121}
122
123
124std::vector<int> SCH_JUNCTION::ViewGetLayers() const
125{
127}
128
129
131{
132 if( m_diameter != 0 )
134 else if( Schematic() )
136 else
138
139 if( m_lastResolvedDiameter != 1 ) // Diameter 1 means user doesn't want to draw junctions
140 {
141 // If we know what we're connected to, then enforce a minimum size of 170% of the
142 // connected wire width:
143 if( !IsConnectivityDirty() )
144 {
146 GetEffectiveNetClass()->GetWireWidth() * 1.7 );
147 }
148 }
149
150 return SHAPE_CIRCLE( m_pos, std::max( m_lastResolvedDiameter / 2, 1 ) );
151}
152
153
155{
156 BOX2I bbox( m_pos );
157 bbox.Inflate( getEffectiveShape().GetRadius() );
158
159 return bbox;
160}
161
162
164{
165 MIRROR( m_pos.y, aCenter );
166}
167
168
170{
171 MIRROR( m_pos.x, aCenter );
172}
173
174
175void SCH_JUNCTION::Rotate( const VECTOR2I& aCenter, bool aRotateCCW )
176{
177 RotatePoint( m_pos, aCenter, aRotateCCW ? ANGLE_90 : ANGLE_270 );
178}
179
180
181void SCH_JUNCTION::GetEndPoints( std::vector <DANGLING_END_ITEM>& aItemList )
182{
183 DANGLING_END_ITEM item( JUNCTION_END, this, m_pos );
184 aItemList.push_back( item );
185}
186
187
188std::vector<VECTOR2I> SCH_JUNCTION::GetConnectionPoints() const
189{
190 return { m_pos };
191}
192
193
194#if defined(DEBUG)
195void SCH_JUNCTION::Show( int nestLevel, std::ostream& os ) const
196{
197 // XML output:
198 wxString s = GetClass();
199
200 NestedSpace( nestLevel, os ) << '<' << s.Lower().mb_str() << m_pos << ", " << m_diameter
201 << "/>\n";
202}
203#endif
204
205
206void SCH_JUNCTION::SetDiameter( int aDiameter )
207{
208 m_diameter = aDiameter;
209 m_lastResolvedDiameter = aDiameter;
210}
211
212
222
223
224void SCH_JUNCTION::SetColor( const COLOR4D& aColor )
225{
226 m_color = aColor;
227 m_lastResolvedColor = aColor;
228}
229
230
232{
233 return getEffectiveShape().GetRadius() * 2;
234}
235
236
237bool SCH_JUNCTION::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
238{
239 if( aAccuracy >= 0 )
240 return getEffectiveShape().Collide( SEG( aPosition, aPosition ), aAccuracy );
241 else
242 return aPosition == m_pos;
243}
244
245
246bool SCH_JUNCTION::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
247{
249 return false;
250
251 if( aContained )
252 {
253 BOX2I selRect( aRect );
254
255 return selRect.Inflate( aAccuracy ).Contains( GetBoundingBox() );
256 }
257 else
258 {
259 SHAPE_CIRCLE junction = getEffectiveShape();
260 SHAPE_RECT selRect( aRect.GetPosition(), aRect.GetWidth(), aRect.GetHeight() );
261
262 return selRect.Collide( &junction, aAccuracy );
263 }
264}
265
266
267bool SCH_JUNCTION::HitTest( const SHAPE_LINE_CHAIN& aPoly, bool aContained ) const
268{
270 return false;
271
272 return KIGEOM::ShapeHitTest( aPoly, getEffectiveShape(), aContained );
273}
274
275
277 const SCH_SHEET_PATH* aInstance ) const
278{
279 // Do not compare to ourself.
280 if( aItem == this )
281 return false;
282
283 const SCH_JUNCTION* junction = dynamic_cast<const SCH_JUNCTION*>( aItem );
284
285 // Don't compare against a different SCH_ITEM.
286 wxCHECK( junction, false );
287
288 return GetPosition() != junction->GetPosition();
289}
290
291
292bool SCH_JUNCTION::doIsConnected( const VECTOR2I& aPosition ) const
293{
294 return m_pos == aPosition;
295}
296
297
298void SCH_JUNCTION::Plot( PLOTTER* aPlotter, bool aBackground, const SCH_PLOT_OPTS& aPlotOpts,
299 int aUnit, int aBodyStyle, const VECTOR2I& aOffset, bool aDimmed )
300{
301 if( aBackground )
302 return;
303
304 RENDER_SETTINGS* settings = aPlotter->RenderSettings();
305 COLOR4D color = GetJunctionColor();
306
307 if( color == COLOR4D::UNSPECIFIED )
308 color = settings->GetLayerColor( GetLayer() );
309
310 if( color.m_text && Schematic() )
311 color = COLOR4D( ResolveText( *color.m_text, &Schematic()->CurrentSheet() ) );
312
313 aPlotter->SetColor( color );
314
316}
317
318
323
324
325bool SCH_JUNCTION::operator <( const SCH_ITEM& aItem ) const
326{
327 if( Type() != aItem.Type() )
328 return Type() < aItem.Type();
329
330 if( GetLayer() != aItem.GetLayer() )
331 return GetLayer() < aItem.GetLayer();
332
333 const SCH_JUNCTION* junction = static_cast<const SCH_JUNCTION*>( &aItem );
334
335 if( GetPosition().x != junction->GetPosition().x )
336 return GetPosition().x < junction->GetPosition().x;
337
338 if( GetPosition().y != junction->GetPosition().y )
339 return GetPosition().y < junction->GetPosition().y;
340
341 if( GetDiameter() != junction->GetDiameter() )
342 return GetDiameter() < junction->GetDiameter();
343
344 return GetColor() < junction->GetColor();
345}
346
347
348void SCH_JUNCTION::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
349{
350 aList.emplace_back( _( "Junction" ), wxEmptyString );
351
352 aList.emplace_back( _( "Size" ), aFrame->MessageTextFromValue( GetEffectiveDiameter() ) );
353
354 if( !IsConnectivityDirty() && dynamic_cast<SCH_EDIT_FRAME*>( aFrame ) )
356}
357
358
359bool SCH_JUNCTION::operator==( const SCH_ITEM& aOther ) const
360{
361 if( Type() != aOther.Type() )
362 return false;
363
364 const SCH_JUNCTION& other = static_cast<const SCH_JUNCTION&>( aOther );
365
366 if( m_pos != other.m_pos )
367 return false;
368
369 if( m_diameter != other.m_diameter )
370 return false;
371
372 if( m_color != other.m_color )
373 return false;
374
375 return true;
376}
377
378
379double SCH_JUNCTION::Similarity( const SCH_ITEM& aOther ) const
380{
381 if( m_Uuid == aOther.m_Uuid )
382 return 1.0;
383
384 if( aOther.Type() != Type() )
385 return 0.0;
386
387 const SCH_JUNCTION& other = static_cast<const SCH_JUNCTION&>( aOther );
388
389 double similarity = 1.0;
390
391 if( m_pos != other.m_pos )
392 similarity *= 0.9;
393
394 if( m_diameter != other.m_diameter )
395 similarity *= 0.9;
396
397 if( m_color != other.m_color )
398 similarity *= 0.9;
399
400 return similarity;
401}
402
403
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:130
BITMAPS
A list of all bitmap identifiers.
BOX2< VECTOR2I > BOX2I
Definition box2.h:917
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:985
constexpr const Vec & GetPosition() const
Definition box2.h:214
constexpr BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition box2.h:556
constexpr size_type GetWidth() const
Definition box2.h:217
constexpr size_type GetHeight() const
Definition box2.h:218
constexpr bool Contains(const Vec &aPoint) const
Definition box2.h:171
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition color4d.h:400
Helper class used to store the state of schematic items that can be connected to other schematic item...
Definition sch_item.h:101
The base class for create windows for drawing purpose.
const KIID m_Uuid
Definition eda_item.h:599
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
EDA_ITEM_FLAGS m_flags
Definition eda_item.h:608
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType, bool isSCH_ITEM=false, bool isBOARD_ITEM=false)
Definition eda_item.cpp:84
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
std::shared_ptr< wxString > m_text
Definition color4d.h:397
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
Definition kiid.h:46
COLOR4D GetSchematicColor(bool aIsForSave=false) const
Definition netclass.h:244
Base plotter engine class.
Definition plotter.h:138
virtual void Circle(const VECTOR2I &pos, int diametre, FILL_T fill, int width)=0
RENDER_SETTINGS * RenderSettings()
Definition plotter.h:169
virtual void SetColor(const COLOR4D &color)=0
Provide class metadata.
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
static PROPERTY_MANAGER & Instance()
PROPERTY_BASE & AddProperty(PROPERTY_BASE *aProperty, const wxString &aGroup=wxEmptyString)
Register a property.
int GetJunctionSize()
Accessor that computes the current junction size.
SCHEMATIC_SETTINGS & Settings() const
Schematic editor (Eeschema) main window.
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
SCHEMATIC * Schematic() const
Search the item hierarchy to find a SCHEMATIC.
Definition sch_item.cpp:302
bool IsLocked() const override
Definition sch_item.cpp:179
std::shared_ptr< NETCLASS > GetEffectiveNetClass(const SCH_SHEET_PATH *aSheet=nullptr) const
Definition sch_item.cpp:733
SCH_LAYER_ID GetLayer() const
Return the layer this item is on.
Definition sch_item.h:353
bool IsConnectivityDirty() const
Definition sch_item.h:635
SCH_ITEM(EDA_ITEM *aParent, KICAD_T aType, int aUnit=0, int aBodyStyle=0)
Definition sch_item.cpp:65
wxString ResolveText(const wxString &aText, const SCH_SHEET_PATH *aPath, int aDepth=0) const
Definition sch_item.cpp:464
SCH_LAYER_ID m_layer
Definition sch_item.h:843
void Plot(PLOTTER *aPlotter, bool aBackground, const SCH_PLOT_OPTS &aPlotOpts, int aUnit, int aBodyStyle, const VECTOR2I &aOffset, bool aDimmed) override
Plot the item to aPlotter.
void SetColor(const KIGFX::COLOR4D &aColor)
VECTOR2I m_pos
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
void swapData(SCH_ITEM *aItem) override
Swap the internal data structures aItem with the schematic item.
virtual bool operator<(const SCH_ITEM &aItem) const override
int GetEffectiveDiameter() const
double Similarity(const SCH_ITEM &aOther) const override
Return a measure of how likely the other object is to represent the same object.
SHAPE_CIRCLE getEffectiveShape() const
SCH_JUNCTION(const VECTOR2I &aPosition=VECTOR2I(0, 0), int aDiameter=0, SCH_LAYER_ID aLayer=LAYER_JUNCTION)
BITMAPS GetMenuImage() const override
Return a pointer to an image to be used in menus.
int m_diameter
Zero is user default.
bool operator==(const SCH_ITEM &aOther) const override
void SetDiameter(int aDiameter)
void GetEndPoints(std::vector< DANGLING_END_ITEM > &aItemList) override
Add the schematic item end points to aItemList if the item has end points.
void MirrorHorizontally(int aCenter) override
Mirror item horizontally about aCenter.
int GetDiameter() const
KIGFX::COLOR4D GetJunctionColor() const
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
void GetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, std::vector< MSG_PANEL_ITEM > &aList) override
Populate aList of MSG_PANEL_ITEM objects with it's internal state for display purposes.
bool doIsConnected(const VECTOR2I &aPosition) const override
Provide the object specific test to see if it is connected to aPosition.
std::vector< VECTOR2I > GetConnectionPoints() const override
Add all the connection points for this item to aPoints.
bool HasConnectivityChanges(const SCH_ITEM *aItem, const SCH_SHEET_PATH *aInstance=nullptr) const override
Check if aItem has connectivity changes against this object.
VECTOR2I GetPosition() const override
KIGFX::COLOR4D m_color
KIGFX::COLOR4D::UNSPECIFIED is user default.
KIGFX::COLOR4D GetColor() const
void MirrorVertically(int aCenter) override
Mirror item vertically about aCenter.
void Rotate(const VECTOR2I &aCenter, bool aRotateCCW) override
Rotate the item around aCenter 90 degrees in the clockwise direction.
int m_lastResolvedDiameter
wxString GetClass() const override
Return the class name.
std::vector< int > ViewGetLayers() const override
Return the layers the item is drawn on (which may be more than its "home" layer)
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
KIGFX::COLOR4D m_lastResolvedColor
bool HitTest(const VECTOR2I &aPosition, int aAccuracy=0) const override
Test if aPosition is inside or on the boundary of this item.
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Definition seg.h:38
bool Collide(const SEG &aSeg, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if the boundary of shape (this) lies closer to the segment aSeg than aClearance,...
int GetRadius() const
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
bool Collide(const SHAPE *aShape, int aClearance, VECTOR2I *aActual) const override
Check if the boundary of shape (this) lies closer to the shape aShape than aClearance,...
Definition shape_rect.h:147
wxString MessageTextFromValue(double aValue, bool aAddUnitLabel=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
A lower-precision version of StringFromValue().
#define DEFAULT_JUNCTION_DIAM
The default junction diameter in mils. (can be changed in preference menu)
#define DEFAULT_WIRE_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:474
static constexpr EDA_ANGLE ANGLE_270
Definition eda_angle.h:477
@ FILLED_SHAPE
Fill with object color.
Definition eda_fill.h:31
#define STRUCT_DELETED
flag indication structures to be erased
#define SKIP_STRUCT
flag indicating that the structure should be ignored
a few functions useful in geometry calculations.
SCH_LAYER_ID
Eeschema drawing layers.
Definition layer_ids.h:480
@ LAYER_SELECTION_SHADOWS
Definition layer_ids.h:526
constexpr void MIRROR(T &aPoint, const T &aMirrorRef)
Updates aPoint with the mirror of aPoint relative to the aMirrorRef.
Definition mirror.h:44
bool ShapeHitTest(const SHAPE_LINE_CHAIN &aHitter, const SHAPE &aHittee, bool aHitteeContained)
Perform a shape-to-shape hit test.
std::optional< wxString > AppendConnectionInfo(const SCH_ITEM &aItem, std::vector< MSG_PANEL_ITEM > &aList, const SCH_SHEET_PATH *aPath=nullptr)
Append connection details; return its name for a signal, or no value for a bus/missing row.
KICOMMON_API void PackColor(types::Color &aOutput, const KIGFX::COLOR4D &aInput)
KICOMMON_API int UnpackDistance(const types::Distance &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API KIGFX::COLOR4D UnpackColor(const types::Color &aInput)
KICOMMON_API void PackCustomProperties(google::protobuf::RepeatedPtrField< types::CustomProperty > *aOutput, const EDA_ITEM &aItem)
KICOMMON_API VECTOR2I UnpackVector2(const types::Vector2 &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API void PackDistance(types::Distance &aOutput, int aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API void PackVector2(types::Vector2 &aOutput, const VECTOR2I &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API void UnpackCustomProperties(const google::protobuf::RepeatedPtrField< types::CustomProperty > &aInput, EDA_ITEM &aItem)
#define _HKI(x)
Definition page_info.cpp:40
#define TYPE_HASH(x)
Macro to generate unique identifier for a type.
Definition property.h:74
@ PT_SIZE
Size expressed in distance units (mm/inch)
Definition property.h:63
#define REGISTER_TYPE(x)
Helper macro to map type hashes to names.
@ JUNCTION_END
Definition sch_item.h:86
static struct SCH_JUNCTION_DESC _SCH_JUNCTION_DESC
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_JUNCTION_T
Definition typeinfo.h:155
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708