StepExporter.cpp 17 KB

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