KiCad PCB EDA Suite
dxf_import_plugin.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 
26 #ifndef DXF2BRD_ITEMS_H
27 #define DXF2BRD_ITEMS_H
28 
29 #include "graphics_import_plugin.h"
31 
32 #include "dl_dxf.h"
33 #include "dl_creationadapter.h"
34 #include "wx/wx.h"
35 #include <list>
36 
37 class BOARD;
38 class BOARD_ITEM;
39 
44 {
45  double m_x;
46  double m_y;
47  double m_weight;
48 
49  SPLINE_CTRL_POINT( double a_x, double a_y, double a_weight )
50  : m_x( a_x ), m_y( a_y ), m_weight( a_weight )
51  {}
52 };
53 
58 {
59 public:
60  int m_EntityType; // the DXF type of entity
61  int m_EntityParseStatus; // Inside a entity: status od parsing:
62  // 0 = no entity
63  // 1 = first item of entity
64  // 2 = entity in progress
65  int m_EntityFlag; // a info flag to parse entities
66 
67  VECTOR2D m_LastCoordinate; // the last vertex coordinate read (unit = mm)
68  VECTOR2D m_PolylineStart; // The first point of the polyline entity, when reading a polyline (unit = mm)
69  double m_BulgeVertex; // the last vertex bulge value read
70 
71  // for spline parsing: parameters
72  unsigned int m_SplineDegree;
73  unsigned int m_SplineKnotsCount;
74  unsigned int m_SplineControlCount;
75  unsigned int m_SplineFitCount;
76  double m_SplineTangentStartX; // tangeant dir X for the start point
77  double m_SplineTangentStartY; // tangeant dir Y for the start point
78  double m_SplineTangentEndX; // tangeant dir X for the end point
79  double m_SplineTangentEndY; // tangeant dir Y for the end point
80 
81  // for spline parsing: buffers to store control points, fit points and knot
82  std::vector<double> m_SplineKnotsList; // knots list, code 40
83  // control points list coordinates, code 10, 20 & 30 (only X and Y cood and Weight)
84  std::vector<SPLINE_CTRL_POINT> m_SplineControlPointList;
85  // fit points list, code 11, 21 & 31 (only X and Y cood)
86  std::vector<VECTOR2D> m_SplineFitPointList;
87 
88  DXF2BRD_ENTITY_DATA() { Clear(); };
89 
90  // Reset the entity parameters
91  void Clear()
92  {
93  m_EntityType = DL_UNKNOWN;
94  m_EntityParseStatus = 0;
95  m_EntityFlag = 0;
96  m_SplineDegree = 1;
97  m_SplineKnotsCount = 0;
98  m_SplineControlCount = 0;
99  m_SplineFitCount = 0;
100  m_SplineTangentStartX = 0.0;
101  m_SplineTangentStartY = 0.0;
102  m_SplineTangentEndX = 0.0;
103  m_SplineTangentEndY = 0.0;
104  m_SplineKnotsList.clear();
105  m_SplineControlPointList.clear();
106  m_SplineFitPointList.clear();
107  }
108 };
109 
110 
115 class DXF_IMPORT_PLUGIN : public GRAPHICS_IMPORT_PLUGIN, public DL_CreationAdapter
116 {
117 private:
118  double m_xOffset; // X coord offset for conversion (in mm)
119  double m_yOffset; // Y coord offset for conversion (in mm)
120  double m_defaultThickness; // default line thickness for conversion (in mm)
121  double m_DXF2mm; // The scale factor to convert DXF units to mm
122  int m_brdLayer; // The board layer to place imported DXF items
123  int m_version; // the dxf version, not used here
124  std::string m_codePage; // The code page, not used here
125  bool m_importAsfootprintGraphicItems; // Use module items instead of board items when true.
126  // true when the items are imported in the footprint editor
127  std::string m_messages; // messages generated during dxf file parsing.
128  // Each message ends by '\n'
129  DXF2BRD_ENTITY_DATA m_curr_entity; // the current entity parameters when parsing a DXF entity
130 
131  double m_minX, m_maxX; // handles image size in mm
132  double m_minY, m_maxY; // handles image size in mm
133 
135 
136 
137 public:
140 
141  const wxString GetName() const override
142  {
143  return "AutoCAD DXF";
144  }
145 
146  const wxArrayString GetFileExtensions() const override
147  {
148  wxArrayString list;
149  list.Add( "dxf" );
150  return list;
151  }
152 
153  bool Load( const wxString& aFileName ) override;
154  bool Import() override;
155 
156  double GetImageWidth() const override;
157  double GetImageHeight() const override;
158 
159  void updateImageLimits( const VECTOR2D& aPoint );
160 
166  void ImportAsFootprintGraphic( bool aImportAsFootprintGraphic )
167  {
168  m_importAsfootprintGraphicItems = aImportAsFootprintGraphic;
169  }
170 
171 
178  void SetDefaultLineWidthMM( double aWidth )
179  {
180  m_defaultThickness = aWidth;
181  }
182 
191  void SetOffset( double aOffsetX, double aOffsetY )
192  {
193  m_xOffset = aOffsetX;
194  m_yOffset = aOffsetY;
195  }
196 
201  void SetBrdLayer( int aBrdLayer ) { m_brdLayer = aBrdLayer; }
202 
209  bool ImportDxfFile( const wxString& aFile );
210 
214  const std::string& GetMessages() const override
215  {
216  return m_messages;
217  }
218 
219 
220 private:
221  // report message to keep trace of not supported dxf entities:
222  void reportMsg( const char* aMessage );
223 
224  // coordinate conversions from dxf file to mm
225  double mapX( double aDxfCoordX );
226  double mapY( double aDxfCoordY );
227  double mapDim( double aDxfValue );
228  // mapWidth returns ( in mm) the aDxfValue if aDxfWidth > 0
229  // or m_defaultThickness
230  double mapWidth( double aDxfWidth );
231 
232  // Functions to aid in the creation of a Polyline
233  void insertLine( const VECTOR2D& aSegStart, const VECTOR2D& aSegEnd, int aWidth );
234  void insertArc( const VECTOR2D& aSegStart, const VECTOR2D& aSegEnd,
235  double aBulge, int aWidth );
236  // Add a dxf spline (stored in m_curr_entity) to the board, after conversion to segments
237  void insertSpline( int aWidth );
238 
239  // Methods from DL_CreationAdapter:
240  // They are something like"call back" fonctions,
241  // called when the corresponding object is read in dxf file
242 
246  virtual void setVariableString( const std::string& key, const std::string& value,
247  int code ) override;
248 
252  virtual void setVariableInt( const std::string& key, int value, int code ) override;
253 
257  virtual void setVariableDouble( const std::string& key, double value, int code ) override {}
258 
259  virtual void addLayer( const DL_LayerData& aData ) override;
260  virtual void addLine( const DL_LineData& aData) override;
261  virtual void addCircle( const DL_CircleData& aData ) override;
262  virtual void addArc( const DL_ArcData& aData ) override;
263  //virtual void addLWPolyline( const DRW_LWPolyline& aData ) override;
264  virtual void addText( const DL_TextData& aData ) override;
265  virtual void addPolyline( const DL_PolylineData& aData ) override;
266 
268  virtual void addVertex( const DL_VertexData& aData ) override;
269  virtual void addMText( const DL_MTextData& aData) override;
270  virtual void addTextStyle( const DL_StyleData& aData ) override;
271 
272  virtual void endEntity() override;
273 
275  virtual void addSpline( const DL_SplineData& aData ) override;
276 
278  virtual void addControlPoint( const DL_ControlPointData& aData ) override;
279 
281  virtual void addFitPoint( const DL_FitPointData& aData ) override;
282 
284  virtual void addKnot( const DL_KnotData& aData ) override;
285 
286  // Not yet handled DXF entities:
287  virtual void addDimAlign( const DL_DimensionData&,
288  const DL_DimAlignedData& ) override { reportMsg( "DL_Dimension not managed" ); }
289  virtual void addDimLinear( const DL_DimensionData&,
290  const DL_DimLinearData& ) override { reportMsg( "DL_Dimension not managed" ); }
291  virtual void addDimRadial( const DL_DimensionData&,
292  const DL_DimRadialData& ) override { reportMsg( "DL_Dimension not managed" ); }
293  virtual void addDimDiametric( const DL_DimensionData&,
294  const DL_DimDiametricData& ) override { reportMsg( "DL_Dimension not managed" ); }
295  virtual void addDimAngular( const DL_DimensionData&,
296  const DL_DimAngularData& ) override { reportMsg( "DL_Dimension not managed" ); }
297  virtual void addDimAngular3P( const DL_DimensionData&,
298  const DL_DimAngular3PData& ) override { reportMsg( "DL_Dimension not managed" ); }
299  virtual void addDimOrdinate( const DL_DimensionData&,
300  const DL_DimOrdinateData& ) override { reportMsg( "DL_Dimension not managed" ); }
301  virtual void addLeader( const DL_LeaderData& ) override { reportMsg( "DL_Leader not managed" ); }
302  virtual void addLeaderVertex( const DL_LeaderVertexData& ) override { reportMsg( "DL_LeaderVertex not managed" ); }
303 
304  virtual void addHatch( const DL_HatchData& ) override { reportMsg( "DL_Hatch not managed" ); }
305 
306  virtual void addTrace( const DL_TraceData& ) override { reportMsg( "DL_Trace not managed" ); }
307  virtual void add3dFace( const DL_3dFaceData& ) override { reportMsg( "DL_3dFace not managed" ); }
308  virtual void addSolid( const DL_SolidData& ) override { reportMsg( "DL_Solid not managed" ); }
309 
310  virtual void addImage( const DL_ImageData& ) override { reportMsg( "DL_ImageDa not managed" ); }
311  virtual void linkImage( const DL_ImageDefData& ) override { reportMsg( "DL_ImageDef not managed" ); }
312  virtual void addHatchLoop( const DL_HatchLoopData& ) override { reportMsg( "DL_HatchLoop not managed" ); }
313  virtual void addHatchEdge( const DL_HatchEdgeData& ) override { reportMsg( "DL_HatchEdge not managed" ); }
314 
323  static wxString toDxfString( const wxString& aStr );
324 
328  static wxString toNativeString( const wxString& aData );
329 
330  void writeLine();
331  void writeMtext();
332 };
333 
334 #endif // DXF2BRD_ITEMS_H
DXF2BRD_ENTITY_DATA m_curr_entity
virtual void add3dFace(const DL_3dFaceData &) override
SPLINE_CTRL_POINT(double a_x, double a_y, double a_weight)
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class...
Interface for vector graphics import plugins.
unsigned int m_SplineFitCount
virtual void addHatchEdge(const DL_HatchEdgeData &) override
unsigned int m_SplineControlCount
virtual void addDimAngular3P(const DL_DimensionData &, const DL_DimAngular3PData &) override
A helper class to store a spline control point (in X,Y plane only)
A helper class to parse a DXF entity (polyline and spline)
void SetOffset(double aOffsetX, double aOffsetY)
Set the coordinate offset between the imported dxf items and Pcbnew.
virtual void addDimAlign(const DL_DimensionData &, const DL_DimAlignedData &) override
virtual void addDimRadial(const DL_DimensionData &, const DL_DimRadialData &) override
std::vector< VECTOR2D > m_SplineFitPointList
void ImportAsFootprintGraphic(bool aImportAsFootprintGraphic)
Allows the import DXF items converted to board graphic items or footprint graphic items...
void SetDefaultLineWidthMM(double aWidth)
Set the default line width when importing dxf items like lines to Pcbnew.
virtual void addDimLinear(const DL_DimensionData &, const DL_DimLinearData &) override
virtual void addLeader(const DL_LeaderData &) override
virtual void addImage(const DL_ImageData &) override
virtual void addDimDiametric(const DL_DimensionData &, const DL_DimDiametricData &) override
const wxString GetName() const override
Returns the plugin name.
virtual void addDimOrdinate(const DL_DimensionData &, const DL_DimOrdinateData &) override
unsigned int m_SplineKnotsCount
unsigned int m_SplineDegree
const std::string & GetMessages() const override
virtual void setVariableDouble(const std::string &key, double value, int code) override
Called for every double variable in the DXF file (e.g.
virtual void addLeaderVertex(const DL_LeaderVertexData &) override
This class import DXF ASCII files and convert basic entities to board entities.
virtual void addTrace(const DL_TraceData &) override
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:171
static double mapY(int y)
virtual void linkImage(const DL_ImageDefData &) override
static double mapX(int x)
const wxArrayString GetFileExtensions() const override
Returns a string array of the file extensions handled by this plugin.
virtual void addSolid(const DL_SolidData &) override
KICAD_PLUGIN_EXPORT SCENEGRAPH * Load(char const *aFileName)
reads a model file and creates a generic display structure
void SetBrdLayer(int aBrdLayer)
Set the layer number to import dxf items.
GRAPHICS_IMPORTER_BUFFER m_internalImporter
virtual void addHatch(const DL_HatchData &) override
std::vector< double > m_SplineKnotsList
std::vector< SPLINE_CTRL_POINT > m_SplineControlPointList
virtual void addDimAngular(const DL_DimensionData &, const DL_DimAngularData &) override
virtual void addHatchLoop(const DL_HatchLoopData &) override