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>
34#include <math/matrix3x3.h>
36
37#include <list>
38
39class BOARD;
40class BOARD_ITEM;
41
46{
47 double m_x;
48 double m_y;
49 double m_weight;
50
51 SPLINE_CTRL_POINT( double a_x, double a_y, double a_weight )
52 : m_x( a_x ), m_y( a_y ), m_weight( a_weight )
53 {}
54};
55
60{
61public:
63
64 // Reset the entity parameters
65 void Clear()
66 {
67 m_EntityType = DL_UNKNOWN;
69 m_EntityFlag = 0;
78 m_BulgeVertex = 0.0;
79 m_SplineKnotsList.clear();
82 }
83
84 int m_EntityType; // the DXF type of entity
85 int m_EntityParseStatus; // Inside a entity: status of parsing:
86 // 0 = no entity
87 // 1 = first item of entity
88 // 2 = entity in progress
89 int m_EntityFlag; // a info flag to parse entities
90
91 VECTOR2D m_LastCoordinate; // the last vertex coordinate read (unit = mm)
92 VECTOR2D m_PolylineStart; // The first point of the polyline entity, when reading a
93 // polyline (unit = mm)
94 double m_BulgeVertex; // the last vertex bulge value read
95
96 // for spline parsing: parameters
97 unsigned int m_SplineDegree;
98 unsigned int m_SplineKnotsCount;
100 unsigned int m_SplineFitCount;
101 double m_SplineTangentStartX; // tangent dir X for the start point
102 double m_SplineTangentStartY; // tangent dir Y for the start point
103 double m_SplineTangentEndX; // tangent dir X for the end point
104 double m_SplineTangentEndY; // tangent dir Y for the end point
105
106 // for spline parsing: buffers to store control points, fit points and knot
107 std::vector<double> m_SplineKnotsList; // knots list, code 40
108 // control points list coordinates, code 10, 20 & 30 (only X and Y cood and Weight)
109 std::vector<SPLINE_CTRL_POINT> m_SplineControlPointList;
110 // fit points list, code 11, 21 & 31 (only X and Y cood)
111 std::vector<VECTOR2D> m_SplineFitPointList;
112};
113
114// Magic constants as defined by dxf specification for line weight
115#define DXF_IMPORT_LINEWEIGHT_BY_LAYER -1
116#define DXF_IMPORT_LINEWEIGHT_BY_BLOCK -2
117#define DXF_IMPORT_LINEWEIGHT_BY_LW_DEFAULT -3
118
123{
124public:
125 wxString m_layerName;
127
128 DXF_IMPORT_LAYER( const wxString& aName, int aLineWeight )
129 {
130 m_layerName = aName;
131 m_lineWeight = aLineWeight;
132 }
133};
134
139{
140public:
141 wxString m_name;
143
145
146 DXF_IMPORT_BLOCK( const wxString& aName, double aX, double aY )
147 {
148 m_name = aName;
149 m_baseX = aX;
150 m_baseY = aY;
151 }
152};
153
158{
159public:
160 wxString m_name;
163 bool m_bold;
165
166 DXF_IMPORT_STYLE( const wxString& aName, double aTextHeight, double aWidthFactor, bool aBold,
167 bool aItalic )
168 {
169 m_name = aName;
170 m_textHeight = aTextHeight;
171 m_widthFactor = aWidthFactor;
172 m_bold = aBold;
173 m_italic = aItalic;
174 }
175};
176
177
182{
183 DEFAULT = 0,
184 INCHES = 1,
185 FEET = 2,
186 MILLIMETERS = 4,
187 CENTIMETERS = 5,
188 METERS = 6,
189 MICROINCHES = 8,
190 MILS = 9,
191 YARDS = 10,
192 ANGSTROMS = 11,
193 NANOMETERS = 12,
194 MICRONS = 13,
195 DECIMETERS = 14,
196 DECAMETERS = 15,
197 HECTOMETERS = 16,
198 GIGAMETERS = 17,
199 ASTRONOMICAL = 18,
200 LIGHTYEARS = 19,
201 PARSECS = 20
202};
203
204
209#if 0 //defined(DEBUG)
210// For dxf import debug:
211#define ON_UNSUPPORTED( error_msg ) wxLogMessage( wxT( error_msg ) )
212#else
213#define ON_UNSUPPORTED( error_msg )
214#endif
215
216class DXF_IMPORT_PLUGIN : public GRAPHICS_IMPORT_PLUGIN, public DL_CreationAdapter
217{
218public:
221
222 const wxString GetName() const override
223 {
224 return "AutoCAD DXF";
225 }
226
227 const std::vector<std::string> GetFileExtensions() const override
228 {
229 static std::vector<std::string> exts = { "dxf" };
230 return exts;
231 }
232
233 bool Load( const wxString& aFileName ) override;
234 bool Import() override;
235
236 double GetImageWidth() const override;
237 double GetImageHeight() const override;
238
239 void updateImageLimits( const VECTOR2D& aPoint );
240
241 virtual void SetImporter( GRAPHICS_IMPORTER* aImporter ) override;
242
249 void ImportAsFootprintGraphic( bool aImportAsFootprintGraphic )
250 {
251 m_importAsFPShapes = aImportAsFootprintGraphic;
252 }
253
262 {
263 m_currentUnit = aUnit;
264 }
265
274 void SetDefaultLineWidthMM( double aWidth )
275 {
276 m_defaultThickness = aWidth;
277 }
278
279 void SetLineWidthMM( double aWidth ) override { SetDefaultLineWidthMM( aWidth ); }
280
290 void SetOffset( double aOffsetX, double aOffsetY )
291 {
292 m_xOffset = aOffsetX;
293 m_yOffset = aOffsetY;
294 }
295
301 void SetBrdLayer( int aBrdLayer ) { m_brdLayer = aBrdLayer; }
302
308 bool ImportDxfFile( const wxString& aFile );
309
313 const wxString& GetMessages() const override
314 {
315 return m_messages;
316 }
317
318private:
319 // report message to keep trace of not supported dxf entities:
320 void reportMsg( const wxString& aMessage );
321
322 // coordinate conversions from dxf file to mm
323 double mapX( double aDxfCoordX );
324 double mapY( double aDxfCoordY );
325 double mapDim( double aDxfValue );
326 double lineWeightToWidth( int lw, DXF_IMPORT_LAYER* aLayer );
327 double getCurrentUnitScale();
328
329 MATRIX3x3D getArbitraryAxis( DL_Extrusion* aData );
330
335 VECTOR3D wcsToOcs( const MATRIX3x3D& arbitraryAxis, VECTOR3D point );
336
341 VECTOR3D ocsToWcs( const MATRIX3x3D& arbitraryAxis, VECTOR3D point );
342
349 DXF_IMPORT_LAYER* getImportLayer( const std::string& aLayerName );
350
357 DXF_IMPORT_BLOCK* getImportBlock( const std::string& aBlockName );
358
365 DXF_IMPORT_STYLE* getImportStyle( const std::string& aStyleName );
366
367 // Functions to aid in the creation of a Polyline.
368 void insertLine( const VECTOR2D& aSegStart, const VECTOR2D& aSegEnd, double aWidth );
369 void insertArc( const VECTOR2D& aSegStart, const VECTOR2D& aSegEnd,
370 double aBulge, double aWidth );
371
372 // Add a dxf spline (stored in m_curr_entity) to the board, after conversion to segments.
373 void insertSpline( double aWidth );
374
375 // Methods from DL_CreationAdapter:
376 // They are something like"call back" functions,
377 // called when the corresponding object is read in dxf file
378
382 virtual void setVariableString( const std::string& key, const std::string& value,
383 int code ) override;
384
388 virtual void setVariableInt( const std::string& key, int value, int code ) override;
389
393 virtual void setVariableDouble( const std::string& key, double value, int code ) override {}
394
395 virtual void addLayer( const DL_LayerData& aData ) override;
396 virtual void addLine( const DL_LineData& aData ) override;
397 virtual void addLinetype( const DL_LinetypeData& data ) override;
398
406 virtual void addBlock( const DL_BlockData& ) override;
407 virtual void endBlock() override;
408 virtual void addTextStyle( const DL_StyleData& aData ) override;
409 virtual void addPoint( const DL_PointData& aData ) override;
410
411 virtual void addCircle( const DL_CircleData& aData ) override;
412 virtual void addArc( const DL_ArcData& aData ) override;
413 void addEllipse( const DL_EllipseData& aData ) override;
414 //virtual void addLWPolyline( const DRW_LWPolyline& aData ) override;
415 virtual void addText( const DL_TextData& aData ) override;
416 virtual void addPolyline( const DL_PolylineData& aData ) override;
417
418 /* Inserts blocks where specified by insert data */
419 virtual void addInsert( const DL_InsertData& aData ) override;
420
424 virtual void addVertex( const DL_VertexData& aData ) override;
425 virtual void addMText( const DL_MTextData& aData) override;
426
427 virtual void endEntity() override;
428
432 virtual void addSpline( const DL_SplineData& aData ) override;
433
437 virtual void addControlPoint( const DL_ControlPointData& aData ) override;
438
442 virtual void addFitPoint( const DL_FitPointData& aData ) override;
443
447 virtual void addKnot( const DL_KnotData& aData ) override;
448
449 // Not yet handled DXF entities:
450 virtual void addXLine( const DL_XLineData& ) override { ON_UNSUPPORTED( "addXLine" ); }
451
452 virtual void addRay( const DL_RayData& ) override { ON_UNSUPPORTED( "addRay" ); }
453
454 virtual void addArcAlignedText( const DL_ArcAlignedTextData& ) override
455 { ON_UNSUPPORTED( "addArcAlignedText" ); }
456
457 virtual void addAttribute( const DL_AttributeData& ) override
458 { ON_UNSUPPORTED( "addAttribute" ); }
459
460 virtual void addDimAlign( const DL_DimensionData&,
461 const DL_DimAlignedData& ) override { ON_UNSUPPORTED( "addDimAlign" ); }
462 virtual void addDimLinear( const DL_DimensionData&,
463 const DL_DimLinearData& ) override { ON_UNSUPPORTED( "addDimLinear" ); }
464 virtual void addDimRadial( const DL_DimensionData&,
465 const DL_DimRadialData& ) override { ON_UNSUPPORTED( "addDimRadial" ); }
466 virtual void addDimDiametric( const DL_DimensionData&,
467 const DL_DimDiametricData& ) override { ON_UNSUPPORTED( "addDimDiametric" ); }
468 virtual void addDimAngular( const DL_DimensionData&,
469 const DL_DimAngular2LData& ) override { ON_UNSUPPORTED( "addDimAngular" ); }
470 virtual void addDimAngular3P( const DL_DimensionData&,
471 const DL_DimAngular3PData& ) override { ON_UNSUPPORTED( "addDimAngular3P" ); }
472 virtual void addDimOrdinate( const DL_DimensionData&,
473 const DL_DimOrdinateData& ) override { ON_UNSUPPORTED( "addDimOrdinate" ); }
474
475 virtual void addLeader( const DL_LeaderData& ) override
476 { ON_UNSUPPORTED( "addLeader" ); }
477
478 virtual void addLeaderVertex( const DL_LeaderVertexData& ) override
479 { ON_UNSUPPORTED( "addLeaderVertex" ); }
480
481 virtual void addHatch( const DL_HatchData& ) override { ON_UNSUPPORTED( "addHatch" ); }
482
483 virtual void addTrace( const DL_TraceData& ) override { ON_UNSUPPORTED( "addTrace" ); }
484 virtual void add3dFace( const DL_3dFaceData& ) override { ON_UNSUPPORTED( "add3dFace" ); }
485
486 virtual void addSolid( const DL_SolidData& ) override { ON_UNSUPPORTED( "addSolid" ); }
487
488 virtual void addImage( const DL_ImageData& ) override { ON_UNSUPPORTED( "addImage" ); }
489 virtual void linkImage( const DL_ImageDefData& ) override {}
490
491 virtual void addHatchLoop( const DL_HatchLoopData& ) override
492 {
493 ON_UNSUPPORTED( "addHatchLoop" );
494 }
495
496 virtual void addHatchEdge( const DL_HatchEdgeData& ) override
497 {
498 ON_UNSUPPORTED( "addHatchEdge" );
499 }
500
501 virtual void addXRecord( const std::string& ) override { ON_UNSUPPORTED( "addXRecord" ); }
502
503 virtual void addXRecordString( int, const std::string& ) override
504 {
505 ON_UNSUPPORTED( "addXRecordString" );
506 }
507
508 virtual void addXRecordReal( int, double ) override { ON_UNSUPPORTED( "addXRecordReal" ); }
509 virtual void addXRecordInt( int, int ) override { ON_UNSUPPORTED( "addXRecordInt" ); }
510 virtual void addXRecordBool( int, bool ) override { ON_UNSUPPORTED( "addXRecordBool" ); }
511
512 virtual void addXDataApp( const std::string& ) override { ON_UNSUPPORTED( "addXDataApp" ); }
513 virtual void addXDataString( int, const std::string& ) override
514 {
515 ON_UNSUPPORTED( "addXDataString" );
516 }
517
518 virtual void addXDataReal( int, double ) override { ON_UNSUPPORTED( "addXDataReal" ); }
519 virtual void addXDataInt( int, int ) override { ON_UNSUPPORTED( "addXDataInt" ); }
520
529 static wxString toDxfString( const wxString& aStr );
530
534 static wxString toNativeString( const wxString& aData );
535
536 void writeLine();
538
539private:
540 double m_xOffset; // X coord offset for conversion (in mm)
541 double m_yOffset; // Y coord offset for conversion (in mm)
542 double m_defaultThickness; // default line thickness for conversion (in mm)
543 int m_brdLayer; // The board layer to place imported DXF items
544 int m_version; // the dxf version, not used here
545 std::string m_codePage; // The code page, not used here
546 bool m_importAsFPShapes; // Use footprint items instead of board items when true.
547 // true when the items are imported in the footprint editor
548 wxString m_messages; // messages generated during dxf file parsing.
549 // Each message ends by '\n'
550 DXF2BRD_ENTITY_DATA m_curr_entity; // the current entity parameters when parsing a DXF entity
551
552 double m_minX, m_maxX; // handles image size in mm
553 double m_minY, m_maxY; // handles image size in mm
554
555 DXF_IMPORT_UNITS m_currentUnit; // current unit during import
556 int m_importCoordinatePrecision; // current precision for linear units (points)
557 int m_importAnglePrecision; // current precision for angles
558
560
561 // List of layers as we import, used just to grab props for objects.
562 std::vector<std::unique_ptr<DXF_IMPORT_LAYER>> m_layers;
563 std::vector<std::unique_ptr<DXF_IMPORT_BLOCK>> m_blocks; // List of blocks as we import
564 std::vector<std::unique_ptr<DXF_IMPORT_STYLE>> m_styles; // List of blocks as we import
566};
567
568#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:58
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
void addEllipse(const DL_EllipseData &aData) override
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
VECTOR3D ocsToWcs(const MATRIX3x3D &arbitraryAxis, VECTOR3D point)
Converts a given object coordinate point to world coordinate using the given arbitrary axis vectors.
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.
virtual void addLeaderVertex(const DL_LeaderVertexData &) override
virtual void addXRecordString(int, const std::string &) override
MATRIX3x3D getArbitraryAxis(DL_Extrusion *aData)
double GetImageWidth() const override
Return image width from original imported file.
virtual void addDimLinear(const DL_DimensionData &, const DL_DimLinearData &) override
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
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.
VECTOR3D wcsToOcs(const MATRIX3x3D &arbitraryAxis, VECTOR3D point)
Converts a given world coordinate point to object coordinate using the given arbitrary axis vectors.
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.
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.