| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | #include <osg/io_utils> |
|---|
| 17 | #include <osg/PolygonMode> |
|---|
| 18 | #include <iomanip> |
|---|
| 19 | |
|---|
| 20 | #include "DXFWriterNodeVisitor.h" |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | class DxfPrimitiveIndexWriter : public osg::PrimitiveIndexFunctor { |
|---|
| 63 | |
|---|
| 64 | public: |
|---|
| 65 | DxfPrimitiveIndexWriter(std::ostream& fout,osg::Geometry* geo,const Layer &layer,AcadColor &acad, |
|---|
| 66 | const osg::Matrix& m = osg::Matrix::identity(),bool writeTriangleAs3DFace = true) : |
|---|
| 67 | osg::PrimitiveIndexFunctor(), |
|---|
| 68 | _fout(fout), |
|---|
| 69 | _geo(geo), |
|---|
| 70 | _layer(layer), |
|---|
| 71 | _acad(acad), |
|---|
| 72 | _m(m), |
|---|
| 73 | _writeTriangleAs3DFace(writeTriangleAs3DFace) |
|---|
| 74 | { |
|---|
| 75 | |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | virtual void setVertexArray(unsigned int,const osg::Vec2*) {} |
|---|
| 79 | |
|---|
| 80 | virtual void setVertexArray(unsigned int ,const osg::Vec3* ) {} |
|---|
| 81 | |
|---|
| 82 | virtual void setVertexArray(unsigned int,const osg::Vec4* ) {} |
|---|
| 83 | |
|---|
| 84 | virtual void setVertexArray(unsigned int,const osg::Vec2d*) {} |
|---|
| 85 | |
|---|
| 86 | virtual void setVertexArray(unsigned int ,const osg::Vec3d* ) {} |
|---|
| 87 | |
|---|
| 88 | virtual void setVertexArray(unsigned int,const osg::Vec4d* ) {} |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | void write(unsigned int i,int c) |
|---|
| 92 | { |
|---|
| 93 | const osg::Vec3 point = ((osg::Vec3Array *)_geo->getVertexArray())->at(i) * _m; |
|---|
| 94 | _fout <<c+10<<"\n "<<point.x()<<"\n"<<20+c<<"\n "<<point.y()<<"\n"<<30+c<<"\n "<<point.z()<<"\n"; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | void writeTriangle(unsigned int i1, unsigned int i2, unsigned int i3) |
|---|
| 99 | { |
|---|
| 100 | if (_writeTriangleAs3DFace) |
|---|
| 101 | { |
|---|
| 102 | _fout << "0 \n3DFACE\n 8\n"<<_layer._name<<"\n"; |
|---|
| 103 | if ( _layer._color ) { |
|---|
| 104 | _fout << "62\n"<<_layer._color<<"\n"; |
|---|
| 105 | } else { |
|---|
| 106 | _fout << "62\n"<<_acad.findColor(DXFWriterNodeVisitor::getNodeRGB(_geo,i1))<<"\n"; |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | } |
|---|
| 110 | write(i1,0); |
|---|
| 111 | write(i2,1); |
|---|
| 112 | write(i3,2); |
|---|
| 113 | write(i1,3); |
|---|
| 114 | } |
|---|
| 115 | else |
|---|
| 116 | { |
|---|
| 117 | _fout << "0 \nLINE\n 8\n"<<_layer._name<<"\n"; |
|---|
| 118 | if ( _layer._color ) { |
|---|
| 119 | _fout << "62\n"<<_layer._color<<"\n"; |
|---|
| 120 | } else { |
|---|
| 121 | _fout << "62\n"<<_acad.findColor(DXFWriterNodeVisitor::getNodeRGB(_geo,i1))<<"\n"; |
|---|
| 122 | } |
|---|
| 123 | write(i1,0); |
|---|
| 124 | write(i2,1); |
|---|
| 125 | |
|---|
| 126 | _fout << "0 \nLINE\n 8\n"<<_layer._name<<"\n"; |
|---|
| 127 | if ( _layer._color ) { |
|---|
| 128 | _fout << "62\n"<<_layer._color<<"\n"; |
|---|
| 129 | } else { |
|---|
| 130 | _fout << "62\n"<<_acad.findColor(DXFWriterNodeVisitor::getNodeRGB(_geo,i2))<<"\n"; |
|---|
| 131 | } |
|---|
| 132 | write(i2,0); |
|---|
| 133 | write(i3,1); |
|---|
| 134 | |
|---|
| 135 | _fout << "0 \nLINE\n 8\n"<<_layer._name<<"\n"; |
|---|
| 136 | if ( _layer._color ) { |
|---|
| 137 | _fout << "62\n"<<_layer._color<<"\n"; |
|---|
| 138 | } else { |
|---|
| 139 | _fout << "62\n"<<_acad.findColor(DXFWriterNodeVisitor::getNodeRGB(_geo,i3))<<"\n"; |
|---|
| 140 | } |
|---|
| 141 | write(i3,0); |
|---|
| 142 | write(i1,1); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | void writeLine(unsigned int i1, unsigned int i2) |
|---|
| 149 | { |
|---|
| 150 | _fout << "0 \nLINE\n 8\n"<<_layer._name<<"\n"; |
|---|
| 151 | if ( _layer._color ) { |
|---|
| 152 | _fout << "62\n"<<_layer._color<<"\n"; |
|---|
| 153 | } else { |
|---|
| 154 | _fout << "62\n"<<_acad.findColor(DXFWriterNodeVisitor::getNodeRGB(_geo,i1))<<"\n"; |
|---|
| 155 | } |
|---|
| 156 | write(i1,0); |
|---|
| 157 | write(i2,1); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | void writePoint(unsigned int i1) |
|---|
| 162 | { |
|---|
| 163 | _fout << "0 \nPOINT\n 8\n"<<_layer._name<<"\n"; |
|---|
| 164 | if ( _layer._color ) { |
|---|
| 165 | _fout << "62\n"<<_layer._color<<"\n"; |
|---|
| 166 | } else { |
|---|
| 167 | _fout << "62\n"<<_acad.findColor(DXFWriterNodeVisitor::getNodeRGB(_geo,i1))<<"\n"; |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | } |
|---|
| 171 | write(i1,0); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | virtual void begin(GLenum mode) |
|---|
| 175 | { |
|---|
| 176 | _modeCache = mode; |
|---|
| 177 | _indexCache.clear(); |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | virtual void vertex(unsigned int vert) |
|---|
| 181 | { |
|---|
| 182 | _indexCache.push_back(vert); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | virtual void end() |
|---|
| 186 | { |
|---|
| 187 | if (!_indexCache.empty()) |
|---|
| 188 | { |
|---|
| 189 | drawElements(_modeCache,_indexCache.size(),&_indexCache.front()); |
|---|
| 190 | } |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | virtual void drawArrays(GLenum mode,GLint first,GLsizei count); |
|---|
| 194 | |
|---|
| 195 | virtual void drawElements(GLenum mode,GLsizei count,const GLubyte* indices) |
|---|
| 196 | { |
|---|
| 197 | drawElementsImplementation<GLubyte>(mode, count, indices); |
|---|
| 198 | } |
|---|
| 199 | virtual void drawElements(GLenum mode,GLsizei count,const GLushort* indices) |
|---|
| 200 | { |
|---|
| 201 | drawElementsImplementation<GLushort>(mode, count, indices); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | virtual void drawElements(GLenum mode,GLsizei count,const GLuint* indices) |
|---|
| 205 | { |
|---|
| 206 | drawElementsImplementation<GLuint>(mode, count, indices); |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | protected: |
|---|
| 210 | |
|---|
| 211 | template<typename T>void drawElementsImplementation(GLenum mode, GLsizei count, const T* indices) |
|---|
| 212 | { |
|---|
| 213 | if (indices==0 || count==0) return; |
|---|
| 214 | |
|---|
| 215 | typedef const T* IndexPointer; |
|---|
| 216 | |
|---|
| 217 | switch(mode) |
|---|
| 218 | { |
|---|
| 219 | case(GL_TRIANGLES): |
|---|
| 220 | { |
|---|
| 221 | IndexPointer ilast = &indices[count]; |
|---|
| 222 | for(IndexPointer iptr=indices;iptr<ilast;iptr+=3) |
|---|
| 223 | writeTriangle(*iptr,*(iptr+1),*(iptr+2)); |
|---|
| 224 | |
|---|
| 225 | break; |
|---|
| 226 | } |
|---|
| 227 | case(GL_TRIANGLE_STRIP): |
|---|
| 228 | { |
|---|
| 229 | IndexPointer iptr = indices; |
|---|
| 230 | for(GLsizei i=2;i<count;++i,++iptr) |
|---|
| 231 | { |
|---|
| 232 | if ((i%2)) writeTriangle(*(iptr),*(iptr+2),*(iptr+1)); |
|---|
| 233 | else writeTriangle(*(iptr),*(iptr+1),*(iptr+2)); |
|---|
| 234 | } |
|---|
| 235 | break; |
|---|
| 236 | } |
|---|
| 237 | case(GL_QUADS): |
|---|
| 238 | { |
|---|
| 239 | IndexPointer iptr = indices; |
|---|
| 240 | for(GLsizei i=3;i<count;i+=4,iptr+=4) |
|---|
| 241 | { |
|---|
| 242 | writeTriangle(*(iptr),*(iptr+1),*(iptr+2)); |
|---|
| 243 | writeTriangle(*(iptr),*(iptr+2),*(iptr+3)); |
|---|
| 244 | } |
|---|
| 245 | break; |
|---|
| 246 | } |
|---|
| 247 | case(GL_QUAD_STRIP): |
|---|
| 248 | { |
|---|
| 249 | IndexPointer iptr = indices; |
|---|
| 250 | for(GLsizei i=3;i<count;i+=2,iptr+=2) |
|---|
| 251 | { |
|---|
| 252 | writeTriangle(*(iptr),*(iptr+1),*(iptr+2)); |
|---|
| 253 | writeTriangle(*(iptr+1),*(iptr+3),*(iptr+2)); |
|---|
| 254 | } |
|---|
| 255 | break; |
|---|
| 256 | } |
|---|
| 257 | case(GL_POLYGON): |
|---|
| 258 | case(GL_TRIANGLE_FAN): |
|---|
| 259 | { |
|---|
| 260 | IndexPointer iptr = indices; |
|---|
| 261 | unsigned int first = *iptr; |
|---|
| 262 | ++iptr; |
|---|
| 263 | for(GLsizei i=2;i<count;++i,++iptr) |
|---|
| 264 | { |
|---|
| 265 | writeTriangle(first,*(iptr),*(iptr+1)); |
|---|
| 266 | } |
|---|
| 267 | break; |
|---|
| 268 | } |
|---|
| 269 | case(GL_POINTS): |
|---|
| 270 | { |
|---|
| 271 | IndexPointer ilast = &indices[count]; |
|---|
| 272 | for(IndexPointer iptr=indices;iptr<ilast;++iptr) |
|---|
| 273 | |
|---|
| 274 | { |
|---|
| 275 | writePoint(*iptr); |
|---|
| 276 | } |
|---|
| 277 | break; |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | case(GL_LINES): |
|---|
| 281 | { |
|---|
| 282 | IndexPointer ilast = &indices[count]; |
|---|
| 283 | for(IndexPointer iptr=indices;iptr<ilast;iptr+=2) |
|---|
| 284 | { |
|---|
| 285 | writeLine(*iptr, *(iptr+1)); |
|---|
| 286 | } |
|---|
| 287 | break; |
|---|
| 288 | } |
|---|
| 289 | case(GL_LINE_STRIP): |
|---|
| 290 | { |
|---|
| 291 | |
|---|
| 292 | IndexPointer ilast = &indices[count]; |
|---|
| 293 | for(IndexPointer iptr=indices+1;iptr<ilast;iptr+=2) |
|---|
| 294 | |
|---|
| 295 | { |
|---|
| 296 | writeLine(*(iptr-1), *iptr); |
|---|
| 297 | } |
|---|
| 298 | break; |
|---|
| 299 | } |
|---|
| 300 | case(GL_LINE_LOOP): |
|---|
| 301 | { |
|---|
| 302 | IndexPointer ilast = &indices[count]; |
|---|
| 303 | for(IndexPointer iptr=indices+1;iptr<ilast;iptr+=2) |
|---|
| 304 | { |
|---|
| 305 | writeLine(*(iptr-1), *iptr); |
|---|
| 306 | } |
|---|
| 307 | writeLine(*ilast, *indices); |
|---|
| 308 | break; |
|---|
| 309 | } |
|---|
| 310 | |
|---|
| 311 | default: |
|---|
| 312 | |
|---|
| 313 | break; |
|---|
| 314 | } |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | private: |
|---|
| 318 | |
|---|
| 319 | DxfPrimitiveIndexWriter& operator = (const DxfPrimitiveIndexWriter&) { return *this; } |
|---|
| 320 | |
|---|
| 321 | std::ostream& _fout; |
|---|
| 322 | GLenum _modeCache; |
|---|
| 323 | std::vector<GLuint> _indexCache; |
|---|
| 324 | osg::Geometry* _geo; |
|---|
| 325 | |
|---|
| 326 | Layer _layer; |
|---|
| 327 | AcadColor _acad; |
|---|
| 328 | osg::Matrix _m; |
|---|
| 329 | |
|---|
| 330 | bool _writeTriangleAs3DFace; |
|---|
| 331 | }; |
|---|
| 332 | |
|---|
| 333 | |
|---|
| 334 | void DxfPrimitiveIndexWriter::drawArrays(GLenum mode,GLint first,GLsizei count) |
|---|
| 335 | { |
|---|
| 336 | switch(mode) |
|---|
| 337 | { |
|---|
| 338 | case(GL_TRIANGLES): |
|---|
| 339 | { |
|---|
| 340 | unsigned int pos=first; |
|---|
| 341 | for(GLsizei i=2;i<count;i+=3,pos+=3) |
|---|
| 342 | { |
|---|
| 343 | writeTriangle(pos,pos+1,pos+2); |
|---|
| 344 | } |
|---|
| 345 | break; |
|---|
| 346 | } |
|---|
| 347 | case(GL_TRIANGLE_STRIP): |
|---|
| 348 | { |
|---|
| 349 | unsigned int pos=first; |
|---|
| 350 | for(GLsizei i=2;i<count;++i,++pos) |
|---|
| 351 | { |
|---|
| 352 | if ((i%2)) writeTriangle(pos,pos+2,pos+1); |
|---|
| 353 | else writeTriangle(pos,pos+1,pos+2); |
|---|
| 354 | } |
|---|
| 355 | break; |
|---|
| 356 | } |
|---|
| 357 | case(GL_QUADS): |
|---|
| 358 | { |
|---|
| 359 | unsigned int pos=first; |
|---|
| 360 | for(GLsizei i=3;i<count;i+=4,pos+=4) |
|---|
| 361 | { |
|---|
| 362 | writeTriangle(pos,pos+1,pos+2); |
|---|
| 363 | writeTriangle(pos,pos+2,pos+3); |
|---|
| 364 | } |
|---|
| 365 | break; |
|---|
| 366 | } |
|---|
| 367 | case(GL_QUAD_STRIP): |
|---|
| 368 | { |
|---|
| 369 | unsigned int pos=first; |
|---|
| 370 | for(GLsizei i=3;i<count;i+=2,pos+=2) |
|---|
| 371 | { |
|---|
| 372 | writeTriangle(pos,pos+1,pos+2); |
|---|
| 373 | writeTriangle(pos+1,pos+3,pos+2); |
|---|
| 374 | } |
|---|
| 375 | break; |
|---|
| 376 | } |
|---|
| 377 | case(GL_POLYGON): |
|---|
| 378 | case(GL_TRIANGLE_FAN): |
|---|
| 379 | { |
|---|
| 380 | unsigned int pos=first+1; |
|---|
| 381 | for(GLsizei i=2;i<count;++i,++pos) |
|---|
| 382 | { |
|---|
| 383 | writeTriangle(first,pos,pos+1); |
|---|
| 384 | } |
|---|
| 385 | break; |
|---|
| 386 | } |
|---|
| 387 | case(GL_POINTS): |
|---|
| 388 | { |
|---|
| 389 | |
|---|
| 390 | for(GLsizei i=0;i<count;++i) |
|---|
| 391 | { |
|---|
| 392 | writePoint(i); |
|---|
| 393 | } |
|---|
| 394 | break; |
|---|
| 395 | } |
|---|
| 396 | |
|---|
| 397 | case(GL_LINES): |
|---|
| 398 | { |
|---|
| 399 | for(GLsizei i=0;i<count;i+=2) |
|---|
| 400 | { |
|---|
| 401 | writeLine(i, i+1); |
|---|
| 402 | } |
|---|
| 403 | break; |
|---|
| 404 | } |
|---|
| 405 | case(GL_LINE_STRIP): |
|---|
| 406 | { |
|---|
| 407 | for(GLsizei i=1;i<count;++i) |
|---|
| 408 | { |
|---|
| 409 | writeLine(i-1, i); |
|---|
| 410 | } |
|---|
| 411 | break; |
|---|
| 412 | } |
|---|
| 413 | case(GL_LINE_LOOP): |
|---|
| 414 | { |
|---|
| 415 | for(GLsizei i=1;i<count;++i) |
|---|
| 416 | { |
|---|
| 417 | writeLine(i-1, i); |
|---|
| 418 | } |
|---|
| 419 | writeLine(count-1, 0); |
|---|
| 420 | break; |
|---|
| 421 | } |
|---|
| 422 | default: |
|---|
| 423 | OSG_WARN << "DXFWriterNodeVisitor :: can't handle mode " << mode << std::endl; |
|---|
| 424 | break; |
|---|
| 425 | } |
|---|
| 426 | } |
|---|
| 427 | |
|---|
| 428 | |
|---|
| 429 | |
|---|
| 430 | std::string DXFWriterNodeVisitor::getLayerName(const std::string& defaultvalue) |
|---|
| 431 | { |
|---|
| 432 | |
|---|
| 433 | std::string layerName=defaultvalue; |
|---|
| 434 | std::transform(layerName.begin(), layerName.end(), layerName.begin(), toupper); |
|---|
| 435 | |
|---|
| 436 | |
|---|
| 437 | size_t found=0; |
|---|
| 438 | const std::string allowed("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_-"); |
|---|
| 439 | while ( (found=layerName.find_first_not_of(allowed)) != std::string::npos) { |
|---|
| 440 | layerName[found] = '-'; |
|---|
| 441 | } |
|---|
| 442 | |
|---|
| 443 | |
|---|
| 444 | for (std::vector<Layer>::iterator itr=_layers.begin();itr!=_layers.end();itr++) { |
|---|
| 445 | if (itr->_name == layerName ) { |
|---|
| 446 | std::stringstream ss; |
|---|
| 447 | ss << defaultvalue<< "_" << _layers.size(); |
|---|
| 448 | layerName = ss.str(); |
|---|
| 449 | break; |
|---|
| 450 | } |
|---|
| 451 | } |
|---|
| 452 | |
|---|
| 453 | if (layerName.empty()) layerName = "0"; |
|---|
| 454 | |
|---|
| 455 | return layerName; |
|---|
| 456 | |
|---|
| 457 | } |
|---|
| 458 | |
|---|
| 459 | |
|---|
| 460 | |
|---|
| 461 | |
|---|
| 462 | |
|---|
| 463 | |
|---|
| 464 | |
|---|
| 465 | |
|---|
| 466 | |
|---|
| 467 | |
|---|
| 468 | |
|---|
| 469 | |
|---|
| 470 | |
|---|
| 471 | |
|---|
| 472 | |
|---|
| 473 | |
|---|
| 474 | |
|---|
| 475 | void DXFWriterNodeVisitor::processStateSet(osg::StateSet* ss) |
|---|
| 476 | { |
|---|
| 477 | |
|---|
| 478 | |
|---|
| 479 | osg::PolygonMode * pm = dynamic_cast<osg::PolygonMode *>(ss->getAttribute(osg::StateAttribute::POLYGONMODE)); |
|---|
| 480 | if (pm) |
|---|
| 481 | { |
|---|
| 482 | if (pm->getMode(osg::PolygonMode::FRONT)==osg::PolygonMode::LINE) _writeTriangleAs3DFace = false; |
|---|
| 483 | } |
|---|
| 484 | } |
|---|
| 485 | |
|---|
| 486 | void DXFWriterNodeVisitor::processGeometry(osg::Geometry* geo, osg::Matrix& m) |
|---|
| 487 | { |
|---|
| 488 | |
|---|
| 489 | |
|---|
| 490 | |
|---|
| 491 | if (geo->getVertexArray() && geo->getVertexArray()->getNumElements() ) { |
|---|
| 492 | |
|---|
| 493 | processStateSet(_currentStateSet.get()); |
|---|
| 494 | |
|---|
| 495 | if ( _firstPass ) { |
|---|
| 496 | |
|---|
| 497 | _layer._name = getLayerName( geo->getName().empty() ? geo->getParent(0)->getName() : geo->getName() ); |
|---|
| 498 | OSG_DEBUG << "adding Layer " << _layer._name << std::endl; |
|---|
| 499 | |
|---|
| 500 | |
|---|
| 501 | if ( osg::Geometry::BIND_OVERALL == geo->getColorBinding() ) { |
|---|
| 502 | _layer._color = _acadColor.findColor(getNodeRGB(geo)); |
|---|
| 503 | } else if ( osg::Geometry::BIND_OFF== geo->getColorBinding() ) { |
|---|
| 504 | _layer._color = 0xff; |
|---|
| 505 | } else { |
|---|
| 506 | _layer._color = 0; |
|---|
| 507 | } |
|---|
| 508 | _layers.push_back(_layer); |
|---|
| 509 | |
|---|
| 510 | } else { |
|---|
| 511 | _layer = _layers[_count++]; |
|---|
| 512 | OSG_DEBUG << "writing Layer " << _layer._name << std::endl; |
|---|
| 513 | if ( geo->getNumPrimitiveSets() ) { |
|---|
| 514 | for(unsigned int i = 0; i < geo->getNumPrimitiveSets(); ++i) |
|---|
| 515 | { |
|---|
| 516 | osg::PrimitiveSet* ps = geo->getPrimitiveSet(i); |
|---|
| 517 | DxfPrimitiveIndexWriter pif(_fout, geo,_layer,_acadColor,m,_writeTriangleAs3DFace); |
|---|
| 518 | ps->accept(pif); |
|---|
| 519 | } |
|---|
| 520 | } else { |
|---|
| 521 | |
|---|
| 522 | |
|---|
| 523 | if ( geo->getVertexArray() ) { |
|---|
| 524 | osg::Vec3Array* data=static_cast<osg::Vec3Array*>(geo->getVertexArray()); |
|---|
| 525 | for (unsigned int ii=0;ii<data->getNumElements();ii++) |
|---|
| 526 | { |
|---|
| 527 | osg::Vec3 point = data->at(ii) * m; |
|---|
| 528 | _fout << "0 \nVERTEX\n 8\n"<<_layer._name<<"\n"; |
|---|
| 529 | if ( _layer._color ) { |
|---|
| 530 | _fout << "62\n"<<_layer._color<<"\n"; |
|---|
| 531 | } else { |
|---|
| 532 | _fout << "62\n"<<_acadColor.findColor(getNodeRGB(geo,ii))<<"\n"; |
|---|
| 533 | } |
|---|
| 534 | _fout<<" 10\n"<<point.x()<<"\n 20\n"<<point.y()<<"\n 30\n"<<point.z()<<"\n"; |
|---|
| 535 | } |
|---|
| 536 | } |
|---|
| 537 | } |
|---|
| 538 | } |
|---|
| 539 | } |
|---|
| 540 | } |
|---|
| 541 | |
|---|
| 542 | |
|---|
| 543 | |
|---|
| 544 | void DXFWriterNodeVisitor::apply( osg::Geode &node ) |
|---|
| 545 | { |
|---|
| 546 | |
|---|
| 547 | pushStateSet(node.getStateSet()); |
|---|
| 548 | osg::Matrix m = osg::computeLocalToWorld(getNodePath()); |
|---|
| 549 | unsigned int count = node.getNumDrawables(); |
|---|
| 550 | |
|---|
| 551 | for ( unsigned int i = 0; i < count; i++ ) |
|---|
| 552 | { |
|---|
| 553 | osg::Geometry *g = node.getDrawable( i )->asGeometry(); |
|---|
| 554 | if ( g != NULL ) |
|---|
| 555 | { |
|---|
| 556 | pushStateSet(g->getStateSet()); |
|---|
| 557 | processGeometry(g,m); |
|---|
| 558 | popStateSet(g->getStateSet()); |
|---|
| 559 | } |
|---|
| 560 | } |
|---|
| 561 | |
|---|
| 562 | |
|---|
| 563 | popStateSet(node.getStateSet()); |
|---|
| 564 | } |
|---|
| 565 | |
|---|
| 566 | |
|---|
| 567 | bool DXFWriterNodeVisitor::writeHeader(const osg::BoundingSphere &bound) |
|---|
| 568 | { |
|---|
| 569 | if ( _layers.empty() ) { |
|---|
| 570 | return false; |
|---|
| 571 | } |
|---|
| 572 | _fout << "999\n written by OpenSceneGraph" << std::endl; |
|---|
| 573 | |
|---|
| 574 | _fout << "0\nSECTION\n2\nHEADER\n"; |
|---|
| 575 | _fout << "9\n$ACADVER\n1\nAC1006\n"; |
|---|
| 576 | |
|---|
| 577 | _fout << "9\n$EXTMIN\n10\n"<<bound.center().x()-bound.radius()<<"\n20\n"<<bound.center().y()-bound.radius()<<"\n30\n"<<bound.center().z()-bound.radius()<<"\n"; |
|---|
| 578 | _fout << "9\n$EXTMAX\n10\n"<<bound.center().x()+bound.radius()<<"\n20\n"<<bound.center().y()+bound.radius()<<"\n30\n"<<bound.center().z()+bound.radius()<<"\n"; |
|---|
| 579 | |
|---|
| 580 | _fout << "0\nENDSEC\n0\nSECTION\n2\nTABLES\n"; |
|---|
| 581 | _fout << "0\nTABLE\n2\nLAYER\n"; |
|---|
| 582 | |
|---|
| 583 | for (std::vector<Layer>::iterator itr=_layers.begin();itr!=_layers.end();itr++) { |
|---|
| 584 | if ( itr->_color ) { |
|---|
| 585 | _fout<<"0\nLAYER\n2\n"<<itr->_name<<"\n70\n0\n62\n"<<itr->_color<<"\n6\nContinuous\n"; |
|---|
| 586 | } else { |
|---|
| 587 | _fout<<"0\nLAYER\n2\n"<<itr->_name<<"\n70\n0\n62\n255\n6\nContinuous\n"; |
|---|
| 588 | } |
|---|
| 589 | } |
|---|
| 590 | |
|---|
| 591 | _fout << "0\nENDTAB\n0\nENDSEC\n"; |
|---|
| 592 | |
|---|
| 593 | _fout << "0\nSECTION\n2\nENTITIES\n"; |
|---|
| 594 | _firstPass=false; |
|---|
| 595 | _count=0; |
|---|
| 596 | |
|---|
| 597 | return true; |
|---|
| 598 | } |
|---|
| 599 | |
|---|
| 600 | void DXFWriterNodeVisitor::writeFooter() |
|---|
| 601 | { |
|---|
| 602 | _fout << "0\nENDSEC\n0\nEOF"; |
|---|
| 603 | _fout << std::endl; |
|---|
| 604 | } |
|---|