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 
125  virtual void SetAngle( double aAngle, bool aUpdateEnd = true );
126  double GetAngle() const { return m_angle; }
127 
128  void SetShape( PCB_SHAPE_TYPE_T aShape ) { m_shape = aShape; }
129  PCB_SHAPE_TYPE_T GetShape() const { return m_shape; }
130 
131  void SetBezControl1( const wxPoint& aPoint ) { m_bezierC1 = aPoint; }
132  const wxPoint& GetBezControl1() const { return m_bezierC1; }
133 
134  void SetBezControl2( const wxPoint& aPoint ) { m_bezierC2 = aPoint; }
135  const wxPoint& GetBezControl2() const { return m_bezierC2; }
136 
137  void SetPosition( const wxPoint& aPos ) override;
138  wxPoint GetPosition() const override;
139 
144  const wxPoint& GetStart() const { return m_start; }
145  int GetStartY() { return m_start.y; }
146  int GetStartX() { return m_start.x; }
147  void SetStart( const wxPoint& aStart ) { m_start = aStart; }
148  void SetStartY( int y ) { m_start.y = y; }
149  void SetStartX( int x ) { m_start.x = x; }
150 
155  const wxPoint& GetEnd() const { return m_end; }
156  int GetEndY() { return m_end.y; }
157  int GetEndX() { return m_end.x; }
158  void SetEnd( const wxPoint& aEnd ) { m_end = aEnd; }
159  void SetEndY( int y ) { m_end.y = y; }
160  void SetEndX( int x ) { m_end.x = x; }
161 
166  const wxPoint& GetThirdPoint() const { return m_thirdPoint; }
167  int GetThirdPointY() { return m_thirdPoint.y; }
168  int GetThirdPointX() { return m_thirdPoint.x; }
169  void SetThirdPoint( const wxPoint& aPoint ) { m_thirdPoint = aPoint; }
170  void SetThirdPointY( int y ) { m_thirdPoint.y = y; }
171  void SetThirdPointX( int x ) { m_thirdPoint.x = x; }
172 
173  // Some attributes are read only, since they are "calculated" from
174  // m_Start, m_End, and m_Angle.
175  // No Set...() function for these attributes.
176 
177  wxPoint GetCenter() const override;
178  wxPoint GetArcStart() const { return m_end; }
179  wxPoint GetArcEnd() const;
180  wxPoint GetArcMid() const;
181  std::vector<wxPoint> GetRectCorners() const;
182 
187  double GetArcAngleStart() const;
188 
193  double GetArcAngleEnd() const;
194 
200  int GetRadius() const
201  {
202  double radius = GetLineLength( m_start, m_end );
203 
204  // don't allow degenerate arcs
205  return std::max( 1, KiROUND( radius ) );
206  }
207 
212  void SetArcStart( const wxPoint& aArcStartPoint )
213  {
214  m_end = aArcStartPoint;
215  }
216 
221  void SetArcEnd( const wxPoint& aArcEndPoint )
222  {
223  m_thirdPoint = aArcEndPoint;
224  }
225 
228  void SetCenter( const wxPoint& aCenterPoint ) { m_start = aCenterPoint; }
229 
230  const wxPoint GetFocusPosition() const override
231  {
232  return GetCenter();
233  }
234 
241  FOOTPRINT* GetParentFootprint() const;
242 
243  // Accessors:
244  const std::vector<wxPoint>& GetBezierPoints() const { return m_bezierPoints; }
245 
251  const std::vector<wxPoint> BuildPolyPointsList() const;
252 
256  int GetPointCount() const;
257 
258  // Accessors to the polygonal shape
260  const SHAPE_POLY_SET& GetPolyShape() const { return m_poly; }
261 
265  bool IsPolyShapeValid() const;
266 
267  void SetPolyShape( const SHAPE_POLY_SET& aShape ) { m_poly = aShape; }
268 
269  void SetBezierPoints( const std::vector<wxPoint>& aPoints )
270  {
271  m_bezierPoints = aPoints;
272  }
273 
282  void RebuildBezierToSegmentsPointsList( int aMinSegLen );
283 
284  void SetPolyPoints( const std::vector<wxPoint>& aPoints );
285 
289  std::vector<SHAPE*> MakeEffectiveShapes() const; // fixme: move to shape_compound
290  std::shared_ptr<SHAPE> GetEffectiveShape( PCB_LAYER_ID aLayer = UNDEFINED_LAYER ) const override;
291 
292  void GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList ) override;
293 
294  const EDA_RECT GetBoundingBox() const override;
295 
296  bool HitTest( const wxPoint& aPosition, int aAccuracy = 0 ) const override;
297  bool HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy = 0 ) const override;
298 
299  wxString GetClass() const override
300  {
301  return wxT( "PCB_SHAPE" );
302  }
303 
309  double GetLength() const;
310 
311  virtual void Move( const wxPoint& aMoveVector ) override;
312 
313  virtual void Rotate( const wxPoint& aRotCentre, double aAngle ) override;
314 
315  virtual void Flip( const wxPoint& aCentre, bool aFlipLeftRight ) override;
316 
317  void Scale( double aScale );
318 
331  PCB_LAYER_ID aLayer, int aClearanceValue,
332  int aError, ERROR_LOC aErrorLoc,
333  bool ignoreLineWidth = false ) const override;
334 
335  virtual wxString GetSelectMenuText( EDA_UNITS aUnits ) const override;
336 
337  virtual BITMAP_DEF GetMenuImage() const override;
338 
339  virtual EDA_ITEM* Clone() const override;
340 
341  virtual const BOX2I ViewBBox() const override;
342 
343  virtual void SwapData( BOARD_ITEM* aImage ) override;
344 
346  {
347  bool operator()( const BOARD_ITEM* aFirst, const BOARD_ITEM* aSecond ) const;
348  };
349 
350 #if defined(DEBUG)
351  void Show( int nestLevel, std::ostream& os ) const override { ShowDummy( os ); }
352 #endif
353 };
354 
355 #endif // PCB_SHAPE_H
void SetStartX(int x)
Definition: pcb_shape.h:149
wxPoint GetArcEnd() const
Definition: pcb_shape.cpp:375
bool IsPolyShapeValid() const
Definition: pcb_shape.cpp:1191
LINE_READER is an abstract class from which implementation specific LINE_READERs may be derived to re...
Definition: richio.h:82
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:209
PNG memory record (file in memory).
Definition: bitmap_def.h:29
SHAPE_POLY_SET & GetPolyShape()
Definition: pcb_shape.h:259
const wxPoint & GetEnd() const
Function GetEnd returns the ending point of the graphic.
Definition: pcb_shape.h:155
int GetEndY()
Definition: pcb_shape.h:156
virtual void SwapData(BOARD_ITEM *aImage) override
Swap data between aItem and aImage.
Definition: pcb_shape.cpp:1214
void SetBezControl2(const wxPoint &aPoint)
Definition: pcb_shape.h:134
BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
Definition: board_item.h:86
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
Function Clone creates a duplicate of this item with linked list members set to NULL.
Definition: pcb_shape.cpp:920
int GetRadius() const
Function GetRadius returns the radius of this item Has meaning only for arc and circle.
Definition: pcb_shape.h:200
std::vector< wxPoint > GetRectCorners() const
Definition: pcb_shape.cpp:942
int GetWidth() const
Definition: pcb_shape.h:118
double GetArcAngleStart() const
function GetArcAngleStart()
Definition: pcb_shape.cpp:415
bool operator()(const BOARD_ITEM *aFirst, const BOARD_ITEM *aSecond) const
Definition: pcb_shape.cpp:1237
void computeArcBBox(EDA_RECT &aBBox) const
Definition: pcb_shape.cpp:979
polygon (not yet used for tracks, but could be in microwave apps)
Definition: board_item.h:56
virtual const BOX2I ViewBBox() const override
Function ViewBBox() returns the bounding box of the item covering all its layers.
Definition: pcb_shape.cpp:926
usual segment : line with rounded ends
Definition: board_item.h:52
Arcs (with rounded ends)
Definition: board_item.h:54
segment with non rounded ends
Definition: board_item.h:53
std::shared_ptr< SHAPE > GetEffectiveShape(PCB_LAYER_ID aLayer=UNDEFINED_LAYER) const override
Function GetEffectiveShape Some pad shapes can be complex (rounded/chamfered rectangle),...
Definition: pcb_shape.cpp:1168
The base class for create windows for drawing purpose.
virtual void Rotate(const wxPoint &aRotCentre, double aAngle) override
Function Rotate Rotate this object.
Definition: pcb_shape.cpp:197
search types array terminator (End Of Types)
Definition: typeinfo.h:82
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:78
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:178
wxPoint m_thirdPoint
Definition: pcb_shape.h:50
bool HitTest(const wxPoint &aPosition, int aAccuracy=0) const override
Function HitTest tests if aPosition is contained within or on the bounding box of an item.
Definition: pcb_shape.cpp:617
virtual void Move(const wxPoint &aMoveVector) override
Function Move 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:1174
PCB_LAYER_ID
A quick note on layer IDs:
PCB_SHAPE_TYPE_T
Enum PCB_SHAPE_TYPE_T is the set of shapes for PCB graphics and tracks and footprint graphics in the ...
Definition: board_item.h:50
void SetEndY(int y)
Definition: pcb_shape.h:159
#define NULL
void SetShape(PCB_SHAPE_TYPE_T aShape)
Definition: pcb_shape.h:128
last value for this list
Definition: board_item.h:58
SHAPE_POLY_SET m_poly
Definition: pcb_shape.h:58
SHAPE_POLY_SET.
wxPoint GetCenter() const override
Function GetCenter()
Definition: pcb_shape.cpp:343
const wxPoint & GetStart() const
Function GetStart returns the starting point of the graphic.
Definition: pcb_shape.h:144
int GetEndX()
Definition: pcb_shape.h:157
const std::vector< wxPoint > & GetBezierPoints() const
Definition: pcb_shape.h:244
virtual bool IsType(const KICAD_T aScanTypes[]) const
Function IsType Checks whether the item is one of the listed types.
Definition: eda_item.h:250
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:429
void SetCenter(const wxPoint &aCenterPoint)
For arcs and circles:
Definition: pcb_shape.h:228
void SetBezControl1(const wxPoint &aPoint)
Definition: pcb_shape.h:131
int GetThirdPointX()
Definition: pcb_shape.h:168
void SetEndX(int x)
Definition: pcb_shape.h:160
virtual wxString GetSelectMenuText(EDA_UNITS aUnits) const override
Function GetSelectMenuText returns the text to display to be used in the selection clarification cont...
Definition: pcb_shape.cpp:906
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:166
bool IsType(const KICAD_T aScanTypes[]) const override
Function IsType Checks 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:1053
virtual BITMAP_DEF GetMenuImage() const override
Function GetMenuImage returns a pointer to an image to be used in menus.
Definition: pcb_shape.cpp:914
wxString GetClass() const override
Function GetClass returns the class name.
Definition: pcb_shape.h:299
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:457
void SetStart(const wxPoint &aStart)
Definition: pcb_shape.h:147
const SHAPE_POLY_SET & GetPolyShape() const
Definition: pcb_shape.h:260
void SetThirdPointX(int x)
Definition: pcb_shape.h:171
const wxPoint & GetBezControl2() const
Definition: pcb_shape.h:135
void SetThirdPointY(int y)
Definition: pcb_shape.h:170
const EDA_RECT GetBoundingBox() const override
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes.
Definition: pcb_shape.cpp:544
double GetAngle() const
Definition: pcb_shape.h:126
EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
const wxPoint GetFocusPosition() const override
Function GetFocusPosition similar to GetPosition, but allows items to return their visual center rath...
Definition: pcb_shape.h:230
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
EDA_ITEM is a base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:148
int GetPointCount() const
Definition: pcb_shape.cpp:1203
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:317
void SetArcEnd(const wxPoint &aArcEndPoint)
Initialize the end arc point.
Definition: pcb_shape.h:221
PCB_LAYER_ID m_layer
Definition: board_item.h:89
static bool ClassOf(const EDA_ITEM *aItem)
Definition: pcb_shape.h:73
ring
Definition: board_item.h:55
wxPoint m_bezierC1
Definition: pcb_shape.h:54
void SetStartY(int y)
Definition: pcb_shape.h:148
virtual void Flip(const wxPoint &aCentre, bool aFlipLeftRight) override
Function Flip Flip this object, i.e.
Definition: pcb_shape.cpp:252
void SetPolyShape(const SHAPE_POLY_SET &aShape)
Definition: pcb_shape.h:267
wxPoint GetArcMid() const
Definition: pcb_shape.cpp:393
PCB_SHAPE_TYPE_T GetShape() const
Definition: pcb_shape.h:129
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)
Function SetAngle sets the angle for arcs, and normalizes it within the range 0 - 360 degrees.
Definition: pcb_shape.cpp:444
wxPoint m_bezierC2
Definition: pcb_shape.h:55
class PCB_SHAPE, a segment not on copper layers
Definition: typeinfo.h:91
Bezier Curve.
Definition: board_item.h:57
void GetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, std::vector< MSG_PANEL_ITEM > &aList) override
Function GetMsgPanelInfo populates aList of MSG_PANEL_ITEM objects with it's internal state for displ...
Definition: pcb_shape.cpp:466
int GetStartY()
Definition: pcb_shape.h:145
void SetThirdPoint(const wxPoint &aPoint)
Definition: pcb_shape.h:169
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:132
void SetPolyPoints(const std::vector< wxPoint > &aPoints)
Definition: pcb_shape.cpp:1043
const std::vector< wxPoint > buildBezierToSegmentsPointsList(int aMinSegLen) const
Definition: pcb_shape.cpp:330
void SetBezierPoints(const std::vector< wxPoint > &aPoints)
Definition: pcb_shape.h:269
void SetEnd(const wxPoint &aEnd)
Definition: pcb_shape.h:158
int GetStartX()
Definition: pcb_shape.h:146
void SetArcStart(const wxPoint &aArcStartPoint)
Initialize the start arc point.
Definition: pcb_shape.h:212
int GetThirdPointY()
Definition: pcb_shape.h:167
KICAD_T Type() const
Function Type()
Definition: eda_item.h:181
double GetLength() const
Function GetLength returns the length of the track using the hypotenuse calculation.
Definition: pcb_shape.cpp:76