StepExporter.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2020, 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. #ifndef ASSIMP_BUILD_NO_EXPORT
  35. #ifndef ASSIMP_BUILD_NO_STEP_EXPORTER
  36. #include "AssetLib/Step/StepExporter.h"
  37. #include "PostProcessing/ConvertToLHProcess.h"
  38. #include <assimp/Bitmap.h>
  39. #include <assimp/BaseImporter.h>
  40. #include <assimp/fast_atof.h>
  41. #include <assimp/SceneCombiner.h>
  42. #include <assimp/Exceptional.h>
  43. #include <assimp/DefaultIOSystem.h>
  44. #include <assimp/IOSystem.hpp>
  45. #include <assimp/scene.h>
  46. #include <assimp/light.h>
  47. #include <iostream>
  48. #include <ctime>
  49. #include <set>
  50. #include <map>
  51. #include <list>
  52. #include <memory>
  53. //
  54. #if _MSC_VER > 1500 || (defined __GNUC___)
  55. # define ASSIMP_STEP_USE_UNORDERED_MULTIMAP
  56. # else
  57. # define step_unordered_map map
  58. # define step_unordered_multimap multimap
  59. #endif
  60. #ifdef ASSIMP_STEP_USE_UNORDERED_MULTIMAP
  61. # include <unordered_map>
  62. # if _MSC_VER > 1600
  63. # define step_unordered_map unordered_map
  64. # define step_unordered_multimap unordered_multimap
  65. # else
  66. # define step_unordered_map tr1::unordered_map
  67. # define step_unordered_multimap tr1::unordered_multimap
  68. # endif
  69. #endif
  70. typedef std::step_unordered_map<aiVector3D*, int> VectorIndexUMap;
  71. /* Tested with Step viewer v4 from www.ida-step.net */
  72. using namespace Assimp;
  73. namespace Assimp
  74. {
  75. // ------------------------------------------------------------------------------------------------
  76. // Worker function for exporting a scene to Collada. Prototyped and registered in Exporter.cpp
  77. void ExportSceneStep(const char* pFile,IOSystem* pIOSystem, const aiScene* pScene, const ExportProperties* pProperties)
  78. {
  79. std::string path = DefaultIOSystem::absolutePath(std::string(pFile));
  80. std::string file = DefaultIOSystem::completeBaseName(std::string(pFile));
  81. // create/copy Properties
  82. ExportProperties props(*pProperties);
  83. // invoke the exporter
  84. StepExporter iDoTheExportThing( pScene, pIOSystem, path, file, &props);
  85. // we're still here - export successfully completed. Write result to the given IOSYstem
  86. std::unique_ptr<IOStream> outfile (pIOSystem->Open(pFile,"wt"));
  87. if (outfile == nullptr) {
  88. throw DeadlyExportError("could not open output .stp file: " + std::string(pFile));
  89. }
  90. // XXX maybe use a small wrapper around IOStream that behaves like std::stringstream in order to avoid the extra copy.
  91. outfile->Write( iDoTheExportThing.mOutput.str().c_str(), static_cast<size_t>(iDoTheExportThing.mOutput.tellp()),1);
  92. }
  93. } // end of namespace Assimp
  94. namespace {
  95. // Collect world transformations for each node
  96. void CollectTrafos(const aiNode* node, std::map<const aiNode*, aiMatrix4x4>& trafos) {
  97. const aiMatrix4x4& parent = node->mParent ? trafos[node->mParent] : aiMatrix4x4();
  98. trafos[node] = parent * node->mTransformation;
  99. for (unsigned int i = 0; i < node->mNumChildren; ++i) {
  100. CollectTrafos(node->mChildren[i], trafos);
  101. }
  102. }
  103. // Generate a flat list of the meshes (by index) assigned to each node
  104. void CollectMeshes(const aiNode* node, std::multimap<const aiNode*, unsigned int>& meshes) {
  105. for (unsigned int i = 0; i < node->mNumMeshes; ++i) {
  106. meshes.insert(std::make_pair(node, node->mMeshes[i]));
  107. }
  108. for (unsigned int i = 0; i < node->mNumChildren; ++i) {
  109. CollectMeshes(node->mChildren[i], meshes);
  110. }
  111. }
  112. }
  113. // ------------------------------------------------------------------------------------------------
  114. // Constructor for a specific scene to export
  115. StepExporter::StepExporter(const aiScene* pScene, IOSystem* pIOSystem, const std::string& path,
  116. const std::string& file, const ExportProperties* pProperties) :
  117. mProperties(pProperties), mIOSystem(pIOSystem), mFile(file), mPath(path),
  118. mScene(pScene), endstr(";\n") {
  119. CollectTrafos(pScene->mRootNode, trafos);
  120. CollectMeshes(pScene->mRootNode, meshes);
  121. // make sure that all formatting happens using the standard, C locale and not the user's current locale
  122. mOutput.imbue(std::locale("C"));
  123. mOutput.precision(ASSIMP_AI_REAL_TEXT_PRECISION);
  124. // start writing
  125. WriteFile();
  126. }
  127. // ------------------------------------------------------------------------------------------------
  128. // Starts writing the contents
  129. void StepExporter::WriteFile()
  130. {
  131. // see http://shodhganga.inflibnet.ac.in:8080/jspui/bitstream/10603/14116/11/11_chapter%203.pdf
  132. // note, that all realnumber values must be comma separated in x files
  133. mOutput.setf(std::ios::fixed);
  134. // precision for double
  135. // see http://stackoverflow.com/questions/554063/how-do-i-print-a-double-value-with-full-precision-using-cout
  136. mOutput.precision(ASSIMP_AI_REAL_TEXT_PRECISION);
  137. // standard color
  138. aiColor4D fColor;
  139. fColor.r = 0.8f;
  140. fColor.g = 0.8f;
  141. fColor.b = 0.8f;
  142. int ind = 100; // the start index to be used
  143. int faceEntryLen = 30; // number of entries for a triangle/face
  144. // prepare unique (count triangles and vertices)
  145. VectorIndexUMap uniqueVerts; // use a map to reduce find complexity to log(n)
  146. VectorIndexUMap::iterator it;
  147. int countFace = 0;
  148. for (unsigned int i=0; i<mScene->mNumMeshes; ++i)
  149. {
  150. aiMesh* mesh = mScene->mMeshes[i];
  151. for (unsigned int j=0; j<mesh->mNumFaces; ++j)
  152. {
  153. aiFace* face = &(mesh->mFaces[j]);
  154. if (face->mNumIndices == 3) countFace++;
  155. }
  156. for (unsigned int j=0; j<mesh->mNumVertices; ++j)
  157. {
  158. aiVector3D* v = &(mesh->mVertices[j]);
  159. it =uniqueVerts.find(v);
  160. if (it == uniqueVerts.end())
  161. {
  162. uniqueVerts[v] = -1; // first mark the vector as not transformed
  163. }
  164. }
  165. }
  166. static const unsigned int date_nb_chars = 20;
  167. char date_str[date_nb_chars];
  168. std::time_t date = std::time(nullptr);
  169. std::strftime(date_str, date_nb_chars, "%Y-%m-%dT%H:%M:%S", std::localtime(&date));
  170. // write the header
  171. mOutput << "ISO-10303-21" << endstr;
  172. mOutput << "HEADER" << endstr;
  173. mOutput << "FILE_DESCRIPTION(('STEP AP214'),'1')" << endstr;
  174. mOutput << "FILE_NAME('" << mFile << ".stp','" << date_str << "',(' '),(' '),'Spatial InterOp 3D',' ',' ')" << endstr;
  175. mOutput << "FILE_SCHEMA(('automotive_design'))" << endstr;
  176. mOutput << "ENDSEC" << endstr;
  177. // write the top of data
  178. mOutput << "DATA" << endstr;
  179. mOutput << "#1=MECHANICAL_DESIGN_GEOMETRIC_PRESENTATION_REPRESENTATION(' ',(";
  180. for (int i=0; i<countFace; ++i)
  181. {
  182. mOutput << "#" << i*faceEntryLen + ind + 2*uniqueVerts.size();
  183. if (i!=countFace-1) mOutput << ",";
  184. }
  185. mOutput << "),#6)" << endstr;
  186. mOutput << "#2=PRODUCT_DEFINITION_CONTEXT('',#7,'design')" << endstr;
  187. mOutput << "#3=APPLICATION_PROTOCOL_DEFINITION('INTERNATIONAL STANDARD','automotive_design',1994,#7)" << endstr;
  188. mOutput << "#4=PRODUCT_CATEGORY_RELATIONSHIP('NONE','NONE',#8,#9)" << endstr;
  189. mOutput << "#5=SHAPE_DEFINITION_REPRESENTATION(#10,#11)" << endstr;
  190. mOutput << "#6= (GEOMETRIC_REPRESENTATION_CONTEXT(3)GLOBAL_UNCERTAINTY_ASSIGNED_CONTEXT((#12))GLOBAL_UNIT_ASSIGNED_CONTEXT((#13,#14,#15))REPRESENTATION_CONTEXT('NONE','WORKSPACE'))" << endstr;
  191. mOutput << "#7=APPLICATION_CONTEXT(' ')" << endstr;
  192. mOutput << "#8=PRODUCT_CATEGORY('part','NONE')" << endstr;
  193. mOutput << "#9=PRODUCT_RELATED_PRODUCT_CATEGORY('detail',' ',(#17))" << endstr;
  194. mOutput << "#10=PRODUCT_DEFINITION_SHAPE('NONE','NONE',#18)" << endstr;
  195. mOutput << "#11=MANIFOLD_SURFACE_SHAPE_REPRESENTATION('Root',(#16,#19),#6)" << endstr;
  196. mOutput << "#12=UNCERTAINTY_MEASURE_WITH_UNIT(LENGTH_MEASURE(1.0E-006),#13,'','')" << endstr;
  197. mOutput << "#13=(CONVERSION_BASED_UNIT('METRE',#20)LENGTH_UNIT()NAMED_UNIT(#21))" << endstr;
  198. mOutput << "#14=(NAMED_UNIT(#22)PLANE_ANGLE_UNIT()SI_UNIT($,.RADIAN.))" << endstr;
  199. mOutput << "#15=(NAMED_UNIT(#22)SOLID_ANGLE_UNIT()SI_UNIT($,.STERADIAN.))" << endstr;
  200. mOutput << "#16=SHELL_BASED_SURFACE_MODEL('Root',(#29))" << endstr;
  201. mOutput << "#17=PRODUCT('Root','Root','Root',(#23))" << endstr;
  202. mOutput << "#18=PRODUCT_DEFINITION('NONE','NONE',#24,#2)" << endstr;
  203. mOutput << "#19=AXIS2_PLACEMENT_3D('',#25,#26,#27)" << endstr;
  204. mOutput << "#20=LENGTH_MEASURE_WITH_UNIT(LENGTH_MEASURE(1.0),#28)" << endstr;
  205. mOutput << "#21=DIMENSIONAL_EXPONENTS(1.0,0.0,0.0,0.0,0.0,0.0,0.0)" << endstr;
  206. mOutput << "#22=DIMENSIONAL_EXPONENTS(0.0,0.0,0.0,0.0,0.0,0.0,0.0)" << endstr;
  207. mOutput << "#23=PRODUCT_CONTEXT('',#7,'mechanical')" << endstr;
  208. mOutput << "#24=PRODUCT_DEFINITION_FORMATION_WITH_SPECIFIED_SOURCE(' ','NONE',#17,.NOT_KNOWN.)" << endstr;
  209. mOutput << "#25=CARTESIAN_POINT('',(0.0,0.0,0.0))" << endstr;
  210. mOutput << "#26=DIRECTION('',(0.0,0.0,1.0))" << endstr;
  211. mOutput << "#27=DIRECTION('',(1.0,0.0,0.0))" << endstr;
  212. mOutput << "#28= (NAMED_UNIT(#21)LENGTH_UNIT()SI_UNIT(.MILLI.,.METRE.))" << endstr;
  213. mOutput << "#29=CLOSED_SHELL('',(";
  214. for (int i=0; i<countFace; ++i)
  215. {
  216. mOutput << "#" << i*faceEntryLen + ind + 2*uniqueVerts.size() + 8;
  217. if (i!=countFace-1) mOutput << ",";
  218. }
  219. mOutput << "))" << endstr;
  220. // write all the unique transformed CARTESIAN and VERTEX
  221. for (MeshesByNodeMap::const_iterator it2 = meshes.begin(); it2 != meshes.end(); ++it2)
  222. {
  223. const aiNode& node = *(*it2).first;
  224. unsigned int mesh_idx = (*it2).second;
  225. const aiMesh* mesh = mScene->mMeshes[mesh_idx];
  226. aiMatrix4x4& trafo = trafos[&node];
  227. for (unsigned int i = 0; i < mesh->mNumVertices; ++i)
  228. {
  229. aiVector3D* v = &(mesh->mVertices[i]);
  230. it = uniqueVerts.find(v);
  231. if (it->second >=0 ) continue;
  232. it->second = ind; // this one is new, so set the index (ind)
  233. aiVector3D vt = trafo * (*v); // transform the coordinate
  234. mOutput << "#" << it->second << "=CARTESIAN_POINT('',(" << vt.x << "," << vt.y << "," << vt.z << "))" << endstr;
  235. mOutput << "#" << it->second+1 << "=VERTEX_POINT('',#" << it->second << ")" << endstr;
  236. ind += 2;
  237. }
  238. }
  239. // write the triangles
  240. for (unsigned int i=0; i<mScene->mNumMeshes; ++i)
  241. {
  242. aiMesh* mesh = mScene->mMeshes[i];
  243. for (unsigned int j=0; j<mesh->mNumFaces; ++j)
  244. {
  245. aiFace* face = &(mesh->mFaces[j]);
  246. if (face->mNumIndices != 3) continue;
  247. aiVector3D* v1 = &(mesh->mVertices[face->mIndices[0]]);
  248. aiVector3D* v2 = &(mesh->mVertices[face->mIndices[1]]);
  249. aiVector3D* v3 = &(mesh->mVertices[face->mIndices[2]]);
  250. aiVector3D dv12 = *v2 - *v1;
  251. aiVector3D dv23 = *v3 - *v2;
  252. aiVector3D dv31 = *v1 - *v3;
  253. aiVector3D dv13 = *v3 - *v1;
  254. dv12.Normalize();
  255. dv23.Normalize();
  256. dv31.Normalize();
  257. dv13.Normalize();
  258. int pid1 = uniqueVerts.find(v1)->second;
  259. int pid2 = uniqueVerts.find(v2)->second;
  260. int pid3 = uniqueVerts.find(v3)->second;
  261. // mean vertex color for the face if available
  262. if (mesh->HasVertexColors(0))
  263. {
  264. fColor.r = 0.0;
  265. fColor.g = 0.0;
  266. fColor.b = 0.0;
  267. fColor += mesh->mColors[0][face->mIndices[0]];
  268. fColor += mesh->mColors[0][face->mIndices[1]];
  269. fColor += mesh->mColors[0][face->mIndices[2]];
  270. fColor /= 3.0f;
  271. }
  272. int sid = ind; // the sub index
  273. mOutput << "#" << sid << "=STYLED_ITEM('',(#" << sid+1 << "),#" << sid+8 << ")" << endstr; /* the item that must be referenced in #1 */
  274. /* This is the color information of the Triangle */
  275. mOutput << "#" << sid+1 << "=PRESENTATION_STYLE_ASSIGNMENT((#" << sid+2 << "))" << endstr;
  276. mOutput << "#" << sid+2 << "=SURFACE_STYLE_USAGE(.BOTH.,#" << sid+3 << ")" << endstr;
  277. mOutput << "#" << sid+3 << "=SURFACE_SIDE_STYLE('',(#" << sid+4 << "))" << endstr;
  278. mOutput << "#" << sid+4 << "=SURFACE_STYLE_FILL_AREA(#" << sid+5 << ")" << endstr;
  279. mOutput << "#" << sid+5 << "=FILL_AREA_STYLE('',(#" << sid+6 << "))" << endstr;
  280. mOutput << "#" << sid+6 << "=FILL_AREA_STYLE_COLOUR('',#" << sid+7 << ")" << endstr;
  281. mOutput << "#" << sid+7 << "=COLOUR_RGB(''," << fColor.r << "," << fColor.g << "," << fColor.b << ")" << endstr;
  282. /* this is the geometry */
  283. mOutput << "#" << sid+8 << "=FACE_SURFACE('',(#" << sid+13 << "),#" << sid+9<< ",.T.)" << endstr; /* the face that must be referenced in 29 */
  284. /* 2 directions of the plane */
  285. mOutput << "#" << sid+9 << "=PLANE('',#" << sid+10 << ")" << endstr;
  286. mOutput << "#" << sid+10 << "=AXIS2_PLACEMENT_3D('',#" << pid1 << ", #" << sid+11 << ",#" << sid+12 << ")" << endstr;
  287. mOutput << "#" << sid+11 << "=DIRECTION('',(" << dv12.x << "," << dv12.y << "," << dv12.z << "))" << endstr;
  288. mOutput << "#" << sid+12 << "=DIRECTION('',(" << dv13.x << "," << dv13.y << "," << dv13.z << "))" << endstr;
  289. mOutput << "#" << sid+13 << "=FACE_BOUND('',#" << sid+14 << ",.T.)" << endstr;
  290. mOutput << "#" << sid+14 << "=EDGE_LOOP('',(#" << sid+15 << ",#" << sid+16 << ",#" << sid+17 << "))" << endstr;
  291. /* edge loop */
  292. mOutput << "#" << sid+15 << "=ORIENTED_EDGE('',*,*,#" << sid+18 << ",.T.)" << endstr;
  293. mOutput << "#" << sid+16 << "=ORIENTED_EDGE('',*,*,#" << sid+19 << ",.T.)" << endstr;
  294. mOutput << "#" << sid+17 << "=ORIENTED_EDGE('',*,*,#" << sid+20 << ",.T.)" << endstr;
  295. /* oriented edges */
  296. mOutput << "#" << sid+18 << "=EDGE_CURVE('',#" << pid1+1 << ",#" << pid2+1 << ",#" << sid+21 << ",.F.)" << endstr;
  297. mOutput << "#" << sid+19 << "=EDGE_CURVE('',#" << pid2+1 << ",#" << pid3+1 << ",#" << sid+22 << ",.T.)" << endstr;
  298. mOutput << "#" << sid+20 << "=EDGE_CURVE('',#" << pid3+1 << ",#" << pid1+1 << ",#" << sid+23 << ",.T.)" << endstr;
  299. /* 3 lines and 3 vectors for the lines for the 3 edge curves */
  300. mOutput << "#" << sid+21 << "=LINE('',#" << pid1 << ",#" << sid+24 << ")" << endstr;
  301. mOutput << "#" << sid+22 << "=LINE('',#" << pid2 << ",#" << sid+25 << ")" << endstr;
  302. mOutput << "#" << sid+23 << "=LINE('',#" << pid3 << ",#" << sid+26 << ")" << endstr;
  303. mOutput << "#" << sid+24 << "=VECTOR('',#" << sid+27 << ",1.0)" << endstr;
  304. mOutput << "#" << sid+25 << "=VECTOR('',#" << sid+28 << ",1.0)" << endstr;
  305. mOutput << "#" << sid+26 << "=VECTOR('',#" << sid+29 << ",1.0)" << endstr;
  306. mOutput << "#" << sid+27 << "=DIRECTION('',(" << dv12.x << "," << dv12.y << "," << dv12.z << "))" << endstr;
  307. mOutput << "#" << sid+28 << "=DIRECTION('',(" << dv23.x << "," << dv23.y << "," << dv23.z << "))" << endstr;
  308. mOutput << "#" << sid+29 << "=DIRECTION('',(" << dv31.x << "," << dv31.y << "," << dv31.z << "))" << endstr;
  309. ind += faceEntryLen; // increase counter
  310. }
  311. }
  312. mOutput << "ENDSEC" << endstr; // end of data section
  313. mOutput << "END-ISO-10303-21" << endstr; // end of file
  314. }
  315. #endif
  316. #endif