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.

359 lines
10 KiB

  1. /*
  2. * This program source code file is part of KICAD, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2016 CERN
  5. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * @author Maciej Suminski <maciej.suminski@cern.ch>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, you may find one here:
  21. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  22. * or you may search the http://www.gnu.org website for the version 2 license,
  23. * or you may write to the Free Software Foundation, Inc.,
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  25. */
  26. #ifndef GRAPHICS_IMPORTER_H
  27. #define GRAPHICS_IMPORTER_H
  28. #include "graphics_import_mgr.h"
  29. #include "graphics_import_plugin.h"
  30. #include <eda_text.h>
  31. #include <math/vector2d.h>
  32. #include <gal/color4d.h>
  33. #include <stroke_params.h>
  34. #include <list>
  35. #include <memory>
  36. #include <vector>
  37. class EDA_ITEM;
  38. class EDA_SHAPE;
  39. /**
  40. * A clone of IMPORTED_STROKE, but with floating-point width.
  41. */
  42. class IMPORTED_STROKE
  43. {
  44. public:
  45. IMPORTED_STROKE( double aWidth = 0, LINE_STYLE aPlotStyle = LINE_STYLE::DEFAULT,
  46. const KIGFX::COLOR4D& aColor = KIGFX::COLOR4D::UNSPECIFIED ) :
  47. m_width( aWidth ),
  48. m_plotstyle( aPlotStyle ), m_color( aColor )
  49. {
  50. }
  51. double GetWidth() const { return m_width; }
  52. void SetWidth( double aWidth ) { m_width = aWidth; }
  53. LINE_STYLE GetPlotStyle() const { return m_plotstyle; }
  54. void SetPlotStyle( LINE_STYLE aPlotStyle ) { m_plotstyle = aPlotStyle; }
  55. KIGFX::COLOR4D GetColor() const { return m_color; }
  56. void SetColor( const KIGFX::COLOR4D& aColor ) { m_color = aColor; }
  57. private:
  58. double m_width;
  59. LINE_STYLE m_plotstyle;
  60. KIGFX::COLOR4D m_color;
  61. };
  62. /**
  63. * Interface that creates objects representing shapes for a given data model.
  64. */
  65. class GRAPHICS_IMPORTER
  66. {
  67. public:
  68. enum POLY_FILL_RULE
  69. {
  70. PF_NONZERO = 0,
  71. PF_EVEN_ODD
  72. };
  73. GRAPHICS_IMPORTER();
  74. virtual ~GRAPHICS_IMPORTER()
  75. {
  76. }
  77. /**
  78. * Set the import plugin used to obtain shapes from a file.
  79. */
  80. void SetPlugin( std::unique_ptr<GRAPHICS_IMPORT_PLUGIN> aPlugin )
  81. {
  82. m_plugin = std::move( aPlugin );
  83. if( m_plugin )
  84. m_plugin->SetImporter( this );
  85. }
  86. /**
  87. * Load file and get its basic data
  88. *
  89. */
  90. bool Load( const wxString& aFileName );
  91. /**
  92. * Import shapes from loaded file.
  93. *
  94. * It is important to have the file loaded before importing.
  95. *
  96. * @param aScale allow import graphic items with a non 1:1 import ratio
  97. * VECTOR2D( 1.0, 1.0 ) to import graphics with their actual size.
  98. */
  99. bool Import( const VECTOR2D& aScale = VECTOR2D( 1.0, 1.0 ) );
  100. /**
  101. * Collect warning and error messages after loading/importing.
  102. *
  103. * @return the list of messages in one string. Each message ends by '\n'
  104. */
  105. const wxString& GetMessages() const
  106. {
  107. return m_plugin->GetMessages();
  108. }
  109. void ReportMsg( const wxString& aMessage )
  110. {
  111. m_plugin->ReportMsg( aMessage );
  112. }
  113. /**
  114. * Get original image Width.
  115. *
  116. * @return Width of the loaded image in mm.
  117. */
  118. double GetImageWidthMM() const
  119. {
  120. return m_originalWidth;
  121. }
  122. /**
  123. * Get original image Height
  124. *
  125. * @return Height of the loaded image in mm.
  126. */
  127. double GetImageHeightMM() const
  128. {
  129. return m_originalHeight;
  130. }
  131. /**
  132. * Set the line width for the imported outlines (in mm).
  133. */
  134. void SetLineWidthMM( double aWidth )
  135. {
  136. m_lineWidth = aWidth;
  137. }
  138. /**
  139. * Return the line width used for importing the outlines (in mm).
  140. */
  141. double GetLineWidthMM() const
  142. {
  143. return m_lineWidth;
  144. }
  145. /**
  146. * @return the scale factor affecting the imported shapes.
  147. */
  148. VECTOR2D GetScale() const
  149. {
  150. return m_scale;
  151. }
  152. /**
  153. * @return the offset in millimeters to add to coordinates when importing graphic items.
  154. */
  155. const VECTOR2D& GetImportOffsetMM() const
  156. {
  157. return m_offsetCoordmm;
  158. }
  159. /**
  160. * Set the offset in millimeters to add to coordinates when importing graphic items.
  161. */
  162. void SetImportOffsetMM( const VECTOR2D& aOffset )
  163. {
  164. m_offsetCoordmm = aOffset;
  165. }
  166. /**
  167. * Set the scale factor affecting the imported shapes.
  168. *
  169. * This allows conversion between imported shapes units and millimeters.
  170. */
  171. void SetScale( const VECTOR2D& aScale )
  172. {
  173. m_scale = aScale;
  174. }
  175. /**
  176. * @return the conversion factor from mm to internal unit
  177. */
  178. double GetMillimeterToIuFactor()
  179. {
  180. return m_millimeterToIu;
  181. }
  182. /**
  183. * @return the overall scale factor to convert the imported shapes dimension to mm.
  184. */
  185. VECTOR2D ImportScalingFactor() const
  186. {
  187. return m_scale * m_millimeterToIu;
  188. }
  189. /**
  190. * Return the list of objects representing the imported shapes.
  191. */
  192. std::list<std::unique_ptr<EDA_ITEM>>& GetItems()
  193. {
  194. return m_items;
  195. }
  196. /**
  197. * Empties out the imported shapes list
  198. */
  199. void ClearItems()
  200. {
  201. m_items.clear();
  202. }
  203. /// Default line thickness (in mm).
  204. static constexpr unsigned int DEFAULT_LINE_WIDTH_DFX = 1;
  205. virtual void NewShape( POLY_FILL_RULE aFillRule = PF_NONZERO );
  206. /**
  207. * Create an object representing a line segment.
  208. *
  209. * @param aOrigin is the segment origin point expressed in mm.
  210. * @param aEnd is the segment end point expressed in mm.
  211. * @param aStroke is the shape stroke parameters.
  212. */
  213. virtual void AddLine( const VECTOR2D& aOrigin, const VECTOR2D& aEnd,
  214. const IMPORTED_STROKE& aStroke ) = 0;
  215. /**
  216. * Create an object representing a circle.
  217. *
  218. * @param aCenter is the circle center point expressed in mm.
  219. * @param aRadius is the circle radius expressed in mm.
  220. * @param aStroke is the shape stroke parameters.
  221. */
  222. virtual void AddCircle( const VECTOR2D& aCenter, double aRadius, const IMPORTED_STROKE& aStroke,
  223. bool aFilled, const COLOR4D& aFillColor ) = 0;
  224. /**
  225. * Create an object representing an arc.
  226. *
  227. * @param aCenter is the arc center point expressed in mm.
  228. * @param aStart is the arc arm end point expressed in mm.
  229. * Its length is the arc radius.
  230. * @param aAngle is the arc angle.
  231. * @param aStroke is the shape stroke parameters.
  232. */
  233. virtual void AddArc( const VECTOR2D& aCenter, const VECTOR2D& aStart, const EDA_ANGLE& aAngle,
  234. const IMPORTED_STROKE& aStroke ) = 0;
  235. /**
  236. * Create an object representing a polygon.
  237. *
  238. * @param aVertices is the array of vertices.
  239. * @param aWidth is the stroke width.
  240. * @param aStroke is the shape stroke parameters.
  241. * @param aFillColor is the fill color.
  242. */
  243. virtual void AddPolygon( const std::vector<VECTOR2D>& aVertices, const IMPORTED_STROKE& aStroke,
  244. bool aFilled, const COLOR4D& aFillColor ) = 0;
  245. /**
  246. * Create an object representing a text.
  247. *
  248. * @param aOrigin is the text position.
  249. * @param aText is the displayed text.
  250. * @param aHeight is the text height expressed in mm.
  251. * @param aWidth is the text width expressed in mm.
  252. * @param aOrientation is the text orientation angle expressed in degrees.
  253. * @param aHJustify is the text horizontal justification.
  254. * @param aVJustify is the text vertical justification.
  255. * @param aWidth is the segment thickness in mm. Use -1 for default line thickness
  256. * @param aColor is the shape color
  257. */
  258. virtual void AddText( const VECTOR2D& aOrigin, const wxString& aText, double aHeight,
  259. double aWidth, double aThickness, double aOrientation,
  260. GR_TEXT_H_ALIGN_T aHJustify, GR_TEXT_V_ALIGN_T aVJustify,
  261. const COLOR4D& aColor ) = 0;
  262. /**
  263. * Create an object representing an arc.
  264. *
  265. * @param aStart is the curve start point expressed in mm.
  266. * @param aBezierControl1 is the first Bezier control point expressed in mm.
  267. * @param aBezierControl2 is the second Bezier control point expressed in mm.
  268. * @param aEnd is the curve end point expressed in mm.
  269. * @param aStroke is the shape stroke parameters.
  270. */
  271. virtual void AddSpline( const VECTOR2D& aStart, const VECTOR2D& aBezierControl1,
  272. const VECTOR2D& aBezierControl2, const VECTOR2D& aEnd,
  273. const IMPORTED_STROKE& aStroke ) = 0;
  274. protected:
  275. /// Add an item to the imported shapes list.
  276. void addItem( std::unique_ptr<EDA_ITEM> aItem );
  277. /**
  278. * Configure a shape as a spline or a line segment if it's degenerate.
  279. *
  280. * @return false if the shape is near-zero length and should be ignored.
  281. */
  282. bool setupSplineOrLine( EDA_SHAPE& aShape, int aAccuracy );
  283. /// Factor to convert millimeters to Internal Units.
  284. double m_millimeterToIu;
  285. /// Offset (in mm) for imported coordinates.
  286. VECTOR2D m_offsetCoordmm;
  287. std::vector<POLY_FILL_RULE> m_shapeFillRules;
  288. private:
  289. /// List of imported items.
  290. std::list<std::unique_ptr<EDA_ITEM>> m_items;
  291. /// Plugin used to load a file.
  292. std::unique_ptr<GRAPHICS_IMPORT_PLUGIN> m_plugin;
  293. /// Total image width.
  294. double m_originalWidth;
  295. /// Total image Height.
  296. double m_originalHeight;
  297. /**
  298. * Scale factor applied to the imported graphics.
  299. *
  300. * 1.0 does not change the size of imported items
  301. * scale < 1.0 reduce the size of imported items
  302. */
  303. VECTOR2D m_scale;
  304. /// Default line thickness for the imported graphics.
  305. double m_lineWidth;
  306. };
  307. #endif /* GRAPHICS_IMPORTER_H */