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_dxf.h"
32 #include "dl_creationadapter.h"
33 #include "wx/wx.h"
34 #include <list>
35 
36 class BOARD;
37 class BOARD_ITEM;
38 
43 {
44  double m_x;
45  double m_y;
46  double m_weight;
47 
48  SPLINE_CTRL_POINT( double a_x, double a_y, double a_weight )
49  : m_x( a_x ), m_y( a_y ), m_weight( a_weight )
50  {}
51 };
52 
57 {
58 public:
59  int m_EntityType; // the DXF type of entity
60  int m_EntityParseStatus; // Inside a entity: status of parsing:
61  // 0 = no entity
62  // 1 = first item of entity
63  // 2 = entity in progress
64  int m_EntityFlag; // a info flag to parse entities
65 
66  VECTOR2D m_LastCoordinate; // the last vertex coordinate read (unit = mm)
67  VECTOR2D m_PolylineStart; // The first point of the polyline entity, when reading a
68  // 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; // tangent dir X for the start point
77  double m_SplineTangentStartY; // tangent dir Y for the start point
78  double m_SplineTangentEndX; // tangent dir X for the end point
79  double m_SplineTangentEndY; // tangent 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 
89 
90  // Reset the entity parameters
91  void Clear()
92  {
93  m_EntityType = DL_UNKNOWN;
95  m_EntityFlag = 0;
96  m_SplineDegree = 1;
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_BulgeVertex = 0.0;
105  m_SplineKnotsList.clear();
106  m_SplineControlPointList.clear();
107  m_SplineFitPointList.clear();
108  }
109 };
110 
111 
116 class DXF_IMPORT_PLUGIN : public GRAPHICS_IMPORT_PLUGIN, public DL_CreationAdapter
117 {
118 private:
119  double m_xOffset; // X coord offset for conversion (in mm)
120  double m_yOffset; // Y coord offset for conversion (in mm)
121  double m_defaultThickness; // default line thickness for conversion (in mm)
122  double m_DXF2mm; // The scale factor to convert DXF units to mm
123  int m_brdLayer; // The board layer to place imported DXF items
124  int m_version; // the dxf version, not used here
125  std::string m_codePage; // The code page, not used here
126  bool m_importAsfootprintGraphicItems; // Use module items instead of board items when true.
127  // true when the items are imported in the footprint editor
128  std::string m_messages; // messages generated during dxf file parsing.
129  // Each message ends by '\n'
130  DXF2BRD_ENTITY_DATA m_curr_entity; // the current entity parameters when parsing a DXF entity
131 
132  double m_minX, m_maxX; // handles image size in mm
133  double m_minY, m_maxY; // handles image size in mm
134 
136 
137 
138 public:
141 
142  const wxString GetName() const override
143  {
144  return "AutoCAD DXF";
145  }
146 
147  const wxArrayString GetFileExtensions() const override
148  {
149  wxArrayString list;
150  list.Add( "dxf" );
151  return list;
152  }
153 
154  bool Load( const wxString& aFileName ) override;
155  bool Import() override;
156 
157  double GetImageWidth() const override;
158  double GetImageHeight() const override;
159 
160  void updateImageLimits( const VECTOR2D& aPoint );
161 
162  virtual void SetImporter( GRAPHICS_IMPORTER* aImporter ) override;
163 
169  void ImportAsFootprintGraphic( bool aImportAsFootprintGraphic )
170  {
171  m_importAsfootprintGraphicItems = aImportAsFootprintGraphic;
172  }
173 
174 
181  void SetDefaultLineWidthMM( double aWidth )
182  {
183  m_defaultThickness = aWidth;
184  }
185 
186  void SetLineWidthMM( double aWidth ) override { SetDefaultLineWidthMM( aWidth ); }
187 
195  void SetOffset( double aOffsetX, double aOffsetY )
196  {
197  m_xOffset = aOffsetX;
198  m_yOffset = aOffsetY;
199  }
200 
205  void SetBrdLayer( int aBrdLayer ) { m_brdLayer = aBrdLayer; }
206 
213  bool ImportDxfFile( const wxString& aFile );
214 
218  const std::string& GetMessages() const override
219  {
220  return m_messages;
221  }
222 
223 private:
224  // report message to keep trace of not supported dxf entities:
225  void reportMsg( const char* aMessage );
226 
227  // coordinate conversions from dxf file to mm
228  double mapX( double aDxfCoordX );
229  double mapY( double aDxfCoordY );
230  double mapDim( double aDxfValue );
231  // mapWidth returns ( in mm) the aDxfValue if aDxfWidth > 0
232  // or m_defaultThickness
233  double mapWidth( double aDxfWidth );
234 
235  // Functions to aid in the creation of a Polyline
236  void insertLine( const VECTOR2D& aSegStart, const VECTOR2D& aSegEnd, int aWidth );
237  void insertArc( const VECTOR2D& aSegStart, const VECTOR2D& aSegEnd,
238  double aBulge, int aWidth );
239  // Add a dxf spline (stored in m_curr_entity) to the board, after conversion to segments
240  void insertSpline( int aWidth );
241 
242  // Methods from DL_CreationAdapter:
243  // They are something like"call back" functions,
244  // called when the corresponding object is read in dxf file
245 
249  virtual void setVariableString( const std::string& key, const std::string& value,
250  int code ) override;
251 
255  virtual void setVariableInt( const std::string& key, int value, int code ) override;
256 
260  virtual void setVariableDouble( const std::string& key, double value, int code ) override {}
261 
262  virtual void addLayer( const DL_LayerData& aData ) override;
263  virtual void addLine( const DL_LineData& aData) override;
264  virtual void addCircle( const DL_CircleData& aData ) override;
265  virtual void addArc( const DL_ArcData& aData ) override;
266  //virtual void addLWPolyline( const DRW_LWPolyline& aData ) override;
267  virtual void addText( const DL_TextData& aData ) override;
268  virtual void addPolyline( const DL_PolylineData& aData ) override;
269 
271  virtual void addVertex( const DL_VertexData& aData ) override;
272  virtual void addMText( const DL_MTextData& aData) override;
273  virtual void addTextStyle( const DL_StyleData& aData ) override;
274 
275  virtual void endEntity() override;
276 
278  virtual void addSpline( const DL_SplineData& aData ) override;
279 
281  virtual void addControlPoint( const DL_ControlPointData& aData ) override;
282 
284  virtual void addFitPoint( const DL_FitPointData& aData ) override;
285 
287  virtual void addKnot( const DL_KnotData& aData ) override;
288 
289  // Not yet handled DXF entities:
290  virtual void addDimAlign( const DL_DimensionData&,
291  const DL_DimAlignedData& ) override { reportMsg( "DL_Dimension not managed" ); }
292  virtual void addDimLinear( const DL_DimensionData&,
293  const DL_DimLinearData& ) override { reportMsg( "DL_Dimension not managed" ); }
294  virtual void addDimRadial( const DL_DimensionData&,
295  const DL_DimRadialData& ) override { reportMsg( "DL_Dimension not managed" ); }
296  virtual void addDimDiametric( const DL_DimensionData&,
297  const DL_DimDiametricData& ) override { reportMsg( "DL_Dimension not managed" ); }
298  virtual void addDimAngular( const DL_DimensionData&,
299  const DL_DimAngularData& ) override { reportMsg( "DL_Dimension not managed" ); }
300  virtual void addDimAngular3P( const DL_DimensionData&,
301  const DL_DimAngular3PData& ) override { reportMsg( "DL_Dimension not managed" ); }
302  virtual void addDimOrdinate( const DL_DimensionData&,
303  const DL_DimOrdinateData& ) override { reportMsg( "DL_Dimension not managed" ); }
304  virtual void addLeader( const DL_LeaderData& ) override
305  {
306  reportMsg( "DL_Leader not managed" );
307  }
308 
309  virtual void addLeaderVertex( const DL_LeaderVertexData& ) override
310  {
311  reportMsg( "DL_LeaderVertex not managed" );
312  }
313 
314  virtual void addHatch( const DL_HatchData& ) override { reportMsg( "DL_Hatch not managed" ); }
315 
316  virtual void addTrace( const DL_TraceData& ) override { reportMsg( "DL_Trace not managed" ); }
317  virtual void add3dFace( const DL_3dFaceData& ) override
318  {
319  reportMsg( "DL_3dFace not managed" );
320  }
321 
322  virtual void addSolid( const DL_SolidData& ) override { reportMsg( "DL_Solid not managed" ); }
323 
324  virtual void addImage( const DL_ImageData& ) override { reportMsg( "DL_ImageDa not managed" ); }
325  virtual void linkImage( const DL_ImageDefData& ) override
326  {
327  reportMsg( "DL_ImageDef not managed" );
328  }
329 
330  virtual void addHatchLoop( const DL_HatchLoopData& ) override
331  {
332  reportMsg( "DL_HatchLoop not managed" );
333  }
334 
335  virtual void addHatchEdge( const DL_HatchEdgeData& ) override
336  {
337  reportMsg( "DL_HatchEdge not managed" );
338  }
339 
348  static wxString toDxfString( const wxString& aStr );
349 
353  static wxString toNativeString( const wxString& aData );
354 
355  void writeLine();
356  void writeMtext();
357 };
358 
359 #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.
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.
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:170
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