From 273131c08857464fe7a140331afc864b7ae752f8 Mon Sep 17 00:00:00 2001 From: Yorik van Havre Date: Mon, 4 Jan 2016 18:01:34 -0200 Subject: [PATCH] Draft: finished new DXF importer - fixes #2205 --- src/Mod/Draft/App/DraftDxf.cpp | 33 +++++++++++++++++--------- src/Mod/Draft/App/DraftDxf.h | 3 ++- src/Mod/Draft/App/dxf.cpp | 42 +++++++++++++++++++++++----------- 3 files changed, 53 insertions(+), 25 deletions(-) diff --git a/src/Mod/Draft/App/DraftDxf.cpp b/src/Mod/Draft/App/DraftDxf.cpp index 674496f02..9a75bf8e5 100644 --- a/src/Mod/Draft/App/DraftDxf.cpp +++ b/src/Mod/Draft/App/DraftDxf.cpp @@ -43,6 +43,7 @@ #include #include #include +#include #include #include #include @@ -55,6 +56,7 @@ DraftDxfRead::DraftDxfRead(std::string filepath, App::Document *pcDoc) : CDxfRea document = pcDoc; ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Draft"); optionGroupLayers = hGrp->GetBool("groupLayers",false); + optionImportAnnotations = hGrp->GetBool("dxftext",false); } @@ -129,19 +131,21 @@ void DraftDxfRead::OnReadEllipse(const double* c, double major_radius, double mi void DraftDxfRead::OnReadText(const double *point, const double height, const char* text) { - Base::Vector3d pt(point[0],point[1],point[2]); - if(LayerName().substr(0, 6) != "BLOCKS") { - App::Annotation *pcFeature = (App::Annotation *)document->addObject("App::Annotation", "Text"); - pcFeature->LabelText.setValue(Deformat(text)); - pcFeature->Position.setValue(pt); + if (optionImportAnnotations) { + Base::Vector3d pt(point[0],point[1],point[2]); + if(LayerName().substr(0, 6) != "BLOCKS") { + App::Annotation *pcFeature = (App::Annotation *)document->addObject("App::Annotation", "Text"); + pcFeature->LabelText.setValue(Deformat(text)); + pcFeature->Position.setValue(pt); + } + //else std::cout << "skipped text in block: " << LayerName() << std::endl; } - else std::cout << "skipped text in block: " << LayerName() << std::endl; } void DraftDxfRead::OnReadInsert(const double* point, const double* scale, const char* name, double rotation) { - std::cout << "Inserting block " << name << " rotation " << rotation << " pos " << point[0] << "," << point[1] << "," << point[2] << " scale " << scale[0] << "," << scale[1] << "," << scale[2] << std::endl; + //std::cout << "Inserting block " << name << " rotation " << rotation << " pos " << point[0] << "," << point[1] << "," << point[2] << " scale " << scale[0] << "," << scale[1] << "," << scale[2] << std::endl; for(std::map > ::const_iterator i = layers.begin(); i != layers.end(); ++i) { std::string k = i->first; std::string prefix = "BLOCKS "; @@ -173,7 +177,13 @@ void DraftDxfRead::OnReadInsert(const double* point, const double* scale, const void DraftDxfRead::OnReadDimension(const double* s, const double* e, const double* point, double rotation) { - std::cout << "Dimension: " << std::endl; + if (optionImportAnnotations) { + Base::Interpreter().runString("import Draft"); + Base::Interpreter().runStringArg("p1=FreeCAD.Vector(%f,%f,%f)",s[0],s[1],s[2]); + Base::Interpreter().runStringArg("p2=FreeCAD.Vector(%f,%f,%f)",e[0],e[1],e[2]); + Base::Interpreter().runStringArg("p3=FreeCAD.Vector(%f,%f,%f)",point[0],point[1],point[2]); + Base::Interpreter().runString("Draft.makeDimension(p1,p2,p3)"); + } } @@ -192,7 +202,7 @@ void DraftDxfRead::AddObject(Part::TopoShape *shape) } -const char* DraftDxfRead::Deformat(const char* text) +std::string DraftDxfRead::Deformat(const char* text) { // this function removes DXF formatting from texts std::stringstream ss; @@ -223,10 +233,11 @@ const char* DraftDxfRead::Deformat(const char* text) } } } - else if ( (text[i] != '{') && (text[i] != '}') ) + else if ( (text[i] != '{') && (text[i] != '}') ) { ss << text[i]; + } } - return ss.str().c_str(); + return ss.str(); } diff --git a/src/Mod/Draft/App/DraftDxf.h b/src/Mod/Draft/App/DraftDxf.h index 3a89fd0a4..e4f50b19b 100644 --- a/src/Mod/Draft/App/DraftDxf.h +++ b/src/Mod/Draft/App/DraftDxf.h @@ -48,11 +48,12 @@ namespace DraftUtils // FreeCAD-specific functions void AddObject(Part::TopoShape *shape); //Called by OnRead functions to add Part objects - const char* Deformat(const char* text); // Removes DXF formating from texts + std::string Deformat(const char* text); // Removes DXF formating from texts protected: App::Document *document; bool optionGroupLayers; + bool optionImportAnnotations; std::map > layers; }; } diff --git a/src/Mod/Draft/App/dxf.cpp b/src/Mod/Draft/App/dxf.cpp index a81757dca..d524e014c 100644 --- a/src/Mod/Draft/App/dxf.cpp +++ b/src/Mod/Draft/App/dxf.cpp @@ -806,19 +806,19 @@ bool CDxfRead::ReadText() get_line(); ss.str(m_str); ss >> c[2]; c[2] = mm(c[2]); if(ss.fail()) return false; break; - case 40: + case 40: // text height get_line(); ss.str(m_str); ss >> height; height = mm(height); if(ss.fail()) return false; break; - case 1: + case 1: // text get_line(); DerefACI(); OnReadText(c, height * 25.4 / 72.0, m_str); return(true); - case 62: + case 62: // color index get_line(); ss.str(m_str); ss >> m_aci; if(ss.fail()) return false; @@ -1412,7 +1412,7 @@ bool CDxfRead::ReadDimension() double s[3]; // startpoint double e[3]; // endpoint double p[3]; // dimpoint - double rot = 0.0; // rotation + double rot = -1.0; // rotation while(!((*m_ifs).eof())) { @@ -1436,47 +1436,47 @@ bool CDxfRead::ReadDimension() get_line(); strcpy(m_layer_name, m_str); break; - case 12: + case 13: // start x get_line(); ss.str(m_str); ss >> s[0]; s[0] = mm(s[0]); if(ss.fail()) return false; break; - case 22: + case 23: // start y get_line(); ss.str(m_str); ss >> s[1]; s[1] = mm(s[1]); if(ss.fail()) return false; break; - case 32: + case 33: // start z get_line(); ss.str(m_str); ss >> s[2]; s[2] = mm(s[2]); if(ss.fail()) return false; break; - case 13: + case 14: // end x get_line(); ss.str(m_str); ss >> e[0]; e[0] = mm(e[0]); if(ss.fail()) return false; break; - case 23: + case 24: // end y get_line(); ss.str(m_str); ss >> e[1]; e[1] = mm(e[1]); if(ss.fail()) return false; break; - case 33: + case 34: // end z get_line(); ss.str(m_str); ss >> e[2]; e[2] = mm(e[2]); if(ss.fail()) return false; break; - case 14: + case 10: // dimline x get_line(); ss.str(m_str); ss >> p[0]; p[0] = mm(p[0]); if(ss.fail()) return false; break; - case 24: + case 20: // dimline y get_line(); ss.str(m_str); ss >> p[1]; p[1] = mm(p[1]); if(ss.fail()) return false; break; - case 34: + case 30: // dimline z get_line(); ss.str(m_str); ss >> p[2]; p[2] = mm(p[2]); if(ss.fail()) return false; @@ -1713,6 +1713,14 @@ void CDxfRead::DoRead(const bool ignore_errors /* = false */ ) } continue; } + else if(!strcmp(m_str, "TEXT")){ + if(!ReadText()) + { + printf("CDxfRead::DoRead() Failed to read text\n"); + return; + } + continue; + } else if(!strcmp(m_str, "ELLIPSE")){ if(!ReadEllipse()) { @@ -1761,6 +1769,14 @@ void CDxfRead::DoRead(const bool ignore_errors /* = false */ ) } continue; } + else if (!strcmp(m_str, "DIMENSION")) { + if(!ReadDimension()) + { + printf("CDxfRead::DoRead() Failed to read Dimension\n"); + return; + } + continue; + } } get_line();