D3MFExporter.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, 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_3MF_EXPORTER
  35. #include "D3MFExporter.h"
  36. #include <assimp/Exceptional.h>
  37. #include <assimp/StringUtils.h>
  38. #include <assimp/scene.h>
  39. #include <assimp/DefaultLogger.hpp>
  40. #include <assimp/Exporter.hpp>
  41. #include <assimp/IOStream.hpp>
  42. #include <assimp/IOSystem.hpp>
  43. #include "3MFXmlTags.h"
  44. #include "D3MFOpcPackage.h"
  45. #ifdef ASSIMP_USE_HUNTER
  46. #include <zip/zip.h>
  47. #else
  48. #include <contrib/zip/src/zip.h>
  49. #endif
  50. namespace Assimp {
  51. void ExportScene3MF(const char *pFile, IOSystem *pIOSystem, const aiScene *pScene, const ExportProperties * /*pProperties*/) {
  52. if (nullptr == pIOSystem) {
  53. throw DeadlyExportError("Could not export 3MP archive: " + std::string(pFile));
  54. }
  55. D3MF::D3MFExporter myExporter(pFile, pScene);
  56. if (myExporter.validate()) {
  57. if (pIOSystem->Exists(pFile)) {
  58. if (!pIOSystem->DeleteFile(pFile)) {
  59. throw DeadlyExportError("File exists, cannot override : " + std::string(pFile));
  60. }
  61. }
  62. bool ok = myExporter.exportArchive(pFile);
  63. if (!ok) {
  64. throw DeadlyExportError("Could not export 3MP archive: " + std::string(pFile));
  65. }
  66. }
  67. }
  68. namespace D3MF {
  69. D3MFExporter::D3MFExporter(const char *pFile, const aiScene *pScene) :
  70. mArchiveName(pFile), m_zipArchive(nullptr), mScene(pScene) {
  71. // empty
  72. }
  73. D3MFExporter::~D3MFExporter() {
  74. for (size_t i = 0; i < mRelations.size(); ++i) {
  75. delete mRelations[i];
  76. }
  77. mRelations.clear();
  78. }
  79. bool D3MFExporter::validate() {
  80. if (mArchiveName.empty()) {
  81. return false;
  82. }
  83. if (nullptr == mScene) {
  84. return false;
  85. }
  86. return true;
  87. }
  88. bool D3MFExporter::exportArchive(const char *file) {
  89. bool ok(true);
  90. m_zipArchive = zip_open(file, ZIP_DEFAULT_COMPRESSION_LEVEL, 'w');
  91. if (nullptr == m_zipArchive) {
  92. return false;
  93. }
  94. ok |= exportContentTypes();
  95. ok |= export3DModel();
  96. ok |= exportRelations();
  97. zip_close(m_zipArchive);
  98. m_zipArchive = nullptr;
  99. return ok;
  100. }
  101. bool D3MFExporter::exportContentTypes() {
  102. mContentOutput.clear();
  103. mContentOutput << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
  104. mContentOutput << std::endl;
  105. mContentOutput << "<Types xmlns = \"http://schemas.openxmlformats.org/package/2006/content-types\">";
  106. mContentOutput << std::endl;
  107. mContentOutput << "<Default Extension = \"rels\" ContentType = \"application/vnd.openxmlformats-package.relationships+xml\" />";
  108. mContentOutput << std::endl;
  109. mContentOutput << "<Default Extension = \"model\" ContentType = \"application/vnd.ms-package.3dmanufacturing-3dmodel+xml\" />";
  110. mContentOutput << std::endl;
  111. mContentOutput << "</Types>";
  112. mContentOutput << std::endl;
  113. zipContentType(XmlTag::CONTENT_TYPES_ARCHIVE);
  114. return true;
  115. }
  116. bool D3MFExporter::exportRelations() {
  117. mRelOutput.clear();
  118. mRelOutput << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
  119. mRelOutput << std::endl;
  120. mRelOutput << "<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">";
  121. for (size_t i = 0; i < mRelations.size(); ++i) {
  122. if (mRelations[i]->target[0] == '/') {
  123. mRelOutput << "<Relationship Target=\"" << mRelations[i]->target << "\" ";
  124. } else {
  125. mRelOutput << "<Relationship Target=\"/" << mRelations[i]->target << "\" ";
  126. }
  127. mRelOutput << "Id=\"" << mRelations[i]->id << "\" ";
  128. mRelOutput << "Type=\"" << mRelations[i]->type << "\" />";
  129. mRelOutput << std::endl;
  130. }
  131. mRelOutput << "</Relationships>";
  132. mRelOutput << std::endl;
  133. zipRelInfo("_rels", ".rels");
  134. mRelOutput.flush();
  135. return true;
  136. }
  137. bool D3MFExporter::export3DModel() {
  138. mModelOutput.clear();
  139. writeHeader();
  140. mModelOutput << "<" << XmlTag::model << " " << XmlTag::model_unit << "=\"millimeter\""
  141. << " xmlns=\"http://schemas.microsoft.com/3dmanufacturing/core/2015/02\">"
  142. << std::endl;
  143. mModelOutput << "<" << XmlTag::resources << ">";
  144. mModelOutput << std::endl;
  145. writeMetaData();
  146. writeBaseMaterials();
  147. writeObjects();
  148. mModelOutput << "</" << XmlTag::resources << ">";
  149. mModelOutput << std::endl;
  150. writeBuild();
  151. mModelOutput << "</" << XmlTag::model << ">\n";
  152. OpcPackageRelationship *info = new OpcPackageRelationship;
  153. info->id = "rel0";
  154. info->target = "/3D/3DModel.model";
  155. info->type = XmlTag::PACKAGE_START_PART_RELATIONSHIP_TYPE;
  156. mRelations.push_back(info);
  157. zipModel("3D", "3DModel.model");
  158. mModelOutput.flush();
  159. return true;
  160. }
  161. void D3MFExporter::writeHeader() {
  162. mModelOutput << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
  163. mModelOutput << std::endl;
  164. }
  165. void D3MFExporter::writeMetaData() {
  166. if (nullptr == mScene->mMetaData) {
  167. return;
  168. }
  169. const unsigned int numMetaEntries(mScene->mMetaData->mNumProperties);
  170. if (0 == numMetaEntries) {
  171. return;
  172. }
  173. const aiString *key = nullptr;
  174. const aiMetadataEntry *entry(nullptr);
  175. for (size_t i = 0; i < numMetaEntries; ++i) {
  176. mScene->mMetaData->Get(i, key, entry);
  177. std::string k(key->C_Str());
  178. aiString value;
  179. mScene->mMetaData->Get(k, value);
  180. mModelOutput << "<" << XmlTag::meta << " " << XmlTag::meta_name << "=\"" << key->C_Str() << "\">";
  181. mModelOutput << value.C_Str();
  182. mModelOutput << "</" << XmlTag::meta << ">" << std::endl;
  183. }
  184. }
  185. void D3MFExporter::writeBaseMaterials() {
  186. mModelOutput << "<basematerials id=\"1\">\n";
  187. std::string strName, hexDiffuseColor, tmp;
  188. for (size_t i = 0; i < mScene->mNumMaterials; ++i) {
  189. aiMaterial *mat = mScene->mMaterials[i];
  190. aiString name;
  191. if (mat->Get(AI_MATKEY_NAME, name) != aiReturn_SUCCESS) {
  192. strName = "basemat_" + ai_to_string(i);
  193. } else {
  194. strName = name.C_Str();
  195. }
  196. aiColor4D color;
  197. if (mat->Get(AI_MATKEY_COLOR_DIFFUSE, color) == aiReturn_SUCCESS) {
  198. hexDiffuseColor.clear();
  199. tmp.clear();
  200. // rgbs %
  201. if (color.r <= 1 && color.g <= 1 && color.b <= 1 && color.a <= 1) {
  202. hexDiffuseColor = ai_rgba2hex(
  203. (int)(((ai_real)color.r) * 255),
  204. (int)(((ai_real)color.g) * 255),
  205. (int)(((ai_real)color.b) * 255),
  206. (int)(((ai_real)color.a) * 255),
  207. true);
  208. } else {
  209. hexDiffuseColor = "#";
  210. tmp = ai_decimal_to_hexa((ai_real)color.r);
  211. hexDiffuseColor += tmp;
  212. tmp = ai_decimal_to_hexa((ai_real)color.g);
  213. hexDiffuseColor += tmp;
  214. tmp = ai_decimal_to_hexa((ai_real)color.b);
  215. hexDiffuseColor += tmp;
  216. tmp = ai_decimal_to_hexa((ai_real)color.a);
  217. hexDiffuseColor += tmp;
  218. }
  219. } else {
  220. hexDiffuseColor = "#FFFFFFFF";
  221. }
  222. mModelOutput << "<base name=\"" + strName + "\" " + " displaycolor=\"" + hexDiffuseColor + "\" />\n";
  223. }
  224. mModelOutput << "</basematerials>\n";
  225. }
  226. void D3MFExporter::writeObjects() {
  227. if (nullptr == mScene->mRootNode) {
  228. return;
  229. }
  230. aiNode *root = mScene->mRootNode;
  231. for (unsigned int i = 0; i < root->mNumChildren; ++i) {
  232. aiNode *currentNode(root->mChildren[i]);
  233. if (nullptr == currentNode) {
  234. continue;
  235. }
  236. mModelOutput << "<" << XmlTag::object << " id=\"" << i + 2 << "\" type=\"model\">";
  237. mModelOutput << std::endl;
  238. for (unsigned int j = 0; j < currentNode->mNumMeshes; ++j) {
  239. aiMesh *currentMesh = mScene->mMeshes[currentNode->mMeshes[j]];
  240. if (nullptr == currentMesh) {
  241. continue;
  242. }
  243. writeMesh(currentMesh);
  244. }
  245. mBuildItems.push_back(i);
  246. mModelOutput << "</" << XmlTag::object << ">";
  247. mModelOutput << std::endl;
  248. }
  249. }
  250. void D3MFExporter::writeMesh(aiMesh *mesh) {
  251. if (nullptr == mesh) {
  252. return;
  253. }
  254. mModelOutput << "<"
  255. << XmlTag::mesh
  256. << ">" << "\n";
  257. mModelOutput << "<"
  258. << XmlTag::vertices
  259. << ">" << "\n";
  260. for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
  261. writeVertex(mesh->mVertices[i]);
  262. }
  263. mModelOutput << "</"
  264. << XmlTag::vertices << ">"
  265. << "\n";
  266. const unsigned int matIdx(mesh->mMaterialIndex);
  267. writeFaces(mesh, matIdx);
  268. mModelOutput << "</"
  269. << XmlTag::mesh << ">"
  270. << "\n";
  271. }
  272. void D3MFExporter::writeVertex(const aiVector3D &pos) {
  273. mModelOutput << "<" << XmlTag::vertex << " x=\"" << pos.x << "\" y=\"" << pos.y << "\" z=\"" << pos.z << "\" />";
  274. mModelOutput << std::endl;
  275. }
  276. void D3MFExporter::writeFaces(aiMesh *mesh, unsigned int matIdx) {
  277. if (nullptr == mesh) {
  278. return;
  279. }
  280. if (!mesh->HasFaces()) {
  281. return;
  282. }
  283. mModelOutput << "<"
  284. << XmlTag::triangles << ">"
  285. << "\n";
  286. for (unsigned int i = 0; i < mesh->mNumFaces; ++i) {
  287. aiFace &currentFace = mesh->mFaces[i];
  288. mModelOutput << "<" << XmlTag::triangle << " v1=\"" << currentFace.mIndices[0] << "\" v2=\""
  289. << currentFace.mIndices[1] << "\" v3=\"" << currentFace.mIndices[2]
  290. << "\" pid=\"1\" p1=\"" + ai_to_string(matIdx) + "\" />";
  291. mModelOutput << "\n";
  292. }
  293. mModelOutput << "</"
  294. << XmlTag::triangles
  295. << ">";
  296. mModelOutput << "\n";
  297. }
  298. void D3MFExporter::writeBuild() {
  299. mModelOutput << "<"
  300. << XmlTag::build
  301. << ">"
  302. << "\n";
  303. for (size_t i = 0; i < mBuildItems.size(); ++i) {
  304. mModelOutput << "<" << XmlTag::item << " objectid=\"" << i + 2 << "\"/>";
  305. mModelOutput << "\n";
  306. }
  307. mModelOutput << "</" << XmlTag::build << ">";
  308. mModelOutput << "\n";
  309. }
  310. void D3MFExporter::zipContentType(const std::string &filename) {
  311. addFileInZip(filename, mContentOutput.str());
  312. }
  313. void D3MFExporter::zipModel(const std::string &folder, const std::string &modelName) {
  314. const std::string entry = folder + "/" + modelName;
  315. addFileInZip(entry, mModelOutput.str());
  316. }
  317. void D3MFExporter::zipRelInfo(const std::string &folder, const std::string &relName) {
  318. const std::string entry = folder + "/" + relName;
  319. addFileInZip(entry, mRelOutput.str());
  320. }
  321. void D3MFExporter::addFileInZip(const std::string& entry, const std::string& content) {
  322. if (nullptr == m_zipArchive) {
  323. throw DeadlyExportError("3MF-Export: Zip archive not valid, nullptr.");
  324. }
  325. zip_entry_open(m_zipArchive, entry.c_str());
  326. zip_entry_write(m_zipArchive, content.c_str(), content.size());
  327. zip_entry_close(m_zipArchive);
  328. }
  329. } // Namespace D3MF
  330. } // Namespace Assimp
  331. #endif // ASSIMP_BUILD_NO_3MF_EXPORTER
  332. #endif // ASSIMP_BUILD_NO_EXPORT