From 407a9519aaa57cbbe412d19be5576a35d9391b9c Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Wed, 21 Jun 2017 12:34:20 +0300 Subject: [PATCH] Basic implamentation has been finished. --HG-- branch : feature --- src/libs/vdxf/dxiface.cpp | 258 ++++++++++++++++ src/libs/vdxf/dxiface.h | 235 +++++++++++++++ src/libs/vdxf/libdxfrw/drw_base.h | 16 + src/libs/vdxf/libdxfrw/drw_interface.h | 71 +++-- src/libs/vdxf/libdxfrw/drw_objects.h | 6 + src/libs/vdxf/vdxf.pri | 6 +- src/libs/vdxf/vdxfengine.cpp | 396 +++++++++++-------------- src/libs/vdxf/vdxfengine.h | 9 +- 8 files changed, 740 insertions(+), 257 deletions(-) create mode 100644 src/libs/vdxf/dxiface.cpp create mode 100644 src/libs/vdxf/dxiface.h diff --git a/src/libs/vdxf/dxiface.cpp b/src/libs/vdxf/dxiface.cpp new file mode 100644 index 000000000..18670c0fe --- /dev/null +++ b/src/libs/vdxf/dxiface.cpp @@ -0,0 +1,258 @@ +/****************************************************************************** +** dwg2dxf - Program to convert dwg/dxf to dxf(ascii & binary) ** +** ** +** Copyright (C) 2015 José F. Soriano, rallazz@gmail.com ** +** ** +** This library is free software, licensed under the terms of the GNU ** +** General Public License as published by the Free Software Foundation, ** +** either version 2 of the License, or (at your option) any later version. ** +** You should have received a copy of the GNU General Public License ** +** along with this program. If not, see . ** +******************************************************************************/ + +#include "dxiface.h" +#include "libdxfrw/libdwgr.h" +#include "libdxfrw/libdxfrw.h" + +#include +#include +#include + +dx_iface::dx_iface(const std::string &file, VarMeasurement varMeasurement, VarInsunits varInsunits) + : dxfW(new dxfRW(file.c_str())), + cData() +{ + InitHeader(varMeasurement, varInsunits); + InitLTypes(); + InitLayers(); + InitDimstyles(); + InitTextstyles(); +} + +dx_iface::~dx_iface() +{ + delete dxfW; +} + +bool dx_iface::fileExport(DRW::Version v, bool binary) +{ + bool success = dxfW->write(this, v, binary); + return success; +} + +void dx_iface::writeEntity(DRW_Entity* e){ + switch (e->eType) { + case DRW::POINT: + dxfW->writePoint(static_cast(e)); + break; + case DRW::LINE: + dxfW->writeLine(static_cast(e)); + break; + case DRW::CIRCLE: + dxfW->writeCircle(static_cast(e)); + break; + case DRW::ARC: + dxfW->writeArc(static_cast(e)); + break; + case DRW::SOLID: + dxfW->writeSolid(static_cast(e)); + break; + case DRW::ELLIPSE: + dxfW->writeEllipse(static_cast(e)); + break; + case DRW::LWPOLYLINE: + dxfW->writeLWPolyline(static_cast(e)); + break; + case DRW::POLYLINE: + dxfW->writePolyline(static_cast(e)); + break; + case DRW::SPLINE: + dxfW->writeSpline(static_cast(e)); + break; +// case RS2::EntitySplinePoints: +// writeSplinePoints(static_cast(e)); +// break; +// case RS2::EntityVertex: +// break; + case DRW::INSERT: + dxfW->writeInsert(static_cast(e)); + break; + case DRW::MTEXT: + dxfW->writeMText(static_cast(e)); + break; + case DRW::TEXT: + dxfW->writeText(static_cast(e)); + break; + case DRW::DIMLINEAR: + case DRW::DIMALIGNED: + case DRW::DIMANGULAR: + case DRW::DIMANGULAR3P: + case DRW::DIMRADIAL: + case DRW::DIMDIAMETRIC: + case DRW::DIMORDINATE: + dxfW->writeDimension(static_cast(e)); + break; + case DRW::LEADER: + dxfW->writeLeader(static_cast(e)); + break; + case DRW::HATCH: + dxfW->writeHatch(static_cast(e)); + break; + case DRW::IMAGE: + dxfW->writeImage(static_cast(e), static_cast(e)->path); + break; + default: + break; + } +} + +void dx_iface::writeHeader(DRW_Header &data){ + //complete copy of header vars: + data = cData.headerC; + //or copy one by one: + // for (std::map::iterator it=cData->headerC.vars.begin(); it != cData->headerC.vars.end(); ++it) + // data.vars[it->first] = new DRW_Variant( *(it->second) ); +} + +void dx_iface::writeBlocks(){ + //write each block + for (std::list::iterator it=cData.blocks.begin(); it != cData.blocks.end(); ++it){ + dx_ifaceBlock* bk = *it; + dxfW->writeBlock(bk); + //and write each entity in block + for (std::list::const_iterator it=bk->ent.begin(); it!=bk->ent.end(); ++it) + writeEntity(*it); + } +} + +void dx_iface::writeBlockRecords(){ + for (std::list::iterator it=cData.blocks.begin(); it != cData.blocks.end(); ++it) + dxfW->writeBlockRecord((*it)->name); +} + +void dx_iface::writeEntities(){ + for (std::list::const_iterator it=cData.mBlock->ent.begin(); it!=cData.mBlock->ent.end(); ++it) + writeEntity(*it); +} + +void dx_iface::writeLTypes(){ + for (std::list::iterator it=cData.lineTypes.begin(); it != cData.lineTypes.end(); ++it) + dxfW->writeLineType(&(*it)); +} + +void dx_iface::writeLayers(){ + for (std::list::iterator it=cData.layers.begin(); it != cData.layers.end(); ++it) + dxfW->writeLayer(&(*it)); +} + +void dx_iface::writeTextstyles(){ + for (std::list::iterator it=cData.textStyles.begin(); it != cData.textStyles.end(); ++it) + dxfW->writeTextstyle(&(*it)); +} + +void dx_iface::writeVports(){ + for (std::list::iterator it=cData.VPorts.begin(); it != cData.VPorts.end(); ++it) + dxfW->writeVport(&(*it)); +} + +void dx_iface::writeDimstyles(){ + for (std::list::iterator it=cData.dimStyles.begin(); it != cData.dimStyles.end(); ++it) + dxfW->writeDimstyle(&(*it)); +} + +void dx_iface::writeAppId(){ + for (std::list::iterator it=cData.appIds.begin(); it != cData.appIds.end(); ++it) + dxfW->writeAppId(&(*it)); +} + +void dx_iface::InitHeader(VarMeasurement varMeasurement, VarInsunits varInsunits) +{ + cData.headerC.addComment("Valentina DXF File"); + + // 1 = Clockwise angles, 0 = Counterclockwise + cData.headerC.addInt("$ANGDIR", 0, 70);// Qt use counterclockwise + + // Sets drawing units: 0 = English; 1 = Metric + cData.headerC.addInt("$MEASUREMENT", static_cast(varMeasurement), 70); + cData.headerC.addInt("$INSUNITS", static_cast(varInsunits), 70); + cData.headerC.addDouble("$DIMSCALE", 1.0, 40); + + // Official documentation says that initial value is 1.0, however LibreCAD has trouble if not set this value + // explicitly. + cData.headerC.addDouble("$DIMLFAC", 1.0, 40); + + QString dateTime = QDateTime::currentDateTime().toString("yyyyMMdd.HHmmsszzz"); + dateTime.chop(1);// we need hundredths of a second + cData.headerC.addStr("$TDCREATE", dateTime.toStdString(), 40); +} + +void dx_iface::InitLTypes() +{ + cData.lineTypes.push_back(DRW_LType("ACAD_ISO02W100", "ACAD_ISO02W100")); + cData.lineTypes.push_back(DRW_LType("ACAD_ISO02W100", "ACAD_ISO02W100")); + cData.lineTypes.push_back(DRW_LType("ACAD_ISO03W100", "ACAD_ISO03W100")); + cData.lineTypes.push_back(DRW_LType("ACAD_ISO04W100", "ACAD_ISO04W100")); + cData.lineTypes.push_back(DRW_LType("ACAD_ISO05W100", "ACAD_ISO05W100")); + cData.lineTypes.push_back(DRW_LType("BORDER", "BORDER")); + cData.lineTypes.push_back(DRW_LType("BORDER2", "BORDER2")); + cData.lineTypes.push_back(DRW_LType("BORDERX2", "BORDERX2")); + cData.lineTypes.push_back(DRW_LType("CENTER", "CENTER")); + cData.lineTypes.push_back(DRW_LType("CENTER2", "CENTER2")); + cData.lineTypes.push_back(DRW_LType("CENTERX2", "CENTERX2")); + cData.lineTypes.push_back(DRW_LType("DASHDOT", "DASHDOT")); + cData.lineTypes.push_back(DRW_LType("DASHDOT2", "DASHDOT2")); + cData.lineTypes.push_back(DRW_LType("DASHDOTX2", "DASHDOTX2")); + cData.lineTypes.push_back(DRW_LType("DASHED", "DASHED")); + cData.lineTypes.push_back(DRW_LType("DASHED2", "DASHED2")); + cData.lineTypes.push_back(DRW_LType("DASHEDX2", "DASHEDX2")); + cData.lineTypes.push_back(DRW_LType("DIVIDE", "DIVIDE")); + cData.lineTypes.push_back(DRW_LType("DIVIDE2", "DIVIDE2")); + cData.lineTypes.push_back(DRW_LType("DIVIDEX2", "DIVIDEX2")); + cData.lineTypes.push_back(DRW_LType("DOT", "DOT")); + cData.lineTypes.push_back(DRW_LType("DOT2", "DOT2")); + cData.lineTypes.push_back(DRW_LType("DOTX2", "DOTX2")); +} + +void dx_iface::InitLayers() +{ + DRW_Layer defLayer; + defLayer.name = "0"; + defLayer.color = DRW::black; // default color + defLayer.lWeight = DRW_LW_Conv::width17; // default width + defLayer.lineType = "CONTINUOUS"; // default line style + cData.layers.push_back(defLayer); + +// DRW_Layer mLayer; +// mLayer.name = "mainlayer"; +// mLayer.color = DRW::black; // default color +// mLayer.lWeight = 100; // default width +// mLayer.lineType = "CONTINUOUS"; // default line style +// cData.lineTypes.push_back(mLayer); +} + +void dx_iface::InitDimstyles() +{ + DRW_Dimstyle style; + style.dimasz = 1; + style.dimexe = 1; + style.dimexo = 1; + style.dimgap = 1; + style.dimtxt = 1; + + cData.dimStyles.push_back(style); +} + +void dx_iface::InitTextstyles() +{ + DRW_Textstyle style; + style.name = "Standard"; + style.lastHeight = 2.5; + style.font = "txt"; + + cData.textStyles.push_back(style); +} + +void dx_iface::AddEntity(DRW_Entity *e) +{ + cData.mBlock->ent.push_back(e); +} diff --git a/src/libs/vdxf/dxiface.h b/src/libs/vdxf/dxiface.h new file mode 100644 index 000000000..809698e94 --- /dev/null +++ b/src/libs/vdxf/dxiface.h @@ -0,0 +1,235 @@ +/****************************************************************************** +** dwg2dxf - Program to convert dwg/dxf to dxf(ascii & binary) ** +** ** +** Copyright (C) 2015 José F. Soriano, rallazz@gmail.com ** +** ** +** This library is free software, licensed under the terms of the GNU ** +** General Public License as published by the Free Software Foundation, ** +** either version 2 of the License, or (at your option) any later version. ** +** You should have received a copy of the GNU General Public License ** +** along with this program. If not, see . ** +******************************************************************************/ + +#ifndef DX_IFACE_H +#define DX_IFACE_H + +#include "libdxfrw/drw_interface.h" +#include "libdxfrw/libdxfrw.h" +#include "dxfdef.h" + +//class to store image data and path from DRW_ImageDef +class dx_ifaceImg : public DRW_Image { +public: + dx_ifaceImg(){} + dx_ifaceImg(const DRW_Image& p):DRW_Image(p){} + ~dx_ifaceImg(){} + std::string path; //stores the image path +}; + +//container class to store entites. +class dx_ifaceBlock : public DRW_Block { +public: + dx_ifaceBlock(){} + dx_ifaceBlock(const DRW_Block& p):DRW_Block(p){} + ~dx_ifaceBlock(){ + for (std::list::const_iterator it=ent.begin(); it!=ent.end(); ++it) + delete *it; + } + std::listent; //stores the entities list +}; + + +//container class to store full dwg/dxf data. +class dx_data { +public: + dx_data(){ + mBlock = new dx_ifaceBlock(); + } + ~dx_data(){ + //cleanup, + for (std::list::const_iterator it=blocks.begin(); it!=blocks.end(); ++it) + delete *it; + delete mBlock; + } + + DRW_Header headerC; //stores a copy of the header vars + std::listlineTypes; //stores a copy of all line types + std::listlayers; //stores a copy of all layers + std::listdimStyles; //stores a copy of all dimension styles + std::listVPorts; //stores a copy of all vports + std::listtextStyles; //stores a copy of all text styles + std::listappIds; //stores a copy of all line types + std::listblocks; //stores a copy of all blocks and the entities in it + std::listimages; //temporary list to find images for link with DRW_ImageDef. Do not delete it!! + + dx_ifaceBlock* mBlock; //container to store model entities + + +}; + +class dx_iface : public DRW_Interface +{ +public: + dx_iface(const std::string& file, VarMeasurement varMeasurement, VarInsunits varInsunits); + virtual ~dx_iface(); + bool fileExport(DRW::Version v, bool binary); + void writeEntity(DRW_Entity* e); + +//reimplement virtual DRW_Interface functions + +//reader part, do nothing + //header + void addHeader(const DRW_Header* data){ + // do nothing + } + + //tables + virtual void addLType(const DRW_LType&){ + // do nothing + } + virtual void addLayer(const DRW_Layer&){ + // do nothing + } + virtual void addDimStyle(const DRW_Dimstyle&){ + // do nothing + } + virtual void addVport(const DRW_Vport&){ + // do nothing + } + virtual void addTextStyle(const DRW_Textstyle&){ + // do nothing + } + virtual void addAppId(const DRW_AppId&){ + // do nothing + } + + //blocks + virtual void addBlock(const DRW_Block&){ + // do nothing + } + virtual void endBlock(){ + // do nothing + } + + virtual void setBlock(const int /*handle*/){}//unused + + //entities + virtual void addPoint(const DRW_Point&){ + // do nothing + } + virtual void addLine(const DRW_Line&){ + // do nothing + } + virtual void addRay(const DRW_Ray&){ + // do nothing + } + virtual void addXline(const DRW_Xline&){ + // do nothing + } + virtual void addArc(const DRW_Arc&){ + // do nothing + } + virtual void addCircle(const DRW_Circle&){ + // do nothing + } + virtual void addEllipse(const DRW_Ellipse&){ + // do nothing + } + virtual void addLWPolyline(const DRW_LWPolyline&){ + // do nothing + } + virtual void addPolyline(const DRW_Polyline&){ + // do nothing + } + virtual void addSpline(const DRW_Spline*){ + // do nothing + } + // ¿para que se usa? + virtual void addKnot(const DRW_Entity&){} + + virtual void addInsert(const DRW_Insert&){ + // do nothing + } + virtual void addTrace(const DRW_Trace&){ + // do nothing + } + virtual void add3dFace(const DRW_3Dface&){ + // do nothing + } + virtual void addSolid(const DRW_Solid&){ + // do nothing + } + virtual void addMText(const DRW_MText&){ + // do nothing + } + virtual void addText(const DRW_Text&){ + // do nothing + } + virtual void addDimAlign(const DRW_DimAligned *){ + // do nothing + } + virtual void addDimLinear(const DRW_DimLinear *){ + // do nothing + } + virtual void addDimRadial(const DRW_DimRadial *){ + // do nothing + } + virtual void addDimDiametric(const DRW_DimDiametric *){ + // do nothing + } + virtual void addDimAngular(const DRW_DimAngular *){ + // do nothing + } + virtual void addDimAngular3P(const DRW_DimAngular3p *){ + // do nothing + } + virtual void addDimOrdinate(const DRW_DimOrdinate *){ + // do nothing + } + virtual void addLeader(const DRW_Leader *){ + // do nothing + } + virtual void addHatch(const DRW_Hatch *){ + // do nothing + } + virtual void addViewport(const DRW_Viewport&){ + // do nothing + } + virtual void addImage(const DRW_Image *){ + // do nothing + } + + virtual void linkImage(const DRW_ImageDef *){ + // do nothing + } + + virtual void addComment(const char* /*comment*/){} + +//writer part, send all in class dx_data to writer + virtual void writeHeader(DRW_Header& data); + virtual void writeBlocks(); + //only send the name, needed by the reader to prepare handles of blocks & blockRecords + virtual void writeBlockRecords(); + //write entities of model space and first paper_space + virtual void writeEntities(); + virtual void writeLTypes(); + virtual void writeLayers(); + virtual void writeTextstyles(); + virtual void writeVports(); + virtual void writeDimstyles(); + virtual void writeAppId(); + + void InitHeader(VarMeasurement varMeasurement, VarInsunits varInsunits); + void InitLTypes(); + void InitLayers(); + void InitDimstyles(); + void InitTextstyles(); + + void AddEntity(DRW_Entity* e); + +private: + dxfRW* dxfW; //pointer to writer, needed to send data + dx_data cData; // class to store or read data +}; + +#endif // DX_IFACE_H diff --git a/src/libs/vdxf/libdxfrw/drw_base.h b/src/libs/vdxf/libdxfrw/drw_base.h index 34e795cc9..207f1e93a 100644 --- a/src/libs/vdxf/libdxfrw/drw_base.h +++ b/src/libs/vdxf/libdxfrw/drw_base.h @@ -101,6 +101,22 @@ enum DBG_LEVEL { //! Special codes for colors enum ColorCodes { + black = 250, + green = 3, + red = 1, + brown = 15, + yellow = 2, + cyan = 4, + magenta = 6, + gray = 8, + blue = 5, + l_blue = 163, + l_green = 121, + l_cyan = 131, + l_red = 23, + l_magenta = 221, + l_gray = 252, + white = 7, ColorByLayer = 256, ColorByBlock = 0 }; diff --git a/src/libs/vdxf/libdxfrw/drw_interface.h b/src/libs/vdxf/libdxfrw/drw_interface.h index 045b025bd..92f0af3c0 100644 --- a/src/libs/vdxf/libdxfrw/drw_interface.h +++ b/src/libs/vdxf/libdxfrw/drw_interface.h @@ -21,17 +21,16 @@ /** * Abstract class (interface) for comunicate dxfReader with the application. - * Inherit your class which takes care of the entities in the - * processed DXF file from this interface. + * Inherit your class which takes care of the entities in the + * processed DXF file from this interface. * * @author Rallaz */ class DRW_Interface { public: - DRW_Interface() { - } - virtual ~DRW_Interface() { - } + DRW_Interface() + {} + virtual ~DRW_Interface() = default; /** Called when header is parsed. */ virtual void addHeader(const DRW_Header* data) = 0; @@ -98,16 +97,16 @@ public: /** Called for every spline */ virtual void addSpline(const DRW_Spline* data) = 0; - - /** Called for every spline knot value */ + + /** Called for every spline knot value */ virtual void addKnot(const DRW_Entity& data) = 0; /** Called for every insert. */ virtual void addInsert(const DRW_Insert& data) = 0; - + /** Called for every trace start */ virtual void addTrace(const DRW_Trace& data) = 0; - + /** Called for every 3dface start */ virtual void add3dFace(const DRW_3Dface& data) = 0; @@ -122,62 +121,62 @@ public: virtual void addText(const DRW_Text& data) = 0; /** - * Called for every aligned dimension entity. + * Called for every aligned dimension entity. */ virtual void addDimAlign(const DRW_DimAligned *data) = 0; /** - * Called for every linear or rotated dimension entity. + * Called for every linear or rotated dimension entity. */ virtual void addDimLinear(const DRW_DimLinear *data) = 0; - /** - * Called for every radial dimension entity. + /** + * Called for every radial dimension entity. */ virtual void addDimRadial(const DRW_DimRadial *data) = 0; - /** - * Called for every diametric dimension entity. + /** + * Called for every diametric dimension entity. */ virtual void addDimDiametric(const DRW_DimDiametric *data) = 0; - /** - * Called for every angular dimension (2 lines version) entity. + /** + * Called for every angular dimension (2 lines version) entity. */ virtual void addDimAngular(const DRW_DimAngular *data) = 0; - /** - * Called for every angular dimension (3 points version) entity. + /** + * Called for every angular dimension (3 points version) entity. */ virtual void addDimAngular3P(const DRW_DimAngular3p *data) = 0; - + /** - * Called for every ordinate dimension entity. + * Called for every ordinate dimension entity. */ virtual void addDimOrdinate(const DRW_DimOrdinate *data) = 0; - - /** - * Called for every leader start. - */ + + /** + * Called for every leader start. + */ virtual void addLeader(const DRW_Leader *data) = 0; - - /** - * Called for every hatch entity. - */ + + /** + * Called for every hatch entity. + */ virtual void addHatch(const DRW_Hatch *data) = 0; - + /** * Called for every viewport entity. */ virtual void addViewport(const DRW_Viewport& data) = 0; /** - * Called for every image entity. - */ + * Called for every image entity. + */ virtual void addImage(const DRW_Image *data) = 0; - /** - * Called for every image definition. - */ + /** + * Called for every image definition. + */ virtual void linkImage(const DRW_ImageDef *data) = 0; /** diff --git a/src/libs/vdxf/libdxfrw/drw_objects.h b/src/libs/vdxf/libdxfrw/drw_objects.h index 65b0e0e19..89d36ec19 100644 --- a/src/libs/vdxf/libdxfrw/drw_objects.h +++ b/src/libs/vdxf/libdxfrw/drw_objects.h @@ -239,6 +239,12 @@ class DRW_LType : public DRW_TableEntry { SETOBJFRIENDS public: DRW_LType() { reset();} + DRW_LType(const UTF8STRING &name, const UTF8STRING &desc) + { + reset(); + this->name = name; + this->desc = desc; + } void reset(){ tType = DRW::LTYPE; diff --git a/src/libs/vdxf/vdxf.pri b/src/libs/vdxf/vdxf.pri index ebb5a47ed..f2f3b3075 100644 --- a/src/libs/vdxf/vdxf.pri +++ b/src/libs/vdxf/vdxf.pri @@ -25,7 +25,8 @@ SOURCES += \ $$PWD/libdxfrw/drw_header.cpp \ $$PWD/libdxfrw/drw_objects.cpp \ $$PWD/libdxfrw/libdwgr.cpp \ - $$PWD/libdxfrw/libdxfrw.cpp + $$PWD/libdxfrw/libdxfrw.cpp \ + $$PWD/dxiface.cpp win32-msvc*:SOURCES += $$PWD/stable.cpp @@ -71,4 +72,5 @@ HEADERS += \ $$PWD/libdxfrw/drw_objects.h \ $$PWD/libdxfrw/libdwgr.h \ $$PWD/libdxfrw/libdxfrw.h \ - $$PWD/libdxfrw/main_doc.h + $$PWD/libdxfrw/main_doc.h \ + $$PWD/dxiface.h diff --git a/src/libs/vdxf/vdxfengine.cpp b/src/libs/vdxf/vdxfengine.cpp index 734532f19..6afa5b11f 100644 --- a/src/libs/vdxf/vdxfengine.cpp +++ b/src/libs/vdxf/vdxfengine.cpp @@ -53,6 +53,7 @@ #include "dxflib/dl_dxf.h" #include "dxflib/dl_entities.h" #include "dxflib/dl_writer_ascii.h" +#include "dxiface.h" //--------------------------------------------------------------------------------------------------------------------- static inline QPaintEngine::PaintEngineFeatures svgEngineFeatures() @@ -77,7 +78,9 @@ VDxfEngine::VDxfEngine() size(), resolution(static_cast(PrintDPI)), fileName(), + m_version(DRW::AC1014), matrix(), + input(), dxf(nullptr), dw(nullptr), varMeasurement(VarMeasurement::Metric), @@ -85,12 +88,7 @@ VDxfEngine::VDxfEngine() { } - //--------------------------------------------------------------------------------------------------------------------- -VDxfEngine::~VDxfEngine() -{ -} - - //--------------------------------------------------------------------------------------------------------------------- +//--------------------------------------------------------------------------------------------------------------------- bool VDxfEngine::begin(QPaintDevice *pdev) { Q_UNUSED(pdev) @@ -100,147 +98,19 @@ bool VDxfEngine::begin(QPaintDevice *pdev) return false; } - dxf = new DL_Dxf(); - DL_Codes::version exportVersion = DL_Codes::AC1015; - QByteArray fileNameArray = getFileName().toLocal8Bit(); - dw = dxf->out(fileNameArray.data(), exportVersion); - - if (dw==nullptr) - { - qWarning("VDxfEngine::begin(), can't open file"); - return false; - } - - dxf->writeHeader(*dw); - - dxf->writeComment(*dw, "Valentina DXF File"); - - dw->dxfString(9, "$ANGDIR"); // 1 = Clockwise angles, 0 = Counterclockwise - dw->dxfInt(70, 0); // Qt use counterclockwise - - dw->dxfString(9, "$MEASUREMENT"); // Sets drawing units: 0 = English; 1 = Metric - dw->dxfInt(70, static_cast(varMeasurement)); - - dw->dxfString(9, "$INSUNITS"); - dw->dxfInt(70, static_cast(varInsunits)); - - dw->dxfString(9, "$DIMSCALE"); - dw->dxfReal(40, 1.0); - - // Official documentation says that initial value is 1.0, however LibreCAD has trouble if not set this value - // explicitly. - dw->dxfString(9, "$DIMLFAC"); - dw->dxfReal(40, 1.0); - - QString dateTime = QDateTime::currentDateTime().toString("yyyyMMdd.HHmmsszzz"); - dateTime.chop(1);// we need hundredths of a second - dw->dxfString(9, "$TDCREATE"); - dw->dxfString(40, dateTime.toUtf8().constData()); - - dw->sectionEnd(); - - dw->sectionTables(); - dxf->writeVPort(*dw); - - dw->tableLinetypes(25); - dxf->writeLinetype(*dw, DL_LinetypeData("BYBLOCK", "BYBLOCK", 0, 0, 0.0)); - dxf->writeLinetype(*dw, DL_LinetypeData("BYLAYER", "BYLAYER", 0, 0, 0.0)); - dxf->writeLinetype(*dw, DL_LinetypeData("CONTINUOUS", "Continuous", 0, 0, 0.0)); - dxf->writeLinetype(*dw, DL_LinetypeData("ACAD_ISO02W100", "ACAD_ISO02W100", 0, 0, 0.0)); - dxf->writeLinetype(*dw, DL_LinetypeData("ACAD_ISO03W100", "ACAD_ISO03W100", 0, 0, 0.0)); - dxf->writeLinetype(*dw, DL_LinetypeData("ACAD_ISO04W100", "ACAD_ISO04W100", 0, 0, 0.0)); - dxf->writeLinetype(*dw, DL_LinetypeData("ACAD_ISO05W100", "ACAD_ISO05W100", 0, 0, 0.0)); - dxf->writeLinetype(*dw, DL_LinetypeData("BORDER", "BORDER", 0, 0, 0.0)); - dxf->writeLinetype(*dw, DL_LinetypeData("BORDER2", "BORDER2", 0, 0, 0.0)); - dxf->writeLinetype(*dw, DL_LinetypeData("BORDERX2", "BORDERX2", 0, 0, 0.0)); - dxf->writeLinetype(*dw, DL_LinetypeData("CENTER", "CENTER", 0, 0, 0.0)); - dxf->writeLinetype(*dw, DL_LinetypeData("CENTER2", "CENTER2", 0, 0, 0.0)); - dxf->writeLinetype(*dw, DL_LinetypeData("CENTERX2", "CENTERX2", 0, 0, 0.0)); - dxf->writeLinetype(*dw, DL_LinetypeData("DASHDOT", "DASHDOT", 0, 0, 0.0)); - dxf->writeLinetype(*dw, DL_LinetypeData("DASHDOT2", "DASHDOT2", 0, 0, 0.0)); - dxf->writeLinetype(*dw, DL_LinetypeData("DASHDOTX2", "DASHDOTX2", 0, 0, 0.0)); - dxf->writeLinetype(*dw, DL_LinetypeData("DASHED", "DASHED", 0, 0, 0.0)); - dxf->writeLinetype(*dw, DL_LinetypeData("DASHED2", "DASHED2", 0, 0, 0.0)); - dxf->writeLinetype(*dw, DL_LinetypeData("DASHEDX2", "DASHEDX2", 0, 0, 0.0)); - dxf->writeLinetype(*dw, DL_LinetypeData("DIVIDE", "DIVIDE", 0, 0, 0.0)); - dxf->writeLinetype(*dw, DL_LinetypeData("DIVIDE2", "DIVIDE2", 0, 0, 0.0)); - dxf->writeLinetype(*dw, DL_LinetypeData("DIVIDEX2", "DIVIDEX2", 0, 0, 0.0)); - dxf->writeLinetype(*dw, DL_LinetypeData("DOT", "DOT", 0, 0, 0.0)); - dxf->writeLinetype(*dw, DL_LinetypeData("DOT2", "DOT2", 0, 0, 0.0)); - dxf->writeLinetype(*dw, DL_LinetypeData("DOTX2", "DOTX2", 0, 0, 0.0)); - dw->tableEnd(); - - int numberOfLayers = 1; - dw->tableLayers(numberOfLayers); - dxf->writeLayer(*dw, - DL_LayerData("0", 0), - DL_Attributes( - std::string(""), // leave empty - DL_Codes::black, // default color - 100, // default width - "CONTINUOUS", // default line style - 1.0)); // default line type scale - - dw->tableEnd(); - - dw->tableStyle(1); - DL_StyleData style("Standard", 0, 0.0, 1.0, 0.0, 0, 2.5, "txt", ""); - style.bold = false; - style.italic = false; - dxf->writeStyle(*dw, style); - dw->tableEnd(); - - dxf->writeView(*dw); - dxf->writeUcs(*dw); - dw->tableAppid(1); - dw->tableAppidEntry(0x12); - dw->dxfString(2, "ACAD"); - dw->dxfInt(70, 0); - dw->tableEnd(); - - dxf->writeDimStyle(*dw, 1, 1, 1, 1, 1); - - dxf->writeBlockRecord(*dw); - dxf->writeBlockRecord(*dw, "layout"); - dw->tableEnd(); - - dw->sectionEnd(); - - dw->sectionBlocks(); - dxf->writeBlock(*dw, DL_BlockData("*Model_Space", 0, 0.0, 0.0, 0.0)); - dxf->writeEndBlock(*dw, "*Model_Space"); - dxf->writeBlock(*dw, DL_BlockData("*Paper_Space", 0, 0.0, 0.0, 0.0)); - dxf->writeEndBlock(*dw, "*Paper_Space"); - dxf->writeBlock(*dw, DL_BlockData("*Paper_Space0", 0, 0.0, 0.0, 0.0)); - dxf->writeEndBlock(*dw, "*Paper_Space0"); - - dxf->writeBlock(*dw, DL_BlockData("layout", 0, 0.0, 0.0, 0.0)); - // ... - // write block entities e.g. with dxf->writeLine(), .. - // ... - dxf->writeEndBlock(*dw, "layout"); - - dw->sectionEnd(); - - dw->sectionEntities(); + input = QSharedPointer(new dx_iface(fileName.toStdString(), varMeasurement, varInsunits)); return true; } - //--------------------------------------------------------------------------------------------------------------------- +//--------------------------------------------------------------------------------------------------------------------- bool VDxfEngine::end() { - dw->sectionEnd(); - dxf->writeObjects(*dw); - dxf->writeObjectsEnd(*dw); - dw->dxfEOF(); - dw->close(); - delete dw; - delete dxf; - return true; + const bool res = input->fileExport(m_version, false); + return res; } - //--------------------------------------------------------------------------------------------------------------------- - // cppcheck-suppress unusedFunction +//--------------------------------------------------------------------------------------------------------------------- +// cppcheck-suppress unusedFunction void VDxfEngine::updateState(const QPaintEngineState &state) { QPaintEngine::DirtyFlags flags = state.state(); @@ -255,7 +125,7 @@ void VDxfEngine::updateState(const QPaintEngineState &state) } } - //--------------------------------------------------------------------------------------------------------------------- +//--------------------------------------------------------------------------------------------------------------------- void VDxfEngine::drawPath(const QPainterPath &path) { const QList subpaths = path.toSubpathPolygons(matrix); @@ -265,21 +135,43 @@ void VDxfEngine::drawPath(const QPainterPath &path) const QPolygonF polygon = subpaths.at(j); if (polygon.isEmpty()) { - return; + continue; } - dxf->writePolyline(*dw, - DL_PolylineData(polygon.size(), 0, 0, 0), - DL_Attributes("0", getPenColor(), -1, getPenStyle(), 1.0)); + if (m_version > DRW::AC1009) + { // Use lwpolyline + DRW_LWPolyline *poly = new DRW_LWPolyline(); + poly->layer = "0"; + poly->color = getPenColor(); + poly->lWeight = DRW_LW_Conv::widthByLayer; + poly->lineType = getPenStyle(); + poly->ltypeScale = 1.0; - for (int i=0; i < polygon.count(); ++i) - { - dxf->writeVertex(*dw, - DL_VertexData(FromPixel(polygon.at(i).x(), varInsunits), + for (int i=0; i < polygon.count(); ++i) + { + poly->addVertex(DRW_Vertex2D(FromPixel(polygon.at(i).x(), varInsunits), + FromPixel(getSize().height() - polygon.at(i).y(), varInsunits), 0)); + } + + input->AddEntity(poly); + } + else + { // Use polyline + DRW_Polyline *poly = new DRW_Polyline(); + poly->layer = "0"; + poly->color = getPenColor(); + poly->lWeight = DRW_LW_Conv::widthByLayer; + poly->lineType = getPenStyle(); + poly->ltypeScale = 1.0; + + for (int i=0; i < polygon.count(); ++i) + { + poly->addVertex(DRW_Vertex(FromPixel(polygon.at(i).x(), varInsunits), FromPixel(getSize().height() - polygon.at(i).y(), varInsunits), 0, 0)); - } + } - dxf->writePolylineEnd(*dw); + input->AddEntity(poly); + } } } @@ -291,14 +183,18 @@ void VDxfEngine::drawLines(const QLineF * lines, int lineCount) const QPointF p1 = matrix.map(lines[i].p1()); const QPointF p2 = matrix.map(lines[i].p2()); - dxf->writeLine(*dw, - DL_LineData(FromPixel(p1.x(), varInsunits), // start point - FromPixel(getSize().height() - p1.y(), varInsunits), - FromPixel(0.0, varInsunits), - FromPixel(p2.x(), varInsunits), // end point - FromPixel(getSize().height() - p2.y(), varInsunits), - FromPixel(0.0, varInsunits)), - DL_Attributes("0", getPenColor(), -1, getPenStyle(), 1.0)); + DRW_Line *line = new DRW_Line(); + line->basePoint = DRW_Coord(FromPixel(p1.x(), varInsunits), + FromPixel(getSize().height() - p1.y(), varInsunits), 0); + line->secPoint = DRW_Coord(FromPixel(p2.x(), varInsunits), + FromPixel(getSize().height() - p2.y(), varInsunits), 0); + line->layer = "0"; + line->color = getPenColor(); + line->lWeight = DRW_LW_Conv::widthByLayer; + line->lineType = getPenStyle(); + line->ltypeScale = 1.0; + + input->AddEntity(line); } } @@ -318,19 +214,42 @@ void VDxfEngine::drawPolygon(const QPointF *points, int pointCount, PolygonDrawM return; } - dxf->writePolyline(*dw, - DL_PolylineData(pointCount, 0, 0, 0), - DL_Attributes("0", getPenColor(), -1, getPenStyle(), 1.0)); + if (m_version > DRW::AC1009) + { // Use lwpolyline + DRW_LWPolyline *poly = new DRW_LWPolyline(); + poly->layer = "0"; + poly->color = getPenColor(); + poly->lWeight = DRW_LW_Conv::widthByLayer; + poly->lineType = getPenStyle(); + poly->ltypeScale = 1.0; - for (int i = 0; i < pointCount; ++i) - { - const QPointF p = matrix.map(points[i]); - dxf->writeVertex(*dw, - DL_VertexData(FromPixel(p.x(), varInsunits), - FromPixel(getSize().height() - p.y(), varInsunits), 0, 0)); + for (int i = 0; i < pointCount; ++i) + { + const QPointF p = matrix.map(points[i]); + poly->addVertex(DRW_Vertex2D(FromPixel(p.x(), varInsunits), + FromPixel(getSize().height() - p.y(), varInsunits), 0)); + } + + input->AddEntity(poly); } + else + { // Use polyline + DRW_Polyline *poly = new DRW_Polyline(); + poly->layer = "0"; + poly->color = getPenColor(); + poly->lWeight = DRW_LW_Conv::widthByLayer; + poly->lineType = getPenStyle(); + poly->ltypeScale = 1.0; - dxf->writePolylineEnd(*dw); + for (int i = 0; i < pointCount; ++i) + { + const QPointF p = matrix.map(points[i]); + poly->addVertex(DRW_Vertex(FromPixel(p.x(), varInsunits), + FromPixel(getSize().height() - p.y(), varInsunits), 0, 0)); + } + + input->AddEntity(poly); + } } //--------------------------------------------------------------------------------------------------------------------- @@ -364,17 +283,21 @@ void VDxfEngine::drawEllipse(const QRectF & rect) ratio = rect.height()/rect.width(); } - dxf->writeEllipse(*dw, - DL_EllipseData(FromPixel(newRect.center().x(), varInsunits), // center X - FromPixel(getSize().height() - newRect.center().y(), varInsunits), // center Y - FromPixel(0, varInsunits), // center Z - FromPixel(majorX, varInsunits), - FromPixel(majorY, varInsunits), - FromPixel(0, varInsunits), - FromPixel(ratio, varInsunits), - 0, 6.28 // startangle and endangle of ellipse in rad - ), - DL_Attributes("0", getPenColor(), -1, getPenStyle(), 1.0)); + DRW_Ellipse *ellipse = new DRW_Ellipse(); + ellipse->basePoint = DRW_Coord(FromPixel(newRect.center().x(), varInsunits), + FromPixel(getSize().height() - newRect.center().y(), varInsunits), 0); + ellipse->secPoint = DRW_Coord(FromPixel(majorX, varInsunits), FromPixel(majorY, varInsunits), 0); + ellipse->ratio = ratio; + ellipse->staparam = 0; + ellipse->endparam = 2*M_PI; + + ellipse->layer = "0"; + ellipse->color = getPenColor(); + ellipse->lWeight = DRW_LW_Conv::widthByLayer; + ellipse->lineType = getPenStyle(); + ellipse->ltypeScale = 1.0; + + input->AddEntity(ellipse); } //--------------------------------------------------------------------------------------------------------------------- @@ -391,23 +314,47 @@ void VDxfEngine::drawTextItem(const QPointF & p, const QTextItem & textItem) const QFont f = textItem.font(); const int textSize = f.pixelSize() == -1 ? f.pointSize() : f.pixelSize(); - dxf->writeText(*dw, - DL_TextData(FromPixel(startPoint.x(), varInsunits), - FromPixel(getSize().height() - startPoint.y(), varInsunits), - FromPixel(0, varInsunits), - FromPixel(startPoint.x(), varInsunits), - FromPixel(getSize().height() - startPoint.y(), varInsunits), - FromPixel(0, varInsunits), - textSize * matrix.m11(), - 1, // relative X scale factor - 0, // flag (0 = default, 2 = Backwards, 4 = Upside down) - 0, // Horizontal justification (0 = Left (default), 1 = Center, 2 = Right,) - 0, // Vertical justification (0 = Baseline (default), 1 = Bottom, 2 = Middle, 3= Top) - textItem.text().toUtf8().constData(), // text data - f.family().toUtf8().constData(), // font - -rotationAngle - ), - DL_Attributes("0", getPenColor(), -1, getPenStyle(), 1.0)); + + if (m_version > DRW::AC1009) + { // Use MTEXT + DRW_MText *text = new DRW_MText(); + text->basePoint = DRW_Coord(FromPixel(startPoint.x(), varInsunits), + FromPixel(getSize().height() - startPoint.y(), varInsunits), 0); + text->secPoint = DRW_Coord(FromPixel(startPoint.x(), varInsunits), + FromPixel(getSize().height() - startPoint.y(), varInsunits), 0); + text->height = textSize * matrix.m11(); + text->text = textItem.text().toStdString(); + text->style = f.family().toStdString(); + text->angle = -rotationAngle; + + text->layer = "0"; + text->color = getPenColor(); + text->lWeight = DRW_LW_Conv::widthByLayer; + text->lineType = getPenStyle(); + text->ltypeScale = 1.0; + + input->AddEntity(text); + } + else + { // Use TEXT + DRW_Text *text = new DRW_Text(); + text->basePoint = DRW_Coord(FromPixel(startPoint.x(), varInsunits), + FromPixel(getSize().height() - startPoint.y(), varInsunits), 0); + text->secPoint = DRW_Coord(FromPixel(startPoint.x(), varInsunits), + FromPixel(getSize().height() - startPoint.y(), varInsunits), 0); + text->height = textSize * matrix.m11(); + text->text = textItem.text().toStdString(); + text->style = f.family().toStdString(); + text->angle = -rotationAngle; + + text->layer = "0"; + text->color = getPenColor(); + text->lWeight = DRW_LW_Conv::widthByLayer; + text->lineType = getPenStyle(); + text->ltypeScale = 1.0; + + input->AddEntity(text); + } } //--------------------------------------------------------------------------------------------------------------------- @@ -416,8 +363,8 @@ QPaintEngine::Type VDxfEngine::type() const return QPaintEngine::User; } - //--------------------------------------------------------------------------------------------------------------------- - // cppcheck-suppress unusedFunction +//--------------------------------------------------------------------------------------------------------------------- +// cppcheck-suppress unusedFunction void VDxfEngine::drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr) { Q_UNUSED(r) @@ -444,26 +391,39 @@ double VDxfEngine::getResolution() const return resolution; } - //--------------------------------------------------------------------------------------------------------------------- +//--------------------------------------------------------------------------------------------------------------------- void VDxfEngine::setResolution(double value) { Q_ASSERT(not isActive()); resolution = value; } - //--------------------------------------------------------------------------------------------------------------------- +//--------------------------------------------------------------------------------------------------------------------- QString VDxfEngine::getFileName() const { return fileName; } - //--------------------------------------------------------------------------------------------------------------------- +//--------------------------------------------------------------------------------------------------------------------- void VDxfEngine::setFileName(const QString &value) { Q_ASSERT(not isActive()); fileName = value; } +//--------------------------------------------------------------------------------------------------------------------- +DRW::Version VDxfEngine::GetVersion() const +{ + return m_version; +} + +//--------------------------------------------------------------------------------------------------------------------- +void VDxfEngine::SetVersion(DRW::Version version) +{ + Q_ASSERT(not isActive()); + m_version = version; +} + //--------------------------------------------------------------------------------------------------------------------- std::string VDxfEngine::getPenStyle() { @@ -490,67 +450,67 @@ int VDxfEngine::getPenColor() if(color == Qt::black) { - return DL_Codes::black; + return DRW::black; } else if(color == Qt::white) { - return DL_Codes::white; + return DRW::white; } else if(color == Qt::darkGray) { - return DL_Codes::gray; + return DRW::gray; } else if(color == Qt::gray) { - return DL_Codes::l_gray; + return DRW::l_gray; } else if(color == Qt::darkMagenta) { - return DL_Codes::magenta; + return DRW::magenta; } else if(color == Qt::magenta) { - return DL_Codes::l_magenta; + return DRW::l_magenta; } else if(color == Qt::cyan) { - return DL_Codes::l_cyan; + return DRW::l_cyan; } else if(color == Qt::darkCyan) { - return DL_Codes::cyan; + return DRW::cyan; } else if(color == Qt::blue) { - return DL_Codes::l_blue; + return DRW::l_blue; } else if(color == Qt::darkBlue) { - return DL_Codes::blue; + return DRW::blue; } else if(color == Qt::darkGreen) { - return DL_Codes::green; + return DRW::green; } else if(color == Qt::green) { - return DL_Codes::l_green; + return DRW::l_green; } else if(color == Qt::darkRed) { - return DL_Codes::red; + return DRW::red; } else if(color == Qt::red) { - return DL_Codes::l_red; + return DRW::l_red; } else if(color == Qt::yellow) { - return DL_Codes::yellow; + return DRW::yellow; } else { - return DL_Codes::bylayer; + return DRW::ColorByLayer; } } diff --git a/src/libs/vdxf/vdxfengine.h b/src/libs/vdxf/vdxfengine.h index 10b4b368f..ef988f6fe 100644 --- a/src/libs/vdxf/vdxfengine.h +++ b/src/libs/vdxf/vdxfengine.h @@ -42,14 +42,16 @@ #include "../vmisc/def.h" #include "dxfdef.h" #include "dxflib/dl_dxf.h" +#include "libdxfrw/drw_base.h" class QTextStream; +class dx_iface; class VDxfEngine : public QPaintEngine { public: VDxfEngine(); - virtual ~VDxfEngine() Q_DECL_OVERRIDE; + virtual ~VDxfEngine() = default; virtual bool begin(QPaintDevice *pdev) Q_DECL_OVERRIDE; virtual bool end() Q_DECL_OVERRIDE; @@ -74,6 +76,9 @@ public: QString getFileName() const; void setFileName(const QString &value); + DRW::Version GetVersion() const; + void SetVersion(DRW::Version version); + std::string getPenStyle(); int getPenColor(); @@ -85,7 +90,9 @@ private: QSize size; double resolution; QString fileName; + DRW::Version m_version; QMatrix matrix; + QSharedPointer input; DL_Dxf* dxf; DL_WriterA* dw; VarMeasurement varMeasurement;