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-2018 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/util.h> // for KiROUND
32 #include <math_for_graphics.h>
33 #include <trigo.h>
35 
36 
37 class LINE_READER;
38 class EDA_DRAW_FRAME;
39 class FOOTPRINT;
40 class MSG_PANEL_ITEM;
41 
42 
43 class PCB_SHAPE : public BOARD_ITEM
44 {
45 protected:
46  int m_width; // thickness of lines ...
47  bool m_filled; // Pretty much what it says on the tin...
48  wxPoint m_start; // Line start point or Circle and Arc center
49  wxPoint m_end; // Line end point or circle and arc start point
50  wxPoint m_thirdPoint; // Used only for Arcs: arc end point
51 
52  PCB_SHAPE_TYPE_T m_shape; // Shape: line, Circle, Arc
53  double m_angle; // Used only for Arcs: Arc angle in 1/10 deg
54  wxPoint m_bezierC1; // Bezier Control Point 1
55  wxPoint m_bezierC2; // Bezier Control Point 2
56 
57  std::vector<wxPoint> m_bezierPoints;
58  SHAPE_POLY_SET m_poly; // Stores the S_POLYGON shape
59 
60  // Computes the bounding box for an arc
61  void computeArcBBox( EDA_RECT& aBBox ) const;
62 
63  const std::vector<wxPoint> buildBezierToSegmentsPointsList( int aMinSegLen ) const;
64 
65 public:
66  PCB_SHAPE( BOARD_ITEM* aParent = NULL, KICAD_T idtype = PCB_SHAPE_T );
67 
68  // Do not create a copy constructor & operator=.
69  // The ones generated by the compiler are adequate.
70 
71  ~PCB_SHAPE();
72 
73  static inline bool ClassOf( const EDA_ITEM* aItem )
74  {
75  return aItem && PCB_SHAPE_T == aItem->Type();
76  }
77 
78  bool IsType( const KICAD_T aScanTypes[] ) const override
79  {
80  if( BOARD_ITEM::IsType( aScanTypes ) )
81  return true;
82 
83  for( const KICAD_T* p = aScanTypes; *p != EOT; ++p )
84  {
85  if( *p == PCB_LOCATE_GRAPHIC_T )
86  return true;
87  else if( *p == PCB_LOCATE_BOARD_EDGE_T )
88  return m_layer == Edge_Cuts;
89  }
90 
91  return false;
92  }
93 
94  void SetFilled( bool aFlag ) { m_filled = aFlag; }
95 
96  bool IsFilled() const
97  {
98  switch( m_shape )
99  {
100  case S_RECT:
101  case S_CIRCLE:
102  case S_POLYGON:
103  return m_filled;
104 
105  case S_SEGMENT:
106  case S_ARC:
107  case S_CURVE:
108  return false;
109 
110  case S_LAST: // Make CLang compiler happy
111  return false;
112  }
113 
114  return false; // Make GCC compiler happy
115  }
116 
117  void SetWidth( int aWidth ) { m_width = aWidth; }
118  int GetWidth() const { return m_width; }
119 
126  virtual void SetAngle( double aAngle, bool aUpdateEnd = true );
127  double GetAngle() const { return m_angle; }
128 
129  void SetShape( PCB_SHAPE_TYPE_T aShape ) { m_shape = aShape; }
130  PCB_SHAPE_TYPE_T GetShape() const { return m_shape; }
131 
132  void SetBezControl1( const wxPoint& aPoint ) { m_bezierC1 = aPoint; }
133  const wxPoint& GetBezControl1() const { return m_bezierC1; }
134 
135  void SetBezControl2( const wxPoint& aPoint ) { m_bezierC2 = aPoint; }
136  const wxPoint& GetBezControl2() const { return m_bezierC2; }
137 
138  void SetPosition( const wxPoint& aPos ) override;
139  wxPoint GetPosition() const override;
140 
145  const wxPoint& GetStart() const { return m_start; }
146  int GetStartY() { return m_start.y; }
147  int GetStartX() { return m_start.x; }
148  void SetStart( const wxPoint& aStart ) { m_start = aStart; }
149  void SetStartY( int y ) { m_start.y = y; }
150  void SetStartX( int x ) { m_start.x = x; }
151 
156  const wxPoint& GetEnd() const { return m_end; }
157  int GetEndY() { return m_end.y; }
158  int GetEndX() { return m_end.x; }
159  void SetEnd( const wxPoint& aEnd ) { m_end = aEnd; }
160  void SetEndY( int y ) { m_end.y = y; }
161  void SetEndX( int x ) { m_end.x = x; }
162 
167  const wxPoint& GetThirdPoint() const { return m_thirdPoint; }
168  int GetThirdPointY() { return m_thirdPoint.y; }
169  int GetThirdPointX() { return m_thirdPoint.x; }
170  void SetThirdPoint( const wxPoint& aPoint ) { m_thirdPoint = aPoint; }
171  void SetThirdPointY( int y ) { m_thirdPoint.y = y; }
172  void SetThirdPointX( int x ) { m_thirdPoint.x = x; }
173 
174  // Some attributes are read only, since they are "calculated" from
175  // m_Start, m_End, and m_Angle.
176  // No Set...() function for these attributes.
177 
178  wxPoint GetCenter() const override;
179  wxPoint GetArcStart() const { return m_end; }
180  wxPoint GetArcEnd() const;
181  wxPoint GetArcMid() const;
182  std::vector<wxPoint> GetRectCorners() const;
183 
188  double GetArcAngleStart() const;
189 
194  double GetArcAngleEnd() const;
195 
201  int GetRadius() const
202  {
203  double radius = GetLineLength( m_start, m_end );
204 
205  // don't allow degenerate arcs
206  return std::max( 1, KiROUND( radius ) );
207  }
208 
213  void SetArcStart( const wxPoint& aArcStartPoint )
214  {
215  m_end = aArcStartPoint;
216  }
217 
222  void SetArcEnd( const wxPoint& aArcEndPoint )
223  {
224  m_thirdPoint = aArcEndPoint;
225  }
226 
229  void SetCenter( const wxPoint& aCenterPoint ) { m_start = aCenterPoint; }
230 
237  void SetArcGeometry( const wxPoint& aStart, const wxPoint& aMid, const wxPoint& aEnd );
238 
239  const wxPoint GetFocusPosition() const override
240  {
241  return GetCenter();
242  }
243 
250  FOOTPRINT* GetParentFootprint() const;
251 
252  // Accessors:
253  const std::vector<wxPoint>& GetBezierPoints() const { return m_bezierPoints; }
254 
260  const std::vector<wxPoint> BuildPolyPointsList() const;
261 
265  int GetPointCount() const;
266 
267  // Accessors to the polygonal shape
269  const SHAPE_POLY_SET& GetPolyShape() const { return m_poly; }
270 
274  bool IsPolyShapeValid() const;
275 
276  void SetPolyShape( const SHAPE_POLY_SET& aShape ) { m_poly = aShape; }
277 
278  void SetBezierPoints( const std::vector<wxPoint>& aPoints )
279  {
280  m_bezierPoints = aPoints;
281  }
282 
291  void RebuildBezierToSegmentsPointsList( int aMinSegLen );
292 
293  void SetPolyPoints( const std::vector<wxPoint>& aPoints );
294 
298  std::vector<SHAPE*> MakeEffectiveShapes() const; // fixme: move to shape_compound
299  std::shared_ptr<SHAPE> GetEffectiveShape( PCB_LAYER_ID aLayer = UNDEFINED_LAYER ) const override;
300 
301  void GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList ) override;
302 
303  const EDA_RECT GetBoundingBox() const override;
304 
305  bool HitTest( const wxPoint& aPosition, int aAccuracy = 0 ) const override;
306  bool HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy = 0 ) const override;
307 
308  wxString GetClass() const override
309  {
310  return wxT( "PCB_SHAPE" );
311  }
312 
318  double GetLength() const;
319 
320  virtual void Move( const wxPoint& aMoveVector ) override;
321 
322  virtual void Rotate( const wxPoint& aRotCentre, double aAngle ) override;
323 
324  virtual void Flip( const wxPoint& aCentre, bool aFlipLeftRight ) override;
325 
326  void Scale( double aScale );
327 
340  PCB_LAYER_ID aLayer, int aClearanceValue,
341  int aError, ERROR_LOC aErrorLoc,
342  bool ignoreLineWidth = false ) const override;
343 
344  virtual wxString GetSelectMenuText( EDA_UNITS aUnits ) const override;
345 
346  virtual BITMAP_DEF GetMenuImage() const override;
347 
348  virtual EDA_ITEM* Clone() const override;
349 
350  virtual const BOX2I ViewBBox() const override;
351 
352  virtual void SwapData( BOARD_ITEM* aImage ) override;
353 
355  {
356  bool operator()( const BOARD_ITEM* aFirst, const BOARD_ITEM* aSecond ) const;
357  };
358 
359 #if defined(DEBUG)
360  void Show( int nestLevel, std::ostream& os ) const override { ShowDummy( os ); }
361 #endif
362 };
363 
364 #endif // PCB_SHAPE_H
void SetStartX(int x)
Definition: pcb_shape.h:150
wxPoint GetArcEnd() const
Definition: pcb_shape.cpp:373
bool IsPolyShapeValid() const
Definition: pcb_shape.cpp:1223
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:96
wxPoint m_end
Definition: pcb_shape.h:49
double GetLineLength(const wxPoint &aPointA, const wxPoint &aPointB)
Return the length of a line segment defined by aPointA and aPointB.
Definition: trigo.h:223
PNG memory record (file in memory).
Definition: bitmap_def.h:29
SHAPE_POLY_SET & GetPolyShape()
Definition: pcb_shape.h:268
const wxPoint & GetEnd() const
Function GetEnd returns the ending point of the graphic.
Definition: pcb_shape.h:156
int GetEndY()
Definition: pcb_shape.h:157
virtual void SwapData(BOARD_ITEM *aImage) override
Swap data between aItem and aImage.
Definition: pcb_shape.cpp:1246
void SetBezControl2(const wxPoint &aPoint)
Definition: pcb_shape.h:135
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:82
void Scale(double aScale)
Definition: pcb_shape.cpp:147
PCB_SHAPE_TYPE_T m_shape
Definition: pcb_shape.h:52
void SetFilled(bool aFlag)
Definition: pcb_shape.h:94
virtual EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: pcb_shape.cpp:944
int GetRadius() const
Function GetRadius returns the radius of this item Has meaning only for arc and circle.
Definition: pcb_shape.h:201
std::vector< wxPoint > GetRectCorners() const
Definition: pcb_shape.cpp:970
int GetWidth() const
Definition: pcb_shape.h:118
double GetArcAngleStart() const
function GetArcAngleStart()
Definition: pcb_shape.cpp:413
bool operator()(const BOARD_ITEM *aFirst, const BOARD_ITEM *aSecond) const
Definition: pcb_shape.cpp:1269
void computeArcBBox(EDA_RECT &aBBox) const
Definition: pcb_shape.cpp:1007
polygon (not yet used for tracks, but could be in microwave apps)
Definition: board_item.h:54
virtual const BOX2I ViewBBox() const override
Return the bounding box of the item covering all its layers.
Definition: pcb_shape.cpp:950
usual segment : line with rounded ends
Definition: board_item.h:50
Arcs (with rounded ends)
Definition: board_item.h:52
segment with non rounded ends
Definition: board_item.h:51
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:1200
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:197
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:179
wxPoint m_thirdPoint
Definition: pcb_shape.h:50
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:641
void SetArcGeometry(const wxPoint &aStart, const wxPoint &aMid, const wxPoint &aEnd)
Set the three controlling points for an arc.
Definition: pcb_shape.cpp:442
virtual void Move(const wxPoint &aMoveVector) override
Move this object.
Definition: pcb_shape.cpp:112
const std::vector< wxPoint > BuildPolyPointsList() const
Build and return the list of corners in a std::vector<wxPoint> It must be used only to convert the SH...
Definition: pcb_shape.cpp:1206
PCB_LAYER_ID
A quick note on layer IDs:
PCB_SHAPE_TYPE_T
The set of shapes for PCB graphics and tracks and footprint graphics in the .m_Shape member.
Definition: board_item.h:48
void SetEndY(int y)
Definition: pcb_shape.h:160
#define NULL
void SetShape(PCB_SHAPE_TYPE_T aShape)
Definition: pcb_shape.h:129
last value for this list
Definition: board_item.h:56
SHAPE_POLY_SET m_poly
Definition: pcb_shape.h:58
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:341
const wxPoint & GetStart() const
Function GetStart returns the starting point of the graphic.
Definition: pcb_shape.h:145
int GetEndX()
Definition: pcb_shape.h:158
const std::vector< wxPoint > & GetBezierPoints() const
Definition: pcb_shape.h:253
virtual bool IsType(const KICAD_T aScanTypes[]) const
Check whether the item is one of the listed types.
Definition: eda_item.h:232
void SetPosition(const wxPoint &aPos) override
Definition: pcb_shape.cpp:61
double m_angle
Definition: pcb_shape.h:53
double GetArcAngleEnd() const
function GetArcAngleEnd()
Definition: pcb_shape.cpp:427
void SetCenter(const wxPoint &aCenterPoint)
For arcs and circles:
Definition: pcb_shape.h:229
void SetBezControl1(const wxPoint &aPoint)
Definition: pcb_shape.h:132
int GetThirdPointX()
Definition: pcb_shape.h:169
void SetEndX(int x)
Definition: pcb_shape.h:161
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:930
wxPoint m_start
Definition: pcb_shape.h:48
EDA_UNITS
Definition: eda_units.h:38
wxPoint GetPosition() const override
Definition: pcb_shape.cpp:67
const wxPoint & GetThirdPoint() const
Function GetThirdPoint returns the third point point of the graphic.
Definition: pcb_shape.h:167
bool IsType(const KICAD_T aScanTypes[]) const override
Check whether the item is one of the listed types.
Definition: pcb_shape.h:78
std::vector< SHAPE * > MakeEffectiveShapes() const
Makes a set of SHAPE objects representing the PCB_SHAPE.
Definition: pcb_shape.cpp:1085
virtual BITMAP_DEF GetMenuImage() const override
Return a pointer to an image to be used in menus.
Definition: pcb_shape.cpp:938
wxString GetClass() const override
Return the class name.
Definition: pcb_shape.h:308
int m_width
Definition: pcb_shape.h:46
FOOTPRINT * GetParentFootprint() const
Function GetParentFootprint returns a pointer to the parent footprint, or NULL if PCB_SHAPE does not ...
Definition: pcb_shape.cpp:478
void SetStart(const wxPoint &aStart)
Definition: pcb_shape.h:148
const SHAPE_POLY_SET & GetPolyShape() const
Definition: pcb_shape.h:269
void SetThirdPointX(int x)
Definition: pcb_shape.h:172
const wxPoint & GetBezControl2() const
Definition: pcb_shape.h:136
void SetThirdPointY(int y)
Definition: pcb_shape.h:171
const EDA_RECT GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition: pcb_shape.cpp:568
double GetAngle() const
Definition: pcb_shape.h:127
Handle the component boundary box.
Definition: eda_rect.h:42
const wxPoint GetFocusPosition() const override
Similar to GetPosition, but allows items to return their visual center rather than their anchor.
Definition: pcb_shape.h:239
void SetWidth(int aWidth)
Definition: pcb_shape.h:117
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:68
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:149
int GetPointCount() const
Definition: pcb_shape.cpp:1235
void RebuildBezierToSegmentsPointsList(int aMinSegLen)
Rebuild the m_BezierPoints vertex list that approximate the Bezier curve by a list of segments Has me...
Definition: pcb_shape.cpp:315
void SetArcEnd(const wxPoint &aArcEndPoint)
Initialize the end arc point.
Definition: pcb_shape.h:222
PCB_LAYER_ID m_layer
Definition: board_item.h:363
static bool ClassOf(const EDA_ITEM *aItem)
Definition: pcb_shape.h:73
ring
Definition: board_item.h:53
wxPoint m_bezierC1
Definition: pcb_shape.h:54
void SetStartY(int y)
Definition: pcb_shape.h:149
virtual void Flip(const wxPoint &aCentre, bool aFlipLeftRight) override
Flip this object, i.e.
Definition: pcb_shape.cpp:252
void SetPolyShape(const SHAPE_POLY_SET &aShape)
Definition: pcb_shape.h:276
wxPoint GetArcMid() const
Definition: pcb_shape.cpp:391
PCB_SHAPE_TYPE_T GetShape() const
Definition: pcb_shape.h:130
bool m_filled
Definition: pcb_shape.h:47
EDA_MSG_ITEM is used EDA_MSG_PANEL as the item type for displaying messages.
Definition: msgpanel.h:54
std::vector< wxPoint > m_bezierPoints
Definition: pcb_shape.h:57
void TransformShapeWithClearanceToPolygon(SHAPE_POLY_SET &aCornerBuffer, PCB_LAYER_ID aLayer, int aClearanceValue, int aError, ERROR_LOC aErrorLoc, bool ignoreLineWidth=false) const override
Function TransformShapeWithClearanceToPolygon Convert the draw segment to a closed polygon Used in fi...
virtual void SetAngle(double aAngle, bool aUpdateEnd=true)
Sets the angle for arcs, and normalizes it within the range 0 - 360 degrees.
Definition: pcb_shape.cpp:465
wxPoint m_bezierC2
Definition: pcb_shape.h:55
class PCB_SHAPE, a segment not on copper layers
Definition: typeinfo.h:90
Bezier Curve.
Definition: board_item.h:55
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:487
int GetStartY()
Definition: pcb_shape.h:146
void SetThirdPoint(const wxPoint &aPoint)
Definition: pcb_shape.h:170
PCB_SHAPE(BOARD_ITEM *aParent=NULL, KICAD_T idtype=PCB_SHAPE_T)
Definition: pcb_shape.cpp:45
const wxPoint & GetBezControl1() const
Definition: pcb_shape.h:133
void SetPolyPoints(const std::vector< wxPoint > &aPoints)
Definition: pcb_shape.cpp:1075
const std::vector< wxPoint > buildBezierToSegmentsPointsList(int aMinSegLen) const
Definition: pcb_shape.cpp:328
void SetBezierPoints(const std::vector< wxPoint > &aPoints)
Definition: pcb_shape.h:278
void SetEnd(const wxPoint &aEnd)
Definition: pcb_shape.h:159
int GetStartX()
Definition: pcb_shape.h:147
void SetArcStart(const wxPoint &aArcStartPoint)
Initialize the start arc point.
Definition: pcb_shape.h:213
int GetThirdPointY()
Definition: pcb_shape.h:168
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:162
double GetLength() const
Function GetLength returns the length of the track using the hypotenuse calculation.
Definition: pcb_shape.cpp:76