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 
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 
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 
167  void ImportAsFootprintGraphic( bool aImportAsFootprintGraphic )
168  {
169  m_importAsfootprintGraphicItems = aImportAsFootprintGraphic;
170  }
171 
172 
179  void SetDefaultLineWidthMM( double aWidth )
180  {
181  m_defaultThickness = aWidth;
182  }
183 
192  void SetOffset( double aOffsetX, double aOffsetY )
193  {
194  m_xOffset = aOffsetX;
195  m_yOffset = aOffsetY;
196  }
197 
202  void SetBrdLayer( int aBrdLayer ) { m_brdLayer = aBrdLayer; }
203 
210  bool ImportDxfFile( const wxString& aFile );
211 
215  const std::string& GetMessages() const override
216  {
217  return m_messages;
218  }
219 
220 
221 private:
222  // report message to keep trace of not supported dxf entities:
223  void reportMsg( const char* aMessage );
224 
225  // coordinate conversions from dxf file to mm
226  double mapX( double aDxfCoordX );
227  double mapY( double aDxfCoordY );
228  double mapDim( double aDxfValue );
229  // mapWidth returns ( in mm) the aDxfValue if aDxfWidth > 0
230  // or m_defaultThickness
231  double mapWidth( double aDxfWidth );
232 
233  // Functions to aid in the creation of a Polyline
234  void insertLine( const VECTOR2D& aSegStart, const VECTOR2D& aSegEnd, int aWidth );
235  void insertArc( const VECTOR2D& aSegStart, const VECTOR2D& aSegEnd,
236  double aBulge, int aWidth );
237  // Add a dxf spline (stored in m_curr_entity) to the board, after conversion to segments
238  void insertSpline( int aWidth );
239 
240  // Methods from DL_CreationAdapter:
241  // They are something like"call back" fonctions,
242  // called when the corresponding object is read in dxf file
243 
247  virtual void setVariableString( const std::string& key, const std::string& value,
248  int code ) override;
249 
253  virtual void setVariableInt( const std::string& key, int value, int code ) override;
254 
258  virtual void setVariableDouble( const std::string& key, double value, int code ) override {}
259 
260  virtual void addLayer( const DL_LayerData& aData ) override;
261  virtual void addLine( const DL_LineData& aData) override;
262  virtual void addCircle( const DL_CircleData& aData ) override;
263  virtual void addArc( const DL_ArcData& aData ) override;
264  //virtual void addLWPolyline( const DRW_LWPolyline& aData ) override;
265  virtual void addText( const DL_TextData& aData ) override;
266  virtual void addPolyline( const DL_PolylineData& aData ) override;
267 
269  virtual void addVertex( const DL_VertexData& aData ) override;
270  virtual void addMText( const DL_MTextData& aData) override;
271  virtual void addTextStyle( const DL_StyleData& aData ) override;
272 
273  virtual void endEntity() override;
274 
276  virtual void addSpline( const DL_SplineData& aData ) override;
277 
279  virtual void addControlPoint( const DL_ControlPointData& aData ) override;
280 
282  virtual void addFitPoint( const DL_FitPointData& aData ) override;
283 
285  virtual void addKnot( const DL_KnotData& aData ) override;
286 
287  // Not yet handled DXF entities:
288  virtual void addDimAlign( const DL_DimensionData&,
289  const DL_DimAlignedData& ) override { reportMsg( "DL_Dimension not managed" ); }
290  virtual void addDimLinear( const DL_DimensionData&,
291  const DL_DimLinearData& ) override { reportMsg( "DL_Dimension not managed" ); }
292  virtual void addDimRadial( const DL_DimensionData&,
293  const DL_DimRadialData& ) override { reportMsg( "DL_Dimension not managed" ); }
294  virtual void addDimDiametric( const DL_DimensionData&,
295  const DL_DimDiametricData& ) override { reportMsg( "DL_Dimension not managed" ); }
296  virtual void addDimAngular( const DL_DimensionData&,
297  const DL_DimAngularData& ) override { reportMsg( "DL_Dimension not managed" ); }
298  virtual void addDimAngular3P( const DL_DimensionData&,
299  const DL_DimAngular3PData& ) override { reportMsg( "DL_Dimension not managed" ); }
300  virtual void addDimOrdinate( const DL_DimensionData&,
301  const DL_DimOrdinateData& ) override { reportMsg( "DL_Dimension not managed" ); }
302  virtual void addLeader( const DL_LeaderData& ) override { reportMsg( "DL_Leader not managed" ); }
303  virtual void addLeaderVertex( const DL_LeaderVertexData& ) override { reportMsg( "DL_LeaderVertex not managed" ); }
304 
305  virtual void addHatch( const DL_HatchData& ) override { reportMsg( "DL_Hatch not managed" ); }
306 
307  virtual void addTrace( const DL_TraceData& ) override { reportMsg( "DL_Trace not managed" ); }
308  virtual void add3dFace( const DL_3dFaceData& ) override { reportMsg( "DL_3dFace not managed" ); }
309  virtual void addSolid( const DL_SolidData& ) override { reportMsg( "DL_Solid not managed" ); }
310 
311  virtual void addImage( const DL_ImageData& ) override { reportMsg( "DL_ImageDa not managed" ); }
312  virtual void linkImage( const DL_ImageDefData& ) override { reportMsg( "DL_ImageDef not managed" ); }
313  virtual void addHatchLoop( const DL_HatchLoopData& ) override { reportMsg( "DL_HatchLoop not managed" ); }
314  virtual void addHatchEdge( const DL_HatchEdgeData& ) override { reportMsg( "DL_HatchEdge not managed" ); }
315 
324  static wxString toDxfString( const wxString& aStr );
325 
329  static wxString toNativeString( const wxString& aData );
330 
331  void writeLine();
332  void writeMtext();
333 };
334 
335 #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)
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)
Converts 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
Loads 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.
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
@breif Returns 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
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:171
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
Returns 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