You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

914 line
26 KiB

  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2012, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file DXFLoader.cpp
  35. * @brief Implementation of the DXF importer class
  36. */
  37. #include "AssimpPCH.h"
  38. #ifndef ASSIMP_BUILD_NO_DXF_IMPORTER
  39. #include "DXFLoader.h"
  40. #include "ParsingUtils.h"
  41. #include "ConvertToLHProcess.h"
  42. #include "fast_atof.h"
  43. #include "DXFHelper.h"
  44. using namespace Assimp;
  45. // AutoCAD Binary DXF<CR><LF><SUB><NULL>
  46. #define AI_DXF_BINARY_IDENT ("AutoCAD Binary DXF\r\n\x1a\0")
  47. #define AI_DXF_BINARY_IDENT_LEN (24)
  48. // default vertex color that all uncolored vertices will receive
  49. #define AI_DXF_DEFAULT_COLOR aiColor4D(0.6f,0.6f,0.6f,0.6f)
  50. // color indices for DXF - 16 are supported, the table is
  51. // taken directly from the DXF spec.
  52. static aiColor4D g_aclrDxfIndexColors[] =
  53. {
  54. aiColor4D (0.6f, 0.6f, 0.6f, 1.0f),
  55. aiColor4D (1.0f, 0.0f, 0.0f, 1.0f), // red
  56. aiColor4D (0.0f, 1.0f, 0.0f, 1.0f), // green
  57. aiColor4D (0.0f, 0.0f, 1.0f, 1.0f), // blue
  58. aiColor4D (0.3f, 1.0f, 0.3f, 1.0f), // light green
  59. aiColor4D (0.3f, 0.3f, 1.0f, 1.0f), // light blue
  60. aiColor4D (1.0f, 0.3f, 0.3f, 1.0f), // light red
  61. aiColor4D (1.0f, 0.0f, 1.0f, 1.0f), // pink
  62. aiColor4D (1.0f, 0.6f, 0.0f, 1.0f), // orange
  63. aiColor4D (0.6f, 0.3f, 0.0f, 1.0f), // dark orange
  64. aiColor4D (1.0f, 1.0f, 0.0f, 1.0f), // yellow
  65. aiColor4D (0.3f, 0.3f, 0.3f, 1.0f), // dark gray
  66. aiColor4D (0.8f, 0.8f, 0.8f, 1.0f), // light gray
  67. aiColor4D (0.0f, 00.f, 0.0f, 1.0f), // black
  68. aiColor4D (1.0f, 1.0f, 1.0f, 1.0f), // white
  69. aiColor4D (0.6f, 0.0f, 1.0f, 1.0f) // violet
  70. };
  71. #define AI_DXF_NUM_INDEX_COLORS (sizeof(g_aclrDxfIndexColors)/sizeof(g_aclrDxfIndexColors[0]))
  72. #define AI_DXF_ENTITIES_MAGIC_BLOCK "$ASSIMP_ENTITIES_MAGIC"
  73. static const aiImporterDesc desc = {
  74. "Drawing Interchange Format (DXF) Importer",
  75. "",
  76. "",
  77. "",
  78. aiImporterFlags_SupportTextFlavour | aiImporterFlags_LimitedSupport,
  79. 0,
  80. 0,
  81. 0,
  82. 0,
  83. "dxf"
  84. };
  85. // ------------------------------------------------------------------------------------------------
  86. // Constructor to be privately used by Importer
  87. DXFImporter::DXFImporter()
  88. {}
  89. // ------------------------------------------------------------------------------------------------
  90. // Destructor, private as well
  91. DXFImporter::~DXFImporter()
  92. {}
  93. // ------------------------------------------------------------------------------------------------
  94. // Returns whether the class can handle the format of the given file.
  95. bool DXFImporter::CanRead( const std::string& pFile, IOSystem* /*pIOHandler*/, bool /*checkSig*/) const
  96. {
  97. return SimpleExtensionCheck(pFile,"dxf");
  98. }
  99. // ------------------------------------------------------------------------------------------------
  100. // Get a list of all supported file extensions
  101. const aiImporterDesc* DXFImporter::GetInfo () const
  102. {
  103. return &desc;
  104. }
  105. // ------------------------------------------------------------------------------------------------
  106. // Imports the given file into the given scene structure.
  107. void DXFImporter::InternReadFile( const std::string& pFile,
  108. aiScene* pScene,
  109. IOSystem* pIOHandler)
  110. {
  111. boost::shared_ptr<IOStream> file = boost::shared_ptr<IOStream>( pIOHandler->Open( pFile) );
  112. // Check whether we can read the file
  113. if( file.get() == NULL) {
  114. throw DeadlyImportError( "Failed to open DXF file " + pFile + "");
  115. }
  116. // check whether this is a binaray DXF file - we can't read binary DXF files :-(
  117. char buff[AI_DXF_BINARY_IDENT_LEN+1] = {0};
  118. file->Read(buff,AI_DXF_BINARY_IDENT_LEN,1);
  119. if (!strncmp(AI_DXF_BINARY_IDENT,buff,AI_DXF_BINARY_IDENT_LEN)) {
  120. throw DeadlyImportError("DXF: Binary files are not supported at the moment");
  121. }
  122. // DXF files can grow very large, so read them via the StreamReader,
  123. // which will choose a suitable strategy.
  124. file->Seek(0,aiOrigin_SET);
  125. StreamReaderLE stream( file );
  126. DXF::LineReader reader (stream);
  127. DXF::FileData output;
  128. // now get all lines of the file and process top-level sections
  129. bool eof = false;
  130. while(!reader.End()) {
  131. // blocks table - these 'build blocks' are later (in ENTITIES)
  132. // referenced an included via INSERT statements.
  133. if (reader.Is(2,"BLOCKS")) {
  134. ParseBlocks(reader,output);
  135. continue;
  136. }
  137. // primary entity table
  138. if (reader.Is(2,"ENTITIES")) {
  139. ParseEntities(reader,output);
  140. continue;
  141. }
  142. // skip unneeded sections entirely to avoid any problems with them
  143. // alltogether.
  144. else if (reader.Is(2,"CLASSES") || reader.Is(2,"TABLES")) {
  145. SkipSection(reader);
  146. continue;
  147. }
  148. else if (reader.Is(2,"HEADER")) {
  149. ParseHeader(reader,output);
  150. continue;
  151. }
  152. // comments
  153. else if (reader.Is(999)) {
  154. DefaultLogger::get()->info("DXF Comment: " + reader.Value());
  155. }
  156. // don't read past the official EOF sign
  157. else if (reader.Is(0,"EOF")) {
  158. eof = true;
  159. break;
  160. }
  161. ++reader;
  162. }
  163. if (!eof) {
  164. DefaultLogger::get()->warn("DXF: EOF reached, but did not encounter DXF EOF marker");
  165. }
  166. ConvertMeshes(pScene,output);
  167. // Now rotate the whole scene by 90 degrees around the x axis to convert from AutoCAD's to Assimp's coordinate system
  168. pScene->mRootNode->mTransformation = aiMatrix4x4(
  169. 1.f,0.f,0.f,0.f,
  170. 0.f,0.f,1.f,0.f,
  171. 0.f,-1.f,0.f,0.f,
  172. 0.f,0.f,0.f,1.f) * pScene->mRootNode->mTransformation;
  173. }
  174. // ------------------------------------------------------------------------------------------------
  175. void DXFImporter::ConvertMeshes(aiScene* pScene, DXF::FileData& output)
  176. {
  177. // the process of resolving all the INSERT statements can grow the
  178. // polycount excessively, so log the original number.
  179. // XXX Option to import blocks as separate nodes?
  180. if (!DefaultLogger::isNullLogger()) {
  181. unsigned int vcount = 0, icount = 0;
  182. BOOST_FOREACH (const DXF::Block& bl, output.blocks) {
  183. BOOST_FOREACH (boost::shared_ptr<const DXF::PolyLine> pl, bl.lines) {
  184. vcount += pl->positions.size();
  185. icount += pl->counts.size();
  186. }
  187. }
  188. DefaultLogger::get()->debug((Formatter::format("DXF: Unexpanded polycount is "),
  189. icount,", vertex count is ",vcount
  190. ));
  191. }
  192. if (! output.blocks.size() ) {
  193. throw DeadlyImportError("DXF: no data blocks loaded");
  194. }
  195. DXF::Block* entities = 0;
  196. // index blocks by name
  197. DXF::BlockMap blocks_by_name;
  198. BOOST_FOREACH (DXF::Block& bl, output.blocks) {
  199. blocks_by_name[bl.name] = &bl;
  200. if ( !entities && bl.name == AI_DXF_ENTITIES_MAGIC_BLOCK ) {
  201. entities = &bl;
  202. }
  203. }
  204. if (!entities) {
  205. throw DeadlyImportError("DXF: no ENTITIES data block loaded");
  206. }
  207. typedef std::map<std::string, unsigned int> LayerMap;
  208. LayerMap layers;
  209. std::vector< std::vector< const DXF::PolyLine*> > corr;
  210. // now expand all block references in the primary ENTITIES block
  211. // XXX this involves heavy memory copying, consider a faster solution for future versions.
  212. ExpandBlockReferences(*entities,blocks_by_name);
  213. unsigned int cur = 0;
  214. BOOST_FOREACH (boost::shared_ptr<const DXF::PolyLine> pl, entities->lines) {
  215. if (pl->positions.size()) {
  216. std::map<std::string, unsigned int>::iterator it = layers.find(pl->layer);
  217. if (it == layers.end()) {
  218. ++pScene->mNumMeshes;
  219. layers[pl->layer] = cur++;
  220. std::vector< const DXF::PolyLine* > pv;
  221. pv.push_back(&*pl);
  222. corr.push_back(pv);
  223. }
  224. else {
  225. corr[(*it).second].push_back(&*pl);
  226. }
  227. }
  228. }
  229. if (!pScene->mNumMeshes) {
  230. throw DeadlyImportError("DXF: this file contains no 3d data");
  231. }
  232. pScene->mMeshes = new aiMesh*[ pScene->mNumMeshes ] ();
  233. BOOST_FOREACH(const LayerMap::value_type& elem, layers){
  234. aiMesh* const mesh = pScene->mMeshes[elem.second] = new aiMesh();
  235. mesh->mName.Set(elem.first);
  236. unsigned int cvert = 0,cface = 0;
  237. BOOST_FOREACH(const DXF::PolyLine* pl, corr[elem.second]){
  238. // sum over all faces since we need to 'verbosify' them.
  239. cvert += std::accumulate(pl->counts.begin(),pl->counts.end(),0);
  240. cface += pl->counts.size();
  241. }
  242. aiVector3D* verts = mesh->mVertices = new aiVector3D[cvert];
  243. aiColor4D* colors = mesh->mColors[0] = new aiColor4D[cvert];
  244. aiFace* faces = mesh->mFaces = new aiFace[cface];
  245. mesh->mNumVertices = cvert;
  246. mesh->mNumFaces = cface;
  247. unsigned int prims = 0;
  248. unsigned int overall_indices = 0;
  249. BOOST_FOREACH(const DXF::PolyLine* pl, corr[elem.second]){
  250. std::vector<unsigned int>::const_iterator it = pl->indices.begin();
  251. BOOST_FOREACH(unsigned int facenumv,pl->counts) {
  252. aiFace& face = *faces++;
  253. face.mIndices = new unsigned int[face.mNumIndices = facenumv];
  254. for (unsigned int i = 0; i < facenumv; ++i) {
  255. face.mIndices[i] = overall_indices++;
  256. ai_assert(pl->positions.size() == pl->colors.size());
  257. if (*it >= pl->positions.size()) {
  258. throw DeadlyImportError("DXF: vertex index out of bounds");
  259. }
  260. *verts++ = pl->positions[*it];
  261. *colors++ = pl->colors[*it++];
  262. }
  263. // set primitive flags now, this saves the extra pass in ScenePreprocessor.
  264. switch(face.mNumIndices) {
  265. case 1:
  266. prims |= aiPrimitiveType_POINT;
  267. break;
  268. case 2:
  269. prims |= aiPrimitiveType_LINE;
  270. break;
  271. case 3:
  272. prims |= aiPrimitiveType_TRIANGLE;
  273. break;
  274. default:
  275. prims |= aiPrimitiveType_POLYGON;
  276. break;
  277. }
  278. }
  279. }
  280. mesh->mPrimitiveTypes = prims;
  281. mesh->mMaterialIndex = 0;
  282. }
  283. GenerateHierarchy(pScene,output);
  284. GenerateMaterials(pScene,output);
  285. }
  286. // ------------------------------------------------------------------------------------------------
  287. void DXFImporter::ExpandBlockReferences(DXF::Block& bl,const DXF::BlockMap& blocks_by_name)
  288. {
  289. BOOST_FOREACH (const DXF::InsertBlock& insert, bl.insertions) {
  290. // first check if the referenced blocks exists ...
  291. const DXF::BlockMap::const_iterator it = blocks_by_name.find(insert.name);
  292. if (it == blocks_by_name.end()) {
  293. DefaultLogger::get()->error((Formatter::format("DXF: Failed to resolve block reference: "),
  294. insert.name,"; skipping"
  295. ));
  296. continue;
  297. }
  298. // XXX this would be the place to implement recursive expansion if needed.
  299. const DXF::Block& bl_src = *(*it).second;
  300. BOOST_FOREACH (boost::shared_ptr<const DXF::PolyLine> pl_in, bl_src.lines) {
  301. boost::shared_ptr<DXF::PolyLine> pl_out = boost::shared_ptr<DXF::PolyLine>(new DXF::PolyLine(*pl_in));
  302. if (bl_src.base.Length() || insert.scale.x!=1.f || insert.scale.y!=1.f || insert.scale.z!=1.f || insert.angle || insert.pos.Length()) {
  303. // manual coordinate system transformation
  304. // XXX order
  305. aiMatrix4x4 trafo, tmp;
  306. aiMatrix4x4::Translation(-bl_src.base,trafo);
  307. trafo *= aiMatrix4x4::Scaling(insert.scale,tmp);
  308. trafo *= aiMatrix4x4::Translation(insert.pos,tmp);
  309. // XXX rotation currently ignored - I didn't find an appropriate sample model.
  310. if (insert.angle != 0.f) {
  311. DefaultLogger::get()->warn("DXF: BLOCK rotation not currently implemented");
  312. }
  313. BOOST_FOREACH (aiVector3D& v, pl_out->positions) {
  314. v *= trafo;
  315. }
  316. }
  317. bl.lines.push_back(pl_out);
  318. }
  319. }
  320. }
  321. // ------------------------------------------------------------------------------------------------
  322. void DXFImporter::GenerateMaterials(aiScene* pScene, DXF::FileData& /*output*/)
  323. {
  324. // generate an almost-white default material. Reason:
  325. // the default vertex color is GREY, so we are
  326. // already at Assimp's usual default color.
  327. // generate a default material
  328. aiMaterial* pcMat = new aiMaterial();
  329. aiString s;
  330. s.Set(AI_DEFAULT_MATERIAL_NAME);
  331. pcMat->AddProperty(&s, AI_MATKEY_NAME);
  332. aiColor4D clrDiffuse(0.9f,0.9f,0.9f,1.0f);
  333. pcMat->AddProperty(&clrDiffuse,1,AI_MATKEY_COLOR_DIFFUSE);
  334. clrDiffuse = aiColor4D(1.0f,1.0f,1.0f,1.0f);
  335. pcMat->AddProperty(&clrDiffuse,1,AI_MATKEY_COLOR_SPECULAR);
  336. clrDiffuse = aiColor4D(0.05f,0.05f,0.05f,1.0f);
  337. pcMat->AddProperty(&clrDiffuse,1,AI_MATKEY_COLOR_AMBIENT);
  338. pScene->mNumMaterials = 1;
  339. pScene->mMaterials = new aiMaterial*[1];
  340. pScene->mMaterials[0] = pcMat;
  341. }
  342. // ------------------------------------------------------------------------------------------------
  343. void DXFImporter::GenerateHierarchy(aiScene* pScene, DXF::FileData& /*output*/)
  344. {
  345. // generate the output scene graph, which is just the root node with a single child for each layer.
  346. pScene->mRootNode = new aiNode();
  347. pScene->mRootNode->mName.Set("<DXF_ROOT>");
  348. if (1 == pScene->mNumMeshes) {
  349. pScene->mRootNode->mMeshes = new unsigned int[ pScene->mRootNode->mNumMeshes = 1 ];
  350. pScene->mRootNode->mMeshes[0] = 0;
  351. }
  352. else
  353. {
  354. pScene->mRootNode->mChildren = new aiNode*[ pScene->mRootNode->mNumChildren = pScene->mNumMeshes ];
  355. for (unsigned int m = 0; m < pScene->mRootNode->mNumChildren;++m) {
  356. aiNode* p = pScene->mRootNode->mChildren[m] = new aiNode();
  357. p->mName = pScene->mMeshes[m]->mName;
  358. p->mMeshes = new unsigned int[p->mNumMeshes = 1];
  359. p->mMeshes[0] = m;
  360. p->mParent = pScene->mRootNode;
  361. }
  362. }
  363. }
  364. // ------------------------------------------------------------------------------------------------
  365. void DXFImporter::SkipSection(DXF::LineReader& reader)
  366. {
  367. for( ;!reader.End() && !reader.Is(0,"ENDSEC"); reader++);
  368. }
  369. // ------------------------------------------------------------------------------------------------
  370. void DXFImporter::ParseHeader(DXF::LineReader& reader, DXF::FileData& /*output*/)
  371. {
  372. for( ;!reader.End() && !reader.Is(0,"ENDSEC"); reader++);
  373. }
  374. // ------------------------------------------------------------------------------------------------
  375. void DXFImporter::ParseBlocks(DXF::LineReader& reader, DXF::FileData& output)
  376. {
  377. while( !reader.End() && !reader.Is(0,"ENDSEC")) {
  378. if (reader.Is(0,"BLOCK")) {
  379. ParseBlock(++reader,output);
  380. continue;
  381. }
  382. ++reader;
  383. }
  384. DefaultLogger::get()->debug((Formatter::format("DXF: got "),
  385. output.blocks.size()," entries in BLOCKS"
  386. ));
  387. }
  388. // ------------------------------------------------------------------------------------------------
  389. void DXFImporter::ParseBlock(DXF::LineReader& reader, DXF::FileData& output)
  390. {
  391. // push a new block onto the stack.
  392. output.blocks.push_back( DXF::Block() );
  393. DXF::Block& block = output.blocks.back();
  394. while( !reader.End() && !reader.Is(0,"ENDBLK")) {
  395. switch(reader.GroupCode()) {
  396. case 2:
  397. block.name = reader.Value();
  398. break;
  399. case 10:
  400. block.base.x = reader.ValueAsFloat();
  401. break;
  402. case 20:
  403. block.base.y = reader.ValueAsFloat();
  404. break;
  405. case 30:
  406. block.base.z = reader.ValueAsFloat();
  407. break;
  408. }
  409. if (reader.Is(0,"POLYLINE")) {
  410. ParsePolyLine(++reader,output);
  411. continue;
  412. }
  413. // XXX is this a valid case?
  414. if (reader.Is(0,"INSERT")) {
  415. DefaultLogger::get()->warn("DXF: INSERT within a BLOCK not currently supported; skipping");
  416. for( ;!reader.End() && !reader.Is(0,"ENDBLK"); ++reader);
  417. break;
  418. }
  419. else if (reader.Is(0,"3DFACE") || reader.Is(0,"LINE") || reader.Is(0,"3DLINE")) {
  420. //http://sourceforge.net/tracker/index.php?func=detail&aid=2970566&group_id=226462&atid=1067632
  421. Parse3DFace(++reader, output);
  422. continue;
  423. }
  424. ++reader;
  425. }
  426. }
  427. // ------------------------------------------------------------------------------------------------
  428. void DXFImporter::ParseEntities(DXF::LineReader& reader, DXF::FileData& output)
  429. {
  430. // push a new block onto the stack.
  431. output.blocks.push_back( DXF::Block() );
  432. DXF::Block& block = output.blocks.back();
  433. block.name = AI_DXF_ENTITIES_MAGIC_BLOCK;
  434. while( !reader.End() && !reader.Is(0,"ENDSEC")) {
  435. if (reader.Is(0,"POLYLINE")) {
  436. ParsePolyLine(++reader,output);
  437. continue;
  438. }
  439. else if (reader.Is(0,"INSERT")) {
  440. ParseInsertion(++reader,output);
  441. continue;
  442. }
  443. else if (reader.Is(0,"3DFACE") || reader.Is(0,"LINE") || reader.Is(0,"3DLINE")) {
  444. //http://sourceforge.net/tracker/index.php?func=detail&aid=2970566&group_id=226462&atid=1067632
  445. Parse3DFace(++reader, output);
  446. continue;
  447. }
  448. ++reader;
  449. }
  450. DefaultLogger::get()->debug((Formatter::format("DXF: got "),
  451. block.lines.size()," polylines and ", block.insertions.size() ," inserted blocks in ENTITIES"
  452. ));
  453. }
  454. void DXFImporter::ParseInsertion(DXF::LineReader& reader, DXF::FileData& output)
  455. {
  456. output.blocks.back().insertions.push_back( DXF::InsertBlock() );
  457. DXF::InsertBlock& bl = output.blocks.back().insertions.back();
  458. while( !reader.End() && !reader.Is(0)) {
  459. switch(reader.GroupCode())
  460. {
  461. // name of referenced block
  462. case 2:
  463. bl.name = reader.Value();
  464. break;
  465. // translation
  466. case 10:
  467. bl.pos.x = reader.ValueAsFloat();
  468. break;
  469. case 20:
  470. bl.pos.y = reader.ValueAsFloat();
  471. break;
  472. case 30:
  473. bl.pos.z = reader.ValueAsFloat();
  474. break;
  475. // scaling
  476. case 41:
  477. bl.scale.x = reader.ValueAsFloat();
  478. break;
  479. case 42:
  480. bl.scale.y = reader.ValueAsFloat();
  481. break;
  482. case 43:
  483. bl.scale.z = reader.ValueAsFloat();
  484. break;
  485. // rotation angle
  486. case 50:
  487. bl.angle = reader.ValueAsFloat();
  488. break;
  489. }
  490. reader++;
  491. }
  492. }
  493. #define DXF_POLYLINE_FLAG_CLOSED 0x1
  494. #define DXF_POLYLINE_FLAG_3D_POLYLINE 0x8
  495. #define DXF_POLYLINE_FLAG_3D_POLYMESH 0x10
  496. #define DXF_POLYLINE_FLAG_POLYFACEMESH 0x40
  497. // ------------------------------------------------------------------------------------------------
  498. void DXFImporter::ParsePolyLine(DXF::LineReader& reader, DXF::FileData& output)
  499. {
  500. output.blocks.back().lines.push_back( boost::shared_ptr<DXF::PolyLine>( new DXF::PolyLine() ) );
  501. DXF::PolyLine& line = *output.blocks.back().lines.back();
  502. unsigned int iguess = 0, vguess = 0;
  503. while( !reader.End() && !reader.Is(0,"ENDSEC")) {
  504. if (reader.Is(0,"VERTEX")) {
  505. ParsePolyLineVertex(++reader,line);
  506. if (reader.Is(0,"SEQEND")) {
  507. break;
  508. }
  509. continue;
  510. }
  511. switch(reader.GroupCode())
  512. {
  513. // flags --- important that we know whether it is a
  514. // polyface mesh or 'just' a line.
  515. case 70:
  516. if (!line.flags) {
  517. line.flags = reader.ValueAsSignedInt();
  518. }
  519. break;
  520. // optional number of vertices
  521. case 71:
  522. vguess = reader.ValueAsSignedInt();
  523. line.positions.reserve(vguess);
  524. break;
  525. // optional number of faces
  526. case 72:
  527. iguess = reader.ValueAsSignedInt();
  528. line.indices.reserve(iguess);
  529. break;
  530. // 8 specifies the layer on which this line is placed on
  531. case 8:
  532. line.layer = reader.Value();
  533. break;
  534. }
  535. reader++;
  536. }
  537. //if (!(line.flags & DXF_POLYLINE_FLAG_POLYFACEMESH)) {
  538. // DefaultLogger::get()->warn((Formatter::format("DXF: polyline not currently supported: "),line.flags));
  539. // output.blocks.back().lines.pop_back();
  540. // return;
  541. //}
  542. if (vguess && line.positions.size() != vguess) {
  543. DefaultLogger::get()->warn((Formatter::format("DXF: unexpected vertex count in polymesh: "),
  544. line.positions.size(),", expected ", vguess
  545. ));
  546. }
  547. if (line.flags & DXF_POLYLINE_FLAG_POLYFACEMESH ) {
  548. if (line.positions.size() < 3 || line.indices.size() < 3) {
  549. DefaultLogger::get()->warn("DXF: not enough vertices for polymesh; ignoring");
  550. output.blocks.back().lines.pop_back();
  551. return;
  552. }
  553. // if these numbers are wrong, parsing might have gone wild.
  554. // however, the docs state that applications are not required
  555. // to set the 71 and 72 fields, respectively, to valid values.
  556. // So just fire a warning.
  557. if (iguess && line.counts.size() != iguess) {
  558. DefaultLogger::get()->warn((Formatter::format("DXF: unexpected face count in polymesh: "),
  559. line.counts.size(),", expected ", iguess
  560. ));
  561. }
  562. }
  563. else if (!line.indices.size() && !line.counts.size()) {
  564. // a polyline - so there are no indices yet.
  565. size_t guess = line.positions.size() + (line.flags & DXF_POLYLINE_FLAG_CLOSED ? 1 : 0);
  566. line.indices.reserve(guess);
  567. line.counts.reserve(guess/2);
  568. for (unsigned int i = 0; i < line.positions.size()/2; ++i) {
  569. line.indices.push_back(i*2);
  570. line.indices.push_back(i*2+1);
  571. line.counts.push_back(2);
  572. }
  573. // closed polyline?
  574. if (line.flags & DXF_POLYLINE_FLAG_CLOSED) {
  575. line.indices.push_back(line.positions.size()-1);
  576. line.indices.push_back(0);
  577. line.counts.push_back(2);
  578. }
  579. }
  580. }
  581. #define DXF_VERTEX_FLAG_PART_OF_POLYFACE 0x80
  582. #define DXF_VERTEX_FLAG_HAS_POSITIONS 0x40
  583. // ------------------------------------------------------------------------------------------------
  584. void DXFImporter::ParsePolyLineVertex(DXF::LineReader& reader, DXF::PolyLine& line)
  585. {
  586. unsigned int cnti = 0, flags = 0;
  587. unsigned int indices[4];
  588. aiVector3D out;
  589. aiColor4D clr = AI_DXF_DEFAULT_COLOR;
  590. while( !reader.End() ) {
  591. if (reader.Is(0)) { // SEQEND or another VERTEX
  592. break;
  593. }
  594. switch (reader.GroupCode())
  595. {
  596. case 8:
  597. // layer to which the vertex belongs to - assume that
  598. // this is always the layer the top-level polyline
  599. // entity resides on as well.
  600. if(reader.Value() != line.layer) {
  601. DefaultLogger::get()->warn("DXF: expected vertex to be part of a polyface but the 0x128 flag isn't set");
  602. }
  603. break;
  604. case 70:
  605. flags = reader.ValueAsUnsignedInt();
  606. break;
  607. // VERTEX COORDINATES
  608. case 10: out.x = reader.ValueAsFloat();break;
  609. case 20: out.y = reader.ValueAsFloat();break;
  610. case 30: out.z = reader.ValueAsFloat();break;
  611. // POLYFACE vertex indices
  612. case 71:
  613. case 72:
  614. case 73:
  615. case 74:
  616. if (cnti == 4) {
  617. DefaultLogger::get()->warn("DXF: more than 4 indices per face not supported; ignoring");
  618. break;
  619. }
  620. indices[cnti++] = reader.ValueAsUnsignedInt();
  621. break;
  622. // color
  623. case 62:
  624. clr = g_aclrDxfIndexColors[reader.ValueAsUnsignedInt() % AI_DXF_NUM_INDEX_COLORS];
  625. break;
  626. };
  627. reader++;
  628. }
  629. if (line.flags & DXF_POLYLINE_FLAG_POLYFACEMESH && !(flags & DXF_VERTEX_FLAG_PART_OF_POLYFACE)) {
  630. DefaultLogger::get()->warn("DXF: expected vertex to be part of a polyface but the 0x128 flag isn't set");
  631. }
  632. if (cnti) {
  633. line.counts.push_back(cnti);
  634. for (unsigned int i = 0; i < cnti; ++i) {
  635. // IMPORTANT NOTE: POLYMESH indices are ONE-BASED
  636. if (indices[i] == 0) {
  637. DefaultLogger::get()->warn("DXF: invalid vertex index, indices are one-based.");
  638. --line.counts.back();
  639. continue;
  640. }
  641. line.indices.push_back(indices[i]-1);
  642. }
  643. }
  644. else {
  645. line.positions.push_back(out);
  646. line.colors.push_back(clr);
  647. }
  648. }
  649. // ------------------------------------------------------------------------------------------------
  650. void DXFImporter::Parse3DFace(DXF::LineReader& reader, DXF::FileData& output)
  651. {
  652. // (note) this is also used for for parsing line entities, so we
  653. // must handle the vertex_count == 2 case as well.
  654. output.blocks.back().lines.push_back( boost::shared_ptr<DXF::PolyLine>( new DXF::PolyLine() ) );
  655. DXF::PolyLine& line = *output.blocks.back().lines.back();
  656. aiVector3D vip[4];
  657. aiColor4D clr = AI_DXF_DEFAULT_COLOR;
  658. bool b[4] = {false,false,false,false};
  659. while( !reader.End() ) {
  660. // next entity with a groupcode == 0 is probably already the next vertex or polymesh entity
  661. if (reader.GroupCode() == 0) {
  662. break;
  663. }
  664. switch (reader.GroupCode())
  665. {
  666. // 8 specifies the layer
  667. case 8:
  668. line.layer = reader.Value();
  669. break;
  670. // x position of the first corner
  671. case 10: vip[0].x = reader.ValueAsFloat();
  672. b[2] = true;
  673. break;
  674. // y position of the first corner
  675. case 20: vip[0].y = reader.ValueAsFloat();
  676. b[2] = true;
  677. break;
  678. // z position of the first corner
  679. case 30: vip[0].z = reader.ValueAsFloat();
  680. b[2] = true;
  681. break;
  682. // x position of the second corner
  683. case 11: vip[1].x = reader.ValueAsFloat();
  684. b[3] = true;
  685. break;
  686. // y position of the second corner
  687. case 21: vip[1].y = reader.ValueAsFloat();
  688. b[3] = true;
  689. break;
  690. // z position of the second corner
  691. case 31: vip[1].z = reader.ValueAsFloat();
  692. b[3] = true;
  693. break;
  694. // x position of the third corner
  695. case 12: vip[2].x = reader.ValueAsFloat();
  696. b[0] = true;
  697. break;
  698. // y position of the third corner
  699. case 22: vip[2].y = reader.ValueAsFloat();
  700. b[0] = true;
  701. break;
  702. // z position of the third corner
  703. case 32: vip[2].z = reader.ValueAsFloat();
  704. b[0] = true;
  705. break;
  706. // x position of the fourth corner
  707. case 13: vip[3].x = reader.ValueAsFloat();
  708. b[1] = true;
  709. break;
  710. // y position of the fourth corner
  711. case 23: vip[3].y = reader.ValueAsFloat();
  712. b[1] = true;
  713. break;
  714. // z position of the fourth corner
  715. case 33: vip[3].z = reader.ValueAsFloat();
  716. b[1] = true;
  717. break;
  718. // color
  719. case 62:
  720. clr = g_aclrDxfIndexColors[reader.ValueAsUnsignedInt() % AI_DXF_NUM_INDEX_COLORS];
  721. break;
  722. };
  723. ++reader;
  724. }
  725. // the fourth corner may even be identical to the third,
  726. // in this case we treat it as if it didn't exist.
  727. if (vip[3] == vip[2]) {
  728. b[1] = false;
  729. }
  730. // sanity checks to see if we got something meaningful
  731. if ((b[1] && !b[0]) || !b[2] || !b[3]) {
  732. DefaultLogger::get()->warn("DXF: unexpected vertex setup in 3DFACE/LINE/FACE entity; ignoring");
  733. output.blocks.back().lines.pop_back();
  734. return;
  735. }
  736. const unsigned int cnt = (2+(b[0]?1:0)+(b[1]?1:0));
  737. line.counts.push_back(cnt);
  738. for (unsigned int i = 0; i < cnt; ++i) {
  739. line.indices.push_back(line.positions.size());
  740. line.positions.push_back(vip[i]);
  741. line.colors.push_back(clr);
  742. }
  743. }
  744. #endif // !! ASSIMP_BUILD_NO_DXF_IMPORTER