KiCad PCB EDA Suite
pcb_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-2021 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 PCB_SHAPE_H
26 #define PCB_SHAPE_H
27 
28 #include <board_item.h>
29 #include <eda_units.h>
30 #include <convert_to_biu.h>
31 #include <math_for_graphics.h>
32 #include <trigo.h>
34 
35 
36 class LINE_READER;
37 class EDA_DRAW_FRAME;
38 class FOOTPRINT;
39 class MSG_PANEL_ITEM;
40 
41 
42 class PCB_SHAPE : public BOARD_ITEM
43 {
44 public:
45  PCB_SHAPE( BOARD_ITEM* aParent = nullptr, KICAD_T idtype = PCB_SHAPE_T );
46 
47  // Do not create a copy constructor & operator=.
48  // The ones generated by the compiler are adequate.
49 
50  ~PCB_SHAPE();
51 
52  static inline bool ClassOf( const EDA_ITEM* aItem )
53  {
54  return aItem && PCB_SHAPE_T == aItem->Type();
55  }
56 
57  bool IsType( const KICAD_T aScanTypes[] ) const override
58  {
59  if( BOARD_ITEM::IsType( aScanTypes ) )
60  return true;
61 
62  for( const KICAD_T* p = aScanTypes; *p != EOT; ++p )
63  {
64  if( *p == PCB_LOCATE_GRAPHIC_T )
65  return true;
66  else if( *p == PCB_LOCATE_BOARD_EDGE_T )
67  return m_layer == Edge_Cuts;
68  }
69 
70  return false;
71  }
72 
73  void SetFilled( bool aFlag ) { m_filled = aFlag; }
74 
75  bool IsFilled() const
76  {
77  switch( m_shape )
78  {
79  case SHAPE_T::RECT:
80  case SHAPE_T::CIRCLE:
81  case SHAPE_T::POLY:
82  return m_filled;
83 
84  case SHAPE_T::SEGMENT:
85  case SHAPE_T::ARC:
86  case SHAPE_T::BEZIER:
87  return false;
88 
89  case SHAPE_T::LAST: // Make CLang compiler happy
90  return false;
91  }
92 
93  return false; // Make GCC compiler happy
94  }
95 
96  void SetWidth( int aWidth ) { m_width = aWidth; }
97  int GetWidth() const { return m_width; }
98 
106  virtual void SetAngle( double aAngle, bool aUpdateEnd = true );
107  double GetAngle() const { return m_angle; }
108 
109  void SetShape( SHAPE_T aShape ) { m_shape = aShape; }
110  SHAPE_T GetShape() const { return m_shape; }
111 
112  void SetBezierC1( const wxPoint& aPoint ) { m_bezierC1 = aPoint; }
113  const wxPoint& GetBezierC1() const { return m_bezierC1; }
114 
115  void SetBezierC2( const wxPoint& aPoint ) { m_bezierC2 = aPoint; }
116  const wxPoint& GetBezierC2() const { return m_bezierC2; }
117 
118  void SetPosition( const wxPoint& aPos ) override;
119  wxPoint GetPosition() const override;
120 
124  const wxPoint& GetStart() const { return m_start; }
125  int GetStartY() { return m_start.y; }
126  int GetStartX() { return m_start.x; }
127  void SetStart( const wxPoint& aStart ) { m_start = aStart; }
128  void SetStartY( int y ) { m_start.y = y; }
129  void SetStartX( int x ) { m_start.x = x; }
130 
134  const wxPoint& GetEnd() const { return m_end; }
135  int GetEndY() { return m_end.y; }
136  int GetEndX() { return m_end.x; }
137  void SetEnd( const wxPoint& aEnd ) { m_end = aEnd; }
138  void SetEndY( int y ) { m_end.y = y; }
139  void SetEndX( int x ) { m_end.x = x; }
140 
144  const wxPoint& GetThirdPoint() const { return m_thirdPoint; }
145  int GetThirdPointY() { return m_thirdPoint.y; }
146  int GetThirdPointX() { return m_thirdPoint.x; }
147  void SetThirdPoint( const wxPoint& aPoint ) { m_thirdPoint = aPoint; }
148  void SetThirdPointY( int y ) { m_thirdPoint.y = y; }
149  void SetThirdPointX( int x ) { m_thirdPoint.x = x; }
150 
151  // Some attributes are read only, since they are "calculated" from
152  // m_Start, m_End, and m_Angle.
153  // No Set...() function for these attributes.
154 
155  wxPoint GetCenter() const override;
156  wxPoint GetArcStart() const { return m_end; }
157  wxPoint GetArcEnd() const;
158  wxPoint GetArcMid() const;
159  std::vector<wxPoint> GetRectCorners() const;
160 
164  double GetArcAngleStart() const;
165 
169  double GetArcAngleEnd() const;
170 
176  int GetRadius() const;
177 
183  void SetArcStart( const wxPoint& aArcStartPoint )
184  {
185  m_end = aArcStartPoint;
186  }
187 
193  void SetArcEnd( const wxPoint& aArcEndPoint )
194  {
195  m_thirdPoint = aArcEndPoint;
196  }
197 
198  void SetCenter( const wxPoint& aCenterPoint ) { m_start = aCenterPoint; }
199 
206  void SetArcGeometry( const wxPoint& aStart, const wxPoint& aMid, const wxPoint& aEnd );
207 
213  const wxPoint GetFocusPosition() const override;
214 
220  FOOTPRINT* GetParentFootprint() const;
221 
222  // Accessors:
223  const std::vector<wxPoint>& GetBezierPoints() const { return m_bezierPoints; }
224 
232  const std::vector<wxPoint> BuildPolyPointsList() const;
233 
237  int GetPointCount() const;
238 
239  // Accessors to the polygonal shape
241  const SHAPE_POLY_SET& GetPolyShape() const { return m_poly; }
242 
246  bool IsPolyShapeValid() const;
247 
248  void SetPolyShape( const SHAPE_POLY_SET& aShape ) { m_poly = aShape; }
249 
250  void SetBezierPoints( const std::vector<wxPoint>& aPoints )
251  {
252  m_bezierPoints = aPoints;
253  }
254 
265  void RebuildBezierToSegmentsPointsList( int aMinSegLen );
266 
267  void SetPolyPoints( const std::vector<wxPoint>& aPoints );
268 
272  std::vector<SHAPE*> MakeEffectiveShapes() const; // fixme: move to shape_compound
273  std::shared_ptr<SHAPE> GetEffectiveShape( PCB_LAYER_ID aLayer = UNDEFINED_LAYER ) const override;
274 
275  void GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList ) override;
276 
277  const EDA_RECT GetBoundingBox() const override;
278 
279  bool HitTest( const wxPoint& aPosition, int aAccuracy = 0 ) const override;
280  bool HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy = 0 ) const override;
281 
282  wxString GetClass() const override
283  {
284  return wxT( "PCB_SHAPE" );
285  }
286 
292  double GetLength() const;
293 
294  virtual void Move( const wxPoint& aMoveVector ) override;
295 
296  virtual void Rotate( const wxPoint& aRotCentre, double aAngle ) override;
297 
298  virtual void Flip( const wxPoint& aCentre, bool aFlipLeftRight ) override;
299 
300  void Scale( double aScale );
301 
314  PCB_LAYER_ID aLayer, int aClearanceValue,
315  int aError, ERROR_LOC aErrorLoc,
316  bool ignoreLineWidth = false ) const override;
317 
318  virtual wxString GetSelectMenuText( EDA_UNITS aUnits ) const override;
319 
320  virtual BITMAPS GetMenuImage() const override;
321 
322  virtual EDA_ITEM* Clone() const override;
323 
324  virtual const BOX2I ViewBBox() const override;
325 
326  virtual void SwapData( BOARD_ITEM* aImage ) override;
327 
329  {
330  bool operator()( const BOARD_ITEM* aFirst, const BOARD_ITEM* aSecond ) const;
331  };
332 
333 #if defined(DEBUG)
334  void Show( int nestLevel, std::ostream& os ) const override { ShowDummy( os ); }
335 #endif
336 
337 protected:
338  // Compute the bounding box for an arc
339  void computeArcBBox( EDA_RECT& aBBox ) const;
340 
341  const std::vector<wxPoint> buildBezierToSegmentsPointsList( int aMinSegLen ) const;
342 
343  int m_width; // thickness of lines ...
344  bool m_filled; // Pretty much what it says on the tin...
345  wxPoint m_start; // Line start point or Circle and Arc center
346  wxPoint m_end; // Line end point or circle and arc start point
347  wxPoint m_thirdPoint; // Used only for Arcs: arc end point
348 
349  SHAPE_T m_shape; // Shape: line, Circle, Arc
350  double m_angle; // Used only for Arcs: Arc angle in 1/10 deg
351  wxPoint m_bezierC1; // Bezier Control Point 1
352  wxPoint m_bezierC2; // Bezier Control Point 2
353 
354  std::vector<wxPoint> m_bezierPoints;
355  SHAPE_POLY_SET m_poly; // Stores the S_POLYGON shape
356 };
357 
358 #endif // PCB_SHAPE_H
void SetStartX(int x)
Definition: pcb_shape.h:129
wxPoint GetArcEnd() const
Definition: pcb_shape.cpp:417
bool IsPolyShapeValid() const
Definition: pcb_shape.cpp:1294
Arcs (with rounded ends)
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition: richio.h:80
bool IsFilled() const
Definition: pcb_shape.h:75
Bezier Curve.
wxPoint m_end
Definition: pcb_shape.h:346
void SetShape(SHAPE_T aShape)
Definition: pcb_shape.h:109
polygon (not yet used for tracks, but could be in microwave apps)
SHAPE_POLY_SET & GetPolyShape()
Definition: pcb_shape.h:240
const wxPoint & GetEnd() const
Return the ending point of the graphic.
Definition: pcb_shape.h:134
int GetEndY()
Definition: pcb_shape.h:135
virtual void SwapData(BOARD_ITEM *aImage) override
Swap data between aItem and aImage.
Definition: pcb_shape.cpp:1317
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:80
void Scale(double aScale)
Definition: pcb_shape.cpp:191
SHAPE_T GetShape() const
Definition: pcb_shape.h:110
void SetFilled(bool aFlag)
Definition: pcb_shape.h:73
virtual EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: pcb_shape.cpp:1022
int GetRadius() const
Return the radius of this item.
Definition: pcb_shape.cpp:487
virtual BITMAPS GetMenuImage() const override
Return a pointer to an image to be used in menus.
Definition: pcb_shape.cpp:1016
std::vector< wxPoint > GetRectCorners() const
Definition: pcb_shape.cpp:1048
int GetWidth() const
Definition: pcb_shape.h:97
double GetArcAngleStart() const
Definition: pcb_shape.cpp:457
bool operator()(const BOARD_ITEM *aFirst, const BOARD_ITEM *aSecond) const
Definition: pcb_shape.cpp:1340
void computeArcBBox(EDA_RECT &aBBox) const
Definition: pcb_shape.cpp:1085
const wxPoint & GetBezierC2() const
Definition: pcb_shape.h:116
usual segment : line with rounded ends
virtual const BOX2I ViewBBox() const override
Return the bounding box of the item covering all its layers.
Definition: pcb_shape.cpp:1028
last value for this list
std::shared_ptr< SHAPE > GetEffectiveShape(PCB_LAYER_ID aLayer=UNDEFINED_LAYER) const override
Some pad shapes can be complex (rounded/chamfered rectangle), even without considering custom shapes.
Definition: pcb_shape.cpp:1271
The base class for create windows for drawing purpose.
virtual void Rotate(const wxPoint &aRotCentre, double aAngle) override
Rotate this object.
Definition: pcb_shape.cpp:241
search types array terminator (End Of Types)
Definition: typeinfo.h:81
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:77
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
wxPoint GetArcStart() const
Definition: pcb_shape.h:156
wxPoint m_thirdPoint
Definition: pcb_shape.h:347
bool HitTest(const wxPoint &aPosition, int aAccuracy=0) const override
Test if aPosition is contained within or on the bounding box of an item.
Definition: pcb_shape.cpp:697
PCB_SHAPE(BOARD_ITEM *aParent=nullptr, KICAD_T idtype=PCB_SHAPE_T)
Definition: pcb_shape.cpp:48
SHAPE_T
The set of shapes for PCB graphics and tracks and footprint graphics in the .m_Shape member.
Definition: board_item.h:46
void SetArcGeometry(const wxPoint &aStart, const wxPoint &aMid, const wxPoint &aEnd)
Set the three controlling points for an arc.
Definition: pcb_shape.cpp:496
virtual void Move(const wxPoint &aMoveVector) override
Move this object.
Definition: pcb_shape.cpp:156
const std::vector< wxPoint > BuildPolyPointsList() const
Build and return the list of corners in a std::vector<wxPoint>
Definition: pcb_shape.cpp:1277
PCB_LAYER_ID
A quick note on layer IDs:
void SetEndY(int y)
Definition: pcb_shape.h:138
SHAPE_POLY_SET m_poly
Definition: pcb_shape.h:355
Represent a set of closed polygons.
wxPoint GetCenter() const override
This defaults to the center of the bounding box if not overridden.
Definition: pcb_shape.cpp:385
const wxPoint & GetStart() const
Return the starting point of the graphic.
Definition: pcb_shape.h:124
const wxPoint & GetBezierC1() const
Definition: pcb_shape.h:113
int GetEndX()
Definition: pcb_shape.h:136
const std::vector< wxPoint > & GetBezierPoints() const
Definition: pcb_shape.h:223
virtual bool IsType(const KICAD_T aScanTypes[]) const
Check whether the item is one of the listed types.
Definition: eda_item.h:183
void SetPosition(const wxPoint &aPos) override
Definition: pcb_shape.cpp:64
double m_angle
Definition: pcb_shape.h:350
double GetArcAngleEnd() const
Definition: pcb_shape.cpp:472
void SetCenter(const wxPoint &aCenterPoint)
Definition: pcb_shape.h:198
int GetThirdPointX()
Definition: pcb_shape.h:146
void SetEndX(int x)
Definition: pcb_shape.h:139
virtual wxString GetSelectMenuText(EDA_UNITS aUnits) const override
Return the text to display to be used in the selection clarification context menu when multiple items...
Definition: pcb_shape.cpp:1010
wxPoint m_start
Definition: pcb_shape.h:345
EDA_UNITS
Definition: eda_units.h:38
wxPoint GetPosition() const override
Definition: pcb_shape.cpp:70
const wxPoint & GetThirdPoint() const
Return the third point of the graphic.
Definition: pcb_shape.h:144
bool IsType(const KICAD_T aScanTypes[]) const override
Check whether the item is one of the listed types.
Definition: pcb_shape.h:57
std::vector< SHAPE * > MakeEffectiveShapes() const
Make a set of SHAPE objects representing the PCB_SHAPE.
Definition: pcb_shape.cpp:1163
wxString GetClass() const override
Return the class name.
Definition: pcb_shape.h:282
int m_width
Definition: pcb_shape.h:343
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:32
FOOTPRINT * GetParentFootprint() const
Return the parent footprint or NULL if PCB_SHAPE does not belong to a footprint.
Definition: pcb_shape.cpp:532
void SetStart(const wxPoint &aStart)
Definition: pcb_shape.h:127
const SHAPE_POLY_SET & GetPolyShape() const
Definition: pcb_shape.h:241
void SetThirdPointX(int x)
Definition: pcb_shape.h:149
void SetThirdPointY(int y)
Definition: pcb_shape.h:148
const EDA_RECT GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition: pcb_shape.cpp:622
double GetAngle() const
Definition: pcb_shape.h:107
Handle the component boundary box.
Definition: eda_rect.h:42
const wxPoint GetFocusPosition() const override
Allows items to return their visual center rather than their anchor.
Definition: pcb_shape.cpp:79
void SetWidth(int aWidth)
Definition: pcb_shape.h:96
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:100
int GetPointCount() const
Definition: pcb_shape.cpp:1306
void RebuildBezierToSegmentsPointsList(int aMinSegLen)
Rebuild the m_BezierPoints vertex list that approximate the Bezier curve by a list of segments.
Definition: pcb_shape.cpp:359
void SetArcEnd(const wxPoint &aArcEndPoint)
Initialize the end arc point.
Definition: pcb_shape.h:193
PCB_LAYER_ID m_layer
Definition: board_item.h:352
static bool ClassOf(const EDA_ITEM *aItem)
Definition: pcb_shape.h:52
wxPoint m_bezierC1
Definition: pcb_shape.h:351
void SetStartY(int y)
Definition: pcb_shape.h:128
virtual void Flip(const wxPoint &aCentre, bool aFlipLeftRight) override
Flip this object, i.e.
Definition: pcb_shape.cpp:296
void SetPolyShape(const SHAPE_POLY_SET &aShape)
Definition: pcb_shape.h:248
wxPoint GetArcMid() const
Definition: pcb_shape.cpp:435
void SetBezierC2(const wxPoint &aPoint)
Definition: pcb_shape.h:115
bool m_filled
Definition: pcb_shape.h:344
EDA_MSG_PANEL items for displaying messages.
Definition: msgpanel.h:53
segment with non rounded ends
std::vector< wxPoint > m_bezierPoints
Definition: pcb_shape.h:354
void TransformShapeWithClearanceToPolygon(SHAPE_POLY_SET &aCornerBuffer, PCB_LAYER_ID aLayer, int aClearanceValue, int aError, ERROR_LOC aErrorLoc, bool ignoreLineWidth=false) const override
Convert the draw segment to a closed polygon.
virtual void SetAngle(double aAngle, bool aUpdateEnd=true)
Set the angle for arcs, and normalizes it within the range 0 - 360 degrees.
Definition: pcb_shape.cpp:519
wxPoint m_bezierC2
Definition: pcb_shape.h:352
class PCB_SHAPE, a segment not on copper layers
Definition: typeinfo.h:90
SHAPE_T m_shape
Definition: pcb_shape.h:349
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.
Definition: pcb_shape.cpp:541
int GetStartY()
Definition: pcb_shape.h:125
void SetThirdPoint(const wxPoint &aPoint)
Definition: pcb_shape.h:147
void SetPolyPoints(const std::vector< wxPoint > &aPoints)
Definition: pcb_shape.cpp:1153
const std::vector< wxPoint > buildBezierToSegmentsPointsList(int aMinSegLen) const
Definition: pcb_shape.cpp:372
void SetBezierPoints(const std::vector< wxPoint > &aPoints)
Definition: pcb_shape.h:250
void SetEnd(const wxPoint &aEnd)
Definition: pcb_shape.h:137
int GetStartX()
Definition: pcb_shape.h:126
void SetBezierC1(const wxPoint &aPoint)
Definition: pcb_shape.h:112
void SetArcStart(const wxPoint &aArcStartPoint)
Initialize the start arc point.
Definition: pcb_shape.h:183
int GetThirdPointY()
Definition: pcb_shape.h:145
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:113
double GetLength() const
Return the length of the track using the hypotenuse calculation.
Definition: pcb_shape.cpp:120