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, 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
29
30#include <sch_draw_panel.h>
31#include <plotters/plotter.h>
32#include <bitmaps.h>
33#include <schematic.h>
35#include <sch_no_connect.h>
37#include <default_values.h> // For some default values
38#include <core/mirror.h>
39#include <trigo.h>
40#include <gr_basic.h>
41#include <api/api_utils.h>
42#include <api/schematic/schematic_types.pb.h>
43
44
46 SCH_ITEM( nullptr, SCH_NO_CONNECT_T )
47{
48 m_pos = pos;
49 m_size = schIUScale.MilsToIU( DEFAULT_NOCONNECT_SIZE ); // Default no-connect symbol size.
50
52}
53
54
56{
57 return new SCH_NO_CONNECT( *this );
58}
59
60
61void SCH_NO_CONNECT::Serialize( google::protobuf::Any& aContainer ) const
62{
63 using namespace kiapi::common;
64
65 kiapi::schematic::types::NoConnectMarker marker;
66
67 marker.mutable_id()->set_value( m_Uuid.AsStdString() );
68 PackVector2( *marker.mutable_position(), m_pos, schIUScale );
69 PackDistance( *marker.mutable_size(), m_size, schIUScale );
70 marker.set_locked( IsLocked() ? types::LockedState::LS_LOCKED
71 : types::LockedState::LS_UNLOCKED );
72
73 aContainer.PackFrom( marker );
74}
75
76
77bool SCH_NO_CONNECT::Deserialize( const google::protobuf::Any& aContainer )
78{
79 using namespace kiapi::common;
80
81 kiapi::schematic::types::NoConnectMarker marker;
82
83 if( !aContainer.UnpackTo( &marker ) )
84 return false;
85
86 const_cast<KIID&>( m_Uuid ) = KIID( marker.id().value() );
87 m_pos = UnpackVector2( marker.position(), schIUScale );
88 m_size = UnpackDistance( marker.size(), schIUScale );
89 SetLocked( marker.locked() == types::LockedState::LS_LOCKED );
90 return true;
91}
92
93
95{
96 wxCHECK_RET( ( aItem != nullptr ) && ( aItem->Type() == SCH_NO_CONNECT_T ),
97 wxT( "Cannot swap no connect data with invalid item." ) );
98
99 SCH_NO_CONNECT* item = (SCH_NO_CONNECT*)aItem;
100 std::swap( m_pos, item->m_pos );
101 std::swap( m_size, item->m_size );
102}
103
104
106{
107 int delta = ( GetPenWidth() + GetSize() ) / 2;
108 BOX2I bbox( m_pos );
109
110 bbox.Inflate( delta );
111
112 return bbox;
113}
114
115
116std::vector<int> SCH_NO_CONNECT::ViewGetLayers() const
117{
119}
120
121
122void SCH_NO_CONNECT::GetEndPoints( std::vector< DANGLING_END_ITEM >& aItemList )
123{
125 aItemList.push_back( item );
126}
127
128
130{
131 if( !Schematic() )
132 return 1;
133
134 return std::max( Schematic()->Settings().m_DefaultLineWidth, 1 );
135}
136
137
139{
140 MIRROR( m_pos.y, aCenter );
141}
142
143
145{
146 MIRROR( m_pos.x, aCenter );
147}
148
149
150void SCH_NO_CONNECT::Rotate( const VECTOR2I& aCenter, bool aRotateCCW )
151{
152 RotatePoint( m_pos, aCenter, aRotateCCW ? ANGLE_90 : ANGLE_270 );
153}
154
155
157 const SCH_SHEET_PATH* aInstance ) const
158{
159 // Do not compare to ourself.
160 if( aItem == this )
161 return false;
162
163 const SCH_NO_CONNECT* noConnect = dynamic_cast<const SCH_NO_CONNECT*>( aItem );
164
165 // Don't compare against a different SCH_ITEM.
166 wxCHECK( noConnect, false );
167
168 return GetPosition() != noConnect->GetPosition();
169}
170
171
172std::vector<VECTOR2I> SCH_NO_CONNECT::GetConnectionPoints() const
173{
174 return { m_pos };
175}
176
177
178bool SCH_NO_CONNECT::doIsConnected( const VECTOR2I& aPosition ) const
179{
180 return m_pos == aPosition;
181}
182
183
184bool SCH_NO_CONNECT::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
185{
186 int delta = ( GetPenWidth() + GetSize() ) / 2 + aAccuracy;
187
188 VECTOR2I dist = aPosition - m_pos;
189
190 if( ( std::abs( dist.x ) <= delta ) && ( std::abs( dist.y ) <= delta ) )
191 return true;
192
193 return false;
194}
195
196
197bool SCH_NO_CONNECT::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
198{
199 BOX2I rect = aRect;
200
201 rect.Inflate( aAccuracy );
202
203 if( aContained )
204 return rect.Contains( GetBoundingBox() );
205
206 return rect.Intersects( GetBoundingBox() );
207}
208
209
210bool SCH_NO_CONNECT::HitTest( const SHAPE_LINE_CHAIN& aPoly, bool aContained ) const
211{
212 return KIGEOM::BoxHitTest( aPoly, GetBoundingBox(), aContained );
213}
214
215
216void SCH_NO_CONNECT::Plot( PLOTTER* aPlotter, bool aBackground, const SCH_PLOT_OPTS& aPlotOpts,
217 int aUnit, int aBodyStyle, const VECTOR2I& aOffset, bool aDimmed )
218{
219 if( aBackground )
220 return;
221
222 int delta = GetSize() / 2;
223 int pX = m_pos.x;
224 int pY = m_pos.y;
225 int penWidth = GetEffectivePenWidth( getRenderSettings( aPlotter ) );
226
227 COLOR4D color = aPlotter->RenderSettings()->GetLayerColor( LAYER_NOCONNECT );
228
229 if( color.m_text && Schematic() )
230 color = COLOR4D( ResolveText( *color.m_text, &Schematic()->CurrentSheet() ) );
231
232 aPlotter->SetCurrentLineWidth( penWidth );
233 aPlotter->SetColor( color );
234 aPlotter->MoveTo( VECTOR2I( pX - delta, pY - delta ) );
235 aPlotter->FinishTo( VECTOR2I( pX + delta, pY + delta ) );
236 aPlotter->MoveTo( VECTOR2I( pX + delta, pY - delta ) );
237 aPlotter->FinishTo( VECTOR2I( pX - delta, pY + delta ) );
238}
239
240
245
246
247bool SCH_NO_CONNECT::operator==( const SCH_ITEM& aOther ) const
248{
249 if( aOther.Type() != Type() )
250 return false;
251
252 const SCH_NO_CONNECT* other = static_cast<const SCH_NO_CONNECT*>( &aOther );
253
254 if( m_pos != other->m_pos )
255 return false;
256
257 return true;
258}
259
260
261double SCH_NO_CONNECT::Similarity( const SCH_ITEM& aOther ) const
262{
263 if( m_Uuid == aOther.m_Uuid )
264 return 1.0;
265
266 if( aOther.Type() != Type() )
267 return 0.0;
268
269 const SCH_NO_CONNECT* other = static_cast<const SCH_NO_CONNECT*>( &aOther );
270
271 if( m_pos != other->m_pos )
272 return 0.0;
273
274 return 1.0;
275}
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 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
constexpr bool Intersects(const BOX2< Vec > &aRect) const
Definition box2.h:311
Helper class used to store the state of schematic items that can be connected to other schematic item...
Definition sch_item.h:97
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(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
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
Definition kiid.h:48
Base plotter engine class.
Definition plotter.h:136
void MoveTo(const VECTOR2I &pos)
Definition plotter.h:308
void FinishTo(const VECTOR2I &pos)
Definition plotter.h:318
RENDER_SETTINGS * RenderSettings()
Definition plotter.h:167
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:257
SCH_RENDER_SETTINGS * getRenderSettings(PLOTTER *aPlotter) const
Definition sch_item.h:730
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
void SetLayer(SCH_LAYER_ID aLayer)
Definition sch_item.h:345
SCH_ITEM(EDA_ITEM *aParent, KICAD_T aType, int aUnit=0, int aBodyStyle=0)
Definition sch_item.cpp:56
wxString ResolveText(const wxString &aText, const SCH_SHEET_PATH *aPath, int aDepth=0) const
Definition sch_item.cpp:381
int GetEffectivePenWidth(const SCH_RENDER_SETTINGS *aSettings) const
Definition sch_item.cpp:792
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:497
@ LAYER_NOCONNECT
Definition layer_ids.h:478
constexpr void MIRROR(T &aPoint, const T &aMirrorRef)
Updates aPoint with the mirror of aPoint relative to the aMirrorRef.
Definition mirror.h:45
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:88
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_NO_CONNECT_T
Definition typeinfo.h:161
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687