nuclear@0: /* nuclear@0: Open Asset Import Library (assimp) nuclear@0: ---------------------------------------------------------------------- nuclear@0: nuclear@0: Copyright (c) 2006-2012, assimp team nuclear@0: All rights reserved. nuclear@0: nuclear@0: Redistribution and use of this software in source and binary forms, nuclear@0: with or without modification, are permitted provided that the nuclear@0: following conditions are met: nuclear@0: nuclear@0: * Redistributions of source code must retain the above nuclear@0: copyright notice, this list of conditions and the nuclear@0: following disclaimer. nuclear@0: nuclear@0: * Redistributions in binary form must reproduce the above nuclear@0: copyright notice, this list of conditions and the nuclear@0: following disclaimer in the documentation and/or other nuclear@0: materials provided with the distribution. nuclear@0: nuclear@0: * Neither the name of the assimp team, nor the names of its nuclear@0: contributors may be used to endorse or promote products nuclear@0: derived from this software without specific prior nuclear@0: written permission of the assimp team. nuclear@0: nuclear@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS nuclear@0: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT nuclear@0: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR nuclear@0: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT nuclear@0: OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, nuclear@0: SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT nuclear@0: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, nuclear@0: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY nuclear@0: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT nuclear@0: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE nuclear@0: OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. nuclear@0: nuclear@0: ---------------------------------------------------------------------- nuclear@0: */ nuclear@0: nuclear@0: #include "AssimpPCH.h" nuclear@0: nuclear@0: #if !defined(ASSIMP_BUILD_NO_EXPORT) && !defined(ASSIMP_BUILD_NO_STL_EXPORTER) nuclear@0: nuclear@0: #include "STLExporter.h" nuclear@0: #include "assimp/version.h" nuclear@0: nuclear@0: using namespace Assimp; nuclear@0: namespace Assimp { nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Worker function for exporting a scene to Stereolithograpy. Prototyped and registered in Exporter.cpp nuclear@0: void ExportSceneSTL(const char* pFile,IOSystem* pIOSystem, const aiScene* pScene) nuclear@0: { nuclear@0: // invoke the exporter nuclear@0: STLExporter exporter(pFile, pScene); nuclear@0: nuclear@0: // we're still here - export successfully completed. Write the file. nuclear@0: boost::scoped_ptr outfile (pIOSystem->Open(pFile,"wt")); nuclear@0: if(outfile == NULL) { nuclear@0: throw DeadlyExportError("could not open output .stl file: " + std::string(pFile)); nuclear@0: } nuclear@0: nuclear@0: outfile->Write( exporter.mOutput.str().c_str(), static_cast(exporter.mOutput.tellp()),1); nuclear@0: } nuclear@0: nuclear@0: } // end of namespace Assimp nuclear@0: nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: STLExporter :: STLExporter(const char* _filename, const aiScene* pScene) nuclear@0: : filename(_filename) nuclear@0: , pScene(pScene) nuclear@0: , endl("\n") nuclear@0: { nuclear@0: // make sure that all formatting happens using the standard, C locale and not the user's current locale nuclear@0: const std::locale& l = std::locale("C"); nuclear@0: mOutput.imbue(l); nuclear@0: nuclear@0: const std::string& name = "AssimpScene"; nuclear@0: nuclear@0: mOutput << "solid " << name << endl; nuclear@0: for(unsigned int i = 0; i < pScene->mNumMeshes; ++i) { nuclear@0: WriteMesh(pScene->mMeshes[i]); nuclear@0: } nuclear@0: mOutput << "endsolid " << name << endl; nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: void STLExporter :: WriteMesh(const aiMesh* m) nuclear@0: { nuclear@0: for (unsigned int i = 0; i < m->mNumFaces; ++i) { nuclear@0: const aiFace& f = m->mFaces[i]; nuclear@0: nuclear@0: // we need per-face normals. We specified aiProcess_GenNormals as pre-requisite for this exporter, nuclear@0: // but nonetheless we have to expect per-vertex normals. nuclear@0: aiVector3D nor; nuclear@0: if (m->mNormals) { nuclear@0: for(unsigned int a = 0; a < f.mNumIndices; ++a) { nuclear@0: nor += m->mNormals[f.mIndices[a]]; nuclear@0: } nuclear@0: nor.Normalize(); nuclear@0: } nuclear@0: mOutput << " facet normal " << nor.x << " " << nor.y << " " << nor.z << endl; nuclear@0: mOutput << " outer loop" << endl; nuclear@0: for(unsigned int a = 0; a < f.mNumIndices; ++a) { nuclear@0: const aiVector3D& v = m->mVertices[f.mIndices[a]]; nuclear@0: mOutput << " vertex " << v.x << " " << v.y << " " << v.z << endl; nuclear@0: } nuclear@0: nuclear@0: mOutput << " endloop" << endl; nuclear@0: mOutput << " endfacet" << endl << endl; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: #endif