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) 2019 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 1992-2019 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 DXF2BRD_ITEMS_H
26 #define DXF2BRD_ITEMS_H
27 
28 #include "graphics_import_plugin.h"
30 
31 #include <dl_creationadapter.h>
32 #include <dl_dxf.h>
34 #include <wx/wx.h>
35 
36 #include <list>
37 
38 class BOARD;
39 class BOARD_ITEM;
40 
45 {
46  double m_x;
47  double m_y;
48  double m_weight;
49 
50  SPLINE_CTRL_POINT( double a_x, double a_y, double a_weight )
51  : m_x( a_x ), m_y( a_y ), m_weight( a_weight )
52  {}
53 };
54 
59 {
60 public:
61  int m_EntityType; // the DXF type of entity
62  int m_EntityParseStatus; // Inside a entity: status of parsing:
63  // 0 = no entity
64  // 1 = first item of entity
65  // 2 = entity in progress
66  int m_EntityFlag; // a info flag to parse entities
67 
68  VECTOR2D m_LastCoordinate; // the last vertex coordinate read (unit = mm)
69  VECTOR2D m_PolylineStart; // The first point of the polyline entity, when reading a
70  // polyline (unit = mm)
71  double m_BulgeVertex; // the last vertex bulge value read
72 
73  // for spline parsing: parameters
74  unsigned int m_SplineDegree;
75  unsigned int m_SplineKnotsCount;
76  unsigned int m_SplineControlCount;
77  unsigned int m_SplineFitCount;
78  double m_SplineTangentStartX; // tangent dir X for the start point
79  double m_SplineTangentStartY; // tangent dir Y for the start point
80  double m_SplineTangentEndX; // tangent dir X for the end point
81  double m_SplineTangentEndY; // tangent dir Y for the end point
82 
83  // for spline parsing: buffers to store control points, fit points and knot
84  std::vector<double> m_SplineKnotsList; // knots list, code 40
85  // control points list coordinates, code 10, 20 & 30 (only X and Y cood and Weight)
86  std::vector<SPLINE_CTRL_POINT> m_SplineControlPointList;
87  // fit points list, code 11, 21 & 31 (only X and Y cood)
88  std::vector<VECTOR2D> m_SplineFitPointList;
89 
91 
92  // Reset the entity parameters
93  void Clear()
94  {
95  m_EntityType = DL_UNKNOWN;
97  m_EntityFlag = 0;
98  m_SplineDegree = 1;
101  m_SplineFitCount = 0;
102  m_SplineTangentStartX = 0.0;
103  m_SplineTangentStartY = 0.0;
104  m_SplineTangentEndX = 0.0;
105  m_SplineTangentEndY = 0.0;
106  m_BulgeVertex = 0.0;
107  m_SplineKnotsList.clear();
108  m_SplineControlPointList.clear();
109  m_SplineFitPointList.clear();
110  }
111 };
112 
113 
118 #if 0 //defined(DEBUG)
119 // For dxf import debug:
120 #define ON_UNSUPPORTED( error_msg ) wxLogMessage( error_msg )
121 #else
122 #define ON_UNSUPPORTED( error_msg )
123 #endif
124 
125 class DXF_IMPORT_PLUGIN : public GRAPHICS_IMPORT_PLUGIN, public DL_CreationAdapter
126 {
127 private:
128  double m_xOffset; // X coord offset for conversion (in mm)
129  double m_yOffset; // Y coord offset for conversion (in mm)
130  double m_defaultThickness; // default line thickness for conversion (in mm)
131  double m_DXF2mm; // The scale factor to convert DXF units to mm
132  int m_brdLayer; // The board layer to place imported DXF items
133  int m_version; // the dxf version, not used here
134  bool m_inBlock; // Are we parsing a block
135  std::string m_codePage; // The code page, not used here
136  bool m_importAsfootprintGraphicItems; // Use module items instead of board items when true.
137  // true when the items are imported in the footprint editor
138  std::string m_messages; // messages generated during dxf file parsing.
139  // Each message ends by '\n'
140  DXF2BRD_ENTITY_DATA m_curr_entity; // the current entity parameters when parsing a DXF entity
141 
142  double m_minX, m_maxX; // handles image size in mm
143  double m_minY, m_maxY; // handles image size in mm
144 
146 
147 
148 public:
151 
152  const wxString GetName() const override
153  {
154  return "AutoCAD DXF";
155  }
156 
157  const std::vector<std::string> GetFileExtensions() const override
158  {
159  static std::vector<std::string> exts = { "dxf" };
160  return exts;
161  }
162 
163  bool Load( const wxString& aFileName ) override;
164  bool Import() override;
165 
166  double GetImageWidth() const override;
167  double GetImageHeight() const override;
168 
169  void updateImageLimits( const VECTOR2D& aPoint );
170 
171  virtual void SetImporter( GRAPHICS_IMPORTER* aImporter ) override;
172 
178  void ImportAsFootprintGraphic( bool aImportAsFootprintGraphic )
179  {
180  m_importAsfootprintGraphicItems = aImportAsFootprintGraphic;
181  }
182 
183 
190  void SetDefaultLineWidthMM( double aWidth )
191  {
192  m_defaultThickness = aWidth;
193  }
194 
195  void SetLineWidthMM( double aWidth ) override { SetDefaultLineWidthMM( aWidth ); }
196 
204  void SetOffset( double aOffsetX, double aOffsetY )
205  {
206  m_xOffset = aOffsetX;
207  m_yOffset = aOffsetY;
208  }
209 
214  void SetBrdLayer( int aBrdLayer ) { m_brdLayer = aBrdLayer; }
215 
222  bool ImportDxfFile( const wxString& aFile );
223 
227  const std::string& GetMessages() const override
228  {
229  return m_messages;
230  }
231 
232 private:
233  // report message to keep trace of not supported dxf entities:
234  void reportMsg( const char* aMessage );
235 
236  // coordinate conversions from dxf file to mm
237  double mapX( double aDxfCoordX );
238  double mapY( double aDxfCoordY );
239  double mapDim( double aDxfValue );
240  // mapWidth returns ( in mm) the aDxfValue if aDxfWidth > 0
241  // or m_defaultThickness
242  double mapWidth( double aDxfWidth );
243 
244  // Functions to aid in the creation of a Polyline
245  void insertLine( const VECTOR2D& aSegStart, const VECTOR2D& aSegEnd, int aWidth );
246  void insertArc( const VECTOR2D& aSegStart, const VECTOR2D& aSegEnd,
247  double aBulge, int aWidth );
248  // Add a dxf spline (stored in m_curr_entity) to the board, after conversion to segments
249  void insertSpline( int aWidth );
250 
251  // Methods from DL_CreationAdapter:
252  // They are something like"call back" functions,
253  // called when the corresponding object is read in dxf file
254 
258  virtual void setVariableString( const std::string& key, const std::string& value,
259  int code ) override;
260 
264  virtual void setVariableInt( const std::string& key, int value, int code ) override;
265 
269  virtual void setVariableDouble( const std::string& key, double value, int code ) override {}
270 
271  virtual void addLayer( const DL_LayerData& aData ) override;
272  virtual void addLine( const DL_LineData& aData) override;
273 
280  virtual void addBlock( const DL_BlockData& ) override;
281  virtual void endBlock() override;
282 
283  virtual void addCircle( const DL_CircleData& aData ) override;
284  virtual void addArc( const DL_ArcData& aData ) override;
285  //virtual void addLWPolyline( const DRW_LWPolyline& aData ) override;
286  virtual void addText( const DL_TextData& aData ) override;
287  virtual void addPolyline( const DL_PolylineData& aData ) override;
288 
290  virtual void addVertex( const DL_VertexData& aData ) override;
291  virtual void addMText( const DL_MTextData& aData) override;
292  virtual void addTextStyle( const DL_StyleData& aData ) override;
293 
294  virtual void endEntity() override;
295 
297  virtual void addSpline( const DL_SplineData& aData ) override;
298 
300  virtual void addControlPoint( const DL_ControlPointData& aData ) override;
301 
303  virtual void addFitPoint( const DL_FitPointData& aData ) override;
304 
306  virtual void addKnot( const DL_KnotData& aData ) override;
307 
308  // Not yet handled DXF entities:
309  virtual void addXLine( const DL_XLineData& ) override { ON_UNSUPPORTED( "addXLine" ); }
310 
311  virtual void addRay( const DL_RayData& ) override { ON_UNSUPPORTED( "addRay" ); }
312 
313  virtual void addArcAlignedText( const DL_ArcAlignedTextData& ) override
314  { ON_UNSUPPORTED( "addArcAlignedText" ); }
315 
316  virtual void addAttribute( const DL_AttributeData& ) override
317  { ON_UNSUPPORTED( "addAttribute" ); }
318 
319  virtual void addDimAlign( const DL_DimensionData&,
320  const DL_DimAlignedData& ) override { ON_UNSUPPORTED( "addDimAlign" ); }
321  virtual void addDimLinear( const DL_DimensionData&,
322  const DL_DimLinearData& ) override { ON_UNSUPPORTED( "addDimLinear" ); }
323  virtual void addDimRadial( const DL_DimensionData&,
324  const DL_DimRadialData& ) override { ON_UNSUPPORTED( "addDimRadial" ); }
325  virtual void addDimDiametric( const DL_DimensionData&,
326  const DL_DimDiametricData& ) override { ON_UNSUPPORTED( "addDimDiametric" ); }
327  virtual void addDimAngular( const DL_DimensionData&,
328  const DL_DimAngularData& ) override { ON_UNSUPPORTED( "addDimAngular" ); }
329  virtual void addDimAngular3P( const DL_DimensionData&,
330  const DL_DimAngular3PData& ) override { ON_UNSUPPORTED( "addDimAngular3P" ); }
331  virtual void addDimOrdinate( const DL_DimensionData&,
332  const DL_DimOrdinateData& ) override { ON_UNSUPPORTED( "addDimOrdinate" ); }
333 
334  virtual void addLeader( const DL_LeaderData& ) override
335  { ON_UNSUPPORTED( "addLeader" ); }
336 
337  virtual void addLeaderVertex( const DL_LeaderVertexData& ) override
338  { ON_UNSUPPORTED( "addLeaderVertex" ); }
339 
340  virtual void addHatch( const DL_HatchData& ) override { ON_UNSUPPORTED( "addHatch" ); }
341 
342  virtual void addTrace( const DL_TraceData& ) override { ON_UNSUPPORTED( "addTrace" ); }
343  virtual void add3dFace( const DL_3dFaceData& ) override { ON_UNSUPPORTED( "add3dFace" ); }
344 
345  virtual void addSolid( const DL_SolidData& ) override { ON_UNSUPPORTED( "addSolid" ); }
346 
347  virtual void addImage( const DL_ImageData& ) override { ON_UNSUPPORTED( "addImage" ); }
348  virtual void linkImage( const DL_ImageDefData& ) override {}
349 
350  virtual void addHatchLoop( const DL_HatchLoopData& ) override { ON_UNSUPPORTED( "addHatchLoop" ); }
351 
352  virtual void addHatchEdge( const DL_HatchEdgeData& ) override { ON_UNSUPPORTED( "addHatchEdge" ); }
353 
354  virtual void addXRecord( const std::string& ) override { ON_UNSUPPORTED( "addXRecord" ); }
355  virtual void addXRecordString( int, const std::string& ) override { ON_UNSUPPORTED( "addXRecordString" ); }
356  virtual void addXRecordReal( int, double ) override { ON_UNSUPPORTED( "addXRecordReal" ); }
357  virtual void addXRecordInt( int, int ) override { ON_UNSUPPORTED( "addXRecordInt" ); }
358  virtual void addXRecordBool( int, bool ) override { ON_UNSUPPORTED( "addXRecordBool" ); }
359 
360  virtual void addXDataApp( const std::string& ) override { ON_UNSUPPORTED( "addXDataApp" ); }
361  virtual void addXDataString( int, const std::string& ) override { ON_UNSUPPORTED( "addXDataString" ); }
362  virtual void addXDataReal( int, double ) override { ON_UNSUPPORTED( "addXDataReal" ); }
363  virtual void addXDataInt( int, int ) override { ON_UNSUPPORTED( "addXDataInt" ); }
364 
373  static wxString toDxfString( const wxString& aStr );
374 
378  static wxString toNativeString( const wxString& aData );
379 
380  void writeLine();
381  void writeMtext();
382 };
383 
384 #endif // DXF2BRD_ITEMS_H
Interface that creates objects representing shapes for a given data model.
virtual void addXRecordInt(int, int) override
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)
void insertArc(const VECTOR2D &aSegStart, const VECTOR2D &aSegEnd, double aBulge, int aWidth)
virtual void addXDataReal(int, double) override
virtual void addXDataString(int, const std::string &) override
double GetImageHeight() const override
Return image height from original imported file.
virtual void addTextStyle(const DL_StyleData &aData) override
virtual void addLine(const DL_LineData &aData) override
virtual void addXRecord(const std::string &) override
virtual void addPolyline(const DL_PolylineData &aData) override
virtual void endBlock() override
BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
virtual void addBlock(const DL_BlockData &) override
Called for each BLOCK in the DXF file These are re-usable elements that may be placed into the model ...
static wxString toDxfString(const wxString &aStr)
Convert a native unicode string into a DXF encoded string.
Interface for vector graphics import plugins.
unsigned int m_SplineFitCount
virtual void addHatchEdge(const DL_HatchEdgeData &) override
double GetImageWidth() const override
Return image width from original imported file.
bool Load(const wxString &aFileName) override
Load file for import.
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)
double mapX(double aDxfCoordX)
void SetOffset(double aOffsetX, double aOffsetY)
Set the coordinate offset between the imported dxf items and Pcbnew.
virtual void addFitPoint(const DL_FitPointData &aData) override
Called for every spline fit point.
void SetLineWidthMM(double aWidth) override
virtual void addSpline(const DL_SplineData &aData) override
Called for every spline.
virtual void addDimAlign(const DL_DimensionData &, const DL_DimAlignedData &) override
virtual void addRay(const DL_RayData &) 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 updateImageLimits(const VECTOR2D &aPoint)
void SetDefaultLineWidthMM(double aWidth)
Set the default line width when importing dxf items like lines to Pcbnew.
virtual void endEntity() override
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
virtual void setVariableInt(const std::string &key, int value, int code) override
Called for every int variable in the DXF file (e.g.
bool Import() override
Actually imports the file.
const wxString GetName() const override
Return the plugin name.
virtual void addVertex(const DL_VertexData &aData) override
Called for every polyline vertex.
virtual void addDimOrdinate(const DL_DimensionData &, const DL_DimOrdinateData &) override
virtual void addXRecordString(int, const std::string &) override
unsigned int m_SplineKnotsCount
unsigned int m_SplineDegree
const std::string & GetMessages() const override
virtual void addKnot(const DL_KnotData &aData) override
Called for every spline knot value.
Definition of file extensions used in Kicad.
virtual void setVariableDouble(const std::string &key, double value, int code) override
Called for every double variable in the DXF file (e.g.
static wxString toNativeString(const wxString &aData)
Converts a DXF encoded string into a native Unicode string.
virtual void addXRecordReal(int, double) override
double mapDim(double aDxfValue)
#define ON_UNSUPPORTED(error_msg)
This class import DXF ASCII files and convert basic entities to board entities.
double mapWidth(double aDxfWidth)
virtual void addControlPoint(const DL_ControlPointData &aData) override
Called for every spline control point.
virtual void addLeaderVertex(const DL_LeaderVertexData &) override
virtual void SetImporter(GRAPHICS_IMPORTER *aImporter) override
Set the receiver of the imported shapes.
virtual void addTrace(const DL_TraceData &) override
virtual void addMText(const DL_MTextData &aData) override
BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:181
void insertSpline(int aWidth)
bool ImportDxfFile(const wxString &aFile)
Implementation of the method used for communicate with this filter.
virtual void linkImage(const DL_ImageDefData &) override
void insertLine(const VECTOR2D &aSegStart, const VECTOR2D &aSegEnd, int aWidth)
const std::vector< std::string > GetFileExtensions() const override
Return a vector of the file extensions handled by this plugin.
virtual void addSolid(const DL_SolidData &) override
virtual void addXLine(const DL_XLineData &) override
virtual void addLayer(const DL_LayerData &aData) override
virtual void addXDataInt(int, int) override
virtual void addArc(const DL_ArcData &aData) override
virtual void addXDataApp(const std::string &) override
void SetBrdLayer(int aBrdLayer)
Set the layer number to import dxf items.
double mapY(double aDxfCoordY)
virtual void addText(const DL_TextData &aData) override
GRAPHICS_IMPORTER_BUFFER m_internalImporter
virtual void addHatch(const DL_HatchData &) override
std::vector< double > m_SplineKnotsList
virtual void addAttribute(const DL_AttributeData &) override
virtual void addXRecordBool(int, bool) override
virtual void setVariableString(const std::string &key, const std::string &value, int code) override
Called for every string variable in the DXF file (e.g.
std::vector< SPLINE_CTRL_POINT > m_SplineControlPointList
void reportMsg(const char *aMessage)
virtual void addDimAngular(const DL_DimensionData &, const DL_DimAngularData &) override
virtual void addArcAlignedText(const DL_ArcAlignedTextData &) override
virtual void addHatchLoop(const DL_HatchLoopData &) override
virtual void addCircle(const DL_CircleData &aData) override