KiCad PCB EDA Suite
Loading...
Searching...
No Matches
eda_shape.h
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) 2018 Jean-Pierre Charras jp.charras at wanadoo.fr
5 * Copyright (C) 1992-2024 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#ifndef EDA_SHAPE_H
26#define EDA_SHAPE_H
27
28#include <trigo.h>
31#include <properties/property.h>
32#include <stroke_params.h>
33
34class LINE_READER;
35class EDA_DRAW_FRAME;
36class FOOTPRINT;
37class MSG_PANEL_ITEM;
38
39using KIGFX::COLOR4D;
40
41enum class SHAPE_T : int
42{
43 UNDEFINED = -1,
44 SEGMENT = 0,
45 RECTANGLE,
46 ARC,
47 CIRCLE,
48 POLY,
49 BEZIER
50};
51
52
53// WARNING: Do not change these values without updating dialogs that depend on their position values
54enum class FILL_T : int
55{
56 NO_FILL = 1,
57 FILLED_SHAPE, // Fill with object color
58 FILLED_WITH_BG_BODYCOLOR, // Fill with background body color
59 FILLED_WITH_COLOR // Fill with a separate color
60};
61
62
63// Holding struct to keep originating midpoint
64struct ARC_MID
65{
70};
71
73{
74public:
75 EDA_SHAPE( SHAPE_T aType, int aLineWidth, FILL_T aFill );
76
77 // Do not create a copy constructor & operator=.
78 // The ones generated by the compiler are adequate.
79
80 virtual ~EDA_SHAPE();
81
82 void SwapShape( EDA_SHAPE* aImage );
83
84 wxString ShowShape() const;
85
86 wxString SHAPE_T_asString() const;
87
88 virtual bool IsProxyItem() const { return m_proxyItem; }
89 virtual void SetIsProxyItem( bool aIsProxy = true ) { m_proxyItem = aIsProxy; }
90
91 bool IsFilled() const
92 {
93 return GetFillMode() != FILL_T::NO_FILL;
94 }
95
96 void SetFilled( bool aFlag )
97 {
98 setFilled( aFlag );
99 }
100
101 void SetFillMode( FILL_T aFill ) { m_fill = aFill; }
102 FILL_T GetFillMode() const { return m_fill; }
103
104 bool IsClosed() const;
105
106 COLOR4D GetFillColor() const { return m_fillColor; }
107 void SetFillColor( const COLOR4D& aColor ) { m_fillColor = aColor; }
108
109 void SetWidth( int aWidth ) { m_stroke.SetWidth( aWidth ); }
110 virtual int GetWidth() const { return m_stroke.GetWidth(); }
111 virtual int GetEffectiveWidth() const { return GetWidth(); }
112
113 void SetLineStyle( const LINE_STYLE aStyle );
114 LINE_STYLE GetLineStyle() const;
115
116 void SetLineColor( const COLOR4D& aColor ) { m_stroke.SetColor( aColor ); }
117 COLOR4D GetLineColor() const { return m_stroke.GetColor(); }
118
119 void SetShape( SHAPE_T aShape ) { m_shape = aShape; }
120 SHAPE_T GetShape() const { return m_shape; }
121
125 const VECTOR2I& GetStart() const { return m_start; }
126 int GetStartY() const { return m_start.y; }
127 int GetStartX() const { return m_start.x; }
128
129 void SetStart( const VECTOR2I& aStart )
130 {
131 m_start = aStart;
132 m_endsSwapped = false;
133 }
134
135 void SetStartY( int y )
136 {
137 m_start.y = y;
138 m_endsSwapped = false;
139 }
140
141 void SetStartX( int x )
142 {
143 m_start.x = x;
144 m_endsSwapped = false;
145 }
146
147 void SetCenterY( int y )
148 {
149 m_end.y += y - m_start.y;
150 m_start.y = y;
151 }
152
153 void SetCenterX( int x )
154 {
155 m_end.x += x - m_start.x;
156 m_start.x = x;
157 }
158
162 const VECTOR2I& GetEnd() const { return m_end; }
163 int GetEndY() const { return m_end.y; }
164 int GetEndX() const { return m_end.x; }
165
166 void SetEnd( const VECTOR2I& aEnd )
167 {
168 m_end = aEnd;
169 m_endsSwapped = false;
170 }
171
172 void SetEndY( int aY )
173 {
174 m_end.y = aY;
175 m_endsSwapped = false;
176 }
177
178 void SetEndX( int aX )
179 {
180 m_end.x = aX;
181 m_endsSwapped = false;
182 }
183
184 void SetRadius( int aX )
185 {
186 m_end = m_start + VECTOR2I( aX, 0 );
187 }
188
189 virtual VECTOR2I GetTopLeft() const { return GetStart(); }
190 virtual VECTOR2I GetBotRight() const { return GetEnd(); }
191
192 virtual void SetTop( int val ) { SetStartY( val ); }
193 virtual void SetLeft( int val ) { SetStartX( val ); }
194 virtual void SetRight( int val ) { SetEndX( val ); }
195 virtual void SetBottom( int val ) { SetEndY( val ); }
196
197 void SetBezierC1( const VECTOR2I& aPt ) { m_bezierC1 = aPt; }
198 const VECTOR2I& GetBezierC1() const { return m_bezierC1; }
199
200 void SetBezierC2( const VECTOR2I& aPt ) { m_bezierC2 = aPt; }
201 const VECTOR2I& GetBezierC2() const { return m_bezierC2; }
202
203 VECTOR2I getCenter() const;
204 void SetCenter( const VECTOR2I& aCenter );
205
212 void SetArcAngleAndEnd( const EDA_ANGLE& aAngle, bool aCheckNegativeAngle = false );
213
214 EDA_ANGLE GetArcAngle() const;
215
217
222 bool EndsSwapped() const { return m_endsSwapped; }
223
224 // Some attributes are read only, since they are derived from m_Start, m_End, and m_Angle.
225 // No Set...() function for these attributes.
226
227 VECTOR2I GetArcMid() const;
228 std::vector<VECTOR2I> GetRectCorners() const;
229
234 void CalcArcAngles( EDA_ANGLE& aStartAngle, EDA_ANGLE& aEndAngle ) const;
235
236 int GetRadius() const;
237
244 void SetArcGeometry( const VECTOR2I& aStart, const VECTOR2I& aMid, const VECTOR2I& aEnd );
245
255 void SetCachedArcData( const VECTOR2I& aStart, const VECTOR2I& aMid, const VECTOR2I& aEnd, const VECTOR2I& aCenter );
256
257 const std::vector<VECTOR2I>& GetBezierPoints() const { return m_bezierPoints; }
258
266 void DupPolyPointsList( std::vector<VECTOR2I>& aBuffer ) const;
267
271 int GetPointCount() const;
272
273 // Accessors to the polygonal shape
275 const SHAPE_POLY_SET& GetPolyShape() const { return m_poly; }
276
280 bool IsPolyShapeValid() const;
281
282 void SetPolyShape( const SHAPE_POLY_SET& aShape )
283 {
284 m_poly = aShape;
285
286 for( int ii = 0; ii < m_poly.OutlineCount(); ++ii )
287 {
288 if( m_poly.HoleCount( ii ) )
289 {
291 break;
292 }
293 }
294 }
295
296 void SetPolyPoints( const std::vector<VECTOR2I>& aPoints );
297
308 void RebuildBezierToSegmentsPointsList( int aMinSegLen );
309
316 virtual std::vector<SHAPE*> MakeEffectiveShapes( bool aEdgeOnly = false ) const
317 {
318 return makeEffectiveShapes( aEdgeOnly );
319 }
320
321 void ShapeGetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList );
322
323 void SetLength( const double& aLength );
324
325 void SetRectangle( const long long int& aHeight, const long long int& aWidth );
326
327 void SetSegmentAngle( const EDA_ANGLE& aAngle );
328
329 bool IsClockwiseArc() const;
330
334 double GetLength() const;
335
336 int GetRectangleHeight() const;
337 int GetRectangleWidth() const;
338
349 void TransformShapeToPolygon( SHAPE_POLY_SET& aBuffer, int aClearance, int aError,
350 ERROR_LOC aErrorLoc, bool ignoreLineWidth = false ) const;
351
352 int Compare( const EDA_SHAPE* aOther ) const;
353
354 double Similarity( const EDA_SHAPE& aOther ) const;
355
356 bool operator==( const EDA_SHAPE& aOther ) const;
357
358protected:
359 wxString getFriendlyName() const;
360
361 void setPosition( const VECTOR2I& aPos );
362 VECTOR2I getPosition() const;
363
364 virtual void setFilled( bool aFlag )
365 {
366 m_fill = aFlag ? FILL_T::FILLED_SHAPE : FILL_T::NO_FILL;
367 }
368
369 void move( const VECTOR2I& aMoveVector );
370 void rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle );
371 void flip( const VECTOR2I& aCentre, bool aFlipLeftRight );
372 void scale( double aScale );
373
374 const BOX2I getBoundingBox() const;
375
376 void computeArcBBox( BOX2I& aBBox ) const;
377
378 bool hitTest( const VECTOR2I& aPosition, int aAccuracy = 0 ) const;
379 bool hitTest( const BOX2I& aRect, bool aContained, int aAccuracy = 0 ) const;
380
381 const std::vector<VECTOR2I> buildBezierToSegmentsPointsList( int aMinSegLen ) const;
382
383 void beginEdit( const VECTOR2I& aStartPoint );
384 bool continueEdit( const VECTOR2I& aPosition );
385 void calcEdit( const VECTOR2I& aPosition );
386
391 void endEdit( bool aClosed = true );
392 void setEditState( int aState ) { m_editState = aState; }
393
402 // fixme: move to shape_compound
403 std::vector<SHAPE*> makeEffectiveShapes( bool aEdgeOnly, bool aLineChainOnly = false ) const;
404
405protected:
406 bool m_endsSwapped; // true if start/end were swapped e.g. SetArcAngleAndEnd
407 SHAPE_T m_shape; // Shape: line, Circle, Arc
408 STROKE_PARAMS m_stroke; // Line style, width, etc.
411
412 long long int m_rectangleHeight;
413 long long int m_rectangleWidth;
414
417
418 VECTOR2I m_start; // Line start point or Circle center
419 VECTOR2I m_end; // Line end point or Circle 3 o'clock point
420
421 VECTOR2I m_arcCenter; // Used only for Arcs: arc end point
422 ARC_MID m_arcMidData; // Used to store originating data
423
424 VECTOR2I m_bezierC1; // Bezier Control Point 1
425 VECTOR2I m_bezierC2; // Bezier Control Point 2
426
427 std::vector<VECTOR2I> m_bezierPoints;
428 SHAPE_POLY_SET m_poly; // Stores the S_POLYGON shape
429
431 bool m_proxyItem; // A shape storing proxy information (ie: a pad
432 // number box, thermal spoke template, etc.)
433};
434
435#ifndef SWIG
438#endif
439
440#endif // EDA_SHAPE_H
Generic cubic Bezier representation.
Definition: bezier_curves.h:78
The base class for create windows for drawing purpose.
EDA_ANGLE GetArcAngle() const
Definition: eda_shape.cpp:663
SHAPE_T m_shape
Definition: eda_shape.h:407
void SetStartX(int x)
Definition: eda_shape.h:141
bool m_proxyItem
Definition: eda_shape.h:431
bool m_endsSwapped
Definition: eda_shape.h:406
const VECTOR2I & GetBezierC2() const
Definition: eda_shape.h:201
void SetBezierC2(const VECTOR2I &aPt)
Definition: eda_shape.h:200
void SetCenter(const VECTOR2I &aCenter)
Definition: eda_shape.cpp:545
VECTOR2I getCenter() const
Definition: eda_shape.cpp:519
int GetStartY() const
Definition: eda_shape.h:126
void SetLength(const double &aLength)
Definition: eda_shape.cpp:177
int m_editState
Definition: eda_shape.h:430
virtual VECTOR2I GetTopLeft() const
Definition: eda_shape.h:189
void rotate(const VECTOR2I &aRotCentre, const EDA_ANGLE &aAngle)
Definition: eda_shape.cpp:338
void flip(const VECTOR2I &aCentre, bool aFlipLeftRight)
Definition: eda_shape.cpp:395
FILL_T GetFillMode() const
Definition: eda_shape.h:102
virtual ~EDA_SHAPE()
Definition: eda_shape.cpp:57
long long int m_rectangleHeight
Definition: eda_shape.h:412
void SetEndY(int aY)
Definition: eda_shape.h:172
void RebuildBezierToSegmentsPointsList(int aMinSegLen)
Rebuild the m_bezierPoints vertex list that approximate the Bezier curve by a list of segments.
Definition: eda_shape.cpp:480
virtual int GetEffectiveWidth() const
Definition: eda_shape.h:111
COLOR4D GetLineColor() const
Definition: eda_shape.h:117
int GetEndX() const
Definition: eda_shape.h:164
int GetRectangleWidth() const
Definition: eda_shape.cpp:165
void SetLineStyle(const LINE_STYLE aStyle)
Definition: eda_shape.cpp:1788
const std::vector< VECTOR2I > buildBezierToSegmentsPointsList(int aMinSegLen) const
Definition: eda_shape.cpp:506
void calcEdit(const VECTOR2I &aPosition)
Definition: eda_shape.cpp:1414
void SetStartY(int y)
Definition: eda_shape.h:135
virtual std::vector< SHAPE * > MakeEffectiveShapes(bool aEdgeOnly=false) const
Make a set of SHAPE objects representing the EDA_SHAPE.
Definition: eda_shape.h:316
SHAPE_POLY_SET & GetPolyShape()
Definition: eda_shape.h:274
bool IsFilled() const
Definition: eda_shape.h:91
void SetCenterY(int y)
Definition: eda_shape.h:147
void CalcArcAngles(EDA_ANGLE &aStartAngle, EDA_ANGLE &aEndAngle) const
Calc arc start and end angles such that aStartAngle < aEndAngle.
Definition: eda_shape.cpp:577
void ShapeGetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, std::vector< MSG_PANEL_ITEM > &aList)
Definition: eda_shape.cpp:733
void SetFilled(bool aFlag)
Definition: eda_shape.h:96
bool operator==(const EDA_SHAPE &aOther) const
Definition: eda_shape.cpp:1803
int GetRadius() const
Definition: eda_shape.cpp:593
SHAPE_T GetShape() const
Definition: eda_shape.h:120
void SetPolyShape(const SHAPE_POLY_SET &aShape)
Definition: eda_shape.h:282
virtual VECTOR2I GetBotRight() const
Definition: eda_shape.h:190
virtual void SetBottom(int val)
Definition: eda_shape.h:195
VECTOR2I m_arcCenter
Definition: eda_shape.h:421
double m_segmentLength
Definition: eda_shape.h:415
void SetCenterX(int x)
Definition: eda_shape.h:153
virtual void SetTop(int val)
Definition: eda_shape.h:192
bool continueEdit(const VECTOR2I &aPosition)
Definition: eda_shape.cpp:1380
wxString ShowShape() const
Definition: eda_shape.cpp:62
ARC_MID m_arcMidData
Definition: eda_shape.h:422
void SetFillColor(const COLOR4D &aColor)
Definition: eda_shape.h:107
int GetEndY() const
Definition: eda_shape.h:163
std::vector< SHAPE * > makeEffectiveShapes(bool aEdgeOnly, bool aLineChainOnly=false) const
Make a set of SHAPE objects representing the EDA_SHAPE.
Definition: eda_shape.cpp:1214
bool hitTest(const VECTOR2I &aPosition, int aAccuracy=0) const
Definition: eda_shape.cpp:844
void SetCachedArcData(const VECTOR2I &aStart, const VECTOR2I &aMid, const VECTOR2I &aEnd, const VECTOR2I &aCenter)
Set the data used for mid point caching.
Definition: eda_shape.cpp:616
void SetEndX(int aX)
Definition: eda_shape.h:178
void beginEdit(const VECTOR2I &aStartPoint)
Definition: eda_shape.cpp:1339
VECTOR2I m_start
Definition: eda_shape.h:418
int GetPointCount() const
Definition: eda_shape.cpp:1331
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition: eda_shape.h:162
bool IsClosed() const
Definition: eda_shape.cpp:212
void SetRadius(int aX)
Definition: eda_shape.h:184
void SetStart(const VECTOR2I &aStart)
Definition: eda_shape.h:129
void DupPolyPointsList(std::vector< VECTOR2I > &aBuffer) const
Duplicate the list of corners in a std::vector<VECTOR2I>
Definition: eda_shape.cpp:1307
LINE_STYLE GetLineStyle() const
Definition: eda_shape.cpp:1794
void endEdit(bool aClosed=true)
Finishes editing the shape.
Definition: eda_shape.cpp:1568
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition: eda_shape.h:125
void SetLineColor(const COLOR4D &aColor)
Definition: eda_shape.h:116
COLOR4D GetFillColor() const
Definition: eda_shape.h:106
void SetRectangle(const long long int &aHeight, const long long int &aWidth)
Definition: eda_shape.cpp:188
void SetShape(SHAPE_T aShape)
Definition: eda_shape.h:119
void SwapShape(EDA_SHAPE *aImage)
Definition: eda_shape.cpp:1605
std::vector< VECTOR2I > GetRectCorners() const
Definition: eda_shape.cpp:1139
void SetSegmentAngle(const EDA_ANGLE &aAngle)
Definition: eda_shape.cpp:202
std::vector< VECTOR2I > m_bezierPoints
Definition: eda_shape.h:427
void setPosition(const VECTOR2I &aPos)
Definition: eda_shape.cpp:106
virtual void setFilled(bool aFlag)
Definition: eda_shape.h:364
const std::vector< VECTOR2I > & GetBezierPoints() const
Definition: eda_shape.h:257
virtual bool IsProxyItem() const
Definition: eda_shape.h:88
void computeArcBBox(BOX2I &aBBox) const
Definition: eda_shape.cpp:1154
void SetEnd(const VECTOR2I &aEnd)
Definition: eda_shape.h:166
bool EndsSwapped() const
Have the start and end points been swapped since they were set?
Definition: eda_shape.h:222
const SHAPE_POLY_SET & GetPolyShape() const
Definition: eda_shape.h:275
void SetBezierC1(const VECTOR2I &aPt)
Definition: eda_shape.h:197
void SetArcGeometry(const VECTOR2I &aStart, const VECTOR2I &aMid, const VECTOR2I &aEnd)
Set the three controlling points for an arc.
Definition: eda_shape.cpp:625
virtual void SetLeft(int val)
Definition: eda_shape.h:193
double GetLength() const
Definition: eda_shape.cpp:123
wxString SHAPE_T_asString() const
Definition: eda_shape.cpp:89
int GetStartX() const
Definition: eda_shape.h:127
double Similarity(const EDA_SHAPE &aOther) const
Definition: eda_shape.cpp:1848
const VECTOR2I & GetBezierC1() const
Definition: eda_shape.h:198
VECTOR2I m_end
Definition: eda_shape.h:419
const BOX2I getBoundingBox() const
Definition: eda_shape.cpp:790
void SetArcAngleAndEnd(const EDA_ANGLE &aAngle, bool aCheckNegativeAngle=false)
Set the end point from the angle center and start.
Definition: eda_shape.cpp:691
SHAPE_POLY_SET m_poly
Definition: eda_shape.h:428
int GetRectangleHeight() const
Definition: eda_shape.cpp:153
virtual int GetWidth() const
Definition: eda_shape.h:110
VECTOR2I getPosition() const
Definition: eda_shape.cpp:112
bool IsClockwiseArc() const
Definition: eda_shape.cpp:674
STROKE_PARAMS m_stroke
Definition: eda_shape.h:408
EDA_ANGLE m_segmentAngle
Definition: eda_shape.h:416
void setEditState(int aState)
Definition: eda_shape.h:392
void SetPolyPoints(const std::vector< VECTOR2I > &aPoints)
Definition: eda_shape.cpp:1204
void TransformShapeToPolygon(SHAPE_POLY_SET &aBuffer, int aClearance, int aError, ERROR_LOC aErrorLoc, bool ignoreLineWidth=false) const
Convert the shape to a closed polygon.
Definition: eda_shape.cpp:1669
wxString getFriendlyName() const
Definition: eda_shape.cpp:706
VECTOR2I m_bezierC1
Definition: eda_shape.h:424
FILL_T m_fill
Definition: eda_shape.h:409
COLOR4D m_fillColor
Definition: eda_shape.h:410
void SetWidth(int aWidth)
Definition: eda_shape.h:109
EDA_ANGLE GetSegmentAngle() const
Definition: eda_shape.cpp:654
virtual void SetRight(int val)
Definition: eda_shape.h:194
void SetFillMode(FILL_T aFill)
Definition: eda_shape.h:101
virtual void SetIsProxyItem(bool aIsProxy=true)
Definition: eda_shape.h:89
long long int m_rectangleWidth
Definition: eda_shape.h:413
VECTOR2I m_bezierC2
Definition: eda_shape.h:425
bool IsPolyShapeValid() const
Definition: eda_shape.cpp:1324
int Compare(const EDA_SHAPE *aOther) const
Definition: eda_shape.cpp:1628
VECTOR2I GetArcMid() const
Definition: eda_shape.cpp:563
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition: richio.h:93
EDA_MSG_PANEL items for displaying messages.
Definition: msgpanel.h:54
Represent a set of closed polygons.
void Fracture(POLYGON_MODE aFastMode)
Convert a set of polygons with holes to a single outline with "slits"/"fractures" connecting the oute...
int HoleCount(int aOutline) const
Returns the number of holes in a given outline.
int OutlineCount() const
Return the number of outlines in the set.
Simple container to manage line stroke parameters.
Definition: stroke_params.h:81
int GetWidth() const
Definition: stroke_params.h:91
void SetWidth(int aWidth)
Definition: stroke_params.h:92
void SetColor(const KIGFX::COLOR4D &aColor)
Definition: stroke_params.h:98
KIGFX::COLOR4D GetColor() const
Definition: stroke_params.h:97
SHAPE_T
Definition: eda_shape.h:42
@ ARC
use RECTANGLE instead of RECT to avoid collision in a Windows header
FILL_T
Definition: eda_shape.h:55
@ FILLED_WITH_COLOR
@ FILLED_WITH_BG_BODYCOLOR
@ FILLED_SHAPE
a few functions useful in geometry calculations.
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
#define DECLARE_ENUM_TO_WXANY(type)
Definition: property.h:729
const int scale
LINE_STYLE
Dashed line types.
Definition: stroke_params.h:48
VECTOR2I end
Definition: eda_shape.h:68
VECTOR2I center
Definition: eda_shape.h:69
VECTOR2I start
Definition: eda_shape.h:67
VECTOR2I mid
Definition: eda_shape.h:66
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588