diff --git a/src/Mod/Draft/App/DraftDxf.cpp b/src/Mod/Draft/App/DraftDxf.cpp index f7fd15c937..4a3558ae2d 100644 --- a/src/Mod/Draft/App/DraftDxf.cpp +++ b/src/Mod/Draft/App/DraftDxf.cpp @@ -41,6 +41,7 @@ #include #include +#include #include #include #include @@ -124,22 +125,53 @@ void DraftDxfRead::OnReadEllipse(const double* c, double major_radius, double mi } -void DraftDxfRead::OnReadText(const double *point, const double height, const std::string text) +void DraftDxfRead::OnReadText(const double *point, const double height, const char* text) { // not yet implemented } +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] << std::endl; + for(std::map > ::const_iterator i = layers.begin(); i != layers.end(); ++i) { + std::string k = i->first; + std::string prefix = "BLOCKS "; + prefix += name; + prefix += " "; + if(k.substr(0, prefix.size()) == prefix) { + BRep_Builder builder; + TopoDS_Compound comp; + builder.MakeCompound(comp); + std::vector v = i->second; + for(std::vector::const_iterator j = v.begin(); j != v.end(); ++j) { + const TopoDS_Shape& sh = (*j)->_Shape; + if (!sh.IsNull()) + builder.Add(comp, sh); + } + if (!comp.IsNull()) { + Part::TopoShape* pcomp = new Part::TopoShape(comp); + Base::Matrix4D mat; + mat.scale(scale[0],scale[1],scale[2]); + mat.rotZ(rotation); + mat.move(point[0],point[1],point[2]); + pcomp->transformShape(mat,true); + AddObject(pcomp); + } + } + } +} + + void DraftDxfRead::AddObject(Part::TopoShape *shape) { - if (optionGroupLayers) { - std::cout << "layer:" << LayerName() << std::endl; - std::vector vec; - if (layers.count(LayerName())) - vec = layers[LayerName()]; - vec.push_back(shape); - layers[LayerName()] = vec; - } else { + //std::cout << "layer:" << LayerName() << std::endl; + std::vector vec; + if (layers.count(LayerName())) + vec = layers[LayerName()]; + vec.push_back(shape); + layers[LayerName()] = vec; + if (!optionGroupLayers) { Part::Feature *pcFeature = (Part::Feature *)document->addObject("Part::Feature", "Shape"); pcFeature->Shape.setValue(*shape); } @@ -156,18 +188,20 @@ void DraftDxfRead::AddGraphics() const builder.MakeCompound(comp); std::string k = i->first; std::vector v = i->second; - std::cout << "joining:" << k << " size " << v.size() << std::endl; - for(std::vector::const_iterator j = v.begin(); j != v.end(); ++j) { - const TopoDS_Shape& sh = (*j)->_Shape; - if (!sh.IsNull()) - builder.Add(comp, sh); + if(k.substr(0, 6) != "BLOCKS") { + std::cout << "joining:" << k << " size " << v.size() << std::endl; + for(std::vector::const_iterator j = v.begin(); j != v.end(); ++j) { + const TopoDS_Shape& sh = (*j)->_Shape; + if (!sh.IsNull()) + builder.Add(comp, sh); + } + if (!comp.IsNull()) { + std::cout << "valid shape" << std::endl; + Part::Feature *pcFeature = (Part::Feature *)document->addObject("Part::Feature", k.c_str()); + pcFeature->Shape.setValue(comp); + } + else std::cout << "invalid shape" << std::endl; } - if (!comp.IsNull()) { - std::cout << "valid shape" << std::endl; - Part::Feature *pcFeature = (Part::Feature *)document->addObject("Part::Feature", k.c_str()); - pcFeature->Shape.setValue(comp); - } - else std::cout << "invalid shape" << std::endl; } } } diff --git a/src/Mod/Draft/App/DraftDxf.h b/src/Mod/Draft/App/DraftDxf.h index 63375a8ff7..c9ab0f834e 100644 --- a/src/Mod/Draft/App/DraftDxf.h +++ b/src/Mod/Draft/App/DraftDxf.h @@ -37,11 +37,12 @@ namespace DraftUtils // CDxfRead's virtual functions void OnReadLine(const double* s, const double* e, bool hidden); void OnReadPoint(const double* s); - void OnReadText(const double* point, const double height, const std::string text); + void OnReadText(const double* point, const double height, const char* text); void OnReadArc(const double* s, const double* e, const double* c, bool dir, bool hidden); void OnReadCircle(const double* s, const double* c, bool dir, bool hidden); void OnReadEllipse(const double* c, double major_radius, double minor_radius, double rotation, double start_angle, double end_angle, bool dir); void OnReadSpline(struct SplineData& sd); + void OnReadInsert(const double* point, const double* scale, const char* name, double rotation); void AddGraphics() const; // FreeCAD-specific functions diff --git a/src/Mod/Draft/App/dxf.cpp b/src/Mod/Draft/App/dxf.cpp index febda59e71..fed2fcad60 100644 --- a/src/Mod/Draft/App/dxf.cpp +++ b/src/Mod/Draft/App/dxf.cpp @@ -1311,6 +1311,102 @@ void CDxfRead::OnReadEllipse(const double* c, const double* m, double ratio, dou OnReadEllipse(c, major_radius, minor_radius, rotation, start_angle, end_angle, true); } + +bool CDxfRead::ReadInsert() +{ + double c[3]; // coordinate + double s[3]; // scale + double rot = 0.0; // rotation + char name[1024]; + s[0] = 1.0; + s[1] = 1.0; + s[2] = 1.0; + + while(!((*m_ifs).eof())) + { + get_line(); + int n; + if(sscanf(m_str, "%d", &n) != 1) + { + printf("CDxfRead::ReadInsert() Failed to read integer from '%s'\n", m_str); + return false; + } + std::istringstream ss; + ss.imbue(std::locale("C")); + switch(n){ + case 0: + // next item found + DerefACI(); + OnReadInsert(c, s, name, rot * Pi/180); + return(true); + case 8: + // Layer name follows + get_line(); + strcpy(m_layer_name, m_str); + break; + case 10: + // coord x + get_line(); + ss.str(m_str); ss >> c[0]; c[0] = mm(c[0]); if(ss.fail()) return false; + break; + case 20: + // coord y + get_line(); + ss.str(m_str); ss >> c[1]; c[1] = mm(c[1]); if(ss.fail()) return false; + break; + case 30: + // coord z + get_line(); + ss.str(m_str); ss >> c[2]; c[2] = mm(c[2]); if(ss.fail()) return false; + break; + case 41: + // scale x + get_line(); + ss.str(m_str); ss >> s[0]; if(ss.fail()) return false; + break; + case 42: + // scale y + get_line(); + ss.str(m_str); ss >> s[1]; if(ss.fail()) return false; + break; + case 43: + // scale z + get_line(); + ss.str(m_str); ss >> s[2]; if(ss.fail()) return false; + break; + case 50: + // rotation + get_line(); + ss.str(m_str); ss >> rot; if(ss.fail()) return false; + break; + case 2: + // block name + get_line(); + strcpy(name, m_str); + break; + case 62: + // color index + get_line(); + ss.str(m_str); ss >> m_aci; if(ss.fail()) return false; + break; + case 100: + case 39: + case 210: + case 220: + case 230: + // skip the next line + get_line(); + break; + default: + // skip the next line + get_line(); + break; + } + } + return false; +} + + void CDxfRead::get_line() { if (m_unused_line[0] != '\0') @@ -1555,6 +1651,14 @@ void CDxfRead::DoRead(const bool ignore_errors /* = false */ ) } continue; } + else if (!strcmp(m_str, "INSERT")) { + if(!ReadInsert()) + { + printf("CDxfRead::DoRead() Failed to read Insert\n"); + return; + } + continue; + } } get_line(); diff --git a/src/Mod/Draft/App/dxf.h b/src/Mod/Draft/App/dxf.h index bb895f6d3b..645ada294c 100644 --- a/src/Mod/Draft/App/dxf.h +++ b/src/Mod/Draft/App/dxf.h @@ -124,6 +124,7 @@ private: void OnReadArc(double start_angle, double end_angle, double radius, const double* c, double z_extrusion_dir, bool hidden); void OnReadCircle(const double* c, double radius, bool hidden); void OnReadEllipse(const double* c, const double* m, double ratio, double start_angle, double end_angle); + bool ReadInsert(); void get_line(); void put_line(const char *value); @@ -150,6 +151,7 @@ public: virtual void OnReadCircle(const double* s, const double* c, bool dir, bool hidden){} virtual void OnReadEllipse(const double* c, double major_radius, double minor_radius, double rotation, double start_angle, double end_angle, bool dir){} virtual void OnReadSpline(struct SplineData& sd){} + virtual void OnReadInsert(const double* point, const double* scale, const char* name, double rotation){} virtual void AddGraphics() const { } std::string LayerName() const;