DXFLoader.cpp 30 KB

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