Skip to content

Commit

Permalink
Merge pull request #61 from vwaurich/dxf
Browse files Browse the repository at this point in the history
an own basic dxf file reader
  • Loading branch information
adeas31 committed Nov 29, 2016
2 parents a06a95d + 238ea3c commit 5ee52d3
Show file tree
Hide file tree
Showing 3 changed files with 335 additions and 1 deletion.
289 changes: 289 additions & 0 deletions OMEdit/OMEditGUI/Animation/ExtraShapes.cpp
Expand Up @@ -417,3 +417,292 @@ Spring::Spring(float r, float rWire, float nWindings, float l) :
//this->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS, 0, mpOuterVertices->size()));
}


/*!
* \brief getAutoCADRGB
* get rgb color values for AutoCAD colorcoding accoridng to: http://sub-atomic.com/~moses/acadcolors.html
* \param int colorCode
*/
osg::Vec4f getAutoCADRGB(int colorCode)
{
osg::Vec4f col;
switch (colorCode)
{
case(0) :
col = osg::Vec4f(0.0f / 255.0f, 0.0f / 255.0f, 0.0f / 255.0f, 1.0);
break;
case(1) :
col = osg::Vec4f(255.0f / 255.0f, 0.0f / 255.0f, 0.0f / 255.0f, 1.0);
break;
case(2) :
col = osg::Vec4f(255.0f / 255.0f, 255.0f / 255.0f, 0.0f / 255.0f, 1.0);
break;
case(3) :
col = osg::Vec4f(0.0f / 255.0f, 255.0f / 255.0f, 0.0f / 255.0f, 1.0);
break;
case(4) :
col = osg::Vec4f(0.0f / 255.0f, 255.0f / 255.0f, 255.0f / 255.0f, 1.0);
break;
case(30) :
col = osg::Vec4f(255.0f / 255.0f, 127.0f / 255.0f, 0.f / 255.0f, 1.0);
break;
case(251) :
col = osg::Vec4f(80.0f / 255.0f, 80.0f / 255.0f, 80.0f / 255.0f, 1.0);
break;
default:
col = osg::Vec4f(0 / 255, 0 / 255, 0 / 255, 1.0);
break;
}
return col;
}

/*!
* \brief constructor for DXF3dFace
*/
DXF3dFace::DXF3dFace()
:
vec1(),
vec2(),
vec3(),
vec4(),
layer(""),
colorCode(0),
color()
{
}

/*!
* \brief desctructor for DXF3dFace
*/
DXF3dFace::~DXF3dFace()
{
}

/*!
* \brief DXF3dFace::dumpDXF3DFace
* dumps information aboput 3d face on stdout
*/
void DXF3dFace::dumpDXF3DFace()
{
std::cout << "3-DFACE (" << vec1[0] <<", " << vec1[1]<<", "<< vec1[2]<<")"
<<"(" << vec2[0] <<", " << vec2[1]<<", "<< vec2[2]<<")"
<< "("<< vec3[0] << ", "<< vec3[1]<<", "<< vec3[2] << ")"
<<"(" << vec4[0] << ", "<< vec4[1]<<", "<< vec4[2]<< ")" <<std::endl;

}

/*!
* \brief DXF3dFace::fill3dFace
* fills a 3d face object with information from the textstream
* \param QTextStream* stream
*/
QString DXF3dFace::fill3dFace(QTextStream* stream)
{
QString line = "";
int done = 0;
int lineCode = 0;
while (!done)
{
line = stream->readLine();
if (!line.compare("3DFACE"))
{
done = 1;
}
lineCode = line.toInt();

switch (lineCode)
{
case (8) :
//layer name
layer = stream->readLine().toInt();
break;
case (62) :
//color number
colorCode = stream->readLine().toInt();
color = getAutoCADRGB(colorCode);
break;
case (10) :
//first corner x
vec1[0] = stream->readLine().toDouble();
break;
case (20) :
//first corner y
vec1[1] = stream->readLine().toDouble();
break;
case (30) :
//first corner z
vec1[2] = stream->readLine().toDouble();
break;
case (11) :
//second corner x
vec2[0] = stream->readLine().toDouble();
break;
case (21) :
//second corner y
vec2[1] = stream->readLine().toDouble();
break;
case (31) :
//second corner z
vec2[2] = stream->readLine().toDouble();
break;
case (12) :
//third corner x
vec3[0] = stream->readLine().toDouble();
break;
case (22) :
//third corner y
vec3[1] = stream->readLine().toDouble();
break;
case (32) :
//third corner z
vec3[2] = stream->readLine().toDouble();
break;
case (13) :
//fourth corner x
vec4[0] = stream->readLine().toDouble();
break;
case (23) :
//fourth corner y
vec4[1] = stream->readLine().toDouble();
break;
case (33) :
//fourth corner z
vec4[2] = stream->readLine().toDouble();
break;
case (70) :
//invisible edge flag
stream->readLine().toInt();
break;
default:
done = 1;
break;
}
}
return line;
}

/*!
* \brief DXF3dFace::calcNormals
* calculates normal vector for the facet
*/
osg::Vec3f DXF3dFace::calcNormals()
{
osg::Vec3f v1 = osg::Vec3f(vec1[0]- vec2[0], vec1[1] - vec2[1], vec1[2] - vec2[2]);
osg::Vec3f v2 = osg::Vec3f(vec1[0] - vec3[0], vec1[1] - vec3[1], vec1[2] - vec3[2]);
osg::Vec3f normal = normalize(cross(normalize(v1), normalize(v2)));
return normal;
}

/*!
* \brief DXFile constructor
* \param std::string filename
*/
DXFile::DXFile(std::string filename)
: osg::Geometry()
{
// parse dxf file and fill 3dface objects
fileName = filename;
QFile* dxfFile = new QFile(QString::fromStdString(filename));
if (dxfFile->open(QIODevice::ReadOnly))
{
QTextStream* in = new QTextStream(dxfFile);
//count all 3d faces
int num3dFaces = in->readAll().count(QString("3DFACE"));

//reset textstream
in->seek(0);

// prepare drawing objects
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array(num3dFaces * 4);
osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array(num3dFaces * 4);
osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array(num3dFaces * 4);

// fill face objects
DXF3dFace* faces = new DXF3dFace[num3dFaces];
QString line = in->readLine();
int faceIdx = 0;

int done = 0;
while (!done)
{
if (!line.compare("SECTION")) {
int secID = in->readLine().toInt();
//std::cout << "enter section" << secID << std::endl;
line = in->readLine();
}
else if (!line.compare("ENTITIES")) {
int entityID = in->readLine().toInt();
//std::cout << "enter entity" << entityID << std::endl;
line = in->readLine();
}
else if (!line.compare("3DFACE")) {
//std::cout << "fill face entity" << std::endl;

//add vertices
line = faces[faceIdx].fill3dFace(in);
(*vertices)[(faceIdx*4) + 0] = faces[faceIdx].vec1;
(*vertices)[(faceIdx * 4) + 1] = faces[faceIdx].vec2;
(*vertices)[(faceIdx * 4) + 2] = faces[faceIdx].vec3;
(*vertices)[(faceIdx * 4) + 3] = faces[faceIdx].vec4;
//add colors
(*colors)[(faceIdx * 4) + 0] = faces[faceIdx].color;
(*colors)[(faceIdx * 4) + 1] = faces[faceIdx].color;
(*colors)[(faceIdx * 4) + 2] = faces[faceIdx].color;
(*colors)[(faceIdx * 4) + 3] = faces[faceIdx].color;
//add normals
(*normals)[(faceIdx * 4) + 0] = faces[faceIdx].calcNormals();
(*normals)[(faceIdx * 4) + 1] = faces[faceIdx].calcNormals();
(*normals)[(faceIdx * 4) + 2] = faces[faceIdx].calcNormals();
(*normals)[(faceIdx * 4) + 3] = faces[faceIdx].calcNormals();

faceIdx = faceIdx + 1;
}
else if (!line.compare("ENDSEC")) {
//std::cout << "close section" << std::endl;
line = in->readLine();
}
else if (!line.compare("EOF")) {
done = 1;
}
else {
line = in->readLine();
}
}
dxfFile->close();

//add planes
this->setVertexArray(vertices);
for (int i = 0; i < num3dFaces; i++)
{
if (faces[i].vec1 == faces[i].vec4) {
//std::cout << "its a triangle" << std::endl;
//faces[i].dumpDXF3DFace();
osg::ref_ptr<osg::DrawElementsUInt> facette = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 3);
(*facette)[0] = (i * 4) + 0;
(*facette)[1] = (i * 4) + 1;
(*facette)[2] = (i * 4) + 2;
this->addPrimitiveSet(facette);

//normal calculation
osg::Vec3f normal = faces[i].calcNormals();
}
else
{
//std::cout << "its a quad" << std::endl;
osg::ref_ptr<osg::DrawElementsUInt> facette = new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS, 4);
(*facette)[0] = (i * 4) + 0;
(*facette)[1] = (i * 4) + 1;
(*facette)[2] = (i * 4) + 2;
(*facette)[3] = (i * 4) + 3;
this->addPrimitiveSet(facette);
}
}
//add normals
this->setNormalArray(normals);
this->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
//add colors
this->setColorArray(colors);
this->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
}
}

38 changes: 38 additions & 0 deletions OMEdit/OMEditGUI/Animation/ExtraShapes.h
Expand Up @@ -35,12 +35,16 @@
#ifndef EXTRASHAPES_H
#define EXTRASHAPES_H

#include "Visualizer.h"

#include <osg/Node>
#include <osg/Group>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/Shape>

#include <qTextStream>
#include <qFile>

class Pipecylinder : public osg::Geometry
{
Expand Down Expand Up @@ -68,4 +72,38 @@ class Spring : public osg::Geometry
osg::Vec3Array* mpSplineVertices;
};

class DXF3dFace
{
public:
DXF3dFace();
~DXF3dFace();
QString fill3dFace(QTextStream* stream);
void dumpDXF3DFace();
osg::Vec3f calcNormals();

public:
osg::Vec3 vec1;
osg::Vec3 vec2;
osg::Vec3 vec3;
osg::Vec3 vec4;
std::string layer;
int colorCode;
osg::Vec4f color;
};

class DXFile : public osg::Geometry
{
public:
/*-----------------------------------------
* CONSTRUCTORS
*---------------------------------------*/
DXFile(std::string filename);
~DXFile() = default;

//members
public:
std::string fileName;
};


#endif //end EXTRASHAPES_H
9 changes: 8 additions & 1 deletion OMEdit/OMEditGUI/Animation/Visualizer.cpp
Expand Up @@ -372,7 +372,7 @@ int OSGScene::setUpScene(std::vector<ShapeObject> allShapes)
osg::ref_ptr<osg::MatrixTransform> transf = new osg::MatrixTransform();

//cad node
if ((shape._type.compare("dxf") == 0) or (shape._type.compare("stl") == 0))
if (shape._type.compare("stl") == 0)
{
//std::cout<<"Its a CAD and the filename is "<<shape._fileName<<std::endl;
osg::ref_ptr<osg::Node> node = osgDB::readNodeFile(shape._fileName);
Expand All @@ -382,6 +382,13 @@ int OSGScene::setUpScene(std::vector<ShapeObject> allShapes)
node->setStateSet(ss);
transf->addChild(node.get());
}
else if ((shape._type.compare("dxf") == 0)) {
std::string name = shape._fileName;
DXFile* shape = new DXFile(name);
geode = new osg::Geode();
geode->addDrawable(shape);
transf->addChild(geode);
}
//geode with shape drawable
else
{
Expand Down

0 comments on commit 5ee52d3

Please sign in to comment.