#include <stdio.h>
#include <vector>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <assert.h>
#include <QtCore/QFile>
#if defined(JAMBUILD)
#include <Mudbox/mudbox.h>
#else
#include "../../include/Mudbox/mudbox.h"
#endif
#include "utility.hpp"
namespace mudbox
{
MB_PLUGIN( "STL Exporter", "STL file export plugin", "Autodesk", "http://www.mudbox3d.com", 0 );
class STLExporter :
public BaseExporter,
public Singleton<STLExporter>
{
public:
DECLARE_CLASS;
STLExporter() {
}
virtual QString Extension() const { return "stl"; };
virtual QString Description() const { return "Stereolithography File"; };
virtual QVector<FileExtension> SupportedExtensions( void ) const
{
QVector<FileExtension> ret;
ret.push_back(FileExtension("stl", "Stereolithography File"));
return ret;
}
void Export( const QString &sFileName, const QString &sFormat = QString() )
{
if (GetNumMeshes() == 0)
MB_ERROR("There are no meshes to export");
if (GetNumMeshes() > 1)
MB_ERROR("Multiple meshes can not be exported to this format");
const Mesh* m = GetMesh(0);
if (IsMeshOpen(m))
MB_ERROR("Only continuous meshes can be exported");
QFile ofile(sFileName);
ofile.open(QFile::WriteOnly);
const uint STL_HEADER_SIZE = 80;
char header[STL_HEADER_SIZE] = "";
std::fill(header, header + STL_HEADER_SIZE, 0);
ofile.write(header, STL_HEADER_SIZE);
uint32 cnt = m->FaceCount();
if ( m->Type() == Mesh::typeQuadric )
cnt *= 2;
WriteUInt32(ofile, cnt);
if ( m->Type() == Mesh::typeTriangular )
{
for ( uint i = 0; i < m->FaceCount(); ++i ) {
const Vector& normal = m->FaceNormal(i);
WriteVectorFloat32(ofile, normal);
for (uint j = 0; j < 3; ++j) {
const Vector& v = m->TriangleVertexPosition( i, j );
WriteVectorFloat32(ofile, v);
}
WriteUInt16(ofile, 0);
}
}
else if ( m->Type() == Mesh::typeQuadric )
{
for ( uint i = 0; i < m->FaceCount(); ++i ) {
const Vector& normal = m->FaceNormal(i);
WriteVectorFloat32(ofile, normal);
for (uint j = 0; j < 4; ++j) {
if (j != 1) {
const Vector& v = m->QuadVertexPosition( i, j );
WriteVectorFloat32(ofile, v);
}
}
WriteUInt16(ofile, 0);
WriteVectorFloat32(ofile, normal);
for (uint j = 0; j < 4; ++j) {
if (j != 3) {
const Vector& v = m->QuadVertexPosition( i, j );
WriteVectorFloat32(ofile, v);
}
}
WriteUInt16(ofile, 0);
}
}
ofile.close();
};
};
IMPLEMENT_CLASS( STLExporter, Exporter, "STL Exporter" );
}