KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2023 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#include "wx/translation.h"
31
32#include <dl_creationadapter.h>
33#include <dl_dxf.h>
34#include <math/vector3.h>
35#include <math/matrix3x3.h>
37
38#include <list>
39
40class BOARD;
41class BOARD_ITEM;
42
47{
48 double m_x;
49 double m_y;
50 double m_weight;
51
52 SPLINE_CTRL_POINT( double a_x, double a_y, double a_weight )
53 : m_x( a_x ), m_y( a_y ), m_weight( a_weight )
54 {}
55};
56
61{
62public:
64
65 // Reset the entity parameters
66 void Clear()
67 {
68 m_EntityType = DL_UNKNOWN;
70 m_EntityFlag = 0;
79 m_BulgeVertex = 0.0;
80 m_SplineKnotsList.clear();
83 }
84
85 int m_EntityType; // the DXF type of entity
86 int m_EntityParseStatus; // Inside a entity: status of parsing:
87 // 0 = no entity
88 // 1 = first item of entity
89 // 2 = entity in progress
90 int m_EntityFlag; // a info flag to parse entities
91
92 VECTOR2D m_LastCoordinate; // the last vertex coordinate read (unit = mm)
93 VECTOR2D m_PolylineStart; // The first point of the polyline entity, when reading a
94 // polyline (unit = mm)
95 double m_BulgeVertex; // the last vertex bulge value read
96
97 // for spline parsing: parameters
98 unsigned int m_SplineDegree;
99 unsigned int m_SplineKnotsCount;
101 unsigned int m_SplineFitCount;
102 double m_SplineTangentStartX; // tangent dir X for the start point
103 double m_SplineTangentStartY; // tangent dir Y for the start point
104 double m_SplineTangentEndX; // tangent dir X for the end point
105 double m_SplineTangentEndY; // tangent dir Y for the end point
106
107 // for spline parsing: buffers to store control points, fit points and knot
108 std::vector<double> m_SplineKnotsList; // knots list, code 40
109 // control points list coordinates, code 10, 20 & 30 (only X and Y cood and Weight)
110 std::vector<SPLINE_CTRL_POINT> m_SplineControlPointList;
111 // fit points list, code 11, 21 & 31 (only X and Y cood)
112 std::vector<VECTOR2D> m_SplineFitPointList;
113};
114
115// Magic constants as defined by dxf specification for line weight
116#define DXF_IMPORT_LINEWEIGHT_BY_LAYER -1
117#define DXF_IMPORT_LINEWEIGHT_BY_BLOCK -2
118#define DXF_IMPORT_LINEWEIGHT_BY_LW_DEFAULT -3
119
124{
125public:
126 wxString m_layerName;
128
129 DXF_IMPORT_LAYER( const wxString& aName, int aLineWeight )
130 {
131 m_layerName = aName;
132 m_lineWeight = aLineWeight;
133 }
134};
135
140{
141public:
142 wxString m_name;
144
146
147 DXF_IMPORT_BLOCK( const wxString& aName, double aX, double aY )
148 {
149 m_name = aName;
150 m_baseX = aX;
151 m_baseY = aY;
152 }
153};
154
159{
160public:
161 wxString m_name;
164 bool m_bold;
166
167 DXF_IMPORT_STYLE( const wxString& aName, double aTextHeight, double aWidthFactor, bool aBold,
168 bool aItalic )
169 {
170 m_name = aName;
171 m_textHeight = aTextHeight;
172 m_widthFactor = aWidthFactor;
173 m_bold = aBold;
174 m_italic = aItalic;
175 }
176};
177
178
183{
184 DEFAULT = 0,
185 INCHES = 1,
186 FEET = 2,
187 MILLIMETERS = 4,
188 CENTIMETERS = 5,
189 METERS = 6,
190 MICROINCHES = 8,
191 MILS = 9,
192 YARDS = 10,
193 ANGSTROMS = 11,
194 NANOMETERS = 12,
195 MICRONS = 13,
196 DECIMETERS = 14,
197 DECAMETERS = 15,
198 HECTOMETERS = 16,
199 GIGAMETERS = 17,
200 ASTRONOMICAL = 18,
201 LIGHTYEARS = 19,
202 PARSECS = 20
203};
204
205
210#define ON_UNSUPPORTED( error_msg ) reportMessage( wxString::Format( _( "%s is not supported." ), \
211 error_msg ) )
212
213class DXF_IMPORT_PLUGIN : public GRAPHICS_IMPORT_PLUGIN, public DL_CreationAdapter
214{
215public:
218
219 const wxString GetName() const override
220 {
221 return "AutoCAD DXF";
222 }
223
224 const std::vector<std::string> GetFileExtensions() const override
225 {
226 static std::vector<std::string> exts = { "dxf" };
227 return exts;
228 }
229
230 bool Load( const wxString& aFileName ) override;
231 bool LoadFromMemory( const wxMemoryBuffer& aMemBuffer ) override;
232 bool Import() override;
233
234 double GetImageWidth() const override;
235 double GetImageHeight() const override;
236 BOX2D GetImageBBox() const override;
237
238 void updateImageLimits( const VECTOR2D& aPoint );
239
240 virtual void SetImporter( GRAPHICS_IMPORTER* aImporter ) override;
241
248 void ImportAsFootprintGraphic( bool aImportAsFootprintGraphic )
249 {
250 m_importAsFPShapes = aImportAsFootprintGraphic;
251 }
252
261 {
262 m_currentUnit = aUnit;
263 }
264
273 void SetDefaultLineWidthMM( double aWidth )
274 {
275 m_defaultThickness = aWidth;
276 }
277
278 void SetLineWidthMM( double aWidth ) override { SetDefaultLineWidthMM( aWidth ); }
279
289 void SetOffset( double aOffsetX, double aOffsetY )
290 {
291 m_xOffset = aOffsetX;
292 m_yOffset = aOffsetY;
293 }
294
300 void SetBrdLayer( int aBrdLayer ) { m_brdLayer = aBrdLayer; }
301
307 bool ImportDxfFile( const wxString& aFile );
308
314 bool ImportDxfFile( const wxMemoryBuffer& aMemBuffer );
315
319 const wxString& GetMessages() const override
320 {
321 return m_messages;
322 }
323 // report message to keep trace of not supported dxf entities:
324 void ReportMsg( const wxString& aMessage ) override;
325
326private:
327
328 // coordinate conversions from dxf file to mm
329 double mapX( double aDxfCoordX );
330 double mapY( double aDxfCoordY );
331 double mapDim( double aDxfValue );
332 double lineWeightToWidth( int lw, DXF_IMPORT_LAYER* aLayer );
333 double getCurrentUnitScale();
334
335 MATRIX3x3D getArbitraryAxis( DL_Extrusion* aData );
336
341 VECTOR3D wcsToOcs( const MATRIX3x3D& arbitraryAxis, VECTOR3D point );
342
347 VECTOR3D ocsToWcs( const MATRIX3x3D& arbitraryAxis, VECTOR3D point );
348
355 DXF_IMPORT_LAYER* getImportLayer( const std::string& aLayerName );
356
363 DXF_IMPORT_BLOCK* getImportBlock( const std::string& aBlockName );
364
371 DXF_IMPORT_STYLE* getImportStyle( const std::string& aStyleName );
372
373 // Functions to aid in the creation of a Polyline.
374 void insertLine( const VECTOR2D& aSegStart, const VECTOR2D& aSegEnd, double aWidth );
375 void insertArc( const VECTOR2D& aSegStart, const VECTOR2D& aSegEnd,
376 double aBulge, double aWidth );
377
378 // Add a dxf spline (stored in m_curr_entity) to the board, after conversion to segments.
379 void insertSpline( double aWidth );
380
381 // Methods from DL_CreationAdapter:
382 // They are something like"call back" functions,
383 // called when the corresponding object is read in dxf file
384
388 virtual void setVariableString( const std::string& key, const std::string& value,
389 int code ) override;
390
394 virtual void setVariableInt( const std::string& key, int value, int code ) override;
395
399 virtual void setVariableDouble( const std::string& key, double value, int code ) override {}
400
401 virtual void addLayer( const DL_LayerData& aData ) override;
402 virtual void addLine( const DL_LineData& aData ) override;
403 virtual void addLinetype( const DL_LinetypeData& data ) override;
404
412 virtual void addBlock( const DL_BlockData& ) override;
413 virtual void endBlock() override;
414 virtual void addTextStyle( const DL_StyleData& aData ) override;
415 virtual void addPoint( const DL_PointData& aData ) override;
416
417 virtual void addCircle( const DL_CircleData& aData ) override;
418 virtual void addArc( const DL_ArcData& aData ) override;
419 void addEllipse( const DL_EllipseData& aData ) override;
420 //virtual void addLWPolyline( const DRW_LWPolyline& aData ) override;
421 virtual void addText( const DL_TextData& aData ) override;
422 virtual void addPolyline( const DL_PolylineData& aData ) override;
423
424 /* Inserts blocks where specified by insert data */
425 virtual void addInsert( const DL_InsertData& aData ) override;
426
430 virtual void addVertex( const DL_VertexData& aData ) override;
431
432 virtual void addMTextChunk( const std::string& text ) override;
433 virtual void addMText( const DL_MTextData& aData ) override;
434
435 virtual void endEntity() override;
436
440 virtual void addSpline( const DL_SplineData& aData ) override;
441
445 virtual void addControlPoint( const DL_ControlPointData& aData ) override;
446
450 virtual void addFitPoint( const DL_FitPointData& aData ) override;
451
455 virtual void addKnot( const DL_KnotData& aData ) override;
456
457 // Not yet handled DXF entities:
458 virtual void addXLine( const DL_XLineData& ) override
459 {
460 ReportMsg( _( "DXF construction lines not currently supported." ) );
461 }
462 virtual void addRay( const DL_RayData& ) override
463 {
464 ReportMsg( _( "DXF construction lines not currently supported." ) );
465 }
466
467 virtual void addArcAlignedText( const DL_ArcAlignedTextData& ) override
468 {
469 ReportMsg( _( "DXF arc-aligned text not currently supported." ) );
470 }
471
472 virtual void addDimAlign( const DL_DimensionData&, const DL_DimAlignedData& ) override
473 {
474 ReportMsg( _( "DXF dimensions not currently supported." ) );
475 }
476 virtual void addDimLinear( const DL_DimensionData&, const DL_DimLinearData& ) override
477 {
478 ReportMsg( _( "DXF dimensions not currently supported." ) );
479 }
480 virtual void addDimRadial( const DL_DimensionData&, const DL_DimRadialData& ) override
481 {
482 ReportMsg( _( "DXF dimensions not currently supported." ) );
483 }
484 virtual void addDimDiametric( const DL_DimensionData&, const DL_DimDiametricData& ) override
485 {
486 ReportMsg( _( "DXF dimensions not currently supported." ) );
487 }
488 virtual void addDimAngular( const DL_DimensionData&, const DL_DimAngular2LData& ) override
489 {
490 ReportMsg( _( "DXF dimensions not currently supported." ) );
491 }
492 virtual void addDimAngular3P( const DL_DimensionData&, const DL_DimAngular3PData& ) override
493 {
494 ReportMsg( _( "DXF dimensions not currently supported." ) );
495 }
496 virtual void addDimOrdinate( const DL_DimensionData&, const DL_DimOrdinateData& ) override
497 {
498 ReportMsg( _( "DXF dimensions not currently supported." ) );
499 }
500 virtual void addLeader( const DL_LeaderData& ) override
501 {
502 ReportMsg( _( "DXF dimensions not currently supported." ) );
503 }
504 virtual void addLeaderVertex( const DL_LeaderVertexData& ) override { }
505
506 virtual void addHatch( const DL_HatchData& ) override
507 {
508 ReportMsg( _( "DXF hatches not currently supported." ) );
509 }
510 virtual void addHatchLoop( const DL_HatchLoopData& ) override { }
511 virtual void addHatchEdge( const DL_HatchEdgeData& ) override { }
512
513 virtual void addTrace( const DL_TraceData& ) override
514 {
515 ReportMsg( _( "DXF traces not currently supported." ) );
516 }
517
518 virtual void add3dFace( const DL_3dFaceData& ) override
519 {
520 ReportMsg( _( "DXF 3dfaces not currently supported." ) );
521 }
522
523 virtual void addSolid( const DL_SolidData& ) override
524 {
525 ReportMsg( _( "DXF solids not currently supported." ) );
526 }
527
528 virtual void addImage( const DL_ImageData& ) override
529 {
530 ReportMsg( _( "DXF images not currently supported." ) );
531 }
532 virtual void linkImage( const DL_ImageDefData& ) override {}
533
534 // Eat these silently
535 virtual void addAttribute( const DL_AttributeData& ) override { }
536 virtual void addXRecord( const std::string& ) override { }
537 virtual void addXRecordString( int, const std::string& ) override { }
538 virtual void addXRecordReal( int, double ) override { }
539 virtual void addXRecordInt( int, int ) override { }
540 virtual void addXRecordBool( int, bool ) override { }
541 virtual void addXDataApp( const std::string& ) override { }
542 virtual void addXDataString( int, const std::string& ) override { }
543 virtual void addXDataReal( int, double ) override { }
544 virtual void addXDataInt( int, int ) override { }
545
554 static wxString toDxfString( const wxString& aStr );
555
559 static wxString toNativeString( const wxString& aData );
560
561 void writeLine();
563
564private:
565 double m_xOffset; // X coord offset for conversion (in mm)
566 double m_yOffset; // Y coord offset for conversion (in mm)
567 double m_defaultThickness; // default line thickness for conversion (in mm)
568 int m_brdLayer; // The board layer to place imported DXF items
569 int m_version; // the dxf version, not used here
570 std::string m_codePage; // The code page, not used here
571 bool m_importAsFPShapes; // Use footprint items instead of board items when true.
572 // true when the items are imported in the footprint editor
573 wxString m_messages; // messages generated during dxf file parsing.
574 // Each message ends by '\n'
575 DXF2BRD_ENTITY_DATA m_curr_entity; // the current entity parameters when parsing a DXF entity
576
577 std::string m_mtextContent; // Contents of MText.
578
579 double m_minX, m_maxX; // handles image size in mm
580 double m_minY, m_maxY; // handles image size in mm
581
582 DXF_IMPORT_UNITS m_currentUnit; // current unit during import
583 int m_importCoordinatePrecision; // current precision for linear units (points)
584 int m_importAnglePrecision; // current precision for angles
585
587
588 // List of layers as we import, used just to grab props for objects.
589 std::vector<std::unique_ptr<DXF_IMPORT_LAYER>> m_layers;
590 std::vector<std::unique_ptr<DXF_IMPORT_BLOCK>> m_blocks; // List of blocks as we import
591 std::vector<std::unique_ptr<DXF_IMPORT_STYLE>> m_styles; // List of blocks as we import
593};
594
595#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:80
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:290
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.
bool LoadFromMemory(const wxMemoryBuffer &aMemBuffer) override
Set memory buffer with content for import.
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
std::string m_mtextContent
BOX2D GetImageBBox() const override
Return image bounding box from original imported file.
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
void ReportMsg(const wxString &aMessage) override
virtual void addXRecord(const std::string &) override
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 addMTextChunk(const std::string &text) override
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.
DXF_IMPORT_UNITS
DXF Units enum with values as specified in DXF 2012 Specification.
#define _(s)
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.