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, 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 <sch_draw_panel.h>
26#include <trigo.h>
27#include <common.h>
28#include <plotters/plotter.h>
29#include <bitmaps.h>
30#include <core/mirror.h>
31#include <geometry/shape_rect.h>
33#include <sch_painter.h>
34#include <sch_junction.h>
35#include <sch_edit_frame.h>
36#include <sch_connection.h>
37#include <schematic.h>
39#include <connection_graph.h>
40#include <string_utils.h>
41#include <api/api_utils.h>
42#include <api/schematic/schematic_types.pb.h>
43#include <properties/property.h>
45
46
47SCH_JUNCTION::SCH_JUNCTION( const VECTOR2I& aPosition, int aDiameter, SCH_LAYER_ID aLayer ) :
48 SCH_ITEM( nullptr, SCH_JUNCTION_T )
49{
50 m_pos = aPosition;
52 m_diameter = aDiameter;
53 m_layer = aLayer;
54
57}
58
59
61{
62 return new SCH_JUNCTION( *this );
63}
64
65
66void SCH_JUNCTION::Serialize( google::protobuf::Any& aContainer ) const
67{
68 using namespace kiapi::common;
69
70 kiapi::schematic::types::Junction junction;
71
72 junction.mutable_id()->set_value( m_Uuid.AsStdString() );
73 PackVector2( *junction.mutable_position(), m_pos, schIUScale );
74 PackDistance( *junction.mutable_diameter(), m_diameter, schIUScale );
75
77 PackColor( *junction.mutable_color(), m_color );
78
79 junction.set_locked( IsLocked() ? types::LockedState::LS_LOCKED
80 : types::LockedState::LS_UNLOCKED );
81
82 aContainer.PackFrom( junction );
83}
84
85
86bool SCH_JUNCTION::Deserialize( const google::protobuf::Any& aContainer )
87{
88 using namespace kiapi::common;
89
90 kiapi::schematic::types::Junction junction;
91
92 if( !aContainer.UnpackTo( &junction ) )
93 return false;
94
95 const_cast<KIID&>( m_Uuid ) = KIID( junction.id().value() );
96 m_pos = UnpackVector2( junction.position(), schIUScale );
97 m_diameter = UnpackDistance( junction.diameter(), schIUScale );
98
99 if( junction.has_color() )
100 m_color = UnpackColor( junction.color() );
101 else
103
104 SetLocked( junction.locked() == types::LockedState::LS_LOCKED );
105 return true;
106}
107
108
110{
111 wxCHECK_RET( ( aItem != nullptr ) && ( aItem->Type() == SCH_JUNCTION_T ),
112 wxT( "Cannot swap junction data with invalid item." ) );
113
114 SCH_JUNCTION* item = (SCH_JUNCTION*) aItem;
115 std::swap( m_pos, item->m_pos );
116 std::swap( m_diameter, item->m_diameter );
117 std::swap( m_color, item->m_color );
118}
119
120
121std::vector<int> SCH_JUNCTION::ViewGetLayers() const
122{
124}
125
126
128{
129 if( m_diameter != 0 )
131 else if( Schematic() )
133 else
135
136 if( m_lastResolvedDiameter != 1 ) // Diameter 1 means user doesn't want to draw junctions
137 {
138 // If we know what we're connected to, then enforce a minimum size of 170% of the
139 // connected wire width:
140 if( !IsConnectivityDirty() )
141 {
143 GetEffectiveNetClass()->GetWireWidth() * 1.7 );
144 }
145 }
146
147 return SHAPE_CIRCLE( m_pos, std::max( m_lastResolvedDiameter / 2, 1 ) );
148}
149
150
152{
153 BOX2I bbox( m_pos );
154 bbox.Inflate( getEffectiveShape().GetRadius() );
155
156 return bbox;
157}
158
159
161{
162 MIRROR( m_pos.y, aCenter );
163}
164
165
167{
168 MIRROR( m_pos.x, aCenter );
169}
170
171
172void SCH_JUNCTION::Rotate( const VECTOR2I& aCenter, bool aRotateCCW )
173{
174 RotatePoint( m_pos, aCenter, aRotateCCW ? ANGLE_90 : ANGLE_270 );
175}
176
177
178void SCH_JUNCTION::GetEndPoints( std::vector <DANGLING_END_ITEM>& aItemList )
179{
180 DANGLING_END_ITEM item( JUNCTION_END, this, m_pos );
181 aItemList.push_back( item );
182}
183
184
185std::vector<VECTOR2I> SCH_JUNCTION::GetConnectionPoints() const
186{
187 return { m_pos };
188}
189
190
191#if defined(DEBUG)
192void SCH_JUNCTION::Show( int nestLevel, std::ostream& os ) const
193{
194 // XML output:
195 wxString s = GetClass();
196
197 NestedSpace( nestLevel, os ) << '<' << s.Lower().mb_str() << m_pos << ", " << m_diameter
198 << "/>\n";
199}
200#endif
201
202
203void SCH_JUNCTION::SetDiameter( int aDiameter )
204{
205 m_diameter = aDiameter;
206 m_lastResolvedDiameter = aDiameter;
207}
208
209
219
220
221void SCH_JUNCTION::SetColor( const COLOR4D& aColor )
222{
223 m_color = aColor;
224 m_lastResolvedColor = aColor;
225}
226
227
229{
230 return getEffectiveShape().GetRadius() * 2;
231}
232
233
234bool SCH_JUNCTION::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
235{
236 if( aAccuracy >= 0 )
237 return getEffectiveShape().Collide( SEG( aPosition, aPosition ), aAccuracy );
238 else
239 return aPosition == m_pos;
240}
241
242
243bool SCH_JUNCTION::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
244{
246 return false;
247
248 if( aContained )
249 {
250 BOX2I selRect( aRect );
251
252 return selRect.Inflate( aAccuracy ).Contains( GetBoundingBox() );
253 }
254 else
255 {
256 SHAPE_CIRCLE junction = getEffectiveShape();
257 SHAPE_RECT selRect( aRect.GetPosition(), aRect.GetWidth(), aRect.GetHeight() );
258
259 return selRect.Collide( &junction, aAccuracy );
260 }
261}
262
263
264bool SCH_JUNCTION::HitTest( const SHAPE_LINE_CHAIN& aPoly, bool aContained ) const
265{
267 return false;
268
269 return KIGEOM::ShapeHitTest( aPoly, getEffectiveShape(), aContained );
270}
271
272
274 const SCH_SHEET_PATH* aInstance ) const
275{
276 // Do not compare to ourself.
277 if( aItem == this )
278 return false;
279
280 const SCH_JUNCTION* junction = dynamic_cast<const SCH_JUNCTION*>( aItem );
281
282 // Don't compare against a different SCH_ITEM.
283 wxCHECK( junction, false );
284
285 return GetPosition() != junction->GetPosition();
286}
287
288
289bool SCH_JUNCTION::doIsConnected( const VECTOR2I& aPosition ) const
290{
291 return m_pos == aPosition;
292}
293
294
295void SCH_JUNCTION::Plot( PLOTTER* aPlotter, bool aBackground, const SCH_PLOT_OPTS& aPlotOpts,
296 int aUnit, int aBodyStyle, const VECTOR2I& aOffset, bool aDimmed )
297{
298 if( aBackground )
299 return;
300
301 RENDER_SETTINGS* settings = aPlotter->RenderSettings();
302 COLOR4D color = GetJunctionColor();
303
304 if( color == COLOR4D::UNSPECIFIED )
305 color = settings->GetLayerColor( GetLayer() );
306
307 if( color.m_text && Schematic() )
308 color = COLOR4D( ResolveText( *color.m_text, &Schematic()->CurrentSheet() ) );
309
310 aPlotter->SetColor( color );
311
313}
314
315
320
321
322bool SCH_JUNCTION::operator <( const SCH_ITEM& aItem ) const
323{
324 if( Type() != aItem.Type() )
325 return Type() < aItem.Type();
326
327 if( GetLayer() != aItem.GetLayer() )
328 return GetLayer() < aItem.GetLayer();
329
330 const SCH_JUNCTION* junction = static_cast<const SCH_JUNCTION*>( &aItem );
331
332 if( GetPosition().x != junction->GetPosition().x )
333 return GetPosition().x < junction->GetPosition().x;
334
335 if( GetPosition().y != junction->GetPosition().y )
336 return GetPosition().y < junction->GetPosition().y;
337
338 if( GetDiameter() != junction->GetDiameter() )
339 return GetDiameter() < junction->GetDiameter();
340
341 return GetColor() < junction->GetColor();
342}
343
344
345void SCH_JUNCTION::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
346{
347 aList.emplace_back( _( "Junction" ), wxEmptyString );
348
349 aList.emplace_back( _( "Size" ), aFrame->MessageTextFromValue( GetEffectiveDiameter() ) );
350
351 SCH_CONNECTION* conn = nullptr;
352
353 if( !IsConnectivityDirty() && dynamic_cast<SCH_EDIT_FRAME*>( aFrame ) )
354 conn = Connection();
355
356 if( conn )
357 {
358 conn->AppendInfoToMsgPanel( aList );
359
360 if( !conn->IsBus() )
361 {
362 aList.emplace_back( _( "Resolved Netclass" ),
363 UnescapeString( GetEffectiveNetClass()->GetHumanReadableName() ) );
364 }
365 }
366}
367
368
369bool SCH_JUNCTION::operator==( const SCH_ITEM& aOther ) const
370{
371 if( Type() != aOther.Type() )
372 return false;
373
374 const SCH_JUNCTION& other = static_cast<const SCH_JUNCTION&>( aOther );
375
376 if( m_pos != other.m_pos )
377 return false;
378
379 if( m_diameter != other.m_diameter )
380 return false;
381
382 if( m_color != other.m_color )
383 return false;
384
385 return true;
386}
387
388
389double SCH_JUNCTION::Similarity( const SCH_ITEM& aOther ) const
390{
391 if( m_Uuid == aOther.m_Uuid )
392 return 1.0;
393
394 if( aOther.Type() != Type() )
395 return 0.0;
396
397 const SCH_JUNCTION& other = static_cast<const SCH_JUNCTION&>( aOther );
398
399 double similarity = 1.0;
400
401 if( m_pos != other.m_pos )
402 similarity *= 0.9;
403
404 if( m_diameter != other.m_diameter )
405 similarity *= 0.9;
406
407 if( m_color != other.m_color )
408 similarity *= 0.9;
409
410 return similarity;
411}
412
413
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:127
BITMAPS
A list of all bitmap identifiers.
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:990
constexpr const Vec & GetPosition() const
Definition box2.h:211
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 size_type GetWidth() const
Definition box2.h:214
constexpr size_type GetHeight() const
Definition box2.h:215
constexpr bool Contains(const Vec &aPoint) const
Definition box2.h:168
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition color4d.h:402
Helper class used to store the state of schematic items that can be connected to other schematic item...
Definition sch_item.h:97
The base class for create windows for drawing purpose.
const KIID m_Uuid
Definition eda_item.h:528
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:112
EDA_ITEM_FLAGS m_flags
Definition eda_item.h:539
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType, bool isSCH_ITEM=false, bool isBOARD_ITEM=false)
Definition eda_item.cpp:41
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:105
std::shared_ptr< wxString > m_text
Definition color4d.h:399
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:48
COLOR4D GetSchematicColor(bool aIsForSave=false) const
Definition netclass.h:216
Base plotter engine class.
Definition plotter.h:136
virtual void Circle(const VECTOR2I &pos, int diametre, FILL_T fill, int width)=0
RENDER_SETTINGS * RenderSettings()
Definition plotter.h:167
virtual void SetColor(const COLOR4D &color)=0
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()
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
Each graphical item can have a SCH_CONNECTION describing its logical connection (to a bus or net).
bool IsBus() const
void AppendInfoToMsgPanel(std::vector< MSG_PANEL_ITEM > &aList) const
Adds information about the connection object to aList.
Schematic editor (Eeschema) main window.
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
SCHEMATIC * Schematic() const
Search the item hierarchy to find a SCHEMATIC.
Definition sch_item.cpp:272
bool IsLocked() const override
Definition sch_item.cpp:152
std::shared_ptr< NETCLASS > GetEffectiveNetClass(const SCH_SHEET_PATH *aSheet=nullptr) const
Definition sch_item.cpp:527
SCH_LAYER_ID GetLayer() const
Return the layer this item is on.
Definition sch_item.h:344
bool IsConnectivityDirty() const
Definition sch_item.h:591
SCH_ITEM(EDA_ITEM *aParent, KICAD_T aType, int aUnit=0, int aBodyStyle=0)
Definition sch_item.cpp:56
SCH_CONNECTION * Connection(const SCH_SHEET_PATH *aSheet=nullptr) const
Retrieve the connection associated with this object in the given sheet.
Definition sch_item.cpp:491
wxString ResolveText(const wxString &aText, const SCH_SHEET_PATH *aPath, int aDepth=0) const
Definition sch_item.cpp:381
SCH_LAYER_ID m_layer
Definition sch_item.h:781
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.
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.
COLOR4D m_lastResolvedColor
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.
COLOR4D m_color
COLOR4D::UNSPECIFIED is user default.
void MirrorHorizontally(int aCenter) override
Mirror item horizontally about aCenter.
COLOR4D GetColor() const
int GetDiameter() const
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
void SetColor(const COLOR4D &aColor)
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.
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:42
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 *aMTV) const override
Check if the boundary of shape (this) lies closer to the shape aShape than aClearance,...
Definition shape_rect.h:151
wxString MessageTextFromValue(double aValue, bool aAddUnitLabel=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
A lower-precision version of StringFromValue().
The common library.
#define DEFAULT_JUNCTION_DIAM
The default bus and wire entry size in mils.
#define DEFAULT_WIRE_WIDTH_MILS
The default bus 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
#define STRUCT_DELETED
flag indication structures to be erased
#define SKIP_STRUCT
flag indicating that the structure should be ignored
@ FILLED_SHAPE
Fill with object color.
Definition eda_shape.h:61
a few functions useful in geometry calculations.
SCH_LAYER_ID
Eeschema drawing layers.
Definition layer_ids.h:451
@ LAYER_SELECTION_SHADOWS
Definition layer_ids.h:497
constexpr void MIRROR(T &aPoint, const T &aMirrorRef)
Updates aPoint with the mirror of aPoint relative to the aMirrorRef.
Definition mirror.h:45
bool ShapeHitTest(const SHAPE_LINE_CHAIN &aHitter, const SHAPE &aHittee, bool aHitteeContained)
Perform a shape-to-shape hit test.
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 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)
#define _HKI(x)
Definition page_info.cpp:44
#define TYPE_HASH(x)
Definition property.h:74
@ PT_SIZE
Size expressed in distance units (mm/inch)
Definition property.h:63
#define REGISTER_TYPE(x)
@ JUNCTION_END
Definition sch_item.h:82
static struct SCH_JUNCTION_DESC _SCH_JUNCTION_DESC
wxString UnescapeString(const wxString &aSource)
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_JUNCTION_T
Definition typeinfo.h:160
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687