DXFLoader.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development 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 Development 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 Implementation of the DXF importer class */
  35. #include "DXFLoader.h"
  36. #include "ParsingUtils.h"
  37. #include "fast_atof.h"
  38. #include "MaterialSystem.h"
  39. // public ASSIMP headers
  40. #include "../include/aiScene.h"
  41. #include "../include/aiAssert.h"
  42. #include "../include/IOStream.h"
  43. #include "../include/IOSystem.h"
  44. #include "../include/DefaultLogger.h"
  45. // boost headers
  46. #include <boost/scoped_ptr.hpp>
  47. using namespace Assimp;
  48. // ------------------------------------------------------------------------------------------------
  49. // Constructor to be privately used by Importer
  50. DXFImporter::DXFImporter()
  51. {
  52. // nothing to do here
  53. }
  54. // ------------------------------------------------------------------------------------------------
  55. // Destructor, private as well
  56. DXFImporter::~DXFImporter()
  57. {
  58. // nothing to do here
  59. }
  60. // ------------------------------------------------------------------------------------------------
  61. // Returns whether the class can handle the format of the given file.
  62. bool DXFImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler) const
  63. {
  64. // simple check of file extension is enough for the moment
  65. std::string::size_type pos = pFile.find_last_of('.');
  66. // no file extension - can't read
  67. if( pos == std::string::npos)
  68. return false;
  69. std::string extension = pFile.substr( pos);
  70. return !(extension.length() != 4 || extension[0] != '.' ||
  71. extension[1] != 'd' && extension[1] != 'D' ||
  72. extension[2] != 'x' && extension[2] != 'X' ||
  73. extension[3] != 'f' && extension[3] != 'F');
  74. }
  75. // ------------------------------------------------------------------------------------------------
  76. bool DXFImporter::GetNextLine()
  77. {
  78. if(!SkipLine(&buffer))return false;
  79. if(!SkipSpaces(&buffer))return GetNextLine();
  80. return true;
  81. }
  82. // ------------------------------------------------------------------------------------------------
  83. bool DXFImporter::GetNextToken()
  84. {
  85. SkipSpaces(&buffer);
  86. groupCode = strtol10s(buffer,&buffer);
  87. if(!GetNextLine())return false;
  88. // copy the data line to a separate buffer
  89. char* m = cursor, *end = &cursor[4096];
  90. while (!IsLineEnd ( *buffer ) && m < end)
  91. *m++ = *buffer++;
  92. *m = '\0';
  93. GetNextLine();
  94. return true;
  95. }
  96. // ------------------------------------------------------------------------------------------------
  97. // Imports the given file into the given scene structure.
  98. void DXFImporter::InternReadFile( const std::string& pFile,
  99. aiScene* pScene, IOSystem* pIOHandler)
  100. {
  101. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile));
  102. // Check whether we can read from the file
  103. if( file.get() == NULL)
  104. throw new ImportErrorException( "Failed to open DXF file " + pFile + "");
  105. // read the contents of the file in a buffer
  106. unsigned int m = (unsigned int)file->FileSize();
  107. std::vector<char> buffer2(m+1);
  108. buffer = &buffer2[0];
  109. file->Read( &buffer2[0], m,1);
  110. buffer2[m] = '\0';
  111. mDefaultLayer = NULL;
  112. // now get all lines of the file
  113. while (GetNextToken())
  114. {
  115. if (2 == groupCode)
  116. {
  117. // ENTITIES section
  118. if (!::strcmp(cursor,"ENTITIES"))
  119. if (!ParseEntities())break;
  120. // other sections such as BLOCK - skip them to make
  121. // sure there will be no name conflicts
  122. else
  123. {
  124. bool b = false;
  125. while (GetNextToken())
  126. {
  127. if (!groupCode && !::strcmp(cursor,"ENDSEC"))
  128. {b = true;break;}
  129. }
  130. if (!b)break;
  131. }
  132. }
  133. // print comment strings
  134. else if (999 == groupCode)
  135. {
  136. DefaultLogger::get()->info(std::string( cursor ));
  137. }
  138. else if (!groupCode && !::strcmp(cursor,"EOF"))
  139. break;
  140. }
  141. if (mLayers.empty())
  142. throw new ImportErrorException("DXF: this file contains no 3d data");
  143. pScene->mMeshes = new aiMesh*[pScene->mNumMeshes = (unsigned int)mLayers.size()];
  144. m = 0;
  145. for (std::vector<LayerInfo>::const_iterator it = mLayers.begin(),end = mLayers.end();
  146. it != end;++it)
  147. {
  148. // generate the output mesh
  149. aiMesh* pMesh = pScene->mMeshes[m++] = new aiMesh();
  150. const std::vector<aiVector3D>& vPositions = (*it).vPositions;
  151. pMesh->mNumFaces = (unsigned int)vPositions.size() / 4u;
  152. pMesh->mFaces = new aiFace[pMesh->mNumFaces];
  153. aiVector3D* vpOut = pMesh->mVertices = new aiVector3D[vPositions.size()];
  154. const aiVector3D* vp = &vPositions[0];
  155. for (unsigned int i = 0; i < pMesh->mNumFaces;++i)
  156. {
  157. aiFace& face = pMesh->mFaces[i];
  158. // check whether we need four indices here
  159. if (vp[3] != vp[2])
  160. {
  161. face.mNumIndices = 4;
  162. }
  163. else face.mNumIndices = 3;
  164. face.mIndices = new unsigned int[face.mNumIndices];
  165. for (unsigned int a = 0; a < face.mNumIndices;++a)
  166. {
  167. *vpOut++ = vp[a];
  168. face.mIndices[a] = pMesh->mNumVertices++;
  169. }
  170. vp += 4;
  171. }
  172. }
  173. // generate the output scene graph
  174. pScene->mRootNode = new aiNode();
  175. pScene->mRootNode->mName.Set("<DXF_ROOT>");
  176. if (1 == pScene->mNumMeshes)
  177. {
  178. pScene->mRootNode->mMeshes = new unsigned int[ pScene->mRootNode->mNumMeshes = 1 ];
  179. pScene->mRootNode->mMeshes[0] = 0;
  180. }
  181. else
  182. {
  183. pScene->mRootNode->mChildren = new aiNode*[ pScene->mRootNode->mNumChildren = pScene->mNumMeshes ];
  184. for (m = 0; m < pScene->mRootNode->mNumChildren;++m)
  185. {
  186. aiNode* p = pScene->mRootNode->mChildren[m] = new aiNode();
  187. p->mName.length = ::strlen( mLayers[m].name );
  188. ::strcpy(p->mName.data, mLayers[m].name);
  189. p->mMeshes = new unsigned int[p->mNumMeshes = 1];
  190. p->mMeshes[0] = m;
  191. p->mParent = pScene->mRootNode;
  192. }
  193. }
  194. // generate a default material
  195. MaterialHelper* pcMat = new MaterialHelper();
  196. aiString s;
  197. s.Set(AI_DEFAULT_MATERIAL_NAME);
  198. pcMat->AddProperty(&s, AI_MATKEY_NAME);
  199. aiColor4D clrDiffuse(0.6f,0.6f,0.6f,1.0f);
  200. pcMat->AddProperty(&clrDiffuse,1,AI_MATKEY_COLOR_DIFFUSE);
  201. clrDiffuse = aiColor4D(1.0f,1.0f,1.0f,1.0f);
  202. pcMat->AddProperty(&clrDiffuse,1,AI_MATKEY_COLOR_SPECULAR);
  203. clrDiffuse = aiColor4D(0.05f,0.05f,0.05f,1.0f);
  204. pcMat->AddProperty(&clrDiffuse,1,AI_MATKEY_COLOR_AMBIENT);
  205. pScene->mNumMaterials = 1;
  206. pScene->mMaterials = new aiMaterial*[1];
  207. pScene->mMaterials[0] = pcMat;
  208. // --- everything destructs automatically ---
  209. }
  210. // ------------------------------------------------------------------------------------------------
  211. bool DXFImporter::ParseEntities()
  212. {
  213. while (GetNextToken())
  214. {
  215. if (!groupCode)
  216. {
  217. while (true) {
  218. if (!::strcmp(cursor,"3DFACE"))
  219. if (!Parse3DFace()) return false; else continue;
  220. break;
  221. };
  222. if (!::strcmp(cursor,"ENDSEC"))
  223. return true;
  224. }
  225. }
  226. return false;
  227. }
  228. // ------------------------------------------------------------------------------------------------
  229. bool DXFImporter::Parse3DFace()
  230. {
  231. bool ret = false;
  232. LayerInfo* out = NULL;
  233. aiVector3D vip[4]; // -- vectors are initialized to zero
  234. while (GetNextToken())
  235. {
  236. switch (groupCode)
  237. {
  238. case 0: ret = true;break;
  239. // 8 specifies the layer
  240. case 8:
  241. {
  242. for (std::vector<LayerInfo>::iterator it = mLayers.begin(),end = mLayers.end();
  243. it != end;++it)
  244. {
  245. if (!::strcmp( (*it).name, cursor ))
  246. {
  247. out = &(*it);
  248. break;
  249. }
  250. }
  251. if (!out)
  252. {
  253. // we don't have this layer yet
  254. mLayers.push_back(LayerInfo());
  255. out = &mLayers.back();
  256. ::strcpy(out->name,cursor);
  257. }
  258. break;
  259. }
  260. // x position of the first corner
  261. case 10: vip[0].x = fast_atof(cursor);break;
  262. // y position of the first corner
  263. case 20: vip[0].y = -fast_atof(cursor);break;
  264. // z position of the first corner
  265. case 30: vip[0].z = fast_atof(cursor);break;
  266. // x position of the second corner
  267. case 11: vip[1].x = fast_atof(cursor);break;
  268. // y position of the second corner
  269. case 21: vip[1].y = -fast_atof(cursor);break;
  270. // z position of the second corner
  271. case 31: vip[1].z = fast_atof(cursor);break;
  272. // x position of the third corner
  273. case 12: vip[2].x = fast_atof(cursor);break;
  274. // y position of the third corner
  275. case 22: vip[2].y = -fast_atof(cursor);break;
  276. // z position of the third corner
  277. case 32: vip[2].z = fast_atof(cursor);break;
  278. // x position of the fourth corner
  279. case 13: vip[3].x = fast_atof(cursor);break;
  280. // y position of the fourth corner
  281. case 23: vip[3].y = -fast_atof(cursor);break;
  282. // z position of the fourth corner
  283. case 33: vip[3].z = fast_atof(cursor);break;
  284. };
  285. if (ret)break;
  286. }
  287. // use a default layer if necessary
  288. if (!out)
  289. {
  290. if (!mDefaultLayer)
  291. {
  292. mLayers.push_back(LayerInfo());
  293. mDefaultLayer = &mLayers.back();
  294. }
  295. out = mDefaultLayer;
  296. }
  297. // add the faces to the face list for this layer
  298. out->vPositions.push_back(vip[0]);
  299. out->vPositions.push_back(vip[1]);
  300. out->vPositions.push_back(vip[2]);
  301. out->vPositions.push_back(vip[3]);
  302. return ret;
  303. }