Exporter.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2010, 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 Exporter.cpp
  35. Assimp export interface. While it's public interface bears many similarities
  36. to the import interface (in fact, it is largely symmetric), the internal
  37. implementations differs a lot. Exporters are considered stateless and are
  38. simple callbacks which we maintain in a global list along with their
  39. description strings.
  40. Here we implement only the C++ interface (Assimp::Exporter).
  41. */
  42. #include "AssimpPCH.h"
  43. #ifndef ASSIMP_BUILD_NO_EXPORT
  44. #include "DefaultIOSystem.h"
  45. #include "BlobIOSystem.h"
  46. namespace Assimp {
  47. // ------------------------------------------------------------------------------------------------
  48. // Exporter worker function prototypes. Should not be necessary to #ifndef them, it's just a prototype
  49. void ExportSceneCollada(const char*,IOSystem*, const aiScene*);
  50. void ExportScene3DS(const char*, IOSystem*, const aiScene*) {}
  51. /// Function pointer type of a Export worker function
  52. typedef void (*fpExportFunc)(const char*,IOSystem*,const aiScene*);
  53. // ------------------------------------------------------------------------------------------------
  54. /// Internal description of an Assimp export format option
  55. struct ExportFormatEntry
  56. {
  57. /// Public description structure to be returned by aiGetExportFormatDescription()
  58. aiExportFormatDesc mDescription;
  59. // Worker function to do the actual exporting
  60. fpExportFunc mExportFunction;
  61. // Constructor to fill all entries
  62. ExportFormatEntry( const char* pId, const char* pDesc, const char* pExtension, fpExportFunc pFunction)
  63. {
  64. mDescription.id = pId;
  65. mDescription.description = pDesc;
  66. mDescription.fileExtension = pExtension;
  67. mExportFunction = pFunction;
  68. }
  69. };
  70. // ------------------------------------------------------------------------------------------------
  71. // global array of all export formats which Assimp supports in its current build
  72. ExportFormatEntry gExporters[] =
  73. {
  74. #ifndef ASSIMP_BUILD_NO_COLLADA_EXPORTER
  75. ExportFormatEntry( "collada", "COLLADA - Digital Asset Exchange Schema", "dae", &ExportSceneCollada),
  76. #endif
  77. #ifndef ASSIMP_BUILD_NO_3DS_EXPORTER
  78. ExportFormatEntry( "3ds", "Autodesk 3DS (legacy format)", "3ds" , &ExportScene3DS),
  79. #endif
  80. };
  81. class ExporterPimpl {
  82. public:
  83. ExporterPimpl()
  84. : blob()
  85. , mIOSystem(new Assimp::DefaultIOSystem())
  86. , mIsDefaultIOHandler(true)
  87. {
  88. }
  89. ~ExporterPimpl()
  90. {
  91. delete blob;
  92. }
  93. public:
  94. aiExportDataBlob* blob;
  95. boost::shared_ptr< Assimp::IOSystem > mIOSystem;
  96. bool mIsDefaultIOHandler;
  97. };
  98. #define ASSIMP_NUM_EXPORTERS (sizeof(gExporters)/sizeof(gExporters[0]))
  99. } // end of namespace Assimp
  100. using namespace Assimp;
  101. // ------------------------------------------------------------------------------------------------
  102. Exporter :: Exporter()
  103. : pimpl(new ExporterPimpl())
  104. {
  105. }
  106. // ------------------------------------------------------------------------------------------------
  107. Exporter :: ~Exporter()
  108. {
  109. delete pimpl;
  110. }
  111. // ------------------------------------------------------------------------------------------------
  112. void Exporter :: SetIOHandler( IOSystem* pIOHandler)
  113. {
  114. pimpl->mIsDefaultIOHandler = !pIOHandler;
  115. pimpl->mIOSystem.reset(pIOHandler);
  116. }
  117. // ------------------------------------------------------------------------------------------------
  118. IOSystem* Exporter :: GetIOHandler() const
  119. {
  120. return pimpl->mIOSystem.get();
  121. }
  122. // ------------------------------------------------------------------------------------------------
  123. bool Exporter :: IsDefaultIOHandler() const
  124. {
  125. return pimpl->mIsDefaultIOHandler;
  126. }
  127. // ------------------------------------------------------------------------------------------------
  128. const aiExportDataBlob* Exporter :: ExportToBlob( const aiScene* pScene, const char* pFormatId )
  129. {
  130. if (pimpl->blob) {
  131. delete pimpl->blob;
  132. pimpl->blob = NULL;
  133. }
  134. boost::shared_ptr<IOSystem> old = pimpl->mIOSystem;
  135. BlobIOSystem* blobio = new BlobIOSystem();
  136. pimpl->mIOSystem = boost::shared_ptr<IOSystem>( blobio );
  137. if (AI_SUCCESS != Export(pScene,pFormatId,blobio->GetMagicFileName())) {
  138. pimpl->mIOSystem = old;
  139. return NULL;
  140. }
  141. pimpl->blob = blobio->GetBlobChain();
  142. pimpl->mIOSystem = old;
  143. return pimpl->blob;
  144. }
  145. // ------------------------------------------------------------------------------------------------
  146. aiReturn Exporter :: Export( const aiScene* pScene, const char* pFormatId, const char* pPath )
  147. {
  148. ASSIMP_BEGIN_EXCEPTION_REGION();
  149. for (size_t i = 0; i < ASSIMP_NUM_EXPORTERS; ++i) {
  150. if (!strcmp(gExporters[i].mDescription.id,pFormatId)) {
  151. try {
  152. gExporters[i].mExportFunction(pPath,pimpl->mIOSystem.get(),pScene);
  153. }
  154. catch (DeadlyExportError& err) {
  155. // XXX what to do with the error message? Maybe introduce extra member to hold it, similar to Assimp.Importer
  156. DefaultLogger::get()->error(err.what());
  157. return AI_FAILURE;
  158. }
  159. return AI_SUCCESS;
  160. }
  161. }
  162. ASSIMP_END_EXCEPTION_REGION(aiReturn);
  163. return AI_FAILURE;
  164. }
  165. // ------------------------------------------------------------------------------------------------
  166. const aiExportDataBlob* Exporter :: GetBlob() const
  167. {
  168. return pimpl->blob;
  169. }
  170. // ------------------------------------------------------------------------------------------------
  171. const aiExportDataBlob* Exporter :: GetOrphanedBlob() const
  172. {
  173. const aiExportDataBlob* tmp = pimpl->blob;
  174. pimpl->blob = NULL;
  175. return tmp;
  176. }
  177. // ------------------------------------------------------------------------------------------------
  178. size_t Exporter :: GetExportFormatCount() const
  179. {
  180. return ASSIMP_NUM_EXPORTERS;
  181. }
  182. // ------------------------------------------------------------------------------------------------
  183. const aiExportFormatDesc* Exporter :: GetExportFormatDescription( size_t pIndex ) const
  184. {
  185. if (pIndex >= ASSIMP_NUM_EXPORTERS) {
  186. return NULL;
  187. }
  188. return &gExporters[pIndex].mDescription;
  189. }
  190. #endif // !ASSIMP_BUILD_NO_EXPORT