KiCad PCB EDA Suite
dxf2brd_items.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 "dl_dxf.h"
30 #include "dl_creationadapter.h"
31 #include "wx/wx.h"
32 #include <list>
33 
34 class BOARD;
35 class BOARD_ITEM;
36 
41 {
42  double m_x;
43  double m_y;
44  double m_weight;
45 
46  SPLINE_CTRL_POINT( double a_x, double a_y, double a_weight )
47  : m_x( a_x ), m_y( a_y ), m_weight( a_weight )
48  {}
49 };
50 
55 {
56 public:
57  int m_EntityType; // the DXF type of entity
58  int m_EntityParseStatus; // Inside a entity: status od parsing:
59  // 0 = no entity
60  // 1 = first item of entity
61  // 2 = entity in progress
62  int m_EntityFlag; // a info flag to parse entities
63 
64  wxRealPoint m_LastCoordinate; // the last vertex coordinate read (unit = mm)
65  wxRealPoint m_PolylineStart; // The first point of the polyline entity, when reading a polyline (unit = mm)
66  double m_BulgeVertex; // the last vertex bulge value read
67 
68  // for spline parsing: parameters
69  unsigned int m_SplineDegree;
70  unsigned int m_SplineKnotsCount;
71  unsigned int m_SplineControlCount;
72  unsigned int m_SplineFitCount;
73  double m_SplineTangentStartX; // tangeant dir X for the start point
74  double m_SplineTangentStartY; // tangeant dir Y for the start point
75  double m_SplineTangentEndX; // tangeant dir X for the end point
76  double m_SplineTangentEndY; // tangeant dir Y for the end point
77 
78  // for spline parsing: buffers to store control points, fit points and knot
79  std::vector<double> m_SplineKnotsList; // knots list, code 40
80  // control points list coordinates, code 10, 20 & 30 (only X and Y cood and Weight)
81  std::vector<SPLINE_CTRL_POINT> m_SplineControlPointList;
82  // fit points list, code 11, 21 & 31 (only X and Y cood)
83  std::vector<wxRealPoint> m_SplineFitPointList;
84 
85  DXF2BRD_ENTITY_DATA() { Clear(); };
86 
87  // Reset the entity parameters
88  void Clear()
89  {
90  m_EntityType = DL_UNKNOWN;
91  m_EntityParseStatus = 0;
92  m_EntityFlag = 0;
93  m_SplineDegree = 1;
94  m_SplineKnotsCount = 0;
95  m_SplineControlCount = 0;
96  m_SplineFitCount = 0;
97  m_SplineTangentStartX = 0.0;
98  m_SplineTangentStartY = 0.0;
99  m_SplineTangentEndX = 0.0;
100  m_SplineTangentEndY = 0.0;
101  m_SplineKnotsList.clear();
102  m_SplineControlPointList.clear();
103  m_SplineFitPointList.clear();
104  }
105 };
106 
107 
112 class DXF2BRD_CONVERTER : public DL_CreationAdapter
113 {
114 private:
115  std::list<BOARD_ITEM*> m_newItemsList; // The list of new items added to the board
116  double m_xOffset; // X coord offset for conversion (in mm)
117  double m_yOffset; // Y coord offset for conversion (in mm)
118  double m_defaultThickness; // default line thickness for conversion (in mm)
119  double m_DXF2mm; // The scale factor to convert DXF units to mm
120  int m_brdLayer; // The board layer to place imported DXF items
121  int m_version; // the dxf version, not used here
122  std::string m_codePage; // The code page, not used here
123  bool m_importAsfootprintGraphicItems; // Use module items instead of board items when true.
124  // true when the items are imported in the footprint editor
125  std::string m_messages; // messages generated during dxf file parsing.
126  // Each message ends by '\n'
127  DXF2BRD_ENTITY_DATA m_curr_entity; // the current entity parameters when parsing a DXF entity
128 
129 
130 public:
133 
139  void ImportAsFootprintGraphic( bool aImportAsFootprintGraphic )
140  {
141  m_importAsfootprintGraphicItems = aImportAsFootprintGraphic;
142  }
143 
144 
151  void SetDefaultLineWidthMM( double aWidth )
152  {
153  m_defaultThickness = aWidth;
154  }
155 
164  void SetOffset( double aOffsetX, double aOffsetY )
165  {
166  m_xOffset = aOffsetX;
167  m_yOffset = aOffsetY;
168  }
169 
174  void SetBrdLayer( int aBrdLayer ) { m_brdLayer = aBrdLayer; }
175 
182  bool ImportDxfFile( const wxString& aFile );
183 
187  const std::list<BOARD_ITEM*>& GetItemsList() const
188  {
189  return m_newItemsList;
190  }
191 
195  std::string& GetMessages() { return m_messages; }
196 
197 private:
198  // report message to keep trace of not supported dxf entities:
199  void reportMsg( const char* aMessage );
200 
201  // coordinate conversions from dxf to internal units
202  int mapX( double aDxfCoordX );
203  int mapY( double aDxfCoordY );
204  int mapDim( double aDxfValue );
205  // mapWidth returns ( in internal units) the aDxfValue if aDxfWidth > 0
206  // or m_defaultThickness
207  int mapWidth( double aDxfWidth );
208 
209  // Functions to aid in the creation of a Polyline
210  void insertLine( const wxRealPoint& aSegStart, const wxRealPoint& aSegEnd, int aWidth );
211  void insertArc( const wxRealPoint& aSegStart, const wxRealPoint& aSegEnd,
212  double aBulge, int aWidth );
213  // Add a dxf spline (stored in m_curr_entity) to the board, after conversion to segments
214  void insertSpline( int aWidth );
215 
216  // Methods from DL_CreationAdapter:
217  // They are something like"call back" fonctions,
218  // called when the corresponding object is read in dxf file
219 
223  virtual void setVariableString( const std::string& key, const std::string& value,
224  int code ) override;
225 
229  virtual void setVariableInt( const std::string& key, int value, int code ) override;
230 
234  virtual void setVariableDouble( const std::string& key, double value, int code ) override {}
235 
236  virtual void addLayer( const DL_LayerData& aData ) override;
237  virtual void addLine( const DL_LineData& aData) override;
238  virtual void addCircle( const DL_CircleData& aData ) override;
239  virtual void addArc( const DL_ArcData& aData ) override;
240  //virtual void addLWPolyline( const DRW_LWPolyline& aData ) override;
241  virtual void addText( const DL_TextData& aData ) override;
242  virtual void addPolyline( const DL_PolylineData& aData ) override;
243 
245  virtual void addVertex( const DL_VertexData& aData ) override;
246  virtual void addMText( const DL_MTextData& aData) override;
247  virtual void addTextStyle( const DL_StyleData& aData ) override;
248 
249  virtual void endEntity() override;
250 
252  virtual void addSpline( const DL_SplineData& aData ) override;
253 
255  virtual void addControlPoint( const DL_ControlPointData& aData ) override;
256 
258  virtual void addFitPoint( const DL_FitPointData& aData ) override;
259 
261  virtual void addKnot( const DL_KnotData& aData ) override;
262 
263  // Not yet handled DXF entities:
264  virtual void addDimAlign( const DL_DimensionData&,
265  const DL_DimAlignedData& ) override { reportMsg( "DL_Dimension not managed" ); }
266  virtual void addDimLinear( const DL_DimensionData&,
267  const DL_DimLinearData& ) override { reportMsg( "DL_Dimension not managed" ); }
268  virtual void addDimRadial( const DL_DimensionData&,
269  const DL_DimRadialData& ) override { reportMsg( "DL_Dimension not managed" ); }
270  virtual void addDimDiametric( const DL_DimensionData&,
271  const DL_DimDiametricData& ) override { reportMsg( "DL_Dimension not managed" ); }
272  virtual void addDimAngular( const DL_DimensionData&,
273  const DL_DimAngularData& ) override { reportMsg( "DL_Dimension not managed" ); }
274  virtual void addDimAngular3P( const DL_DimensionData&,
275  const DL_DimAngular3PData& ) override { reportMsg( "DL_Dimension not managed" ); }
276  virtual void addDimOrdinate( const DL_DimensionData&,
277  const DL_DimOrdinateData& ) override { reportMsg( "DL_Dimension not managed" ); }
278  virtual void addLeader( const DL_LeaderData& ) override { reportMsg( "DL_Leader not managed" ); }
279  virtual void addLeaderVertex( const DL_LeaderVertexData& ) override { reportMsg( "DL_LeaderVertex not managed" ); }
280 
281  virtual void addHatch( const DL_HatchData& ) override { reportMsg( "DL_Hatch not managed" ); }
282 
283  virtual void addTrace( const DL_TraceData& ) override { reportMsg( "DL_Trace not managed" ); }
284  virtual void add3dFace( const DL_3dFaceData& ) override { reportMsg( "DL_3dFace not managed" ); }
285  virtual void addSolid( const DL_SolidData& ) override { reportMsg( "DL_Solid not managed" ); }
286 
287  virtual void addImage( const DL_ImageData& ) override { reportMsg( "DL_ImageDa not managed" ); }
288  virtual void linkImage( const DL_ImageDefData& ) override { reportMsg( "DL_ImageDef not managed" ); }
289  virtual void addHatchLoop( const DL_HatchLoopData& ) override { reportMsg( "DL_HatchLoop not managed" ); }
290  virtual void addHatchEdge( const DL_HatchEdgeData& ) override { reportMsg( "DL_HatchEdge not managed" ); }
291 
300  static wxString toDxfString( const wxString& aStr );
301 
305  static wxString toNativeString( const wxString& aData );
306 
307  void writeLine();
308  void writeMtext();
309 };
310 
311 #endif // DXF2BRD_ITEMS_H
SPLINE_CTRL_POINT(double a_x, double a_y, double a_weight)
Definition: dxf2brd_items.h:46
wxRealPoint m_PolylineStart
Definition: dxf2brd_items.h:65
bool m_importAsfootprintGraphicItems
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class...
wxRealPoint m_LastCoordinate
Definition: dxf2brd_items.h:64
virtual void addDimRadial(const DL_DimensionData &, const DL_DimRadialData &) override
unsigned int m_SplineFitCount
Definition: dxf2brd_items.h:72
std::string m_codePage
virtual void addHatch(const DL_HatchData &) override
const std::list< BOARD_ITEM * > & GetItemsList() const
std::vector< wxRealPoint > m_SplineFitPointList
Definition: dxf2brd_items.h:83
unsigned int m_SplineControlCount
Definition: dxf2brd_items.h:71
A helper class to store a spline control point (in X,Y plane only)
Definition: dxf2brd_items.h:40
A helper class to parse a DXF entity (polyline and spline)
Definition: dxf2brd_items.h:54
virtual void addLeaderVertex(const DL_LeaderVertexData &) override
void ImportAsFootprintGraphic(bool aImportAsFootprintGraphic)
Allows the import DXF items converted to board graphic items or footprint graphic items...
virtual void addDimAngular(const DL_DimensionData &, const DL_DimAngularData &) override
std::string m_messages
virtual void addDimOrdinate(const DL_DimensionData &, const DL_DimOrdinateData &) override
This class import DXF ASCII files and convert basic entities to board entities.
std::string & GetMessages()
std::list< BOARD_ITEM * > m_newItemsList
unsigned int m_SplineKnotsCount
Definition: dxf2brd_items.h:70
unsigned int m_SplineDegree
Definition: dxf2brd_items.h:69
virtual void linkImage(const DL_ImageDefData &) override
virtual void addHatchLoop(const DL_HatchLoopData &) override
virtual void addLeader(const DL_LeaderData &) override
virtual void setVariableDouble(const std::string &key, double value, int code) override
Called for every double variable in the DXF file (e.g.
double m_SplineTangentStartX
Definition: dxf2brd_items.h:73
virtual void addSolid(const DL_SolidData &) override
void SetOffset(double aOffsetX, double aOffsetY)
Set the coordinate offset between the imported dxf items and Pcbnew.
void SetBrdLayer(int aBrdLayer)
Set the layer number to import dxf items.
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:170
static double mapY(int y)
virtual void addDimAngular3P(const DL_DimensionData &, const DL_DimAngular3PData &) override
static double mapX(int x)
virtual void add3dFace(const DL_3dFaceData &) override
virtual void addTrace(const DL_TraceData &) override
virtual void addDimDiametric(const DL_DimensionData &, const DL_DimDiametricData &) override
virtual void addHatchEdge(const DL_HatchEdgeData &) override
std::vector< double > m_SplineKnotsList
Definition: dxf2brd_items.h:79
void SetDefaultLineWidthMM(double aWidth)
Set the default line width when importing dxf items like lines to Pcbnew.
virtual void addImage(const DL_ImageData &) override
std::vector< SPLINE_CTRL_POINT > m_SplineControlPointList
Definition: dxf2brd_items.h:81
double m_SplineTangentStartY
Definition: dxf2brd_items.h:74
virtual void addDimLinear(const DL_DimensionData &, const DL_DimLinearData &) override
virtual void addDimAlign(const DL_DimensionData &, const DL_DimAlignedData &) override
DXF2BRD_ENTITY_DATA m_curr_entity