KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_no_connect.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) 2015 Jean-Pierre Charras, jp.charras at wanoadoo.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
25
26#include <sch_draw_panel.h>
27#include <plotters/plotter.h>
28#include <bitmaps.h>
29#include <schematic.h>
31#include <sch_no_connect.h>
33#include <default_values.h> // For some default values
34#include <core/mirror.h>
35#include <trigo.h>
36#include <gr_basic.h>
37#include <api/api_utils.h>
38#include <api/schematic/schematic_types.pb.h>
39
40
42 SCH_ITEM( nullptr, SCH_NO_CONNECT_T )
43{
44 m_pos = pos;
45 m_size = schIUScale.MilsToIU( DEFAULT_NOCONNECT_SIZE ); // Default no-connect symbol size.
46
48}
49
50
52{
53 return new SCH_NO_CONNECT( *this );
54}
55
56
57void SCH_NO_CONNECT::Serialize( google::protobuf::Any& aContainer ) const
58{
59 using namespace kiapi::common;
60
61 kiapi::schematic::types::NoConnectMarker marker;
62
63 marker.mutable_id()->set_value( m_Uuid.AsStdString() );
64 PackVector2( *marker.mutable_position(), m_pos, schIUScale );
65 PackDistance( *marker.mutable_size(), m_size, schIUScale );
66 marker.set_locked( IsLocked() ? types::LockedState::LS_LOCKED
67 : types::LockedState::LS_UNLOCKED );
68
69 aContainer.PackFrom( marker );
70}
71
72
73bool SCH_NO_CONNECT::Deserialize( const google::protobuf::Any& aContainer )
74{
75 using namespace kiapi::common;
76
77 kiapi::schematic::types::NoConnectMarker marker;
78
79 if( !aContainer.UnpackTo( &marker ) )
80 return false;
81
82 const_cast<KIID&>( m_Uuid ) = KIID( marker.id().value() );
83 m_pos = UnpackVector2( marker.position(), schIUScale );
84 m_size = UnpackDistance( marker.size(), schIUScale );
85 SetLocked( marker.locked() == types::LockedState::LS_LOCKED );
86 return true;
87}
88
89
91{
92 wxCHECK_RET( ( aItem != nullptr ) && ( aItem->Type() == SCH_NO_CONNECT_T ),
93 wxT( "Cannot swap no connect data with invalid item." ) );
94
95 SCH_NO_CONNECT* item = (SCH_NO_CONNECT*)aItem;
96 std::swap( m_pos, item->m_pos );
97 std::swap( m_size, item->m_size );
98}
99
100
102{
103 int delta = ( GetPenWidth() + GetSize() ) / 2;
104 BOX2I bbox( m_pos );
105
106 bbox.Inflate( delta );
107
108 return bbox;
109}
110
111
112std::vector<int> SCH_NO_CONNECT::ViewGetLayers() const
113{
115}
116
117
118void SCH_NO_CONNECT::GetEndPoints( std::vector< DANGLING_END_ITEM >& aItemList )
119{
121 aItemList.push_back( item );
122}
123
124
126{
127 if( !Schematic() )
128 return 1;
129
130 return std::max( Schematic()->Settings().m_DefaultLineWidth, 1 );
131}
132
133
135{
136 MIRROR( m_pos.y, aCenter );
137}
138
139
141{
142 MIRROR( m_pos.x, aCenter );
143}
144
145
146void SCH_NO_CONNECT::Rotate( const VECTOR2I& aCenter, bool aRotateCCW )
147{
148 RotatePoint( m_pos, aCenter, aRotateCCW ? ANGLE_90 : ANGLE_270 );
149}
150
151
153 const SCH_SHEET_PATH* aInstance ) const
154{
155 // Do not compare to ourself.
156 if( aItem == this )
157 return false;
158
159 const SCH_NO_CONNECT* noConnect = dynamic_cast<const SCH_NO_CONNECT*>( aItem );
160
161 // Don't compare against a different SCH_ITEM.
162 wxCHECK( noConnect, false );
163
164 return GetPosition() != noConnect->GetPosition();
165}
166
167
168std::vector<VECTOR2I> SCH_NO_CONNECT::GetConnectionPoints() const
169{
170 return { m_pos };
171}
172
173
174bool SCH_NO_CONNECT::doIsConnected( const VECTOR2I& aPosition ) const
175{
176 return m_pos == aPosition;
177}
178
179
180bool SCH_NO_CONNECT::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
181{
182 int delta = ( GetPenWidth() + GetSize() ) / 2 + aAccuracy;
183
184 VECTOR2I dist = aPosition - m_pos;
185
186 if( ( std::abs( dist.x ) <= delta ) && ( std::abs( dist.y ) <= delta ) )
187 return true;
188
189 return false;
190}
191
192
193bool SCH_NO_CONNECT::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
194{
195 BOX2I rect = aRect;
196
197 rect.Inflate( aAccuracy );
198
199 if( aContained )
200 return rect.Contains( GetBoundingBox() );
201
202 return rect.Intersects( GetBoundingBox() );
203}
204
205
206bool SCH_NO_CONNECT::HitTest( const SHAPE_LINE_CHAIN& aPoly, bool aContained ) const
207{
208 return KIGEOM::BoxHitTest( aPoly, GetBoundingBox(), aContained );
209}
210
211
212void SCH_NO_CONNECT::Plot( PLOTTER* aPlotter, bool aBackground, const SCH_PLOT_OPTS& aPlotOpts,
213 int aUnit, int aBodyStyle, const VECTOR2I& aOffset, bool aDimmed )
214{
215 if( aBackground )
216 return;
217
218 int delta = GetSize() / 2;
219 int pX = m_pos.x;
220 int pY = m_pos.y;
221 int penWidth = GetEffectivePenWidth( getRenderSettings( aPlotter ) );
222
223 COLOR4D color = aPlotter->RenderSettings()->GetLayerColor( LAYER_NOCONNECT );
224
225 if( color.m_text && Schematic() )
226 color = COLOR4D( ResolveText( *color.m_text, &Schematic()->CurrentSheet() ) );
227
228 aPlotter->SetCurrentLineWidth( penWidth );
229 aPlotter->SetColor( color );
230 aPlotter->MoveTo( VECTOR2I( pX - delta, pY - delta ) );
231 aPlotter->FinishTo( VECTOR2I( pX + delta, pY + delta ) );
232 aPlotter->MoveTo( VECTOR2I( pX + delta, pY - delta ) );
233 aPlotter->FinishTo( VECTOR2I( pX - delta, pY + delta ) );
234}
235
236
241
242
243bool SCH_NO_CONNECT::operator==( const SCH_ITEM& aOther ) const
244{
245 if( aOther.Type() != Type() )
246 return false;
247
248 const SCH_NO_CONNECT* other = static_cast<const SCH_NO_CONNECT*>( &aOther );
249
250 if( m_pos != other->m_pos )
251 return false;
252
253 return true;
254}
255
256
257double SCH_NO_CONNECT::Similarity( const SCH_ITEM& aOther ) const
258{
259 if( m_Uuid == aOther.m_Uuid )
260 return 1.0;
261
262 if( aOther.Type() != Type() )
263 return 0.0;
264
265 const SCH_NO_CONNECT* other = static_cast<const SCH_NO_CONNECT*>( &aOther );
266
267 if( m_pos != other->m_pos )
268 return 0.0;
269
270 return 1.0;
271}
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:123
BITMAPS
A list of all bitmap identifiers.
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
constexpr bool Intersects(const BOX2< Vec > &aRect) const
Definition box2.h:307
Helper class used to store the state of schematic items that can be connected to other schematic item...
Definition sch_item.h:93
const KIID m_Uuid
Definition eda_item.h:531
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:108
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType, bool isSCH_ITEM=false, bool isBOARD_ITEM=false)
Definition eda_item.cpp:37
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
std::shared_ptr< wxString > m_text
Definition color4d.h:395
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
Definition kiid.h:44
Base plotter engine class.
Definition plotter.h:133
void MoveTo(const VECTOR2I &pos)
Definition plotter.h:305
void FinishTo(const VECTOR2I &pos)
Definition plotter.h:315
RENDER_SETTINGS * RenderSettings()
Definition plotter.h:164
virtual void SetCurrentLineWidth(int width, void *aData=nullptr)=0
Set the line width for the next drawing.
virtual void SetColor(const COLOR4D &color)=0
void SetLocked(bool aLocked) override
Definition sch_item.h:251
SCH_RENDER_SETTINGS * getRenderSettings(PLOTTER *aPlotter) const
Definition sch_item.h:724
SCHEMATIC * Schematic() const
Search the item hierarchy to find a SCHEMATIC.
Definition sch_item.cpp:268
bool IsLocked() const override
Definition sch_item.cpp:148
void SetLayer(SCH_LAYER_ID aLayer)
Definition sch_item.h:339
SCH_ITEM(EDA_ITEM *aParent, KICAD_T aType, int aUnit=0, int aBodyStyle=0)
Definition sch_item.cpp:52
wxString ResolveText(const wxString &aText, const SCH_SHEET_PATH *aPath, int aDepth=0) const
Definition sch_item.cpp:377
int GetEffectivePenWidth(const SCH_RENDER_SETTINGS *aSettings) const
Definition sch_item.cpp:790
bool operator==(const SCH_ITEM &aOther) const override
BITMAPS GetMenuImage() const override
Return a pointer to an image to be used in menus.
std::vector< VECTOR2I > GetConnectionPoints() const override
Add all the connection points for this item to aPoints.
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.
SCH_NO_CONNECT(const VECTOR2I &pos=VECTOR2I(0, 0))
int m_size
Size of the no connect object.
int GetSize() const
void MirrorVertically(int aCenter) override
Mirror item vertically about aCenter.
bool HitTest(const VECTOR2I &aPosition, int aAccuracy=0) const override
Test if aPosition is inside or on the boundary of this item.
VECTOR2I m_pos
Position of the no connect object.
double Similarity(const SCH_ITEM &aOther) const override
Return a measure of how likely the other object is to represent the same object.
std::vector< int > ViewGetLayers() const override
Return the layers the item is drawn on (which may be more than its "home" layer)
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.
bool doIsConnected(const VECTOR2I &aPosition) const override
Provide the object specific test to see if it is connected to aPosition.
void Rotate(const VECTOR2I &aCenter, bool aRotateCCW) override
Rotate the item around aCenter 90 degrees in the clockwise direction.
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
void GetEndPoints(std::vector< DANGLING_END_ITEM > &aItemList) override
Add the schematic item end points to aItemList if the item has end points.
VECTOR2I GetPosition() const override
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
int GetPenWidth() const override
void MirrorHorizontally(int aCenter) override
Mirror item horizontally about aCenter.
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
#define DEFAULT_NOCONNECT_SIZE
The default junction diameter in mils. (can be changed in preference menu)
static constexpr EDA_ANGLE ANGLE_90
Definition eda_angle.h:413
static constexpr EDA_ANGLE ANGLE_270
Definition eda_angle.h:416
a few functions useful in geometry calculations.
@ LAYER_SELECTION_SHADOWS
Definition layer_ids.h:493
@ LAYER_NOCONNECT
Definition layer_ids.h:474
constexpr void MIRROR(T &aPoint, const T &aMirrorRef)
Updates aPoint with the mirror of aPoint relative to the aMirrorRef.
Definition mirror.h:41
bool BoxHitTest(const VECTOR2I &aHitPoint, const BOX2I &aHittee, int aAccuracy)
Perform a point-to-box hit test.
KICOMMON_API int UnpackDistance(const types::Distance &aInput, const EDA_IU_SCALE &aScale)
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)
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:400
@ NO_CONNECT_END
Definition sch_item.h:84
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_NO_CONNECT_T
Definition typeinfo.h:157
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683