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.

558 lines
18 KiB

14 years ago
14 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2004 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
  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 PCB_DIMENSION_H
  25. #define PCB_DIMENSION_H
  26. #include <board_item.h>
  27. #include <pcb_text.h>
  28. #include <geometry/shape.h>
  29. #include <geometry/circle.h>
  30. class LINE_READER;
  31. class MSG_PANEL_ITEM;
  32. /// How to display the units in a dimension's text
  33. enum class DIM_UNITS_FORMAT
  34. {
  35. NO_SUFFIX, // 1234.0
  36. BARE_SUFFIX, // 1234.0 mm
  37. PAREN_SUFFIX // 1234.0 (mm)
  38. };
  39. /// Where to place the text on a dimension
  40. enum class DIM_TEXT_POSITION
  41. {
  42. OUTSIDE, ///< Text appears outside the dimension line (default)
  43. INLINE, ///< Text appears in line with the dimension line
  44. MANUAL ///< Text placement is manually set by the user
  45. };
  46. /**
  47. * Used for storing the units selection in the file because EDA_UNITS alone doesn't cut it
  48. */
  49. enum class DIM_UNITS_MODE
  50. {
  51. INCHES,
  52. MILS,
  53. MILLIMETRES,
  54. AUTOMATIC
  55. };
  56. /**
  57. * Frame to show around dimension text
  58. */
  59. enum class DIM_TEXT_FRAME
  60. {
  61. NONE,
  62. RECTANGLE,
  63. CIRCLE,
  64. ROUNDRECT
  65. };
  66. /**
  67. * Abstract dimension API
  68. *
  69. * Some notes about dimension nomenclature:
  70. *
  71. * - "feature points" are the points being measured by the dimension. For an example, the start
  72. * and end points of a line to be measured. These are the first points picked when drawing a
  73. * new dimension. Dimensions can have one or more feature points: linear dimensions (the only
  74. * type supported in KiCad 5 and earlier) have two feature points; leader dimensions have one;
  75. * and ordinate dimensions can have in theory an unlimited number of feature points.
  76. *
  77. * - "feature lines" are lines that coincide with feature points. Not all dimension types have
  78. * feature lines. The actual start and end of feature lines is calculated from dimension style
  79. * properties (offset from feature point to start of feature line, height of crossbar, and height
  80. * of feature line past crossbar, for example in linear dimensions)
  81. *
  82. * - "crossbar" refers to the perpendicular line (usually with arrows at each end) between feature
  83. * lines on linear dimensions
  84. */
  85. class PCB_DIMENSION_BASE : public BOARD_ITEM
  86. {
  87. public:
  88. PCB_DIMENSION_BASE( BOARD_ITEM* aParent, KICAD_T aType = PCB_DIMENSION_T );
  89. bool IsType( const KICAD_T aScanTypes[] ) const override
  90. {
  91. if( BOARD_ITEM::IsType( aScanTypes ) )
  92. return true;
  93. for( const KICAD_T* p = aScanTypes; *p != EOT; ++p )
  94. {
  95. if( *p == PCB_LOCATE_GRAPHIC_T )
  96. return true;
  97. }
  98. return false;
  99. }
  100. void SetParent( EDA_ITEM* aParent ) override;
  101. /**
  102. * The dimension's origin is the first feature point for the dimension. Every dimension has
  103. * one or more feature points, so every dimension has at least an origin.
  104. * @return the origin point of this dimension
  105. */
  106. virtual const wxPoint& GetStart() const { return m_start; }
  107. virtual void SetStart( const wxPoint& aPoint ) { m_start = aPoint; }
  108. virtual const wxPoint& GetEnd() const { return m_end; }
  109. virtual void SetEnd( const wxPoint& aPoint ) { m_end = aPoint; }
  110. wxPoint GetPosition() const override { return m_start; }
  111. void SetPosition( const wxPoint& aPos ) override { m_start = aPos; }
  112. bool GetOverrideTextEnabled() const { return m_overrideTextEnabled; }
  113. void SetOverrideTextEnabled( bool aOverride ) { m_overrideTextEnabled = aOverride; }
  114. wxString GetOverrideText() const { return m_valueString; }
  115. void SetOverrideText( const wxString& aValue ) { m_valueString = aValue; }
  116. int GetMeasuredValue() const { return m_measuredValue; }
  117. // KiCad normally calculates the measured value but some importers need to set it.
  118. void SetMeasuredValue( int aValue ) { m_measuredValue = aValue; }
  119. /**
  120. * @return the dimension value, rendered with precision / zero suppression but no units, etc
  121. */
  122. wxString GetValueText() const;
  123. /**
  124. * Update the dimension's cached text and geometry.
  125. */
  126. void Update()
  127. {
  128. updateGeometry();
  129. updateText();
  130. }
  131. wxString GetPrefix() const { return m_prefix; }
  132. void SetPrefix( const wxString& aPrefix );
  133. wxString GetSuffix() const { return m_suffix; }
  134. void SetSuffix( const wxString& aSuffix );
  135. void GetUnits( EDA_UNITS& aUnits ) const { aUnits = m_units; }
  136. void SetUnits( EDA_UNITS aUnits );
  137. DIM_UNITS_MODE GetUnitsMode() const;
  138. void SetUnitsMode( DIM_UNITS_MODE aMode );
  139. void SetAutoUnits( bool aAuto = true ) { m_autoUnits = aAuto; }
  140. DIM_UNITS_FORMAT GetUnitsFormat() const { return m_unitsFormat; }
  141. void SetUnitsFormat( const DIM_UNITS_FORMAT aFormat ) { m_unitsFormat = aFormat; }
  142. int GetPrecision() const { return m_precision; }
  143. void SetPrecision( int aPrecision ) { m_precision = aPrecision; }
  144. bool GetSuppressZeroes() const { return m_suppressZeroes; }
  145. void SetSuppressZeroes( bool aSuppress ) { m_suppressZeroes = aSuppress; }
  146. bool GetKeepTextAligned() const { return m_keepTextAligned; }
  147. void SetKeepTextAligned( bool aKeepAligned ) { m_keepTextAligned = aKeepAligned; }
  148. void SetTextPositionMode( DIM_TEXT_POSITION aMode ) { m_textPosition = aMode; }
  149. DIM_TEXT_POSITION GetTextPositionMode() const { return m_textPosition; }
  150. int GetArrowLength() const { return m_arrowLength; }
  151. void SetArrowLength( int aLength ) { m_arrowLength = aLength; }
  152. void SetExtensionOffset( int aOffset ) { m_extensionOffset = aOffset; }
  153. int GetExtensionOffset() const { return m_extensionOffset; }
  154. int GetLineThickness() const { return m_lineThickness; }
  155. void SetLineThickness( int aWidth ) { m_lineThickness = aWidth; }
  156. void SetLayer( PCB_LAYER_ID aLayer ) override;
  157. void SetTextSize( const wxSize& aTextSize )
  158. {
  159. m_text.SetTextSize( aTextSize );
  160. }
  161. /**
  162. * Set the override text - has no effect if m_overrideValue == false.
  163. *
  164. * @param aNewText is the text to use as the value.
  165. */
  166. void SetText( const wxString& aNewText );
  167. /**
  168. * Retrieve the value text or override text, not including prefix or suffix.
  169. *
  170. * @return the value portion of the dimension text (either overridden or not).
  171. */
  172. const wxString GetText() const;
  173. PCB_TEXT& Text() { return m_text; }
  174. const PCB_TEXT& Text() const { return m_text; }
  175. /**
  176. * @return a list of line segments that make up this dimension (for drawing, plotting, etc).
  177. */
  178. const std::vector<std::shared_ptr<SHAPE>>& GetShapes() const { return m_shapes; }
  179. // BOARD_ITEM overrides
  180. void Move( const wxPoint& offset ) override;
  181. void Rotate( const wxPoint& aRotCentre, double aAngle ) override;
  182. void Flip( const wxPoint& aCentre, bool aFlipLeftRight ) override;
  183. /**
  184. * Mirror the dimension relative to a given horizontal axis.
  185. *
  186. * The text is not mirrored. Only its position (and angle) is mirrored. The layer is not
  187. * changed.
  188. *
  189. * @param axis_pos is the vertical axis position to mirror around.
  190. */
  191. void Mirror( const wxPoint& axis_pos, bool aMirrorLeftRight = false );
  192. void GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList ) override;
  193. bool HitTest( const wxPoint& aPosition, int aAccuracy ) const override;
  194. bool HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy = 0 ) const override;
  195. const EDA_RECT GetBoundingBox() const override;
  196. std::shared_ptr<SHAPE> GetEffectiveShape( PCB_LAYER_ID aLayer ) const override;
  197. wxString GetSelectMenuText( EDA_UNITS aUnits ) const override;
  198. const BOX2I ViewBBox() const override;
  199. void TransformShapeWithClearanceToPolygon( SHAPE_POLY_SET& aCornerBuffer, PCB_LAYER_ID aLayer,
  200. int aClearance, int aError, ERROR_LOC aErrorLoc,
  201. bool aIgnoreLineWidth ) const override;
  202. #if defined(DEBUG)
  203. virtual void Show( int nestLevel, std::ostream& os ) const override { ShowDummy( os ); }
  204. #endif
  205. protected:
  206. /**
  207. * Update the cached geometry of the dimension after changing any of its properties.
  208. */
  209. virtual void updateGeometry() = 0;
  210. /**
  211. * Update the text field value from the current geometry (called by updateGeometry normally).
  212. */
  213. virtual void updateText();
  214. template<typename ShapeType>
  215. void addShape( const ShapeType& aShape );
  216. /**
  217. * Find the intersection between a given segment and polygon outline.
  218. *
  219. * @param aPoly is the polygon to collide.
  220. * @param aSeg is the segment to collide.
  221. * @param aStart if true will start from aSeg.A, otherwise aSeg.B.
  222. * @return a point on aSeg that collides with aPoly closest to the start, if one exists.
  223. */
  224. static OPT_VECTOR2I segPolyIntersection( const SHAPE_POLY_SET& aPoly, const SEG& aSeg,
  225. bool aStart = true );
  226. static OPT_VECTOR2I segCircleIntersection( CIRCLE& aCircle, SEG& aSeg, bool aStart = true );
  227. // Value format
  228. bool m_overrideTextEnabled; ///< Manually specify the displayed measurement value
  229. wxString m_valueString; ///< Displayed value when m_overrideValue = true
  230. wxString m_prefix; ///< String prepended to the value
  231. wxString m_suffix; ///< String appended to the value
  232. EDA_UNITS m_units; ///< 0 = inches, 1 = mm
  233. bool m_autoUnits; ///< If true, follow the currently selected UI units
  234. DIM_UNITS_FORMAT m_unitsFormat; ///< How to render the units suffix
  235. int m_precision; ///< Number of digits to display after decimal
  236. bool m_suppressZeroes; ///< Suppress trailing zeroes
  237. // Geometry
  238. int m_lineThickness; ///< Thickness used for all graphics in the dimension
  239. int m_arrowLength; ///< Length of arrow shapes
  240. int m_extensionOffset; ///< Distance from feature points to extension line start
  241. DIM_TEXT_POSITION m_textPosition; ///< How to position the text
  242. bool m_keepTextAligned; ///< Calculate text orientation to match dimension
  243. // Internal
  244. PCB_TEXT m_text; ///< The actual text object
  245. int m_measuredValue; ///< value of PCB dimensions
  246. wxPoint m_start;
  247. wxPoint m_end;
  248. ///< Internal cache of drawn shapes
  249. std::vector<std::shared_ptr<SHAPE>> m_shapes;
  250. static constexpr float s_arrowAngle = 27.5;
  251. };
  252. /**
  253. * For better understanding of the points that make a dimension:
  254. *
  255. * Note: historically KiCad called extension lines "feature lines", and also note that what we
  256. * call the "crossbar line" here is more commonly called the "dimension line"
  257. *
  258. * Start (feature point 1) End (feature point 2)
  259. * | |
  260. * | <-- extension lines --> |
  261. * | |
  262. * | m_arrowG2F m_arrowD2F |
  263. * | / \ |
  264. * Crossbar start |/_______crossbar line________\| Crossbar end
  265. * |\ m_text /|
  266. * | \ / |
  267. * | m_arrowG1F m_arrowD1F |
  268. * | |
  269. * m_featureLineGF m_featureLineDF
  270. */
  271. /**
  272. * An aligned dimension measures the distance between two feature points. It has a crossbar
  273. * (dimension line) that stays parallel with the vector between the feature points.
  274. *
  275. * The height (distance from features to crossbar) can be set directly, or set by manipulating the
  276. * crossbar start or end point (with the point editor).
  277. */
  278. class PCB_DIM_ALIGNED : public PCB_DIMENSION_BASE
  279. {
  280. public:
  281. PCB_DIM_ALIGNED( BOARD_ITEM* aParent, KICAD_T aType = PCB_DIM_ALIGNED_T );
  282. // Do not create a copy constructor & operator=.
  283. // The ones generated by the compiler are adequate.
  284. ~PCB_DIM_ALIGNED() = default;
  285. static inline bool ClassOf( const EDA_ITEM* aItem )
  286. {
  287. return aItem && PCB_DIM_ALIGNED_T == aItem->Type();
  288. }
  289. EDA_ITEM* Clone() const override;
  290. virtual void SwapData( BOARD_ITEM* aImage ) override;
  291. BITMAPS GetMenuImage() const override;
  292. const wxPoint& GetCrossbarStart() const { return m_crossBarStart; }
  293. const wxPoint& GetCrossbarEnd() const { return m_crossBarEnd; }
  294. /**
  295. * Set the distance from the feature points to the crossbar line.
  296. *
  297. * @param aHeight is the new height.
  298. */
  299. void SetHeight( int aHeight ) { m_height = aHeight; }
  300. int GetHeight() const { return m_height; }
  301. /**
  302. * Update the stored height basing on points coordinates.
  303. *
  304. * @param aCrossbarStart is the start point of the crossbar.
  305. */
  306. void UpdateHeight( const wxPoint& aCrossbarStart, const wxPoint& aCrossbarEnd );
  307. void SetExtensionHeight( int aHeight ) { m_extensionHeight = aHeight; }
  308. int GetExtensionHeight() const { return m_extensionHeight; }
  309. /**
  310. * Return the angle of the crossbar.
  311. *
  312. * @return Angle of the crossbar line expressed in radians.
  313. */
  314. double GetAngle() const
  315. {
  316. wxPoint delta( m_end - m_start );
  317. return atan2( (double)delta.y, (double)delta.x );
  318. }
  319. wxString GetClass() const override
  320. {
  321. return wxT( "PCB_DIM_ALIGNED" );
  322. }
  323. void GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList ) override;
  324. protected:
  325. void updateGeometry() override;
  326. void updateText() override;
  327. // Geometry
  328. int m_height; ///< Perpendicular distance from features to crossbar
  329. int m_extensionHeight; ///< Length of extension lines past the crossbar
  330. wxPoint m_crossBarStart; ///< Crossbar start control point
  331. wxPoint m_crossBarEnd; ///< Crossbar end control point
  332. };
  333. /**
  334. * An orthogonal dimension is like an aligned dimension, but the extension lines are locked to the
  335. * X or Y axes, and the measurement is only taken in the X or Y direction.
  336. */
  337. class PCB_DIM_ORTHOGONAL : public PCB_DIM_ALIGNED
  338. {
  339. public:
  340. enum class DIR
  341. {
  342. HORIZONTAL, // Aligned with x-axis
  343. VERTICAL // Aligned with y-axis
  344. };
  345. PCB_DIM_ORTHOGONAL( BOARD_ITEM* aParent );
  346. ~PCB_DIM_ORTHOGONAL() = default;
  347. static inline bool ClassOf( const EDA_ITEM* aItem )
  348. {
  349. return aItem && PCB_DIM_ORTHOGONAL_T == aItem->Type();
  350. }
  351. EDA_ITEM* Clone() const override;
  352. void SwapData( BOARD_ITEM* aImage ) override;
  353. BITMAPS GetMenuImage() const override;
  354. /**
  355. * Set the orientation of the dimension line (so, perpendicular to the feature lines).
  356. *
  357. * @param aOrientation is the orientation the dimension should take.
  358. */
  359. void SetOrientation( DIR aOrientation ) { m_orientation = aOrientation; }
  360. DIR GetOrientation() const { return m_orientation; }
  361. wxString GetClass() const override
  362. {
  363. return wxT( "PCB_DIM_ORTHOGONAL" );
  364. }
  365. void Rotate( const wxPoint& aRotCentre, double aAngle ) override;
  366. protected:
  367. void updateGeometry() override;
  368. void updateText() override;
  369. private:
  370. // Geometry
  371. DIR m_orientation; ///< What axis to lock the dimension line to.
  372. };
  373. /**
  374. * A leader is a dimension-like object pointing to a specific point.
  375. *
  376. * A guide to the geometry of a leader:
  377. *
  378. * a
  379. * _
  380. * |\
  381. * \
  382. * b---c TEXT
  383. *
  384. * Point (a) is m_start, point (b) is m_end, point (c) is the end of the "text line"
  385. * The b-c line is drawn from b to the text center, and clipped on the text bounding box.
  386. */
  387. class PCB_DIM_LEADER : public PCB_DIMENSION_BASE
  388. {
  389. public:
  390. PCB_DIM_LEADER( BOARD_ITEM* aParent );
  391. static inline bool ClassOf( const EDA_ITEM* aItem )
  392. {
  393. return aItem && PCB_DIM_LEADER_T == aItem->Type();
  394. }
  395. EDA_ITEM* Clone() const override;
  396. virtual void SwapData( BOARD_ITEM* aImage ) override;
  397. BITMAPS GetMenuImage() const override;
  398. wxString GetClass() const override
  399. {
  400. return wxT( "PCB_DIM_LEADER" );
  401. }
  402. void SetTextFrame( DIM_TEXT_FRAME aFrame ) { m_textFrame = aFrame; }
  403. DIM_TEXT_FRAME GetTextFrame() const { return m_textFrame; }
  404. void GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList ) override;
  405. protected:
  406. void updateGeometry() override;
  407. private:
  408. DIM_TEXT_FRAME m_textFrame;
  409. };
  410. /**
  411. * Mark the center of a circle or arc with a cross shape.
  412. *
  413. * The size and orientation of the cross is adjustable.
  414. * m_start always marks the center being measured; m_end marks the end of one leg of the cross.
  415. */
  416. class PCB_DIM_CENTER : public PCB_DIMENSION_BASE
  417. {
  418. public:
  419. PCB_DIM_CENTER( BOARD_ITEM* aParent );
  420. static inline bool ClassOf( const EDA_ITEM* aItem )
  421. {
  422. return aItem && PCB_DIM_CENTER_T == aItem->Type();
  423. }
  424. EDA_ITEM* Clone() const override;
  425. virtual void SwapData( BOARD_ITEM* aImage ) override;
  426. BITMAPS GetMenuImage() const override;
  427. wxString GetClass() const override
  428. {
  429. return wxT( "PCB_DIM_CENTER" );
  430. }
  431. const EDA_RECT GetBoundingBox() const override;
  432. const BOX2I ViewBBox() const override;
  433. protected:
  434. void updateGeometry() override;
  435. };
  436. #endif // DIMENSION_H