Skip to content

Commit

Permalink
Draft: Added blocks support to the new dxf importer #2205
Browse files Browse the repository at this point in the history
  • Loading branch information
yorikvanhavre committed Aug 12, 2015
1 parent 73808aa commit 51cff8f
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 21 deletions.
74 changes: 54 additions & 20 deletions src/Mod/Draft/App/DraftDxf.cpp
Expand Up @@ -41,6 +41,7 @@
#include <TopoDS_Shape.hxx>

#include <Base/Parameter.h>
#include <Base/Matrix.h>
#include <App/Application.h>
#include <App/Document.h>
#include <Mod/Part/App/PartFeature.h>
Expand Down Expand Up @@ -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<std::string,std::vector<Part::TopoShape*> > ::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<Part::TopoShape*> v = i->second;
for(std::vector<Part::TopoShape*>::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 <Part::TopoShape*> vec;
if (layers.count(LayerName()))
vec = layers[LayerName()];
vec.push_back(shape);
layers[LayerName()] = vec;
} else {
//std::cout << "layer:" << LayerName() << std::endl;
std::vector <Part::TopoShape*> 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);
}
Expand All @@ -156,18 +188,20 @@ void DraftDxfRead::AddGraphics() const
builder.MakeCompound(comp);
std::string k = i->first;
std::vector<Part::TopoShape*> v = i->second;
std::cout << "joining:" << k << " size " << v.size() << std::endl;
for(std::vector<Part::TopoShape*>::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<Part::TopoShape*>::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;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Mod/Draft/App/DraftDxf.h
Expand Up @@ -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
Expand Down
104 changes: 104 additions & 0 deletions src/Mod/Draft/App/dxf.cpp
Expand Up @@ -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')
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions src/Mod/Draft/App/dxf.h
Expand Up @@ -124,6 +124,7 @@ class CDxfRead{
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);
Expand All @@ -150,6 +151,7 @@ class CDxfRead{
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;
Expand Down

0 comments on commit 51cff8f

Please sign in to comment.