replace lib_dxf by dxflib from qcad in dxf2idf code

This commit is contained in:
jean-pierre charras 2018-07-08 20:12:06 +02:00
parent 72d1597201
commit 7291059489
4 changed files with 184 additions and 220 deletions

View File

@ -282,7 +282,7 @@ void DXF2BRD_CONVERTER::addCircle( const DL_CircleData& aData )
/*
* Import Arc entities.
*/
void DXF2BRD_CONVERTER::addArc( const DL_ArcData& data )
void DXF2BRD_CONVERTER::addArc( const DL_ArcData& aData )
{
DRAWSEGMENT* segm = ( m_importAsfootprintGraphicItems ) ?
static_cast< DRAWSEGMENT* >( new EDGE_MODULE( NULL ) ) : new DRAWSEGMENT;
@ -291,18 +291,18 @@ void DXF2BRD_CONVERTER::addArc( const DL_ArcData& data )
segm->SetShape( S_ARC );
// Init arc centre:
wxPoint center( mapX( data.cx ), mapY( data.cy ) );
wxPoint center( mapX( aData.cx ), mapY( aData.cy ) );
segm->SetCenter( center );
// Init arc start point
double arcStartx = data.radius;
double arcStartx = aData.radius;
double arcStarty = 0;
double startangle = data.angle1;
double endangle = data.angle2;
double startangle = aData.angle1;
double endangle = aData.angle2;
RotatePoint( &arcStartx, &arcStarty, -RAD2DECIDEG( startangle ) );
wxPoint arcStart( mapX( arcStartx + data.cx ),
mapY( arcStarty + data.cy ) );
wxPoint arcStart( mapX( arcStartx + aData.cx ),
mapY( arcStarty + aData.cy ) );
segm->SetArcStart( arcStart );
// calculate arc angle (arcs are CCW, and should be < 0 in Pcbnew)

View File

@ -1,6 +1,6 @@
include_directories(
"${CMAKE_SOURCE_DIR}/include"
"${CMAKE_SOURCE_DIR}/lib_dxf"
"${CMAKE_SOURCE_DIR}/dxflib_qcad"
"${CMAKE_SOURCE_DIR}/utils/idftools"
${OPENGL_INCLUDE_DIR}
${Boost_INCLUDE_DIR}
@ -26,10 +26,10 @@ add_library( idf3 STATIC ${IDF3_FILES} )
add_executable( idfcyl idf_cylinder.cpp )
add_executable( idfrect idf_rect.cpp )
#add_executable( dxf2idf dxf2idfmain.cpp dxf2idf.cpp )
add_executable( dxf2idf dxf2idfmain.cpp dxf2idf.cpp )
add_executable( idf2vrml idf2vrml.cpp )
#target_link_libraries( dxf2idf lib_dxf idf3 ${wxWidgets_LIBRARIES} )
target_link_libraries( dxf2idf lib_dxf idf3 ${wxWidgets_LIBRARIES} )
target_link_libraries( idf2vrml idf3 common ${OPENGL_LIBRARIES} ${wxWidgets_LIBRARIES} )
@ -40,7 +40,7 @@ if( APPLE )
)
else()
install( TARGETS idfcyl idfrect idf2vrml
#dxf2idf
dxf2idf
DESTINATION ${KICAD_BIN}
COMPONENT binary )
endif()

View File

@ -2,6 +2,7 @@
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2014 Cirilo Bernardo
* Copyright (C) 2018 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -23,7 +24,6 @@
#include <cstdio>
#include <iostream>
#include <libdxfrw.h>
#include <dxf2idf.h>
// differences in angle smaller than MIN_ANG are considered equal
@ -51,40 +51,38 @@ DXF2IDF::~DXF2IDF()
bool DXF2IDF::ReadDxf( const std::string& aFile )
{
dxfRW* reader = new dxfRW( aFile.c_str() );
DL_Dxf dxf_reader;
bool success = true;
if( !reader )
return false;
if( !dxf_reader.in( aFile, this ) ) // if file open failed
success = false;
bool success = reader->read( this, true );
delete reader;
return success;
}
void DXF2IDF::addLine( const DRW_Line& data )
void DXF2IDF::addLine( const DL_LineData& aData )
{
IDF_POINT p1, p2;
p1.x = data.basePoint.x * m_scale;
p1.y = data.basePoint.y * m_scale;
p2.x = data.secPoint.x * m_scale;
p2.y = data.secPoint.y * m_scale;
p1.x = aData.x1 * m_scale;
p1.y = aData.y1 * m_scale;
p2.x = aData.x2 * m_scale;
p2.y = aData.y2 * m_scale;
insertLine( p1, p2 );
return;
}
void DXF2IDF::addCircle( const DRW_Circle& data )
void DXF2IDF::addCircle( const DL_CircleData& aData )
{
IDF_POINT p1, p2;
p1.x = data.basePoint.x * m_scale;
p1.y = data.basePoint.y * m_scale;
p1.x = aData.cx * m_scale;
p1.y = aData.cy * m_scale;
p2.x = p1.x + data.radious * m_scale;
p2.x = p1.x + aData.radius * m_scale;
p2.y = p1.y;
IDF_SEGMENT* seg = new IDF_SEGMENT( p1, p2, 360, true );
@ -96,23 +94,23 @@ void DXF2IDF::addCircle( const DRW_Circle& data )
}
void DXF2IDF::addArc( const DRW_Arc& data )
void DXF2IDF::addArc( const DL_ArcData& aData )
{
IDF_POINT p1, p2;
p1.x = data.basePoint.x * m_scale;
p1.y = data.basePoint.y * m_scale;
p1.x = aData.cx * m_scale;
p1.y = aData.cy * m_scale;
// note: DXF circles always run CCW
double ea = data.endangle;
double ea = aData.angle2;
while( ea < data.staangle )
while( ea < aData.angle1 )
ea += M_PI;
p2.x = p1.x + cos( data.staangle ) * data.radious * m_scale;
p2.y = p1.y + sin( data.staangle ) * data.radious * m_scale;
p2.x = p1.x + cos( aData.angle1 ) * aData.radius * m_scale;
p2.y = p1.y + sin( aData.angle1 ) * aData.radius * m_scale;
double angle = ( ea - data.staangle ) * 180.0 / M_PI;
double angle = ( ea - aData.angle1 ) * 180.0 / M_PI;
IDF_SEGMENT* seg = new IDF_SEGMENT( p1, p2, angle, true );
@ -276,158 +274,140 @@ bool DXF2IDF::WriteOutline( FILE* aFile, bool isInch )
return true;
}
void DXF2IDF::addHeader( const DRW_Header* data )
void DXF2IDF::setVariableInt( const std::string& key, int value, int code )
{
std::map<std::string, DRW_Variant*>::const_iterator it;
// Called for every int variable in the DXF file (e.g. "$INSUNITS").
for( it = data->vars.begin(); it != data->vars.end(); ++it )
if( key == "$INSUNITS" ) // Drawing units
{
std::string key = ( (*it).first ).c_str();
if( key == "$INSUNITS" )
switch( value )
{
DRW_Variant* var = (*it).second;
case 1: // inches
m_scale = 25.4;
break;
switch( var->content.i )
{
case 1: // inches
m_scale = 25.4;
break;
case 2: // feet
m_scale = 304.8;
break;
case 2: // feet
m_scale = 304.8;
break;
case 4: // mm
m_scale = 1.0;
break;
case 5: // centimeters
m_scale = 10.0;
break;
case 5: // centimeters
m_scale = 10.0;
break;
case 6: // meters
m_scale = 1000.0;
break;
case 6: // meters
m_scale = 1000.0;
break;
case 8: // microinches
m_scale = 2.54e-5;
break;
case 8: // microinches
m_scale = 2.54e-5;
break;
case 9: // mils
m_scale = 0.0254;
break;
case 9: // mils
m_scale = 0.0254;
break;
case 10: // yards
m_scale = 914.4;
break;
case 10: // yards
m_scale = 914.4;
break;
case 11: // Angstroms
m_scale = 1.0e-7;
break;
case 11: // Angstroms
m_scale = 1.0e-7;
break;
case 12: // nanometers
m_scale = 1.0e-6;
break;
case 12: // nanometers
m_scale = 1.0e-6;
break;
case 13: // micrometers
m_scale = 1.0e-3;
break;
case 13: // micrometers
m_scale = 1.0e-3;
break;
case 14: // decimeters
m_scale = 100.0;
break;
case 14: // decimeters
m_scale = 100.0;
break;
default:
// use the default of 1.0 for:
// 0: Unspecified Units
// 4: mm
// 3: miles
// 7: kilometers
// 15: decameters
// 16: hectometers
// 17: gigameters
// 18: AU
// 19: lightyears
// 20: parsecs
break;
}
default:
// use the default of 1.0 for:
// 0: Unspecified Units
// 3: miles
// 7: kilometers
// 15: decameters
// 16: hectometers
// 17: gigameters
// 18: AU
// 19: lightyears
// 20: parsecs
m_scale = 1.0;
break;
}
return;
}
}
void DXF2IDF::addPolyline(const DL_PolylineData& aData )
{
// Convert DXF Polylines into a series of Lines and Arcs.
// A Polyline (as opposed to a LWPolyline) may be a 3D line or
// even a 3D Mesh. The only type of Polyline which is guaranteed
// to import correctly is a 2D Polyline in X and Y, which is what
// we assume of all Polylines. The width used is the width of the Polyline.
// per-vertex line widths, if present, are ignored.
m_entityParseStatus = 1;
m_entity_flags = aData.flags;
m_entityType = DL_ENTITY_POLYLINE;
}
void DXF2IDF::addVertex( const DL_VertexData& aData )
{
if( m_entityParseStatus == 0 )
return; // Error
if( m_entityParseStatus == 1 ) // This is the first vertex of an entity
{
m_lastCoordinate.x = aData.x * m_scale;
m_lastCoordinate.y = aData.y * m_scale;
m_polylineStart = m_lastCoordinate;
m_bulgeVertex = aData.bulge;
m_entityParseStatus = 2;
return;
}
IDF_POINT seg_end;
seg_end.x = aData.x * m_scale;
seg_end.y = aData.y * m_scale;
insertLine( m_lastCoordinate, seg_end );
m_lastCoordinate = seg_end;
}
void DXF2IDF::endEntity()
{
if( m_entityType == DL_ENTITY_POLYLINE )
{
// Polyline flags bit 0 indicates closed (1) or open (0) polyline
if( m_entity_flags & 1 )
{
if( std::abs( m_bulgeVertex ) < MIN_BULGE )
insertLine( m_lastCoordinate, m_polylineStart );
else
insertArc( m_lastCoordinate, m_polylineStart, m_bulgeVertex );
}
}
m_entityType = 0 ;
m_entity_flags = 0;
m_entityParseStatus = 0;
}
void DXF2IDF::addLWPolyline(const DRW_LWPolyline& data )
{
IDF_POINT poly_start;
IDF_POINT seg_start;
IDF_POINT seg_end;
double bulge = 0.0;
if( !data.vertlist.empty() )
{
DRW_Vertex2D* vertex = data.vertlist[0];
seg_start.x = vertex->x * m_scale;
seg_start.y = vertex->y * m_scale;
poly_start = seg_start;
bulge = vertex->bulge;
}
for( size_t i = 1; i < data.vertlist.size(); ++i )
{
DRW_Vertex2D* vertex = data.vertlist[i];
seg_end.x = vertex->x * m_scale;
seg_end.y = vertex->y * m_scale;
if( std::abs( bulge ) < MIN_BULGE )
insertLine( seg_start, seg_end );
else
insertArc( seg_start, seg_end, bulge );
seg_start = seg_end;
bulge = vertex->bulge;
}
// Polyline flags bit 0 indicates closed (1) or open (0) polyline
if( data.flags & 1 )
{
if( std::abs( bulge ) < MIN_BULGE )
insertLine( seg_start, poly_start );
else
insertArc( seg_start, poly_start, bulge );
}
return;
}
void DXF2IDF::addPolyline(const DRW_Polyline& data )
{
IDF_POINT poly_start;
IDF_POINT seg_start;
IDF_POINT seg_end;
if( !data.vertlist.empty() )
{
DRW_Vertex* vertex = data.vertlist[0];
seg_start.x = vertex->basePoint.x * m_scale;
seg_start.y = vertex->basePoint.y * m_scale;
poly_start = seg_start;
}
for( size_t i = 1; i < data.vertlist.size(); ++i )
{
DRW_Vertex* vertex = data.vertlist[i];
seg_end.x = vertex->basePoint.x * m_scale;
seg_end.y = vertex->basePoint.y * m_scale;
insertLine( seg_start, seg_end );
seg_start = seg_end;
}
// Polyline flags bit 0 indicates closed (1) or open (0) polyline
if( data.flags & 1 )
insertLine( seg_start, poly_start );
return;
}
void DXF2IDF::insertLine( const IDF_POINT& aSegStart, const IDF_POINT& aSegEnd )
{

View File

@ -2,6 +2,7 @@
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2014 Cirilo Bernardo
* Copyright (C) 2018 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -25,20 +26,31 @@
#define DXF2IDF_H
#include <string>
#include <drw_interface.h>
#include "dl_dxf.h"
#include "dl_creationadapter.h"
#include <idf_common.h>
class DXF2IDF : public DRW_Interface
class DXF2IDF : public DL_CreationAdapter
{
private:
std::list< IDF_SEGMENT* > lines; // Unsorted list of graphical segments
double m_scale; // scaling factor to mm
int m_entityType; // the DXF type of entity
int m_entityParseStatus; // Inside a entity: status od parsing:
// 0 = no entity
// 1 = first item of entity
// 2 = entity in progress
int m_entity_flags; // state of flags read from last entity
IDF_POINT m_lastCoordinate; // the last vertex coordinate read (unit = mm)
IDF_POINT m_polylineStart; // The first point of the polyline entity, when reading a polyline (unit = mm)
double m_bulgeVertex; // the last vertex bulge value read
void insertLine( const IDF_POINT& aSegStart, const IDF_POINT& aSegEnd );
void insertArc( const IDF_POINT& aSegStart, const IDF_POINT& aSegEnd, double aBulge );
public:
DXF2IDF() : m_scale( 1.0 ) {};
DXF2IDF() : m_scale( 1.0 ), m_entityType( 0 ), m_entityParseStatus( 0 ),
m_entity_flags( 0 ), m_bulgeVertex( 0.0 ) {};
~DXF2IDF();
bool ReadDxf( const std::string& aFile );
@ -46,58 +58,30 @@ public:
private:
// DRW_Interface implemented callback functions
virtual void addHeader( const DRW_Header* data ) override;
virtual void addLine(const DRW_Line& data) override;
virtual void addArc(const DRW_Arc& data ) override;
virtual void addCircle(const DRW_Circle& data ) override;
virtual void addLWPolyline(const DRW_LWPolyline& data ) override;
virtual void addPolyline(const DRW_Polyline& data ) override;
virtual void addLine( const DL_LineData& aData ) override;
virtual void addArc( const DL_ArcData& aData ) override;
virtual void addCircle( const DL_CircleData& aData ) override;
virtual void addPolyline( const DL_PolylineData& aData ) override;
/** Called for every polyline vertex */
virtual void addVertex( const DL_VertexData& aData ) override;
// DRW_Interface callbacks unsupported by DXF2IDF
virtual void addLType( const DRW_LType& data ) override {}
virtual void addLayer( const DRW_Layer& data ) override {}
virtual void addDimStyle( const DRW_Dimstyle& data ) override {}
virtual void addVport(const DRW_Vport& data) override {}
virtual void addTextStyle(const DRW_Textstyle& data) override {}
virtual void addBlock(const DRW_Block& data ) override {}
virtual void setBlock(const int handle) override {}
virtual void endBlock() override {}
virtual void addPoint(const DRW_Point& data ) override {}
virtual void addRay(const DRW_Ray& data ) override {}
virtual void addXline(const DRW_Xline& data ) override {}
virtual void addEllipse(const DRW_Ellipse& data ) override {}
virtual void addSpline(const DRW_Spline* data ) override {}
virtual void addKnot(const DRW_Entity&) override {}
virtual void addInsert(const DRW_Insert& data ) override {}
virtual void addTrace(const DRW_Trace& data ) override {}
virtual void add3dFace(const DRW_3Dface& data ) override {}
virtual void addSolid(const DRW_Solid& data ) override {}
virtual void addMText(const DRW_MText& data) override {}
virtual void addText(const DRW_Text& data ) override {}
virtual void addDimAlign(const DRW_DimAligned *data ) override {}
virtual void addDimLinear(const DRW_DimLinear *data ) override {}
virtual void addDimRadial(const DRW_DimRadial *data ) override {}
virtual void addDimDiametric(const DRW_DimDiametric *data ) override {}
virtual void addDimAngular(const DRW_DimAngular *data ) override {}
virtual void addDimAngular3P(const DRW_DimAngular3p *data ) override {}
virtual void addDimOrdinate(const DRW_DimOrdinate *data ) override {}
virtual void addLeader(const DRW_Leader *data ) override {}
virtual void addHatch(const DRW_Hatch* data ) override {}
virtual void addViewport(const DRW_Viewport& data) override {}
virtual void addImage(const DRW_Image* data ) override {}
virtual void linkImage(const DRW_ImageDef* data ) override {}
virtual void addComment(const char*) override {}
virtual void writeHeader(DRW_Header& data) override {}
virtual void writeBlocks() override {}
virtual void writeBlockRecords() override {}
virtual void writeEntities() override {}
virtual void writeLTypes() override {}
virtual void writeLayers() override {}
virtual void writeTextstyles() override {}
virtual void writeVports() override {}
virtual void writeDimstyles() override {}
virtual void addAppId( const DRW_AppId& data ) override {}
virtual void writeAppId() override {}
/**
* Called for every string variable in the DXF file (e.g. "$ACADVER").
*/
virtual void setVariableString( const std::string& key, const std::string& value,
int code ) override {};
/**
* Called for every int variable in the DXF file (e.g. "$ACADMAINTVER").
*/
virtual void setVariableInt( const std::string& key, int value, int code ) override;
/**
* Called for every double variable in the DXF file (e.g. "$DIMEXO").
*/
virtual void setVariableDouble( const std::string& key, double value, int code ) override {}
virtual void endEntity() override;
};
#endif // DXF2IDF_H