📄 dxfentity.cpp
字号:
/* dxfReader for OpenSceneGraph Copyright (C) 2005 by GraphArchitecture ( grapharchitecture.com ) * Programmed by Paul de Repentigny <pdr@grapharchitecture.com> * * OpenSceneGraph is (C) 2004 Robert Osfield * * This library is provided as-is, without support of any kind. * * Read DXF docs or OSG docs for any related questions. * * You may contact the author if you have suggestions/corrections/enhancements. */#include "dxfEntity.h"#include "dxfFile.h"#include "scene.h"#include "dxfBlock.h"#include "codeValue.h"using namespace std;using namespace osg;// staticstd::map<std::string, ref_ptr<dxfBasicEntity> > dxfEntity::_registry;RegisterEntityProxy<dxf3DFace> g_dxf3DFace;RegisterEntityProxy<dxfCircle> g_dxfCircle;RegisterEntityProxy<dxfArc> g_dxfArc;RegisterEntityProxy<dxfLine> g_dxfLine;RegisterEntityProxy<dxfVertex> g_dxfVertex;RegisterEntityProxy<dxfPolyline> g_dxfPolyline;RegisterEntityProxy<dxfLWPolyline> g_dxfLWPolyline;RegisterEntityProxy<dxfInsert> g_dxfInsert;RegisterEntityProxy<dxfText> g_dxfText;void dxfBasicEntity::assign(dxfFile* , codeValue& cv){ switch (cv._groupCode) { case 8: _layer = cv._string; break; case 62: _color = cv._short; break; }}voiddxf3DFace::assign(dxfFile* dxf, codeValue& cv){ double d = cv._double; switch (cv._groupCode) { case 10: case 11: case 12: case 13: _vertices[cv._groupCode - 10].x() = d; break; case 20: case 21: case 22: case 23: _vertices[cv._groupCode - 20].y() = d; break; case 30: case 31: case 32: case 33: _vertices[cv._groupCode - 30].z() = d; break; default: dxfBasicEntity::assign(dxf, cv); break; }}voiddxf3DFace::drawScene(scene* sc){ std::vector<Vec3d> vlist; short nfaces = 3; // Hate to do that, but hey, that's written in the DXF specs: if (_vertices[2] != _vertices[3]) nfaces = 4; for (short i = nfaces-1; i >= 0; i--) vlist.push_back(_vertices[i]); if (nfaces == 3) { // to do make sure we're % 3 sc->addTriangles(getLayer(), _color, vlist); } else if (nfaces == 4) { // to do make sure we're % 4 sc->addQuads(getLayer(), _color, vlist); }}voiddxfVertex::assign(dxfFile* dxf, codeValue& cv){ double d = cv._double; // 2005.12.13 pdr: learned today that negative indices mean something and were possible int s = cv._int; // 2005.12.13 pdr: group codes [70,78] now signed int. if ( s < 0 ) s = -s; switch (cv._groupCode) { case 10: _vertex.x() = d; break; case 20: _vertex.y() = d; break; case 30: _vertex.z() = d; break; case 71: _indice1 = s; break; case 72: _indice2 = s; break; case 73: _indice3 = s; break; case 74: _indice4 = s; break; default: dxfBasicEntity::assign(dxf, cv); break; }}voiddxfCircle::assign(dxfFile* dxf, codeValue& cv){ double d = cv._double; //unsigned short s = cv._short; switch (cv._groupCode) { case 10: _center.x() = d; break; case 20: _center.y() = d; break; case 30: _center.z() = d; break; case 40: _radius = d; break; case 210: _ocs.x() = d; break; case 220: _ocs.y() = d; break; case 230: _ocs.z() = d; break; default: dxfBasicEntity::assign(dxf, cv); break; }}voiddxfCircle::drawScene(scene* sc){ Matrixd m; getOCSMatrix(_ocs, m); sc->ocs(m); std::vector<Vec3d> vlist; int numsteps = 360/5; // baaarghf. double angle_step = osg::DegreesToRadians((double)360.0 / (double) numsteps); double angle1 = 0.0f; double angle2 = 0.0f; Vec3d a = _center; Vec3d b,c; for (int r = 0; r < numsteps; r++) { angle1 = angle2; if (r == numsteps - 1) angle2 = 0.0f; else angle2 += angle_step; b = a + Vec3d(_radius * (double) sin(angle1), _radius * (double) cos(angle1), 0); c = a + Vec3d(_radius * (double) sin(angle2), _radius * (double) cos(angle2), 0);// vlist.push_back(a); vlist.push_back(b); vlist.push_back(c); } sc->addLineStrip(getLayer(), _color, vlist);// sc->addTriangles(getLayer(), _color, vlist); sc->ocs_clear();}voiddxfArc::assign(dxfFile* dxf, codeValue& cv){ double d = cv._double; //unsigned short s = cv._short; switch (cv._groupCode) { case 10: _center.x() = d; break; case 20: _center.y() = d; break; case 30: _center.z() = d; break; case 40: _radius = d; break; case 50: _startAngle = d; break; case 51: _endAngle = d; break; case 210: _ocs.x() = d; break; case 220: _ocs.y() = d; break; case 230: _ocs.z() = d; break; default: dxfBasicEntity::assign(dxf, cv); break; }}voiddxfArc::drawScene(scene* sc){ Matrixd m; getOCSMatrix(_ocs, m); sc->ocs(m); std::vector<Vec3d> vlist; double end; double start; if (_startAngle > _endAngle) { start = _startAngle; end = _endAngle + 360; } else { start = _startAngle; end = _endAngle; } double angle_step = DegreesToRadians(end - start); int numsteps = (int)((end - start)/5.0); // hurmghf. say 5 degrees? if (numsteps * 5 < (end - start)) numsteps++; angle_step /= (double) numsteps; end = DegreesToRadians((-_startAngle)+90.0); start = DegreesToRadians((-_endAngle)+90.0); double angle1 = 0.0f; double angle2 = (start); Vec3d a = _center; Vec3d b,c; for (int r = 0; r < numsteps; r++) { angle1 = angle2; angle2 = angle1 + angle_step; b = a + Vec3d(_radius * (double) sin(angle1), _radius * (double) cos(angle1), 0); c = a + Vec3d(_radius * (double) sin(angle2), _radius * (double) cos(angle2), 0); vlist.push_back(b); vlist.push_back(c); } sc->addLineStrip(getLayer(), _color, vlist); sc->ocs_clear();}voiddxfLine::assign(dxfFile* dxf, codeValue& cv){ double d = cv._double; //unsigned short s = cv._short; switch (cv._groupCode) { case 10: _a.x() = d; break; case 20: _a.y() = d; break; case 30: _a.z() = d; break; case 11: _b.x() = d; break; case 21: _b.y() = d; break; case 31: _b.z() = d; break; case 210: _ocs.x() = d; break; case 220: _ocs.y() = d; break; case 230: _ocs.z() = d; break; default: dxfBasicEntity::assign(dxf, cv); break; }}voiddxfLine::drawScene(scene* sc){ Matrixd m; getOCSMatrix(_ocs, m); // don't know why this doesn't work// sc->ocs(m); sc->addLine(getLayer(), _color, _b, _a);// static long lcount = 0;// std::cout << ++lcount << " ";// sc->ocs_clear();}void dxfPolyline::assign(dxfFile* dxf, codeValue& cv){ string s = cv._string; if (cv._groupCode == 0) { if (s == "VERTEX") { _currentVertex = new dxfVertex; _vertices.push_back(_currentVertex); } } else if (_currentVertex) { _currentVertex->assign(dxf, cv); if ((_flag & 64 /*i.e. polymesh*/) && (cv._groupCode == 70 /*i.e. vertex flag*/) && (cv._int && 128 /*i.e. vertex is actually a face*/)) _indices.push_back(_currentVertex); // Add the index only if _currentvertex is actually an index } else { double d = cv._double; switch (cv._groupCode) { case 10: // dummy break; case 20: // dummy break; case 30: _elevation = d; // what is elevation? break; case 70: _flag = cv._int; // 2005.12.13 pdr: group codes [70,78] now signed int. break; case 71: // Meaningful only when _surfacetype == 6, don' trust it for polymeshes. // From the docs : // "The 71 group specifies the number of vertices in the mesh, and the 72 group // specifies the number of faces. Although these counts are correct for all meshes // created with the PFACE command, applications are not required to place correct // values in these fields.)" // Amusing isn't it ? _mcount = cv._int; // 2005.12.13 pdr: group codes [70,78] now signed int. break; case 72: // Meaningful only when _surfacetype == 6, don' trust it for polymeshes. // From the docs : // "The 71 group specifies the number of vertices in the mesh, and the 72 group // specifies the number of faces. Although these counts are correct for all meshes // created with the PFACE command, applications are not required to place correct // values in these fields.)" // Amusing isn't it ? _ncount = cv._int; // 2005.12.13 pdr: group codes [70,78] now signed int. break; case 73: _mdensity = cv._int; // 2005.12.13 pdr: group codes [70,78] now signed int. break; case 74: _ndensity = cv._int; // 2005.12.13 pdr: group codes [70,78] now signed int. break; case 75: _surfacetype = cv._int; // 2005.12.13 pdr: group codes [70,78] now signed int. break; case 210: _ocs.x() = d; break; case 220: _ocs.y() = d; break; case 230: _ocs.z() = d; break; default: dxfBasicEntity::assign(dxf, cv); break; } }}voiddxfPolyline::drawScene(scene* sc){ Matrixd m; getOCSMatrix(_ocs, m); sc->ocs(m); std::vector<Vec3d> vlist; std::vector<Vec3d> qlist; Vec3d a, b, c, d; bool invert_order = false; if (_flag & 16) { std::vector<Vec3d> nlist; Vec3d nr; bool nset = false; //dxfVertex* v = NULL; unsigned int ncount; unsigned int mcount; if (_surfacetype == 6) { // I dont have examples of type 5 and 8, but they may be the same as 6 mcount = _mdensity; ncount = _ndensity; } else { mcount = _mcount; ncount = _ncount; } for (unsigned int n = 0; n < ncount-1; n++) { for (unsigned int m = 1; m < mcount; m++) { // 0 a = _vertices[(m-1)*ncount+n].get()->getVertex(); // 1 b = _vertices[m*ncount+n].get()->getVertex(); // 3 c = _vertices[(m)*ncount+n+1].get()->getVertex(); // 2 d = _vertices[(m-1)*ncount+n+1].get()->getVertex(); if (a == b ) { vlist.push_back(a); vlist.push_back(c); vlist.push_back(d); b = c; c = d; } else if (c == d) { vlist.push_back(a); vlist.push_back(b); vlist.push_back(c); } else { qlist.push_back(a); qlist.push_back(b); qlist.push_back(c); qlist.push_back(d); } if (!nset) { nset = true; nr = (b - a) ^ (c - a); nr.normalize(); } nlist.push_back(a); } } if (_flag & 1) { for (unsigned int n = 0; n < ncount-1; n++) { // 0 a = _vertices[(mcount-1)*ncount+n].get()->getVertex(); // 1 b = _vertices[0*ncount+n].get()->getVertex(); // 3 c = _vertices[(0)*ncount+n+1].get()->getVertex(); // 2 d = _vertices[(mcount-1)*ncount+n+1].get()->getVertex(); if (a == b ) { vlist.push_back(a); vlist.push_back(c); vlist.push_back(d); b = c; c = d;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -