You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

327 lines
10 KiB

  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. #ifndef EDA_SHAPE_H
  25. #define EDA_SHAPE_H
  26. #include <eda_units.h>
  27. #include <convert_to_biu.h>
  28. #include <trigo.h>
  29. #include <geometry/shape_poly_set.h>
  30. #include <geometry/geometry_utils.h>
  31. #include <stroke_params.h>
  32. class LINE_READER;
  33. class EDA_DRAW_FRAME;
  34. class FOOTPRINT;
  35. class MSG_PANEL_ITEM;
  36. using KIGFX::COLOR4D;
  37. enum class SHAPE_T : int
  38. {
  39. SEGMENT = 0,
  40. RECT,
  41. ARC,
  42. CIRCLE,
  43. POLY,
  44. BEZIER,
  45. LAST ///< marker for list end
  46. };
  47. // WARNING: Do not change these values without updating dialogs that depend on their position values
  48. enum class FILL_T : int
  49. {
  50. NO_FILL = 1,
  51. FILLED_SHAPE, // Fill with object color
  52. FILLED_WITH_BG_BODYCOLOR, // Fill with background body color
  53. FILLED_WITH_COLOR // Fill with a separate color
  54. };
  55. class EDA_SHAPE
  56. {
  57. public:
  58. EDA_SHAPE( SHAPE_T aType, int aLineWidth, FILL_T aFill, bool eeWinding );
  59. // Do not create a copy constructor & operator=.
  60. // The ones generated by the compiler are adequate.
  61. ~EDA_SHAPE();
  62. void SwapShape( EDA_SHAPE* aImage );
  63. wxString ShowShape() const;
  64. wxString SHAPE_T_asString() const;
  65. bool IsFilled() const
  66. {
  67. return GetFillMode() != FILL_T::NO_FILL;
  68. }
  69. void SetFilled( bool aFlag )
  70. {
  71. m_fill = aFlag ? FILL_T::FILLED_SHAPE : FILL_T::NO_FILL;
  72. }
  73. void SetFillMode( FILL_T aFill ) { m_fill = aFill; }
  74. FILL_T GetFillMode() const { return m_fill; }
  75. COLOR4D GetFillColor() const { return m_fillColor; }
  76. void SetFillColor( const COLOR4D& aColor ) { m_fillColor = aColor; }
  77. void SetWidth( int aWidth ) { m_stroke.SetWidth( aWidth ); }
  78. int GetWidth() const { return m_stroke.GetWidth(); }
  79. void SetShape( SHAPE_T aShape ) { m_shape = aShape; }
  80. SHAPE_T GetShape() const { return m_shape; }
  81. /**
  82. * Return the starting point of the graphic.
  83. */
  84. const wxPoint& GetStart() const { return m_start; }
  85. int GetStartY() { return m_start.y; }
  86. int GetStartX() { return m_start.x; }
  87. void SetStart( const wxPoint& aStart )
  88. {
  89. m_start = aStart;
  90. m_endsSwapped = false;
  91. }
  92. void SetStartY( int y )
  93. {
  94. m_start.y = y;
  95. m_endsSwapped = false;
  96. }
  97. void SetStartX( int x )
  98. {
  99. m_start.x = x;
  100. m_endsSwapped = false;
  101. }
  102. /**
  103. * Return the ending point of the graphic.
  104. */
  105. const wxPoint& GetEnd() const { return m_end; }
  106. int GetEndY() { return m_end.y; }
  107. int GetEndX() { return m_end.x; }
  108. void SetEnd( const wxPoint& aEnd )
  109. {
  110. m_end = aEnd;
  111. m_endsSwapped = false;
  112. }
  113. void SetEndY( int y )
  114. {
  115. m_end.y = y;
  116. m_endsSwapped = false;
  117. }
  118. void SetEndX( int x )
  119. {
  120. m_end.x = x;
  121. m_endsSwapped = false;
  122. }
  123. void SetBezierC1( const wxPoint& aPt ) { m_bezierC1 = aPt; }
  124. const wxPoint& GetBezierC1() const { return m_bezierC1; }
  125. void SetBezierC2( const wxPoint& aPt ) { m_bezierC2 = aPt; }
  126. const wxPoint& GetBezierC2() const { return m_bezierC2; }
  127. wxPoint getCenter() const;
  128. void SetCenter( const wxPoint& aCenter );
  129. /**
  130. * Set the end point from the angle center and start.
  131. *
  132. * @param aAngle is tenths of degrees.
  133. */
  134. void SetArcAngleAndEnd( double aAngle, bool aCheckNegativeAngle = false );
  135. double GetArcAngle() const;
  136. /**
  137. * Have the start and end points been swapped since they were set?
  138. * @return true if they have
  139. */
  140. bool EndsSwapped() const { return m_endsSwapped; }
  141. // Some attributes are read only, since they are derived from m_Start, m_End, and m_Angle.
  142. // No Set...() function for these attributes.
  143. wxPoint GetArcMid() const;
  144. std::vector<wxPoint> GetRectCorners() const;
  145. /**
  146. * Calc arc start and end angles such that aStartAngle < aEndAngle. Each may be between
  147. * -360.0 and 360.0.
  148. */
  149. void CalcArcAngles( double& aStartAngle, double& aEndAngle ) const;
  150. int GetRadius() const;
  151. /**
  152. * Set the three controlling points for an arc.
  153. *
  154. * NB: these are NOT what's currently stored, so we have to do some calculations behind
  155. * the scenes. However, they are what SHOULD be stored.
  156. */
  157. void SetArcGeometry( const wxPoint& aStart, const wxPoint& aMid, const wxPoint& aEnd );
  158. const std::vector<wxPoint>& GetBezierPoints() const { return m_bezierPoints; }
  159. /**
  160. * Duplicate the list of corners in a std::vector<wxPoint>
  161. *
  162. * It must be used only to convert the SHAPE_POLY_SET internal corner buffer
  163. * to a list of wxPoints, and nothing else, because it duplicates the buffer,
  164. * that is inefficient to know for instance the corner count
  165. */
  166. void DupPolyPointsList( std::vector<wxPoint>& aBuffer ) const;
  167. /**
  168. * @return the number of corners of the polygonal shape
  169. */
  170. int GetPointCount() const;
  171. // Accessors to the polygonal shape
  172. SHAPE_POLY_SET& GetPolyShape() { return m_poly; }
  173. const SHAPE_POLY_SET& GetPolyShape() const { return m_poly; }
  174. /**
  175. * @return true if the polygonal shape is valid (has more than 2 points)
  176. */
  177. bool IsPolyShapeValid() const;
  178. void SetPolyShape( const SHAPE_POLY_SET& aShape ) { m_poly = aShape; }
  179. void SetBezierPoints( const std::vector<wxPoint>& aPoints ) { m_bezierPoints = aPoints; }
  180. /**
  181. * Rebuild the m_BezierPoints vertex list that approximate the Bezier curve
  182. * by a list of segments.
  183. *
  184. * Has meaning only for BEZIER shape.
  185. *
  186. * @param aMinSegLen is the min length of segments approximating the bezier. The shape's last
  187. * segment can be shorter. This parameter avoids having too many very short
  188. * segment in list. Good values are between m_width/2 and m_width.
  189. */
  190. void RebuildBezierToSegmentsPointsList( int aMinSegLen );
  191. void SetPolyPoints( const std::vector<wxPoint>& aPoints );
  192. /**
  193. * Make a set of SHAPE objects representing the EDA_SHAPE. Caller owns the objects.
  194. *
  195. * @param aEdgeOnly indicates only edges should be generated (even if 0 width), and no fill
  196. * shapes.
  197. */
  198. // fixme: move to shape_compound
  199. std::vector<SHAPE*> MakeEffectiveShapes( bool aEdgeOnly = false ) const;
  200. void ShapeGetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList );
  201. /**
  202. * Return the length of the track using the hypotenuse calculation.
  203. *
  204. * @return the length of the track
  205. */
  206. double GetLength() const;
  207. /**
  208. * Convert the shape to a closed polygon.
  209. *
  210. * Used in filling zones calculations. Circles and arcs are approximated by segments.
  211. *
  212. * @param aCornerBuffer is a buffer to store the polygon.
  213. * @param aClearanceValue is the clearance around the pad.
  214. * @param aError is the maximum deviation from a true arc.
  215. * @param aErrorLoc whether any approximation error shoule be placed inside or outside
  216. * @param ignoreLineWidth is used for edge cut items where the line width is only
  217. * for visualization
  218. */
  219. void TransformShapeWithClearanceToPolygon( SHAPE_POLY_SET& aCornerBuffer, int aClearanceValue,
  220. int aError, ERROR_LOC aErrorLoc,
  221. bool ignoreLineWidth ) const;
  222. int Compare( const EDA_SHAPE* aOther ) const;
  223. protected:
  224. void setPosition( const wxPoint& aPos );
  225. wxPoint getPosition() const;
  226. void move( const wxPoint& aMoveVector );
  227. void rotate( const wxPoint& aRotCentre, double aAngle );
  228. void flip( const wxPoint& aCentre, bool aFlipLeftRight );
  229. void scale( double aScale );
  230. // To be implemented by concrete classes
  231. virtual double getParentOrientation() const = 0;
  232. virtual wxPoint getParentPosition() const = 0;
  233. const EDA_RECT getBoundingBox() const;
  234. void computeArcBBox( EDA_RECT& aBBox ) const;
  235. bool hitTest( const wxPoint& aPosition, int aAccuracy = 0 ) const;
  236. bool hitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy = 0 ) const;
  237. const std::vector<wxPoint> buildBezierToSegmentsPointsList( int aMinSegLen ) const;
  238. void beginEdit( const wxPoint& aStartPoint );
  239. bool continueEdit( const wxPoint& aPosition );
  240. void calcEdit( const wxPoint& aPosition );
  241. void endEdit();
  242. void setEditState( int aState ) { m_editState = aState; }
  243. protected:
  244. bool m_endsSwapped; // true if start/end were swapped e.g. SetArcAngleAndEnd
  245. SHAPE_T m_shape; // Shape: line, Circle, Arc
  246. STROKE_PARAMS m_stroke; // Line style, width, etc.
  247. FILL_T m_fill;
  248. COLOR4D m_fillColor;
  249. wxPoint m_start; // Line start point or Circle center
  250. wxPoint m_end; // Line end point or Circle 3 o'clock point
  251. wxPoint m_arcCenter; // Used only for Arcs: arc end point
  252. wxPoint m_bezierC1; // Bezier Control Point 1
  253. wxPoint m_bezierC2; // Bezier Control Point 2
  254. std::vector<wxPoint> m_bezierPoints;
  255. SHAPE_POLY_SET m_poly; // Stores the S_POLYGON shape
  256. int m_editState;
  257. bool m_eeWinding; // Awful hack
  258. };
  259. #endif // EDA_SHAPE_H