From 0db9e1f728d43b9d542cd8860cc7f173ea4695dc Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Tue, 23 Nov 2021 14:43:26 +0200 Subject: [PATCH] Class to handle plot settings object entries. --- src/libs/vdxf/dxiface.cpp | 10 +- src/libs/vdxf/dxiface.h | 1 + src/libs/vdxf/libdxfrw/drw_base.h | 5 +- src/libs/vdxf/libdxfrw/drw_entities.cpp | 4 +- src/libs/vdxf/libdxfrw/drw_entities.h | 4 +- src/libs/vdxf/libdxfrw/drw_header.cpp | 7 + src/libs/vdxf/libdxfrw/drw_header.h | 2 +- src/libs/vdxf/libdxfrw/drw_interface.h | 8 +- src/libs/vdxf/libdxfrw/drw_objects.cpp | 25 ++ src/libs/vdxf/libdxfrw/drw_objects.h | 35 +- src/libs/vdxf/libdxfrw/intern/drw_dbg.cpp | 10 +- src/libs/vdxf/libdxfrw/intern/drw_dbg.h | 2 + src/libs/vdxf/libdxfrw/intern/dxfreader.cpp | 4 + src/libs/vdxf/libdxfrw/intern/dxfreader.h | 2 + src/libs/vdxf/libdxfrw/libdxfrw.cpp | 458 ++++++++++++++------ src/libs/vdxf/libdxfrw/libdxfrw.h | 12 +- src/libs/vdxf/libdxfrw/main_doc.h | 4 +- 17 files changed, 445 insertions(+), 148 deletions(-) diff --git a/src/libs/vdxf/dxiface.cpp b/src/libs/vdxf/dxiface.cpp index ee4acf3b2..1fa4c4887 100644 --- a/src/libs/vdxf/dxiface.cpp +++ b/src/libs/vdxf/dxiface.cpp @@ -125,8 +125,7 @@ void dx_iface::writeHeader(DRW_Header &data){ void dx_iface::writeBlocks(){ //write each block - for (std::list::iterator it=cData.blocks.begin(); it != cData.blocks.end(); ++it){ - dx_ifaceBlock* bk = *it; + for (auto *bk : cData.blocks){ dxfW->writeBlock(bk); //and write each entity in block for (std::list::const_iterator it=bk->ent.begin(); it!=bk->ent.end(); ++it) @@ -169,8 +168,13 @@ void dx_iface::writeDimstyles(){ dxfW->writeDimstyle(&(*it)); } +void dx_iface::writeObjects() +{ + // default implementation for new DRW_Interface method +} + void dx_iface::writeAppId(){ - for (std::list::iterator it=cData.appIds.begin(); it != cData.appIds.end(); ++it) + for (auto it=cData.appIds.begin(); it != cData.appIds.end(); ++it) dxfW->writeAppId(&(*it)); } diff --git a/src/libs/vdxf/dxiface.h b/src/libs/vdxf/dxiface.h index abcc27fbf..bd40d9316 100644 --- a/src/libs/vdxf/dxiface.h +++ b/src/libs/vdxf/dxiface.h @@ -116,6 +116,7 @@ public: virtual void writeTextstyles() override; virtual void writeVports() override; virtual void writeDimstyles() override; + virtual void writeObjects() override; virtual void writeAppId() override; void AddEntity(DRW_Entity* e); diff --git a/src/libs/vdxf/libdxfrw/drw_base.h b/src/libs/vdxf/libdxfrw/drw_base.h index 2c5c17a74..2e8f38d52 100644 --- a/src/libs/vdxf/libdxfrw/drw_base.h +++ b/src/libs/vdxf/libdxfrw/drw_base.h @@ -156,7 +156,8 @@ BAD_READ_CLASSES, /*!< error in classes read process. */ BAD_READ_TABLES, /*!< error in tables read process. */ BAD_READ_BLOCKS, /*!< error in block read process. */ BAD_READ_ENTITIES, /*!< error in entities read process. */ -BAD_READ_OBJECTS /*!< error in objects read process. */ +BAD_READ_OBJECTS, /*!< error in objects read process. */ +BAD_READ_SECTION, /*!< error in sections read process. */ }; enum class DebugLevel { @@ -301,7 +302,7 @@ public: /*!< convert to unitary vector */ void unitize(){ double dist; - dist = sqrt(x*x + y*y + z*z); + dist = hypot(hypot(x, y), z); if (dist > 0.0) { x= x/dist; y= y/dist; diff --git a/src/libs/vdxf/libdxfrw/drw_entities.cpp b/src/libs/vdxf/libdxfrw/drw_entities.cpp index 89bd0edde..6529dfc14 100644 --- a/src/libs/vdxf/libdxfrw/drw_entities.cpp +++ b/src/libs/vdxf/libdxfrw/drw_entities.cpp @@ -607,7 +607,7 @@ void DRW_MText::parseCode(int code, dxfReader *reader){ text = reader->toUtf8String(text); break; case 11: - haveXAxis = true; + hasXAxisVec = true; DRW_Text::parseCode(code, reader); break; case 3: @@ -681,7 +681,7 @@ void DRW_MText::parseCode(int code, dxfReader *reader){ } void DRW_MText::updateAngle(){ - if (haveXAxis) { + if (hasXAxisVec) { angle = atan2(secPoint.y, secPoint.x)*180/M_PI; } } diff --git a/src/libs/vdxf/libdxfrw/drw_entities.h b/src/libs/vdxf/libdxfrw/drw_entities.h index 6bbabd2b9..39d8f2106 100644 --- a/src/libs/vdxf/libdxfrw/drw_entities.h +++ b/src/libs/vdxf/libdxfrw/drw_entities.h @@ -739,7 +739,7 @@ public: DRW_MText() : interlin(1), - haveXAxis(false)//if true needed to recalculate angle + hasXAxisVec(false)//if true needed to recalculate angle { eType = DRW::MTEXT; alignV = static_cast(TopLeft); @@ -753,7 +753,7 @@ protected: public: double interlin; /*!< width factor, code 44 */ private: - bool haveXAxis; + bool hasXAxisVec; }; //! Class to handle vertex diff --git a/src/libs/vdxf/libdxfrw/drw_header.cpp b/src/libs/vdxf/libdxfrw/drw_header.cpp index 8feafd126..92df3ef3a 100644 --- a/src/libs/vdxf/libdxfrw/drw_header.cpp +++ b/src/libs/vdxf/libdxfrw/drw_header.cpp @@ -40,6 +40,13 @@ void DRW_Header::addComment(const std::string &c){ } void DRW_Header::parseCode(int code, dxfReader *reader){ + if (nullptr == curr && 9 != code) { + DRW_DBG("invalid header code: "); + DRW_DBG(code); + DRW_DBG("\n"); + return; + } + switch (code) { case 9: curr = new DRW_Variant(); diff --git a/src/libs/vdxf/libdxfrw/drw_header.h b/src/libs/vdxf/libdxfrw/drw_header.h index 11dd4f33e..0748c7e2b 100644 --- a/src/libs/vdxf/libdxfrw/drw_header.h +++ b/src/libs/vdxf/libdxfrw/drw_header.h @@ -105,7 +105,7 @@ public: private: std::string comments; std::string name; - DRW_Variant* curr; + DRW_Variant* curr {nullptr}; int version; //to use on read duint32 linetypeCtrl; diff --git a/src/libs/vdxf/libdxfrw/drw_interface.h b/src/libs/vdxf/libdxfrw/drw_interface.h index 99585f14d..ed2126b97 100644 --- a/src/libs/vdxf/libdxfrw/drw_interface.h +++ b/src/libs/vdxf/libdxfrw/drw_interface.h @@ -24,7 +24,7 @@ QT_WARNING_DISABLE_GCC("-Wsuggest-final-types") QT_WARNING_DISABLE_GCC("-Wsuggest-final-methods") /** - * Abstract class (interface) for comunicate dxfReader with the application. + * Abstract class (interface) for communicate dxfReader with the application. * Inherit your class which takes care of the entities in the * processed DXF file from this interface. * @@ -179,6 +179,11 @@ public: */ virtual void addComment(const char *) { } + /** + * Called for PLOTSETTINGS object definition. + */ + virtual void addPlotSettings(const DRW_PlotSettings *) {} + virtual void writeHeader(DRW_Header &) { } virtual void writeBlocks() { } virtual void writeBlockRecords() { } @@ -188,6 +193,7 @@ public: virtual void writeTextstyles() { } virtual void writeVports() { } virtual void writeDimstyles() { } + virtual void writeObjects() {} virtual void writeAppId() { } }; diff --git a/src/libs/vdxf/libdxfrw/drw_objects.cpp b/src/libs/vdxf/libdxfrw/drw_objects.cpp index c46939424..9389b9224 100644 --- a/src/libs/vdxf/libdxfrw/drw_objects.cpp +++ b/src/libs/vdxf/libdxfrw/drw_objects.cpp @@ -588,3 +588,28 @@ void DRW_ImageDef::parseCode(int code, dxfReader *reader){ break; } } + +void DRW_PlotSettings::parseCode(int code, dxfReader *reader){ + switch (code) { + case 5: + handle = static_cast(reader->getHandleString()); + break; + case 6: + plotViewName = reader->getUtf8String(); + break; + case 40: + marginLeft = reader->getDouble(); + break; + case 41: + marginBottom = reader->getDouble(); + break; + case 42: + marginRight = reader->getDouble(); + break; + case 43: + marginTop = reader->getDouble(); + break; + default: + break; + } +} diff --git a/src/libs/vdxf/libdxfrw/drw_objects.h b/src/libs/vdxf/libdxfrw/drw_objects.h index 227a4f219..a769ed24d 100644 --- a/src/libs/vdxf/libdxfrw/drw_objects.h +++ b/src/libs/vdxf/libdxfrw/drw_objects.h @@ -35,7 +35,8 @@ namespace DRW { VPORT, BLOCK_RECORD, APPID, - IMAGEDEF + IMAGEDEF, + PLOTSETTINGS }; //pending VIEW, UCS, APPID, VP_ENT_HDR, GROUP, MLINESTYLE, LONG_TRANSACTION, XRECORD, @@ -591,6 +592,38 @@ public: std::map reactors; }; +//! Class to handle plotsettings entries +/*! +* Class to handle plot settings object entries +* @author baranovskiykonstantin@gmail.com +*/ +class DRW_PlotSettings : public DRW_TableEntry { + SETOBJFRIENDS +public: + DRW_PlotSettings() { + reset(); + } + + void reset(){ + tType = DRW::PLOTSETTINGS; + marginLeft = 0.0; + marginBottom = 0.0; + marginRight = 0.0; + marginTop = 0.0; + DRW_TableEntry::reset(); + } + +protected: + void parseCode(int code, dxfReader *reader); + +public: + UTF8STRING plotViewName {}; /*!< Plot view name, code 6 */ + double marginLeft {0.0}; /*!< Size, in millimeters, of unprintable margin on left side of paper, code 40 */ + double marginBottom {0.0}; /*!< Size, in millimeters, of unprintable margin on bottom side of paper, code 41 */ + double marginRight {0.0}; /*!< Size, in millimeters, of unprintable margin on right side of paper, code 42 */ + double marginTop {0.0}; /*!< Size, in millimeters, of unprintable margin on top side of paper, code 43 */ +}; + //! Class to handle AppId entries /*! * Class to handle AppId symbol table entries diff --git a/src/libs/vdxf/libdxfrw/intern/drw_dbg.cpp b/src/libs/vdxf/libdxfrw/intern/drw_dbg.cpp index 4a99e0921..4b51bb821 100644 --- a/src/libs/vdxf/libdxfrw/intern/drw_dbg.cpp +++ b/src/libs/vdxf/libdxfrw/intern/drw_dbg.cpp @@ -17,7 +17,7 @@ DRW_dbg *DRW_dbg::instance{nullptr}; -/*********private clases*************/ +/*********private classes*************/ class print_debug : public DRW::DebugPrinter { public: @@ -76,6 +76,14 @@ void DRW_dbg::print(const std::string &s){ currentPrinter->printS(s); } +void DRW_dbg::print(signed char i){ + currentPrinter->printI(i); +} + +void DRW_dbg::print(unsigned char i){ + currentPrinter->printUI(i); +} + void DRW_dbg::print(int i){ currentPrinter->printI(i); } diff --git a/src/libs/vdxf/libdxfrw/intern/drw_dbg.h b/src/libs/vdxf/libdxfrw/intern/drw_dbg.h index 81a3d8f39..436b34dfe 100644 --- a/src/libs/vdxf/libdxfrw/intern/drw_dbg.h +++ b/src/libs/vdxf/libdxfrw/intern/drw_dbg.h @@ -46,6 +46,8 @@ public: Level getLevel() const; static DRW_dbg *getInstance(); void print(const std::string &s); + void print(signed char i); + void print(unsigned char i); void print(int i); void print(unsigned int i); void print(long long int i); diff --git a/src/libs/vdxf/libdxfrw/intern/dxfreader.cpp b/src/libs/vdxf/libdxfrw/intern/dxfreader.cpp index efd80cc93..9e99e89fb 100644 --- a/src/libs/vdxf/libdxfrw/intern/dxfreader.cpp +++ b/src/libs/vdxf/libdxfrw/intern/dxfreader.cpp @@ -70,6 +70,10 @@ bool dxfReader::readRec(int *codeData) { readDouble(); else if (code < 481) readString(); + else if( 999 == code && m_bIgnoreComments) { + readString(); + return readRec( codeData); + } else if (code > 998 && code < 1009) //skip not used at the v2012 readString(); else if (code < 1060) //TODO this is a floating point double precision?? diff --git a/src/libs/vdxf/libdxfrw/intern/dxfreader.h b/src/libs/vdxf/libdxfrw/intern/dxfreader.h index 564d5c564..60f5e203f 100644 --- a/src/libs/vdxf/libdxfrw/intern/dxfreader.h +++ b/src/libs/vdxf/libdxfrw/intern/dxfreader.h @@ -53,6 +53,7 @@ public: void setVersion(const std::string &v, bool dxfFormat){decoder.setVersion(v, dxfFormat);} void setCodePage(const std::string &c){decoder.setCodePage(c, true);} std::string getCodePage() const { return decoder.getCodePage();} + void setIgnoreComments(const bool bValue) {m_bIgnoreComments = bValue;} protected: virtual bool readCode(int *code) = 0; //return true if successful (not EOF) @@ -74,6 +75,7 @@ protected: private: Q_DISABLE_COPY(dxfReader) DRW_TextCodec decoder; + bool m_bIgnoreComments {false}; }; class dxfReaderBinary : public dxfReader { diff --git a/src/libs/vdxf/libdxfrw/libdxfrw.cpp b/src/libs/vdxf/libdxfrw/libdxfrw.cpp index 0c5a5ca98..855b41fea 100644 --- a/src/libs/vdxf/libdxfrw/libdxfrw.cpp +++ b/src/libs/vdxf/libdxfrw/libdxfrw.cpp @@ -49,7 +49,7 @@ dxfRW::dxfRW(const char* name) dimstyleStd(), applyExt(false), writingBlock(), - elParts(128), //parts munber when convert ellipse to polyline + elParts(128), //parts number when convert ellipse to polyline blockMap(), imageDef(), currHandle() @@ -80,17 +80,16 @@ void dxfRW::setDebug(DRW::DebugLevel lvl){ bool dxfRW::read(DRW_Interface *interface_, bool ext){ drw_assert(fileName.empty() == false); - bool isOk = false; applyExt = ext; std::ifstream filestr; - if ( interface_ == nullptr ) - return isOk; + if (nullptr == interface_) { + return setError(DRW::BAD_UNKNOWN); + } DRW_DBG("dxfRW::read 1def\n"); filestr.open (fileName.c_str(), std::ios_base::in | std::ios::binary); - if (!filestr.is_open()) - return isOk; - if (!filestr.good()) - return isOk; + if (!filestr.is_open() || !filestr.good()) { + return setError(DRW::BAD_OPEN); + } char line[22]; char line2[22] = "AutoCAD Binary DXF\r\n"; @@ -113,7 +112,7 @@ bool dxfRW::read(DRW_Interface *interface_, bool ext){ reader = new dxfReaderAscii(&filestr); } - isOk = processDxf(); + bool isOk = processDxf(); filestr.close(); delete reader; reader = nullptr; @@ -211,6 +210,52 @@ bool dxfRW::writeEntity(DRW_Entity *ent) { if (version > DRW::AC1014) { writer->writeInt16(370, DRW_LW_Conv::lineWidth2dxfInt(ent->lWeight)); } + if (version >= DRW::AC1014) { + writeAppData(ent->appData); + } + return true; +} + +bool dxfRW::writeAppData(const std::list>& appData) { + for(const auto& group : appData) { + //Search for application name + bool found = false; + + for(const auto& data : group) { + if(data.code == 102 && data.type == DRW_Variant::STRING) { + writer->writeString(102, "{" + *(data.content.s)); + found = true; + break; + } + } + + if(found) { + for(const auto& data : group) { + if(data.code == 102) { + continue; + } + + switch(data.type) { + case DRW_Variant::STRING: + writer->writeString(data.code, *(data.content.s)); + break; + + case DRW_Variant::INTEGER: + writer->writeInt32(data.code, data.content.i); + break; + + case DRW_Variant::DOUBLE: + writer->writeDouble(data.code, data.content.i); + break; + + default: + break; + } + } + + writer->writeString(102, "}"); + } + } return true; } @@ -1813,6 +1858,8 @@ bool dxfRW::writeObjects() { imageDef.pop_back(); } + iface->writeObjects(); + return true; } @@ -1865,49 +1912,99 @@ bool dxfRW::writeExtData(const std::vector &ed){ bool dxfRW::processDxf() { DRW_DBG("dxfRW::processDxf() start processing dxf\n"); - int code; - bool more = true; - std::string sectionstr; -// section = secUnknown; + int code = -1; + bool inSection = false; + + reader->setIgnoreComments( false); while (reader->readRec(&code)) { - DRW_DBG(code); DRW_DBG(" processDxf\n"); - if (code == 999) { - header.addComment(reader->getString()); - } else if (code == 0) { - sectionstr = reader->getString(); - DRW_DBG(sectionstr); DRW_DBG(" processDxf\n"); - if (sectionstr == "EOF") { - return true; //found EOF terminate - } - if (sectionstr == "SECTION") { - more = reader->readRec(&code); - DRW_DBG(code); DRW_DBG(" processDxf\n"); - if (!more) - return false; //wrong dxf file - if (code == 2) { - sectionstr = reader->getString(); - DRW_DBG(sectionstr); DRW_DBG(" processDxf\n"); - //found section, process it - if (sectionstr == "HEADER") { - processHeader(); - } else if (sectionstr == "CLASSES") { -// processClasses(); - } else if (sectionstr == "TABLES") { - processTables(); - } else if (sectionstr == "BLOCKS") { - processBlocks(); - } else if (sectionstr == "ENTITIES") { - processEntities(false); - } else if (sectionstr == "OBJECTS") { - processObjects(); + DRW_DBG(code); DRW_DBG(" code\n"); + /* at this level we should only get: + 999 - Comment + 0 - SECTION or EOF + 2 - section name + everything else between "2 - section name" and "0 - ENDSEC" is handled in process() methods + */ + switch (code) { + case 999: // when DXF was created by libdxfrw, first record is a comment with dxfrw version info + header.addComment( reader->getString()); + continue; + + case 0: + // ignore further comments, as libdxfrw doesn't support comments in sections + reader->setIgnoreComments( true); + if (!inSection) { + std::string sectionstr {reader->getString()}; + + if ("SECTION" == sectionstr) { + DRW_DBG(sectionstr); DRW_DBG(" new section\n"); + inSection = true; + continue; + } + if ("EOF" == sectionstr) { + return true; //found EOF terminate } } - } + else { + // in case SECTION was unknown or not supported + if ("ENDSEC" == reader->getString()) { + inSection = false; + } + } + break; + + case 2: + if (inSection) { + bool processed {false}; + std::string sectionname {reader->getString()}; + + DRW_DBG(sectionname); DRW_DBG(" process section\n"); + if ("HEADER" == sectionname) { + processed = processHeader(); + } + else if ("TABLES" == sectionname) { + processed = processTables(); + } + else if ("BLOCKS" == sectionname) { + processed = processBlocks(); + } + else if ("ENTITIES" == sectionname) { + processed = processEntities(false); + } + else if ("OBJECTS" == sectionname) { + processed = processObjects(); + } + else { + //TODO handle CLASSES + + DRW_DBG("section unknown or not supported\n"); + continue; + } + + if (!processed) { + DRW_DBG(" failed\n"); + return setError(DRW::BAD_READ_SECTION); + } + + inSection = false; + } + continue; + + default: + // landing here means an unknown or not supported SECTION + inSection = false; + break; } /* if (!more) return true;*/ } - return true; + + if (0 == code && "EOF" == reader->getString()) { + // in case the final EOF has no newline we end up here! + // this is caused by filestr->good() which is false for missing newline on EOF + return true; + } + + return setError(DRW::BAD_UNKNOWN); } /********* Header Section *********/ @@ -1925,9 +2022,14 @@ bool dxfRW::processHeader() { iface->addHeader(&header); return true; //found ENDSEC terminate } - } else header.parseCode(code, reader); + DRW_DBG("unexpected 0 code in header!\n"); + return setError(DRW::BAD_READ_HEADER); + } + + header.parseCode(code, reader); } - return true; + + return setError(DRW::BAD_READ_HEADER); } /********* Tables Section *********/ @@ -1945,8 +2047,9 @@ bool dxfRW::processTables() { if (sectionstr == "TABLE") { more = reader->readRec(&code); DRW_DBG(code); DRW_DBG("\n"); - if (!more) - return false; //wrong dxf file + if (!more) { + return setError(DRW::BAD_READ_TABLES); //wrong dxf file + } if (code == 2) { sectionstr = reader->getString(); DRW_DBG(sectionstr); DRW_DBG(" processHeader\n\n"); @@ -1976,7 +2079,8 @@ bool dxfRW::processTables() { } } } - return true; + + return setError(DRW::BAD_READ_TABLES); } bool dxfRW::processLType() { @@ -2003,7 +2107,8 @@ bool dxfRW::processLType() { } else if (reading) ltype.parseCode(code, reader); } - return true; + + return setError(DRW::BAD_READ_TABLES); } bool dxfRW::processLayer() { @@ -2028,7 +2133,8 @@ bool dxfRW::processLayer() { } else if (reading) layer.parseCode(code, reader); } - return true; + + return setError(DRW::BAD_READ_TABLES); } bool dxfRW::processDimStyle() { @@ -2053,7 +2159,8 @@ bool dxfRW::processDimStyle() { } else if (reading) dimSty.parseCode(code, reader); } - return true; + + return setError(DRW::BAD_READ_TABLES); } bool dxfRW::processTextStyle(){ @@ -2078,7 +2185,8 @@ bool dxfRW::processTextStyle(){ } else if (reading) TxtSty.parseCode(code, reader); } - return true; + + return setError(DRW::BAD_READ_TABLES); } bool dxfRW::processVports(){ @@ -2103,7 +2211,8 @@ bool dxfRW::processVports(){ } else if (reading) vp.parseCode(code, reader); } - return true; + + return setError(DRW::BAD_READ_TABLES); } bool dxfRW::processAppId(){ @@ -2128,7 +2237,8 @@ bool dxfRW::processAppId(){ } else if (reading) vp.parseCode(code, reader); } - return true; + + return setError(DRW::BAD_READ_TABLES); } /********* Block Section *********/ @@ -2149,7 +2259,8 @@ bool dxfRW::processBlocks() { } } } - return true; + + return setError(DRW::BAD_READ_BLOCKS); } bool dxfRW::processBlock() { @@ -2177,7 +2288,8 @@ bool dxfRW::processBlock() { break; } } - return true; + + return setError(DRW::BAD_READ_BLOCKS); } @@ -2187,69 +2299,74 @@ bool dxfRW::processEntities(bool isblock) { DRW_DBG("dxfRW::processEntities\n"); int code; if (!reader->readRec(&code)){ - return false; + return setError(DRW::BAD_READ_ENTITIES); } bool next = true; if (code == 0) { - nextentity = reader->getString(); + nextentity = reader->getString(); } else if (!isblock) { - return false; //first record in entities is 0 - } + return setError(DRW::BAD_READ_ENTITIES); //first record in entities is 0 + } + bool processed {false}; do { if (nextentity == "ENDSEC" || nextentity == "ENDBLK") { return true; //found ENDSEC or ENDBLK terminate } else if (nextentity == "POINT") { - processPoint(); + processed = processPoint(); } else if (nextentity == "LINE") { - processLine(); + processed = processLine(); } else if (nextentity == "CIRCLE") { - processCircle(); + processed = processCircle(); } else if (nextentity == "ARC") { - processArc(); + processed = processArc(); } else if (nextentity == "ELLIPSE") { - processEllipse(); + processed = processEllipse(); } else if (nextentity == "TRACE") { - processTrace(); + processed = processTrace(); } else if (nextentity == "SOLID") { - processSolid(); + processed = processSolid(); } else if (nextentity == "INSERT") { - processInsert(); + processed = processInsert(); } else if (nextentity == "LWPOLYLINE") { - processLWPolyline(); + processed = processLWPolyline(); } else if (nextentity == "POLYLINE") { - processPolyline(); + processed = processPolyline(); } else if (nextentity == "TEXT") { - processText(); + processed = processText(); } else if (nextentity == "MTEXT") { - processMText(); + processed = processMText(); } else if (nextentity == "HATCH") { - processHatch(); + processed = processHatch(); } else if (nextentity == "SPLINE") { - processSpline(); + processed = processSpline(); } else if (nextentity == "3DFACE") { - process3dface(); + processed = process3dface(); } else if (nextentity == "VIEWPORT") { - processViewport(); + processed = processViewport(); } else if (nextentity == "IMAGE") { - processImage(); + processed = processImage(); } else if (nextentity == "DIMENSION") { - processDimension(); + processed = processDimension(); } else if (nextentity == "LEADER") { - processLeader(); + processed = processLeader(); } else if (nextentity == "RAY") { - processRay(); + processed = processRay(); } else if (nextentity == "XLINE") { - processXline(); + processed = processXline(); } else { - if (reader->readRec(&code)){ - if (code == 0) - nextentity = reader->getString(); - } else - return false; //end of file without ENDSEC + if (!reader->readRec(&code)) { + return setError(DRW::BAD_READ_ENTITIES); //end of file without ENDSEC + } + + if (code == 0) { + nextentity = reader->getString(); + } + processed = true; } - } while (next); - return true; + } while (processed); + + return setError(DRW::BAD_READ_ENTITIES); } bool dxfRW::processEllipse() { @@ -2272,7 +2389,8 @@ bool dxfRW::processEllipse() { break; } } - return true; + + return setError(DRW::BAD_READ_ENTITIES); } bool dxfRW::processTrace() { @@ -2295,7 +2413,8 @@ bool dxfRW::processTrace() { break; } } - return true; + + return setError(DRW::BAD_READ_ENTITIES); } bool dxfRW::processSolid() { @@ -2318,7 +2437,8 @@ bool dxfRW::processSolid() { break; } } - return true; + + return setError(DRW::BAD_READ_ENTITIES); } bool dxfRW::process3dface() { @@ -2339,7 +2459,8 @@ bool dxfRW::process3dface() { break; } } - return true; + + return setError(DRW::BAD_READ_ENTITIES); } bool dxfRW::processViewport() { @@ -2360,7 +2481,8 @@ bool dxfRW::processViewport() { break; } } - return true; + + return setError(DRW::BAD_READ_ENTITIES); } bool dxfRW::processPoint() { @@ -2381,7 +2503,8 @@ bool dxfRW::processPoint() { break; } } - return true; + + return setError(DRW::BAD_READ_ENTITIES); } bool dxfRW::processLine() { @@ -2402,7 +2525,8 @@ bool dxfRW::processLine() { break; } } - return true; + + return setError(DRW::BAD_READ_ENTITIES); } bool dxfRW::processRay() { @@ -2423,7 +2547,8 @@ bool dxfRW::processRay() { break; } } - return true; + + return setError(DRW::BAD_READ_ENTITIES); } bool dxfRW::processXline() { @@ -2444,7 +2569,8 @@ bool dxfRW::processXline() { break; } } - return true; + + return setError(DRW::BAD_READ_ENTITIES); } bool dxfRW::processCircle() { @@ -2467,7 +2593,8 @@ bool dxfRW::processCircle() { break; } } - return true; + + return setError(DRW::BAD_READ_ENTITIES); } bool dxfRW::processArc() { @@ -2490,7 +2617,8 @@ bool dxfRW::processArc() { break; } } - return true; + + return setError(DRW::BAD_READ_ENTITIES); } bool dxfRW::processInsert() { @@ -2511,7 +2639,8 @@ bool dxfRW::processInsert() { break; } } - return true; + + return setError(DRW::BAD_READ_ENTITIES); } bool dxfRW::processLWPolyline() { @@ -2534,7 +2663,8 @@ bool dxfRW::processLWPolyline() { break; } } - return true; + + return setError(DRW::BAD_READ_ENTITIES); } bool dxfRW::processPolyline() { @@ -2560,7 +2690,8 @@ bool dxfRW::processPolyline() { break; } } - return true; + + return setError(DRW::BAD_READ_ENTITIES); } bool dxfRW::processVertex(DRW_Polyline *pl) { @@ -2586,7 +2717,8 @@ bool dxfRW::processVertex(DRW_Polyline *pl) { break; } } - return true; + + return setError(DRW::BAD_READ_ENTITIES); } bool dxfRW::processText() { @@ -2607,7 +2739,8 @@ bool dxfRW::processText() { break; } } - return true; + + return setError(DRW::BAD_READ_ENTITIES); } bool dxfRW::processMText() { @@ -2629,7 +2762,8 @@ bool dxfRW::processMText() { break; } } - return true; + + return setError(DRW::BAD_READ_ENTITIES); } bool dxfRW::processHatch() { @@ -2650,7 +2784,8 @@ bool dxfRW::processHatch() { break; } } - return true; + + return setError(DRW::BAD_READ_ENTITIES); } @@ -2672,7 +2807,8 @@ bool dxfRW::processSpline() { break; } } - return true; + + return setError(DRW::BAD_READ_ENTITIES); } @@ -2694,7 +2830,8 @@ bool dxfRW::processImage() { break; } } - return true; + + return setError(DRW::BAD_READ_ENTITIES); } @@ -2753,7 +2890,8 @@ bool dxfRW::processDimension() { break; } } - return true; + + return setError(DRW::BAD_READ_ENTITIES); } bool dxfRW::processLeader() { @@ -2774,7 +2912,8 @@ bool dxfRW::processLeader() { break; } } - return true; + + return setError(DRW::BAD_READ_ENTITIES); } @@ -2783,30 +2922,37 @@ bool dxfRW::processLeader() { bool dxfRW::processObjects() { DRW_DBG("dxfRW::processObjects\n"); int code; - if (!reader->readRec(&code)){ - return false; + if (!reader->readRec(&code) || 0 != code){ + return setError(DRW::BAD_READ_OBJECTS); //first record in objects must be 0 } - bool next = true; - if (code == 0) { - nextentity = reader->getString(); - } else { - return false; //first record in objects is 0 - } + + bool processed = false; + nextentity = reader->getString(); do { - if (nextentity == "ENDSEC") { + if ("ENDSEC" == nextentity) { return true; //found ENDSEC terminate - } else if (nextentity == "IMAGEDEF") { - processImageDef(); - } else { - if (reader->readRec(&code)){ - if (code == 0) - nextentity = reader->getString(); - } else - return false; //end of file without ENDSEC } - } while (next); - return true; + if ("IMAGEDEF" == nextentity) { + processed = processImageDef(); + } + else if ("PLOTSETTINGS" == nextentity) { + processed = processPlotSettings(); + } + else { + if (!reader->readRec(&code)) { + return setError(DRW::BAD_READ_OBJECTS); //end of file without ENDSEC + } + + if (code == 0) { + nextentity = reader->getString(); + } + processed = true; + } + } + while (processed); + + return setError(DRW::BAD_READ_OBJECTS); } bool dxfRW::processImageDef() { @@ -2827,6 +2973,41 @@ bool dxfRW::processImageDef() { break; } } + + return setError(DRW::BAD_READ_OBJECTS); +} + +bool dxfRW::processPlotSettings() { + DRW_DBG("dxfRW::processPlotSettings"); + int code; + DRW_PlotSettings ps; + while (reader->readRec(&code)) { + DRW_DBG(code); DRW_DBG("\n"); + switch (code) { + case 0: { + nextentity = reader->getString(); + DRW_DBG(nextentity); DRW_DBG("\n"); + iface->addPlotSettings(&ps); + return true; //found new entity or ENDSEC, terminate + } + default: + ps.parseCode(code, reader); + break; + } + } + + return setError(DRW::BAD_READ_OBJECTS); +} + +bool dxfRW::writePlotSettings(DRW_PlotSettings *ent) { + writer->writeString(0, "PLOTSETTINGS"); + writer->writeString(5, toHexStr(++entCount)); + writer->writeString(100, "AcDbPlotSettings"); + writer->writeUtf8String(6, ent->plotViewName); + writer->writeDouble(40, ent->marginLeft); + writer->writeDouble(41, ent->marginBottom); + writer->writeDouble(42, ent->marginRight); + writer->writeDouble(43, ent->marginTop); return true; } @@ -2844,3 +3025,18 @@ std::string dxfRW::toHexStr(int n){ return Convert.str(); #endif } + +DRW::Version dxfRW::getVersion() const { + return version; +} + +DRW::error dxfRW::getError() const +{ + return error; +} + +bool dxfRW::setError(const DRW::error lastError) +{ + error = lastError; + return (DRW::BAD_NONE == error); +} diff --git a/src/libs/vdxf/libdxfrw/libdxfrw.h b/src/libs/vdxf/libdxfrw/libdxfrw.h index 1052dc6b2..f51bb0d77 100644 --- a/src/libs/vdxf/libdxfrw/libdxfrw.h +++ b/src/libs/vdxf/libdxfrw/libdxfrw.h @@ -71,9 +71,12 @@ public: DRW_ImageDef *writeImage(DRW_Image *ent, const std::string &name); bool writeLeader(DRW_Leader *ent); bool writeDimension(DRW_Dimension *ent); - void setEllipseParts(int parts){elParts = parts;} /*!< set parts munber when convert ellipse to polyline */ + void setEllipseParts(int parts){elParts = parts;} /*!< set parts number when convert ellipse to polyline */ + bool writePlotSettings(DRW_PlotSettings *ent); std::string ErrorString() const; + DRW::Version getVersion() const; + DRW::error getError() const; private: Q_DISABLE_COPY(dxfRW) @@ -116,6 +119,7 @@ private: bool processImageDef(); bool processDimension(); bool processLeader(); + bool processPlotSettings(); // bool writeHeader(); bool writeEntity(DRW_Entity *ent); @@ -124,9 +128,13 @@ private: bool writeObjects(); bool writeExtData(const std::vector &ed); static std::string toHexStr(int n);//RLZ removeme + bool writeAppData(const std::list> &appData); + + bool setError(const DRW::error lastError); private: DRW::Version version; + DRW::error error {DRW::BAD_NONE}; std::string fileName; std::string codePage; bool binFile; @@ -141,7 +149,7 @@ private: bool dimstyleStd; bool applyExt; bool writingBlock; - int elParts; /*!< parts munber when convert ellipse to polyline */ + int elParts; /*!< parts number when convert ellipse to polyline */ std::unordered_map blockMap; std::vector imageDef; /*!< imageDef list */ diff --git a/src/libs/vdxf/libdxfrw/main_doc.h b/src/libs/vdxf/libdxfrw/main_doc.h index 2d9342c9e..d2bd5e0b8 100644 --- a/src/libs/vdxf/libdxfrw/main_doc.h +++ b/src/libs/vdxf/libdxfrw/main_doc.h @@ -5,9 +5,9 @@ * This manual documents the use of libdxfrw. * * With libdxfrw you can read and write several parts of a dxf files.

- * Dxf files can be written in assci and binary form, both are supported.

+ * Dxf files can be written in ascii and binary form, both are supported.

* * the complete documentation and examples are pending to free time, - * but to start see DRW_Interface, dxfRW clases + * but to start see DRW_Interface, dxfRW classes */