87#define MIN_BULGE 0.0218
89#define SCALE_FACTOR(x) (x)
107 std::unique_ptr<DXF_IMPORT_LAYER> layer0 =
109 m_layers.push_back( std::move( layer0 ) );
126 catch(
const std::bad_alloc& )
134 reportMsg(
_(
"Memory was exhausted trying to load the DXF, it may be too large." ) );
193 FILE* fp = wxFopen( aFile, wxT(
"rt" ) );
200 bool success = dxf_reader.in( fp,
this );
257 wxString
name = wxString::FromUTF8( aData.name.c_str() );
259 int lw = attributes.getWidth();
266 std::unique_ptr<DXF_IMPORT_LAYER> layer = std::make_unique<DXF_IMPORT_LAYER>(
name, lw );
268 m_layers.push_back( std::move( layer ) );
276 wxString description =
FROM_UTF8( data.description.c_str() );
303 wxString layerName = wxString::FromUTF8( aLayerName.c_str() );
305 if( !layerName.IsEmpty() )
308 [layerName](
const auto& it )
310 return it->m_layerName == layerName;
314 layer = resultIt->get();
324 wxString blockName = wxString::FromUTF8( aBlockName.c_str() );
326 if( !blockName.IsEmpty() )
329 [blockName](
const auto& it )
331 return it->m_name == blockName;
335 block = resultIt->get();
345 wxString styleName = wxString::FromUTF8( aStyleName.c_str() );
347 if( !styleName.IsEmpty() )
350 [styleName](
const auto& it ) { return it->m_name == styleName; } );
353 style = resultIt->get();
370 bufferToUse->
AddLine( start, end, lineWidth );
402 if( aData.startWidth > 0.0 )
403 lineWidth = aData.startWidth / 100.0;
404 else if ( aData.endWidth > 0.0 )
405 lineWidth = aData.endWidth / 100.0;
407 const DL_VertexData* vertex = &aData;
466 wxString
name = wxString::FromUTF8( aData.name.c_str() );
468 std::unique_ptr<DXF_IMPORT_BLOCK> block =
469 std::make_unique<DXF_IMPORT_BLOCK>(
name, aData.bpx, aData.bpy );
471 m_blocks.push_back( std::move( block ) );
486 if( block ==
nullptr )
505 std::unique_ptr<IMPORTED_SHAPE> newShape = shape->clone();
507 newShape->Transform( trans, translation );
525 bufferToUse->
AddCircle( center,
mapDim( aData.radius ), lineWidth, false );
547 VECTOR2D startPoint( aData.radius, 0.0 );
550 mapY( startPoint.
y + centerCoords.
y ) );
563 bufferToUse->
AddArc( center, arcStart,
angle, lineWidth );
594 if( startAngle > endAngle )
599 if( aData.ratio == 1.0 )
603 if( startAngle == endAngle )
605 DL_CircleData circle( aData.cx, aData.cy, aData.cz, radius );
611 DL_ArcData arc( aData.cx, aData.cy, aData.cz, radius,
618 std::vector<BEZIER<double>> splines;
619 ELLIPSE<double> ellipse( center, major, aData.ratio, startAngle, endAngle );
630 bufferToUse->
AddSpline( b.Start, b.C1, b.C2, b.End, lineWidth );
643 std::isnan( aData.apy ) ? 0 : aData.apy,
644 std::isnan( aData.apz ) ? 0 : aData.apz ) );
649 if( aData.vJustification != 0 || aData.hJustification != 0 || aData.hJustification == 4 )
651 if( aData.hJustification != 3 && aData.hJustification != 5 )
663 double textHeight =
mapDim( aData.height );
665 double charWidth = textHeight * 0.9;
667 if( style !=
nullptr )
670 double textWidth = charWidth *
text.length();
671 double textThickness = textHeight/8.0;
681 switch( aData.vJustification )
687 topLeft.
y = textHeight;
688 topRight.
y = textHeight;
694 bottomRight.
y = -textHeight / 2.0;
695 bottomLeft.
y = -textHeight / 2.0;
696 topLeft.
y = textHeight / 2.0;
697 topRight.
y = textHeight / 2.0;
703 bottomLeft.
y = -textHeight;
704 bottomRight.
y = -textHeight;
708 switch( aData.hJustification )
715 bottomRight.
x = textWidth;
716 topRight.
x = textWidth;
723 bottomLeft.
x = -textWidth / 2.0;
724 topLeft.
x = -textWidth / 2.0;
725 bottomRight.
x = textWidth / 2.0;
726 topRight.
x = textWidth / 2.0;
732 bottomLeft.
x = -textWidth;
733 topLeft.
x = -textWidth;
738 wxString sty = wxString::FromUTF8( aData.style.c_str() );
741 if( aData.textgen == 2 )
744 }
else if( aData.textgen == 4 )
754 double angle_degree = aData.angle * 180 / M_PI;
757 double angleInRads = angle_degree * M_PI / 180.0;
758 double cosine = cos(angleInRads);
759 double sine = sin(angleInRads);
763 bufferToUse->
AddText( refPoint,
text, textHeight, charWidth, textThickness, angle_degree,
764 hJustify, vJustify );
767 bottomLeft.
x = bottomLeft.
x * cosine - bottomLeft.
y * sine;
768 bottomLeft.
y = bottomLeft.
x * sine + bottomLeft.
y * cosine;
770 bottomRight.
x = bottomRight.
x * cosine - bottomRight.
y * sine;
771 bottomRight.
y = bottomRight.
x * sine + bottomRight.
y * cosine;
773 topLeft.
x = topLeft.
x * cosine - topLeft.
y * sine;
774 topLeft.
y = topLeft.
x * sine + topLeft.
y * cosine;
776 topRight.
x = topRight.
x * cosine - topRight.
y * sine;
777 topRight.
y = topRight.
x * sine + topRight.
y * cosine;
779 bottomLeft += refPoint;
780 bottomRight += refPoint;
782 topRight += refPoint;
795 wxString attrib, tmp;
799 double textHeight =
mapDim( aData.height );
802 double charWidth = textHeight * 0.9;
803 if( style !=
nullptr )
806 double textWidth = charWidth *
text.length();
807 double textThickness = textHeight/8.0;
832 while(
text.StartsWith( wxT(
"\\" ) ) )
834 attrib <<
text.BeforeFirst(
';' );
835 tmp =
text.AfterFirst(
';' );
848 if( aData.attachmentPoint <= 3 )
852 bottomLeft.
y = -textHeight;
853 bottomRight.
y = -textHeight;
855 else if( aData.attachmentPoint <= 6 )
859 bottomRight.
y = -textHeight / 2.0;
860 bottomLeft.
y = -textHeight / 2.0;
861 topLeft.
y = textHeight / 2.0;
862 topRight.
y = textHeight / 2.0;
868 topLeft.
y = textHeight;
869 topRight.
y = textHeight;
872 if( aData.attachmentPoint % 3 == 1 )
876 bottomRight.
x = textWidth;
877 topRight.
x = textWidth;
879 else if( aData.attachmentPoint % 3 == 2 )
883 bottomLeft.
x = -textWidth / 2.0;
884 topLeft.
x = -textWidth / 2.0;
885 bottomRight.
x = textWidth / 2.0;
886 topRight.
x = textWidth / 2.0;
892 bottomLeft.
x = -textWidth;
893 topLeft.
x = -textWidth;
897 if( data.alignH == 1 )
901 else if( data.alignH == 3 )
910 if( aData.alignV == 1 )
922 double angle_degree = aData.angle * 180/M_PI;
925 double angleInRads = angle_degree * M_PI / 180.0;
926 double cosine = cos(angleInRads);
927 double sine = sin(angleInRads);
932 bufferToUse->
AddText( textpos,
text, textHeight, charWidth,
933 textThickness, angle_degree, hJustify, vJustify );
935 bottomLeft.
x = bottomLeft.
x * cosine - bottomLeft.
y * sine;
936 bottomLeft.
y = bottomLeft.
x * sine + bottomLeft.
y * cosine;
938 bottomRight.
x = bottomRight.
x * cosine - bottomRight.
y * sine;
939 bottomRight.
y = bottomRight.
x * sine + bottomRight.
y * cosine;
941 topLeft.
x = topLeft.
x * cosine - topLeft.
y * sine;
942 topLeft.
y = topLeft.
x * sine + topLeft.
y * cosine;
944 topRight.
x = topRight.
x * cosine - topRight.
y * sine;
945 topRight.
y = topRight.
x * sine + topRight.
y * cosine;
947 bottomLeft += textpos;
948 bottomRight += textpos;
1037 if( key ==
"$DWGCODEPAGE" )
1043 if( key ==
"$AUPREC" )
1049 if( key ==
"$LUPREC" )
1055 if( key ==
"$INSUNITS" )
1139 for(
unsigned i = 0; i<aStr.length(); ++i )
1143 if( c > 175 || c < 11 )
1145 res.append( aStr.Mid( j, i - j ) );
1151 res += wxT(
"\\P" );
1161 res += wxT(
"%%C" );
1166 res += wxT(
"%%D" );
1171 res += wxT(
"%%P" );
1183 res.append( aStr.Mid( j ) );
1195 for(
unsigned i = 0; i < aData.length(); ++i )
1197 if( aData[ i ] == 0x7B )
1199 if( aData[ i + 1 ] == 0x5c && aData[ i + 2 ] == 0x66 )
1202 res.append( aData.Mid( j, i - j ) );
1205 for(
unsigned k = i + 3; k < aData.length(); ++k )
1207 if( aData[ k ] == 0x3B )
1215 for(
unsigned k = i; k < aData.length(); ++k )
1217 if( aData[ k ] == 0x7D )
1219 res.append( aData.Mid( i, k - i ) );
1228 res.append( aData.Mid( j ) );
1233 regexp.Compile( wxT(
"\\\\P" ) );
1234 regexp.Replace( &
res, wxT(
"\n" ) );
1237 regexp.Compile( wxT(
"\\\\~" ) );
1238 regexp.Replace( &
res, wxT(
" " ) );
1241 regexp.Compile( wxT(
"%%[cC]" ) );
1244 regexp.Replace( &
res, wxChar( 0xD8 ) );
1247 regexp.Replace( &
res, wxChar( 0x2205 ) );
1251 regexp.Compile( wxT(
"%%[dD]" ) );
1252 regexp.Replace( &
res, wxChar( 0x00B0 ) );
1254 regexp.Compile( wxT(
"%%[pP]" ) );
1255 regexp.Replace( &
res, wxChar( 0x00B1 ) );
1264 wxString
name = wxString::FromUTF8( aData.name.c_str() );
1266 std::unique_ptr<DXF_IMPORT_STYLE> style =
1267 std::make_unique<DXF_IMPORT_STYLE>(
name, aData.fixedTextHeight, aData.widthFactor, aData.bold, aData.italic );
1269 m_styles.push_back( std::move( style ) );
1284 double lineWidth = 0.0001;
1285 double thickness =
mapDim( std::max( aData.thickness, 0.01 ) );
1289 bufferToUse->
AddCircle( center, thickness, lineWidth,
true );
1299 const VECTOR2D& aSegEnd,
double aWidth )
1306 bufferToUse->
AddLine( origin, end, aWidth );
1314 double aBulge,
double aWidth )
1320 if( aBulge < -2000.0 )
1322 else if( aBulge > 2000.0 )
1325 double ang = 4.0 * atan( aBulge );
1331 double offAng = atan2( ep.
y - sp.
y, ep.
x - sp.
x );
1333 double d = 0.5 * sqrt( (sp.
x - ep.
x) * (sp.
x - ep.
x) + (sp.
y - ep.
y) * (sp.
y - ep.
y) );
1335 double xm = ( sp.
x + ep.
x ) * 0.5;
1336 double ym = ( sp.
y + ep.
y ) * 0.5;
1337 double radius = d / sin( ang * 0.5 );
1343 double dh2 = radius * radius - d * d;
1349 double h = sqrt( dh2 );
1361 else if( ang > M_PI )
1365 double cx = h * cos( offAng ) + xm;
1366 double cy = h * sin( offAng ) + ym;
1383 bufferToUse->
AddArc( center, arc_start,
angle, aWidth );
1393#include "tinysplinecxx.h"
1398 wxLogMessage(
"spl deg %d kn %d ctr %d fit %d",
1414 for(
unsigned int ii = 1; ii < imax; ++ii )
1419 if( startpoint != endpoint )
1426 startpoint = endpoint;
1430 std::vector<double> ctrlp;
1432 for(
unsigned ii = 0; ii < imax; ++ii )
1438 std::vector<double> coords;
1439 tinyspline::BSpline beziers;
1445 dxfspline.setControlPoints( ctrlp );
1447 beziers = tinyspline::BSpline( dxfspline.toBeziers() );
1449 coords = beziers.controlPoints();
1451 catch(
const std::runtime_error& )
1454 reportMsg(
_(
"Invalid spline definition encountered" ) );
1458 size_t order = beziers.order();
1459 size_t dim = beziers.dimension();
1460 size_t numBeziers = ( coords.size() / dim ) / order;
1462 for(
size_t i = 0; i < numBeziers; i++ )
1464 size_t ii = i * dim * order;
1471 if( ii + 4 >= coords.size() )
1473 bezierControl2 = bezierControl1;
1477 bezierControl2 =
VECTOR2D(
mapX( coords[ii + 4] ),
mapY( coords[ii + 5] ) );
1481 if( ii + 6 >= coords.size() )
1483 end = bezierControl2;
1492 bufferToUse->
AddSpline( start, bezierControl1, bezierControl2, end, aWidth );
1512 double direction[3];
1513 aData->getDirection( direction );
1517 if( (
abs( arbZ.
x ) < ( 1.0 / 64.0 ) ) && (
abs( arbZ.
y ) < ( 1.0 / 64.0 ) ) )
1534 return arbitraryAxis * point;
1544 MATRIX3x3 world( worldX, worldY, worldZ );
1546 return world * point;
void TransformEllipseToBeziers(const ELLIPSE< T > &aEllipse, std::vector< BEZIER< T > > &aBeziers)
Transforms an ellipse or elliptical arc into a set of quadratic Bezier curves that approximate it.
Generic cubic Bezier representation.
double m_SplineTangentEndY
unsigned int m_SplineDegree
std::vector< VECTOR2D > m_SplineFitPointList
unsigned int m_SplineFitCount
double m_SplineTangentEndX
std::vector< SPLINE_CTRL_POINT > m_SplineControlPointList
double m_SplineTangentStartY
unsigned int m_SplineKnotsCount
VECTOR2D m_LastCoordinate
unsigned int m_SplineControlCount
double m_SplineTangentStartX
std::vector< double > m_SplineKnotsList
A helper class to hold layer settings temporarily during import.
GRAPHICS_IMPORTER_BUFFER m_buffer
A helper class to hold layer settings temporarily during import.
DXF_IMPORT_UNITS m_currentUnit
virtual void addPolyline(const DL_PolylineData &aData) override
double mapY(double aDxfCoordY)
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 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 addLine(const DL_LineData &aData) 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)
DXF_IMPORT_BLOCK * getImportBlock(const std::string &aBlockName)
Return the import layer block.
double getCurrentUnitScale()
virtual void addBlock(const DL_BlockData &) override
Called for each BLOCK in the DXF file.
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.
DXF_IMPORT_STYLE * getImportStyle(const std::string &aStyleName)
Return the import style.
virtual void addInsert(const DL_InsertData &aData) override
MATRIX3x3D getArbitraryAxis(DL_Extrusion *aData)
double GetImageWidth() const override
Return image width from original imported file.
double GetImageHeight() const override
Return image height from original imported file.
void updateImageLimits(const VECTOR2D &aPoint)
virtual void addTextStyle(const DL_StyleData &aData) 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.
bool ImportDxfFile(const wxString &aFile)
Implementation of the method used for communicate with this filter.
virtual void addArc(const DL_ArcData &aData) 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
double mapDim(double aDxfValue)
void reportMsg(const wxString &aMessage)
int m_importCoordinatePrecision
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
bool Import() override
Actually imports the file.
int m_importAnglePrecision
virtual void endEntity() override
virtual void addControlPoint(const DL_ControlPointData &aData) override
Called for every spline control point.
double m_defaultThickness
GRAPHICS_IMPORTER_BUFFER m_internalImporter
std::vector< std::unique_ptr< DXF_IMPORT_STYLE > > m_styles
bool Load(const wxString &aFileName) override
Load file for import.
DXF_IMPORT_BLOCK * m_currentBlock
DXF2BRD_ENTITY_DATA m_curr_entity
virtual void SetImporter(GRAPHICS_IMPORTER *aImporter) override
Set the receiver of the imported shapes.
virtual void addLinetype(const DL_LinetypeData &data) override
A helper class to hold style settings temporarily during import.
std::list< std::unique_ptr< IMPORTED_SHAPE > > & GetShapes()
void AddCircle(const VECTOR2D &aCenter, double aRadius, double aWidth, bool aFilled) override
Create an object representing a circle.
void AddSpline(const VECTOR2D &aStart, const VECTOR2D &BezierControl1, const VECTOR2D &BezierControl2, const VECTOR2D &aEnd, double aWidth) override
Create an object representing an arc.
void ImportTo(GRAPHICS_IMPORTER &aImporter)
void AddArc(const VECTOR2D &aCenter, const VECTOR2D &aStart, const EDA_ANGLE &aAngle, double aWidth) override
Create an object representing an arc.
void AddText(const VECTOR2D &aOrigin, const wxString &aText, double aHeight, double aWidth, double aThickness, double aOrientation, GR_TEXT_H_ALIGN_T aHJustify, GR_TEXT_V_ALIGN_T aVJustify) override
Create an object representing a text.
void AddShape(std::unique_ptr< IMPORTED_SHAPE > &aShape)
void AddLine(const VECTOR2D &aStart, const VECTOR2D &aEnd, double aWidth) override
Create an object representing a line segment.
Interface that creates objects representing shapes for a given data model.
double GetLineWidthMM() const
Return the line width used for importing the outlines (in mm).
GRAPHICS_IMPORTER * m_importer
< Importer used to create objects representing the imported shapes.
virtual void SetImporter(GRAPHICS_IMPORTER *aImporter)
Set the receiver of the imported shapes.
void SetRotation(T aAngle)
Set the rotation components of the matrix.
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
VECTOR3< T > Normalize()
Compute the normalized vector.
VECTOR3< T > Cross(const VECTOR3< T > &aVector) const
Compute cross product of self with aVector.
#define DXF_IMPORT_LINEWEIGHT_BY_LW_DEFAULT
#define DXF_IMPORT_LINEWEIGHT_BY_LAYER
static constexpr EDA_ANGLE & ANGLE_360
static constexpr EDA_ANGLE & ANGLE_0
This file contains miscellaneous commonly used macros and functions.
static wxString FROM_UTF8(const char *cstring)
Convert a UTF8 encoded C string to a wxString for all wxWidgets build modes.
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
VECTOR2< double > VECTOR2D
VECTOR3< double > VECTOR3D