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-2021 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
30
31#include <dl_creationadapter.h>
32#include <dl_dxf.h>
33#include <math/vector3.h>
35
36#include <list>
37
38class BOARD;
39class 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{
60public:
62
63 // Reset the entity parameters
64 void Clear()
65 {
66 m_EntityType = DL_UNKNOWN;
68 m_EntityFlag = 0;
77 m_BulgeVertex = 0.0;
78 m_SplineKnotsList.clear();
81 }
82
83 int m_EntityType; // the DXF type of entity
84 int m_EntityParseStatus; // Inside a entity: status of parsing:
85 // 0 = no entity
86 // 1 = first item of entity
87 // 2 = entity in progress
88 int m_EntityFlag; // a info flag to parse entities
89
90 VECTOR2D m_LastCoordinate; // the last vertex coordinate read (unit = mm)
91 VECTOR2D m_PolylineStart; // The first point of the polyline entity, when reading a
92 // polyline (unit = mm)
93 double m_BulgeVertex; // the last vertex bulge value read
94
95 // for spline parsing: parameters
96 unsigned int m_SplineDegree;
97 unsigned int m_SplineKnotsCount;
99 unsigned int m_SplineFitCount;
100 double m_SplineTangentStartX; // tangent dir X for the start point
101 double m_SplineTangentStartY; // tangent dir Y for the start point
102 double m_SplineTangentEndX; // tangent dir X for the end point
103 double m_SplineTangentEndY; // tangent dir Y for the end point
104
105 // for spline parsing: buffers to store control points, fit points and knot
106 std::vector<double> m_SplineKnotsList; // knots list, code 40
107 // control points list coordinates, code 10, 20 & 30 (only X and Y cood and Weight)
108 std::vector<SPLINE_CTRL_POINT> m_SplineControlPointList;
109 // fit points list, code 11, 21 & 31 (only X and Y cood)
110 std::vector<VECTOR2D> m_SplineFitPointList;
111};
112
113// Magic constants as defined by dxf specification for line weight
114#define DXF_IMPORT_LINEWEIGHT_BY_LAYER -1
115#define DXF_IMPORT_LINEWEIGHT_BY_BLOCK -2
116#define DXF_IMPORT_LINEWEIGHT_BY_LW_DEFAULT -3
117
122{
123public:
124 wxString m_layerName;
126
127 DXF_IMPORT_LAYER( const wxString& aName, int aLineWeight )
128 {
129 m_layerName = aName;
130 m_lineWeight = aLineWeight;
131 }
132};
133
138{
139public:
140 wxString m_name;
142
144
145 DXF_IMPORT_BLOCK( const wxString& aName, double aX, double aY )
146 {
147 m_name = aName;
148 m_baseX = aX;
149 m_baseY = aY;
150 }
151};
152
157{
158public:
159 wxString m_name;
162 bool m_bold;
164
165 DXF_IMPORT_STYLE( const wxString& aName, double aTextHeight, double aWidthFactor, bool aBold,
166 bool aItalic )
167 {
168 m_name = aName;
169 m_textHeight = aTextHeight;
170 m_widthFactor = aWidthFactor;
171 m_bold = aBold;
172 m_italic = aItalic;
173 }
174};
175
176
181{
182 DEFAULT = 0,
183 INCHES = 1,
184 FEET = 2,
185 MILLIMETERS = 4,
186 CENTIMETERS = 5,
187 METERS = 6,
188 MICROINCHES = 8,
189 MILS = 9,
190 YARDS = 10,
191 ANGSTROMS = 11,
192 NANOMETERS = 12,
193 MICRONS = 13,
194 DECIMETERS = 14,
195 DECAMETERS = 15,
196 HECTOMETERS = 16,
197 GIGAMETERS = 17,
198 ASTRONOMICAL = 18,
199 LIGHTYEARS = 19,
200 PARSECS = 20
201};
202
207{
211};
212
217#if 0 //defined(DEBUG)
218// For dxf import debug:
219#define ON_UNSUPPORTED( error_msg ) wxLogMessage( wxT( error_msg ) )
220#else
221#define ON_UNSUPPORTED( error_msg )
222#endif
223
224class DXF_IMPORT_PLUGIN : public GRAPHICS_IMPORT_PLUGIN, public DL_CreationAdapter
225{
226public:
229
230 const wxString GetName() const override
231 {
232 return "AutoCAD DXF";
233 }
234
235 const std::vector<std::string> GetFileExtensions() const override
236 {
237 static std::vector<std::string> exts = { "dxf" };
238 return exts;
239 }
240
241 bool Load( const wxString& aFileName ) override;
242 bool Import() override;
243
244 double GetImageWidth() const override;
245 double GetImageHeight() const override;
246
247 void updateImageLimits( const VECTOR2D& aPoint );
248
249 virtual void SetImporter( GRAPHICS_IMPORTER* aImporter ) override;
250
257 void ImportAsFootprintGraphic( bool aImportAsFootprintGraphic )
258 {
259 m_importAsFPShapes = aImportAsFootprintGraphic;
260 }
261
270 {
271 m_currentUnit = aUnit;
272 }
273
282 void SetDefaultLineWidthMM( double aWidth )
283 {
284 m_defaultThickness = aWidth;
285 }
286
287 void SetLineWidthMM( double aWidth ) override { SetDefaultLineWidthMM( aWidth ); }
288
298 void SetOffset( double aOffsetX, double aOffsetY )
299 {
300 m_xOffset = aOffsetX;
301 m_yOffset = aOffsetY;
302 }
303
309 void SetBrdLayer( int aBrdLayer ) { m_brdLayer = aBrdLayer; }
310
316 bool ImportDxfFile( const wxString& aFile );
317
321 const wxString& GetMessages() const override
322 {
323 return m_messages;
324 }
325
326private:
327 // report message to keep trace of not supported dxf entities:
328 void reportMsg( const wxString& aMessage );
329
330 // coordinate conversions from dxf file to mm
331 double mapX( double aDxfCoordX );
332 double mapY( double aDxfCoordY );
333 double mapDim( double aDxfValue );
334 double lineWeightToWidth( int lw, DXF_IMPORT_LAYER* aLayer );
335 double getCurrentUnitScale();
336
337 DXF_ARBITRARY_AXIS getArbitraryAxis( DL_Extrusion* aData );
338
343 VECTOR3D wcsToOcs( const DXF_ARBITRARY_AXIS& arbitraryAxis, VECTOR3D point );
344
349 VECTOR3D ocsToWcs( const DXF_ARBITRARY_AXIS& arbitraryAxis, VECTOR3D point );
350
357 DXF_IMPORT_LAYER* getImportLayer( const std::string& aLayerName );
358
365 DXF_IMPORT_BLOCK* getImportBlock( const std::string& aBlockName );
366
373 DXF_IMPORT_STYLE* getImportStyle( const std::string& aStyleName );
374
375 // Functions to aid in the creation of a Polyline.
376 void insertLine( const VECTOR2D& aSegStart, const VECTOR2D& aSegEnd, double aWidth );
377 void insertArc( const VECTOR2D& aSegStart, const VECTOR2D& aSegEnd,
378 double aBulge, double aWidth );
379
380 // Add a dxf spline (stored in m_curr_entity) to the board, after conversion to segments.
381 void insertSpline( double aWidth );
382
383 // Methods from DL_CreationAdapter:
384 // They are something like"call back" functions,
385 // called when the corresponding object is read in dxf file
386
390 virtual void setVariableString( const std::string& key, const std::string& value,
391 int code ) override;
392
396 virtual void setVariableInt( const std::string& key, int value, int code ) override;
397
401 virtual void setVariableDouble( const std::string& key, double value, int code ) override {}
402
403 virtual void addLayer( const DL_LayerData& aData ) override;
404 virtual void addLine( const DL_LineData& aData ) override;
405 virtual void addLinetype( const DL_LinetypeData& data ) override;
406
414 virtual void addBlock( const DL_BlockData& ) override;
415 virtual void endBlock() override;
416 virtual void addTextStyle( const DL_StyleData& aData ) override;
417 virtual void addPoint( const DL_PointData& aData ) override;
418
419 virtual void addCircle( const DL_CircleData& aData ) override;
420 virtual void addArc( const DL_ArcData& aData ) override;
421 //virtual void addLWPolyline( const DRW_LWPolyline& aData ) override;
422 virtual void addText( const DL_TextData& aData ) override;
423 virtual void addPolyline( const DL_PolylineData& aData ) override;
424
425 /* Inserts blocks where specified by insert data */
426 virtual void addInsert( const DL_InsertData& aData ) override;
427
431 virtual void addVertex( const DL_VertexData& aData ) override;
432 virtual void addMText( const DL_MTextData& aData) override;
433
434 virtual void endEntity() override;
435
439 virtual void addSpline( const DL_SplineData& aData ) override;
440
444 virtual void addControlPoint( const DL_ControlPointData& aData ) override;
445
449 virtual void addFitPoint( const DL_FitPointData& aData ) override;
450
454 virtual void addKnot( const DL_KnotData& aData ) override;
455
456 // Not yet handled DXF entities:
457 virtual void addXLine( const DL_XLineData& ) override { ON_UNSUPPORTED( "addXLine" ); }
458
459 virtual void addRay( const DL_RayData& ) override { ON_UNSUPPORTED( "addRay" ); }
460
461 virtual void addArcAlignedText( const DL_ArcAlignedTextData& ) override
462 { ON_UNSUPPORTED( "addArcAlignedText" ); }
463
464 virtual void addAttribute( const DL_AttributeData& ) override
465 { ON_UNSUPPORTED( "addAttribute" ); }
466
467 virtual void addDimAlign( const DL_DimensionData&,
468 const DL_DimAlignedData& ) override { ON_UNSUPPORTED( "addDimAlign" ); }
469 virtual void addDimLinear( const DL_DimensionData&,
470 const DL_DimLinearData& ) override { ON_UNSUPPORTED( "addDimLinear" ); }
471 virtual void addDimRadial( const DL_DimensionData&,
472 const DL_DimRadialData& ) override { ON_UNSUPPORTED( "addDimRadial" ); }
473 virtual void addDimDiametric( const DL_DimensionData&,
474 const DL_DimDiametricData& ) override { ON_UNSUPPORTED( "addDimDiametric" ); }
475 virtual void addDimAngular( const DL_DimensionData&,
476 const DL_DimAngular2LData& ) override { ON_UNSUPPORTED( "addDimAngular" ); }
477 virtual void addDimAngular3P( const DL_DimensionData&,
478 const DL_DimAngular3PData& ) override { ON_UNSUPPORTED( "addDimAngular3P" ); }
479 virtual void addDimOrdinate( const DL_DimensionData&,
480 const DL_DimOrdinateData& ) override { ON_UNSUPPORTED( "addDimOrdinate" ); }
481
482 virtual void addLeader( const DL_LeaderData& ) override
483 { ON_UNSUPPORTED( "addLeader" ); }
484
485 virtual void addLeaderVertex( const DL_LeaderVertexData& ) override
486 { ON_UNSUPPORTED( "addLeaderVertex" ); }
487
488 virtual void addHatch( const DL_HatchData& ) override { ON_UNSUPPORTED( "addHatch" ); }
489
490 virtual void addTrace( const DL_TraceData& ) override { ON_UNSUPPORTED( "addTrace" ); }
491 virtual void add3dFace( const DL_3dFaceData& ) override { ON_UNSUPPORTED( "add3dFace" ); }
492
493 virtual void addSolid( const DL_SolidData& ) override { ON_UNSUPPORTED( "addSolid" ); }
494
495 virtual void addImage( const DL_ImageData& ) override { ON_UNSUPPORTED( "addImage" ); }
496 virtual void linkImage( const DL_ImageDefData& ) override {}
497
498 virtual void addHatchLoop( const DL_HatchLoopData& ) override
499 {
500 ON_UNSUPPORTED( "addHatchLoop" );
501 }
502
503 virtual void addHatchEdge( const DL_HatchEdgeData& ) override
504 {
505 ON_UNSUPPORTED( "addHatchEdge" );
506 }
507
508 virtual void addXRecord( const std::string& ) override { ON_UNSUPPORTED( "addXRecord" ); }
509
510 virtual void addXRecordString( int, const std::string& ) override
511 {
512 ON_UNSUPPORTED( "addXRecordString" );
513 }
514
515 virtual void addXRecordReal( int, double ) override { ON_UNSUPPORTED( "addXRecordReal" ); }
516 virtual void addXRecordInt( int, int ) override { ON_UNSUPPORTED( "addXRecordInt" ); }
517 virtual void addXRecordBool( int, bool ) override { ON_UNSUPPORTED( "addXRecordBool" ); }
518
519 virtual void addXDataApp( const std::string& ) override { ON_UNSUPPORTED( "addXDataApp" ); }
520 virtual void addXDataString( int, const std::string& ) override
521 {
522 ON_UNSUPPORTED( "addXDataString" );
523 }
524
525 virtual void addXDataReal( int, double ) override { ON_UNSUPPORTED( "addXDataReal" ); }
526 virtual void addXDataInt( int, int ) override { ON_UNSUPPORTED( "addXDataInt" ); }
527
536 static wxString toDxfString( const wxString& aStr );
537
541 static wxString toNativeString( const wxString& aData );
542
543 void writeLine();
545
546private:
547 double m_xOffset; // X coord offset for conversion (in mm)
548 double m_yOffset; // Y coord offset for conversion (in mm)
549 double m_defaultThickness; // default line thickness for conversion (in mm)
550 int m_brdLayer; // The board layer to place imported DXF items
551 int m_version; // the dxf version, not used here
552 std::string m_codePage; // The code page, not used here
553 bool m_importAsFPShapes; // Use footprint items instead of board items when true.
554 // true when the items are imported in the footprint editor
555 wxString m_messages; // messages generated during dxf file parsing.
556 // Each message ends by '\n'
557 DXF2BRD_ENTITY_DATA m_curr_entity; // the current entity parameters when parsing a DXF entity
558
559 double m_minX, m_maxX; // handles image size in mm
560 double m_minY, m_maxY; // handles image size in mm
561
562 DXF_IMPORT_UNITS m_currentUnit; // current unit during import
563 int m_importCoordinatePrecision; // current precision for linear units (points)
564 int m_importAnglePrecision; // current precision for angles
565
567
568 // List of layers as we import, used just to grab props for objects.
569 std::vector<std::unique_ptr<DXF_IMPORT_LAYER>> m_layers;
570 std::vector<std::unique_ptr<DXF_IMPORT_BLOCK>> m_blocks; // List of blocks as we import
571 std::vector<std::unique_ptr<DXF_IMPORT_STYLE>> m_styles; // List of blocks as we import
573};
574
575#endif // DXF2BRD_ITEMS_H
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:50
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:265
A helper class to parse a DXF entity (polyline and spline)
unsigned int m_SplineDegree
std::vector< VECTOR2D > m_SplineFitPointList
unsigned int m_SplineFitCount
std::vector< SPLINE_CTRL_POINT > m_SplineControlPointList
unsigned int m_SplineKnotsCount
unsigned int m_SplineControlCount
std::vector< double > m_SplineKnotsList
A helper class to hold layer settings temporarily during import.
GRAPHICS_IMPORTER_BUFFER m_buffer
DXF_IMPORT_BLOCK(const wxString &aName, double aX, double aY)
A helper class to hold layer settings temporarily during import.
DXF_IMPORT_LAYER(const wxString &aName, int aLineWeight)
DXF_IMPORT_UNITS m_currentUnit
virtual void addXDataApp(const std::string &) override
virtual void addPolyline(const DL_PolylineData &aData) override
double mapY(double aDxfCoordY)
virtual void addImage(const DL_ImageData &) override
std::vector< std::unique_ptr< DXF_IMPORT_LAYER > > m_layers
static wxString toNativeString(const wxString &aData)
Convert a DXF encoded string into a native Unicode string.
virtual void addDimAngular(const DL_DimensionData &, const DL_DimAngular2LData &) override
virtual void addDimAngular3P(const DL_DimensionData &, const DL_DimAngular3PData &) override
virtual void addAttribute(const DL_AttributeData &) override
virtual void setVariableInt(const std::string &key, int value, int code) override
Called for every int variable in the DXF file (e.g.
void insertSpline(double aWidth)
virtual void linkImage(const DL_ImageDefData &) override
virtual void addLine(const DL_LineData &aData) override
virtual void addLeader(const DL_LeaderData &) override
void insertLine(const VECTOR2D &aSegStart, const VECTOR2D &aSegEnd, double aWidth)
virtual void addXDataReal(int, double) override
DXF_IMPORT_BLOCK * getImportBlock(const std::string &aBlockName)
Return the import layer block.
virtual void addBlock(const DL_BlockData &) override
Called for each BLOCK in the DXF file.
virtual void addDimAlign(const DL_DimensionData &, const DL_DimAlignedData &) override
virtual void addRay(const DL_RayData &) override
virtual void addLayer(const DL_LayerData &aData) override
virtual void addCircle(const DL_CircleData &aData) override
virtual void addMText(const DL_MTextData &aData) override
DXF_IMPORT_LAYER * getImportLayer(const std::string &aLayerName)
Return the import layer data.
static wxString toDxfString(const wxString &aStr)
Convert a native Unicode string into a DXF encoded string.
virtual void addArcAlignedText(const DL_ArcAlignedTextData &) override
virtual void setVariableDouble(const std::string &key, double value, int code) override
Called for every double variable in the DXF file (e.g.
virtual void addTrace(const DL_TraceData &) override
virtual void addSolid(const DL_SolidData &) override
DXF_IMPORT_STYLE * getImportStyle(const std::string &aStyleName)
Return the import style.
virtual void addInsert(const DL_InsertData &aData) override
const wxString GetName() const override
Return the plugin name.
VECTOR3D ocsToWcs(const DXF_ARBITRARY_AXIS &arbitraryAxis, VECTOR3D point)
Converts a given object coordinate point to world coordinate using the given arbitrary axis vectors.
virtual void addLeaderVertex(const DL_LeaderVertexData &) override
virtual void addXRecordString(int, const std::string &) override
double GetImageWidth() const override
Return image width from original imported file.
virtual void addDimLinear(const DL_DimensionData &, const DL_DimLinearData &) override
DXF_ARBITRARY_AXIS getArbitraryAxis(DL_Extrusion *aData)
double GetImageHeight() const override
Return image height from original imported file.
virtual void addXDataString(int, const std::string &) override
virtual void add3dFace(const DL_3dFaceData &) override
VECTOR3D wcsToOcs(const DXF_ARBITRARY_AXIS &arbitraryAxis, VECTOR3D point)
Converts a given world coordinate point to object coordinate using the given arbitrary axis vectors.
virtual void addHatch(const DL_HatchData &) override
void updateImageLimits(const VECTOR2D &aPoint)
virtual void addTextStyle(const DL_StyleData &aData) override
virtual void addXDataInt(int, int) override
virtual void addVertex(const DL_VertexData &aData) override
Called for every polyline vertex.
virtual void addSpline(const DL_SplineData &aData) override
Called for every spline.
virtual void addText(const DL_TextData &aData) override
virtual void endBlock() 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.
virtual void addKnot(const DL_KnotData &aData) override
Called for every spline knot value.
virtual void addXRecordInt(int, int) override
bool ImportDxfFile(const wxString &aFile)
Implementation of the method used for communicate with this filter.
virtual void addArc(const DL_ArcData &aData) override
virtual void addXRecordReal(int, double) override
void SetLineWidthMM(double aWidth) override
void SetDefaultLineWidthMM(double aWidth)
Set the default line width when importing dxf items like lines to Pcbnew.
virtual void addFitPoint(const DL_FitPointData &aData) override
Called for every spline fit point.
virtual void addPoint(const DL_PointData &aData) override
void SetBrdLayer(int aBrdLayer)
Set the layer number to import dxf items.
double mapDim(double aDxfValue)
virtual void addDimOrdinate(const DL_DimensionData &, const DL_DimOrdinateData &) override
virtual void addDimDiametric(const DL_DimensionData &, const DL_DimDiametricData &) override
virtual void addXRecord(const std::string &) override
void reportMsg(const wxString &aMessage)
virtual void addHatchEdge(const DL_HatchEdgeData &) override
double mapX(double aDxfCoordX)
double lineWeightToWidth(int lw, DXF_IMPORT_LAYER *aLayer)
void insertArc(const VECTOR2D &aSegStart, const VECTOR2D &aSegEnd, double aBulge, double aWidth)
std::vector< std::unique_ptr< DXF_IMPORT_BLOCK > > m_blocks
virtual void addHatchLoop(const DL_HatchLoopData &) override
bool Import() override
Actually imports the file.
virtual void endEntity() override
virtual void addControlPoint(const DL_ControlPointData &aData) override
Called for every spline control point.
GRAPHICS_IMPORTER_BUFFER m_internalImporter
virtual void addXRecordBool(int, bool) override
std::vector< std::unique_ptr< DXF_IMPORT_STYLE > > m_styles
bool Load(const wxString &aFileName) override
Load file for import.
const wxString & GetMessages() const override
virtual void addDimRadial(const DL_DimensionData &, const DL_DimRadialData &) override
void SetUnit(DXF_IMPORT_UNITS aUnit)
Set the default units when importing DXFs.
virtual void addXLine(const DL_XLineData &) override
DXF_IMPORT_BLOCK * m_currentBlock
DXF2BRD_ENTITY_DATA m_curr_entity
void ImportAsFootprintGraphic(bool aImportAsFootprintGraphic)
Allow the import DXF items converted to board graphic items or footprint graphic items.
const std::vector< std::string > GetFileExtensions() const override
Return a vector of the file extensions handled by this plugin.
virtual void SetImporter(GRAPHICS_IMPORTER *aImporter) override
Set the receiver of the imported shapes.
void SetOffset(double aOffsetX, double aOffsetY)
Set the coordinate offset between the imported dxf items and Pcbnew.
virtual void addLinetype(const DL_LinetypeData &data) override
A helper class to hold style settings temporarily during import.
DXF_IMPORT_STYLE(const wxString &aName, double aTextHeight, double aWidthFactor, bool aBold, bool aItalic)
Interface that creates objects representing shapes for a given data model.
Interface for vector graphics import plugins.
#define ON_UNSUPPORTED(error_msg)
This class import DXF ASCII files and convert basic entities to board entities.
DXF_IMPORT_UNITS
DXF Units enum with values as specified in DXF 2012 Specification.
Helper class representing the DXF specification's "arbitrary axis".
A helper class to store a spline control point (in X,Y plane only)
SPLINE_CTRL_POINT(double a_x, double a_y, double a_weight)
Definition of file extensions used in Kicad.