StepExporter.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2015, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. @author: Richard Steffen, 2015
  32. ----------------------------------------------------------------------
  33. */
  34. #include "AssimpPCH.h"
  35. #ifndef ASSIMP_BUILD_NO_EXPORT
  36. #ifndef ASSIMP_BUILD_NO_STEP_EXPORTER
  37. #include "StepExporter.h"
  38. #include "ConvertToLHProcess.h"
  39. #include "Bitmap.h"
  40. #include "fast_atof.h"
  41. #include "SceneCombiner.h"
  42. #include <ctime>
  43. #include <set>
  44. #include <map>
  45. #include <list>
  46. /* Tested with Step viewer v4 from www.ida-step.net */
  47. using namespace Assimp;
  48. namespace Assimp
  49. {
  50. // ------------------------------------------------------------------------------------------------
  51. // Worker function for exporting a scene to Collada. Prototyped and registered in Exporter.cpp
  52. void ExportSceneStep(const char* pFile,IOSystem* pIOSystem, const aiScene* pScene, const ExportProperties* pProperties)
  53. {
  54. std::string path = "";
  55. std::string file = pFile;
  56. // We need to test both types of folder separators because pIOSystem->getOsSeparator() is not reliable.
  57. // Moreover, the path given by some applications is not even consistent with the OS specific type of separator.
  58. const char* end_path = std::max(strrchr(pFile, '\\'), strrchr(pFile, '/'));
  59. if(end_path != NULL) {
  60. path = std::string(pFile, end_path + 1 - pFile);
  61. file = file.substr(end_path + 1 - pFile, file.npos);
  62. std::size_t pos = file.find_last_of('.');
  63. if(pos != file.npos) {
  64. file = file.substr(0, pos);
  65. }
  66. }
  67. // create/copy Properties
  68. ExportProperties props(*pProperties);
  69. // set standard properties if not set
  70. //if (!props.HasPropertyBool(AI_CONFIG_EXPORT_XFILE_64BIT)) props.SetPropertyBool(AI_CONFIG_EXPORT_XFILE_64BIT, false);
  71. // invoke the exporter
  72. StepExporter iDoTheExportThing( pScene, pIOSystem, path, file, &props);
  73. // we're still here - export successfully completed. Write result to the given IOSYstem
  74. boost::scoped_ptr<IOStream> outfile (pIOSystem->Open(pFile,"wt"));
  75. if(outfile == NULL) {
  76. throw DeadlyExportError("could not open output .stp file: " + std::string(pFile));
  77. }
  78. // XXX maybe use a small wrapper around IOStream that behaves like std::stringstream in order to avoid the extra copy.
  79. outfile->Write( iDoTheExportThing.mOutput.str().c_str(), static_cast<size_t>(iDoTheExportThing.mOutput.tellp()),1);
  80. }
  81. } // end of namespace Assimp
  82. namespace {
  83. inline uint64_t toIndexHash(int32_t id1, int32_t id2)
  84. {
  85. // dont wonder that -1/-1 -> hash=-1
  86. uint64_t hash = (uint32_t) id1;
  87. hash = (hash << 32);
  88. hash += (uint32_t) id2;
  89. return hash;
  90. }
  91. inline void fromIndexHash(uint64_t hash, int32_t &id1, int32_t &id2)
  92. {
  93. id1 = (hash & 0xFFFFFFFF00000000) >> 32;
  94. id2 = (hash & 0xFFFFFFFF);
  95. }
  96. // Collect world transformations for each node
  97. void CollectTrafos(const aiNode* node, std::map<const aiNode*, aiMatrix4x4>& trafos) {
  98. const aiMatrix4x4& parent = node->mParent ? trafos[node->mParent] : aiMatrix4x4();
  99. trafos[node] = parent * node->mTransformation;
  100. for (unsigned int i = 0; i < node->mNumChildren; ++i) {
  101. CollectTrafos(node->mChildren[i], trafos);
  102. }
  103. }
  104. // Generate a flat list of the meshes (by index) assigned to each node
  105. void CollectMeshes(const aiNode* node, std::multimap<const aiNode*, unsigned int>& meshes) {
  106. for (unsigned int i = 0; i < node->mNumMeshes; ++i) {
  107. meshes.insert(std::make_pair(node, node->mMeshes[i]));
  108. }
  109. for (unsigned int i = 0; i < node->mNumChildren; ++i) {
  110. CollectMeshes(node->mChildren[i], meshes);
  111. }
  112. }
  113. }
  114. // ------------------------------------------------------------------------------------------------
  115. // Constructor for a specific scene to export
  116. StepExporter::StepExporter(const aiScene* pScene, IOSystem* pIOSystem, const std::string& path, const std::string& file, const ExportProperties* pProperties) : mIOSystem(pIOSystem), mPath(path), mFile(file), mProperties(pProperties)
  117. {
  118. CollectTrafos(pScene->mRootNode, trafos);
  119. CollectMeshes(pScene->mRootNode, meshes);
  120. // make sure that all formatting happens using the standard, C locale and not the user's current locale
  121. mOutput.imbue( std::locale("C") );
  122. mFile = std::string(file);
  123. mPath = std::string(path);
  124. mScene = pScene;
  125. mSceneOwned = false;
  126. // set up strings
  127. endstr = ";\n";
  128. // start writing
  129. WriteFile();
  130. }
  131. // ------------------------------------------------------------------------------------------------
  132. // Destructor
  133. StepExporter::~StepExporter()
  134. {
  135. if(mSceneOwned) {
  136. delete mScene;
  137. }
  138. }
  139. // ------------------------------------------------------------------------------------------------
  140. // Starts writing the contents
  141. void StepExporter::WriteFile()
  142. {
  143. // see http://shodhganga.inflibnet.ac.in:8080/jspui/bitstream/10603/14116/11/11_chapter%203.pdf
  144. // note, that all realnumber values must be comma separated in x files
  145. mOutput.setf(std::ios::fixed);
  146. mOutput.precision(16); // precission for double
  147. aiMatrix4x4 baseTransform = mScene->mRootNode->mTransformation;
  148. int ind = 100; // the start index to be used
  149. int faceEntryLen = 30; // number of entries for a triangle/face
  150. // prepare unique (count triangles and vertices)
  151. std::map<aiVector3D*, int> uniqueVerts; // use a map to reduce find complexity to log(n)
  152. std::map<aiVector3D*, int>::iterator it;
  153. int countFace = 0;
  154. for (unsigned int i=0; i<mScene->mNumMeshes; ++i)
  155. {
  156. aiMesh* mesh = mScene->mMeshes[i];
  157. for (unsigned int j=0; j<mesh->mNumFaces; ++j)
  158. {
  159. aiFace* face = &(mesh->mFaces[j]);
  160. if (face->mNumIndices == 3) countFace++;
  161. }
  162. for (unsigned int j=0; j<mesh->mNumVertices; ++j)
  163. {
  164. aiVector3D* v = &(mesh->mVertices[j]);
  165. it =uniqueVerts.find(v);
  166. if (it == uniqueVerts.end())
  167. {
  168. uniqueVerts[v] = -1; // first mark the vector as not transformed
  169. }
  170. }
  171. }
  172. // write the header
  173. mOutput << "ISO-10303-21" << endstr;
  174. mOutput << "HEADER" << endstr;
  175. mOutput << "FILE_DESCRIPTION(('STEP AP214'),'1')" << endstr;
  176. mOutput << "FILE_NAME('" << mFile << ".stp','2015-04-23T09:26:41',(' '),(' '),'Spatial InterOp 3D',' ',' ')" << endstr;
  177. mOutput << "FILE_SCHEMA(('automotive_design'))" << endstr;
  178. mOutput << "ENDSEC" << endstr;
  179. // write the top of data
  180. mOutput << "DATA" << endstr;
  181. mOutput << "#1=MECHANICAL_DESIGN_GEOMETRIC_PRESENTATION_REPRESENTATION(' ',(";
  182. for (int i=0; i<countFace; ++i)
  183. {
  184. mOutput << "#" << i*faceEntryLen + ind + 2*uniqueVerts.size();
  185. if (i!=countFace-1) mOutput << ",";
  186. }
  187. mOutput << "),#6)" << endstr;
  188. mOutput << "#2=PRODUCT_DEFINITION_CONTEXT('',#7,'design')" << endstr;
  189. mOutput << "#3=APPLICATION_PROTOCOL_DEFINITION('INTERNATIONAL STANDARD','automotive_design',1994,#7)" << endstr;
  190. mOutput << "#4=PRODUCT_CATEGORY_RELATIONSHIP('NONE','NONE',#8,#9)" << endstr;
  191. mOutput << "#5=SHAPE_DEFINITION_REPRESENTATION(#10,#11)" << endstr;
  192. mOutput << "#6= (GEOMETRIC_REPRESENTATION_CONTEXT(3)GLOBAL_UNCERTAINTY_ASSIGNED_CONTEXT((#12))GLOBAL_UNIT_ASSIGNED_CONTEXT((#13,#14,#15))REPRESENTATION_CONTEXT('NONE','WORKSPACE'))" << endstr;
  193. mOutput << "#7=APPLICATION_CONTEXT(' ')" << endstr;
  194. mOutput << "#8=PRODUCT_CATEGORY('part','NONE')" << endstr;
  195. mOutput << "#9=PRODUCT_RELATED_PRODUCT_CATEGORY('detail',' ',(#17))" << endstr;
  196. mOutput << "#10=PRODUCT_DEFINITION_SHAPE('NONE','NONE',#18)" << endstr;
  197. mOutput << "#11=MANIFOLD_SURFACE_SHAPE_REPRESENTATION('Root',(#16,#19),#6)" << endstr;
  198. mOutput << "#12=UNCERTAINTY_MEASURE_WITH_UNIT(LENGTH_MEASURE(1.0E-006),#13,'','')" << endstr;
  199. mOutput << "#13=(CONVERSION_BASED_UNIT('METRE',#20)LENGTH_UNIT()NAMED_UNIT(#21))" << endstr;
  200. mOutput << "#14=(NAMED_UNIT(#22)PLANE_ANGLE_UNIT()SI_UNIT($,.RADIAN.))" << endstr;
  201. mOutput << "#15=(NAMED_UNIT(#22)SOLID_ANGLE_UNIT()SI_UNIT($,.STERADIAN.))" << endstr;
  202. mOutput << "#16=SHELL_BASED_SURFACE_MODEL('Root',(#29))" << endstr;
  203. mOutput << "#17=PRODUCT('Root','Root','Root',(#23))" << endstr;
  204. mOutput << "#18=PRODUCT_DEFINITION('NONE','NONE',#24,#2)" << endstr;
  205. mOutput << "#19=AXIS2_PLACEMENT_3D('',#25,#26,#27)" << endstr;
  206. mOutput << "#20=LENGTH_MEASURE_WITH_UNIT(LENGTH_MEASURE(1.0),#28)" << endstr;
  207. mOutput << "#21=DIMENSIONAL_EXPONENTS(1.0,0.0,0.0,0.0,0.0,0.0,0.0)" << endstr;
  208. mOutput << "#22=DIMENSIONAL_EXPONENTS(0.0,0.0,0.0,0.0,0.0,0.0,0.0)" << endstr;
  209. mOutput << "#23=PRODUCT_CONTEXT('',#7,'mechanical')" << endstr;
  210. mOutput << "#24=PRODUCT_DEFINITION_FORMATION_WITH_SPECIFIED_SOURCE(' ','NONE',#17,.NOT_KNOWN.)" << endstr;
  211. mOutput << "#25=CARTESIAN_POINT('',(0.0,0.0,0.0))" << endstr;
  212. mOutput << "#26=DIRECTION('',(0.0,0.0,1.0))" << endstr;
  213. mOutput << "#27=DIRECTION('',(1.0,0.0,0.0))" << endstr;
  214. mOutput << "#28= (NAMED_UNIT(#21)LENGTH_UNIT()SI_UNIT(.MILLI.,.METRE.))" << endstr;
  215. mOutput << "#29=CLOSED_SHELL('',(";
  216. for (int i=0; i<countFace; ++i)
  217. {
  218. mOutput << "#" << i*faceEntryLen + ind + 2*uniqueVerts.size() + 8;
  219. if (i!=countFace-1) mOutput << ",";
  220. }
  221. mOutput << "))" << endstr;
  222. // write all the unique transformed CARTESIAN and VERTEX
  223. for (MeshesByNodeMap::const_iterator it2 = meshes.begin(); it2 != meshes.end(); it2++)
  224. {
  225. const aiNode& node = *(*it2).first;
  226. unsigned int mesh_idx = (*it2).second;
  227. const aiMesh* mesh = mScene->mMeshes[mesh_idx];
  228. aiMatrix4x4& trafo = trafos[&node];
  229. for (unsigned int i = 0; i < mesh->mNumVertices; ++i)
  230. {
  231. aiVector3D* v = &(mesh->mVertices[i]);
  232. it = uniqueVerts.find(v);
  233. if (it->second >=0 ) continue;
  234. it->second = ind; // this one is new, so set the index (ind)
  235. aiVector3D vt = trafo * (*v); // transform the coordinate
  236. mOutput << "#" << it->second << "=CARTESIAN_POINT('',(" << vt.x << "," << vt.y << "," << vt.z << "))" << endstr;
  237. mOutput << "#" << it->second+1 << "=VERTEX_POINT('',#" << it->second << ")" << endstr;
  238. ind += 2;
  239. }
  240. }
  241. // write the triangles
  242. for (unsigned int i=0; i<mScene->mNumMeshes; ++i)
  243. {
  244. aiMesh* mesh = mScene->mMeshes[i];
  245. for (unsigned int j=0; j<mesh->mNumFaces; ++j)
  246. {
  247. aiFace* face = &(mesh->mFaces[j]);
  248. if (face->mNumIndices != 3) continue;
  249. aiVector3D* v1 = &(mesh->mVertices[face->mIndices[0]]);
  250. aiVector3D* v2 = &(mesh->mVertices[face->mIndices[1]]);
  251. aiVector3D* v3 = &(mesh->mVertices[face->mIndices[2]]);
  252. aiVector3D dv12 = *v2 - *v1;
  253. aiVector3D dv23 = *v3 - *v2;
  254. aiVector3D dv31 = *v1 - *v3;
  255. aiVector3D dv13 = *v3 - *v1;
  256. dv12.Normalize();
  257. dv23.Normalize();
  258. dv31.Normalize();
  259. dv13.Normalize();
  260. int pid1 = uniqueVerts.find(v1)->second;
  261. int pid2 = uniqueVerts.find(v2)->second;
  262. int pid3 = uniqueVerts.find(v3)->second;
  263. int sid = ind; // the sub index
  264. mOutput << "#" << sid << "=STYLED_ITEM('',(#" << sid+1 << "),#" << sid+8 << ")" << endstr; /* the item that must be referenced in #1 */
  265. /* This is the color information of the Triangle */
  266. mOutput << "#" << sid+1 << "=PRESENTATION_STYLE_ASSIGNMENT((#" << sid+2 << "))" << endstr;
  267. mOutput << "#" << sid+2 << "=SURFACE_STYLE_USAGE(.BOTH.,#" << sid+3 << ")" << endstr;
  268. mOutput << "#" << sid+3 << "=SURFACE_SIDE_STYLE('',(#" << sid+4 << "))" << endstr;
  269. mOutput << "#" << sid+4 << "=SURFACE_STYLE_FILL_AREA(#" << sid+5 << ")" << endstr;
  270. mOutput << "#" << sid+5 << "=FILL_AREA_STYLE('',(#" << sid+6 << "))" << endstr;
  271. mOutput << "#" << sid+6 << "=FILL_AREA_STYLE_COLOUR('',#" << sid+7 << ")" << endstr;
  272. mOutput << "#" << sid+7 << "=COLOUR_RGB('',0.0,0.0,1.0)" << endstr;
  273. /* this is the geometry */
  274. mOutput << "#" << sid+8 << "=FACE_SURFACE('',(#" << sid+13 << "),#" << sid+9<< ",.T.)" << endstr; /* the face that must be referenced in 29 */
  275. /* 2 directions of the plane */
  276. mOutput << "#" << sid+9 << "=PLANE('',#" << sid+10 << ")" << endstr;
  277. mOutput << "#" << sid+10 << "=AXIS2_PLACEMENT_3D('',#" << pid1 << ", #" << sid+11 << ",#" << sid+12 << ")" << endstr;
  278. mOutput << "#" << sid+11 << "=DIRECTION('',(" << dv12.x << "," << dv12.y << "," << dv12.z << "))" << endstr;
  279. mOutput << "#" << sid+12 << "=DIRECTION('',(" << dv13.x << "," << dv13.y << "," << dv13.z << "))" << endstr;
  280. mOutput << "#" << sid+13 << "=FACE_BOUND('',#" << sid+14 << ",.T.)" << endstr;
  281. mOutput << "#" << sid+14 << "=EDGE_LOOP('',(#" << sid+15 << ",#" << sid+16 << ",#" << sid+17 << "))" << endstr;
  282. /* edge loop */
  283. mOutput << "#" << sid+15 << "=ORIENTED_EDGE('',*,*,#" << sid+18 << ",.T.)" << endstr;
  284. mOutput << "#" << sid+16 << "=ORIENTED_EDGE('',*,*,#" << sid+19 << ",.T.)" << endstr;
  285. mOutput << "#" << sid+17 << "=ORIENTED_EDGE('',*,*,#" << sid+20 << ",.T.)" << endstr;
  286. /* oriented edges */
  287. mOutput << "#" << sid+18 << "=EDGE_CURVE('',#" << pid1+1 << ",#" << pid2+1 << ",#" << sid+21 << ",.F.)" << endstr;
  288. mOutput << "#" << sid+19 << "=EDGE_CURVE('',#" << pid2+1 << ",#" << pid3+1 << ",#" << sid+22 << ",.T.)" << endstr;
  289. mOutput << "#" << sid+20 << "=EDGE_CURVE('',#" << pid3+1 << ",#" << pid1+1 << ",#" << sid+23 << ",.T.)" << endstr;
  290. /* 3 lines and 3 vectors for the lines for the 3 edge curves */
  291. mOutput << "#" << sid+21 << "=LINE('',#" << pid1 << ",#" << sid+24 << ")" << endstr;
  292. mOutput << "#" << sid+22 << "=LINE('',#" << pid2 << ",#" << sid+25 << ")" << endstr;
  293. mOutput << "#" << sid+23 << "=LINE('',#" << pid3 << ",#" << sid+26 << ")" << endstr;
  294. mOutput << "#" << sid+24 << "=VECTOR('',#" << sid+27 << ",1.0)" << endstr;
  295. mOutput << "#" << sid+25 << "=VECTOR('',#" << sid+28 << ",1.0)" << endstr;
  296. mOutput << "#" << sid+26 << "=VECTOR('',#" << sid+29 << ",1.0)" << endstr;
  297. mOutput << "#" << sid+27 << "=DIRECTION('',(" << dv12.x << "," << dv12.y << "," << dv12.z << "))" << endstr;
  298. mOutput << "#" << sid+28 << "=DIRECTION('',(" << dv23.x << "," << dv23.y << "," << dv23.z << "))" << endstr;
  299. mOutput << "#" << sid+29 << "=DIRECTION('',(" << dv31.x << "," << dv31.y << "," << dv31.z << "))" << endstr;
  300. ind += faceEntryLen; // increase counter
  301. }
  302. }
  303. mOutput << "ENDSEC" << endstr; // end of data section
  304. mOutput << "END-ISO-10303-21" << endstr; // end of file
  305. }
  306. #endif
  307. #endif