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 class DXF_IMPORT_PLUGIN : public GRAPHICS_IMPORT_PLUGIN, public DL_CreationAdapter
119 {
120 private:
121  double m_xOffset; // X coord offset for conversion (in mm)
122  double m_yOffset; // Y coord offset for conversion (in mm)
123  double m_defaultThickness; // default line thickness for conversion (in mm)
124  double m_DXF2mm; // The scale factor to convert DXF units to mm
125  int m_brdLayer; // The board layer to place imported DXF items
126  int m_version; // the dxf version, not used here
127  std::string m_codePage; // The code page, not used here
128  bool m_importAsfootprintGraphicItems; // Use module items instead of board items when true.
129  // true when the items are imported in the footprint editor
130  std::string m_messages; // messages generated during dxf file parsing.
131  // Each message ends by '\n'
132  DXF2BRD_ENTITY_DATA m_curr_entity; // the current entity parameters when parsing a DXF entity
133 
134  double m_minX, m_maxX; // handles image size in mm
135  double m_minY, m_maxY; // handles image size in mm
136 
138 
139 
140 public:
143 
144  const wxString GetName() const override
145  {
146  return "AutoCAD DXF";
147  }
148 
149  const wxArrayString GetFileExtensions() const override
150  {
151  static wxString wildcardExt = formatWildcardExt( "dxf" );
152  return wxArrayString( 1, &wildcardExt );
153  }
154 
155  bool Load( const wxString& aFileName ) override;
156  bool Import() override;
157 
158  double GetImageWidth() const override;
159  double GetImageHeight() const override;
160 
161  void updateImageLimits( const VECTOR2D& aPoint );
162 
163  virtual void SetImporter( GRAPHICS_IMPORTER* aImporter ) override;
164 
170  void ImportAsFootprintGraphic( bool aImportAsFootprintGraphic )
171  {
172  m_importAsfootprintGraphicItems = aImportAsFootprintGraphic;
173  }
174 
175 
182  void SetDefaultLineWidthMM( double aWidth )
183  {
184  m_defaultThickness = aWidth;
185  }
186 
187  void SetLineWidthMM( double aWidth ) override { SetDefaultLineWidthMM( aWidth ); }
188 
196  void SetOffset( double aOffsetX, double aOffsetY )
197  {
198  m_xOffset = aOffsetX;
199  m_yOffset = aOffsetY;
200  }
201 
206  void SetBrdLayer( int aBrdLayer ) { m_brdLayer = aBrdLayer; }
207 
214  bool ImportDxfFile( const wxString& aFile );
215 
219  const std::string& GetMessages() const override
220  {
221  return m_messages;
222  }
223 
224 private:
225  // report message to keep trace of not supported dxf entities:
226  void reportMsg( const char* aMessage );
227 
228  // coordinate conversions from dxf file to mm
229  double mapX( double aDxfCoordX );
230  double mapY( double aDxfCoordY );
231  double mapDim( double aDxfValue );
232  // mapWidth returns ( in mm) the aDxfValue if aDxfWidth > 0
233  // or m_defaultThickness
234  double mapWidth( double aDxfWidth );
235 
236  // Functions to aid in the creation of a Polyline
237  void insertLine( const VECTOR2D& aSegStart, const VECTOR2D& aSegEnd, int aWidth );
238  void insertArc( const VECTOR2D& aSegStart, const VECTOR2D& aSegEnd,
239  double aBulge, int aWidth );
240  // Add a dxf spline (stored in m_curr_entity) to the board, after conversion to segments
241  void insertSpline( int aWidth );
242 
243  // Methods from DL_CreationAdapter:
244  // They are something like"call back" functions,
245  // called when the corresponding object is read in dxf file
246 
250  virtual void setVariableString( const std::string& key, const std::string& value,
251  int code ) override;
252 
256  virtual void setVariableInt( const std::string& key, int value, int code ) override;
257 
261  virtual void setVariableDouble( const std::string& key, double value, int code ) override {}
262 
263  virtual void addLayer( const DL_LayerData& aData ) override;
264  virtual void addLine( const DL_LineData& aData) override;
265  virtual void addCircle( const DL_CircleData& aData ) override;
266  virtual void addArc( const DL_ArcData& aData ) override;
267  //virtual void addLWPolyline( const DRW_LWPolyline& aData ) override;
268  virtual void addText( const DL_TextData& aData ) override;
269  virtual void addPolyline( const DL_PolylineData& aData ) override;
270 
272  virtual void addVertex( const DL_VertexData& aData ) override;
273  virtual void addMText( const DL_MTextData& aData) override;
274  virtual void addTextStyle( const DL_StyleData& aData ) override;
275 
276  virtual void endEntity() override;
277 
279  virtual void addSpline( const DL_SplineData& aData ) override;
280 
282  virtual void addControlPoint( const DL_ControlPointData& aData ) override;
283 
285  virtual void addFitPoint( const DL_FitPointData& aData ) override;
286 
288  virtual void addKnot( const DL_KnotData& aData ) override;
289 
290  // Not yet handled DXF entities:
291  virtual void addDimAlign( const DL_DimensionData&,
292  const DL_DimAlignedData& ) override { reportMsg( "DL_Dimension not managed" ); }
293  virtual void addDimLinear( const DL_DimensionData&,
294  const DL_DimLinearData& ) override { reportMsg( "DL_Dimension not managed" ); }
295  virtual void addDimRadial( const DL_DimensionData&,
296  const DL_DimRadialData& ) override { reportMsg( "DL_Dimension not managed" ); }
297  virtual void addDimDiametric( const DL_DimensionData&,
298  const DL_DimDiametricData& ) override { reportMsg( "DL_Dimension not managed" ); }
299  virtual void addDimAngular( const DL_DimensionData&,
300  const DL_DimAngularData& ) override { reportMsg( "DL_Dimension not managed" ); }
301  virtual void addDimAngular3P( const DL_DimensionData&,
302  const DL_DimAngular3PData& ) override { reportMsg( "DL_Dimension not managed" ); }
303  virtual void addDimOrdinate( const DL_DimensionData&,
304  const DL_DimOrdinateData& ) override { reportMsg( "DL_Dimension not managed" ); }
305  virtual void addLeader( const DL_LeaderData& ) override
306  {
307  reportMsg( "DL_Leader not managed" );
308  }
309 
310  virtual void addLeaderVertex( const DL_LeaderVertexData& ) override
311  {
312  reportMsg( "DL_LeaderVertex not managed" );
313  }
314 
315  virtual void addHatch( const DL_HatchData& ) override { reportMsg( "DL_Hatch not managed" ); }
316 
317  virtual void addTrace( const DL_TraceData& ) override { reportMsg( "DL_Trace not managed" ); }
318  virtual void add3dFace( const DL_3dFaceData& ) override
319  {
320  reportMsg( "DL_3dFace not managed" );
321  }
322 
323  virtual void addSolid( const DL_SolidData& ) override { reportMsg( "DL_Solid not managed" ); }
324 
325  virtual void addImage( const DL_ImageData& ) override { reportMsg( "DL_ImageDa not managed" ); }
326  virtual void linkImage( const DL_ImageDefData& ) override
327  {
328  reportMsg( "DL_ImageDef not managed" );
329  }
330 
331  virtual void addHatchLoop( const DL_HatchLoopData& ) override
332  {
333  reportMsg( "DL_HatchLoop not managed" );
334  }
335 
336  virtual void addHatchEdge( const DL_HatchEdgeData& ) override
337  {
338  reportMsg( "DL_HatchEdge not managed" );
339  }
340 
349  static wxString toDxfString( const wxString& aStr );
350 
354  static wxString toNativeString( const wxString& aData );
355 
356  void writeLine();
357  void writeMtext();
358 };
359 
360 #endif // DXF2BRD_ITEMS_H
Interface that creates objects representing shapes for a given data model.
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)
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 addPolyline(const DL_PolylineData &aData) override
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
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 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
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.
wxString formatWildcardExt(const wxString &aWildcard)
Format wildcard extension to support case sensitive file dialogs.
static wxString toNativeString(const wxString &aData)
Converts a DXF encoded string into a native Unicode string.
double mapDim(double aDxfValue)
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.
This class import DXF ASCII files and convert basic entities to board entities.
virtual void addTrace(const DL_TraceData &) override
virtual void addMText(const DL_MTextData &aData) override
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:161
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 wxArrayString GetFileExtensions() const override
Return a string array of the file extensions handled by this plugin.
virtual void addSolid(const DL_SolidData &) override
virtual void addLayer(const DL_LayerData &aData) override
virtual void addArc(const DL_ArcData &aData) 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 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 addHatchLoop(const DL_HatchLoopData &) override
virtual void addCircle(const DL_CircleData &aData) override