Skip to content

Commit

Permalink
+ Support of export to X3D for meshes
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Nov 25, 2013
1 parent cbbf3ee commit b7a9b77
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/Mod/Mesh/App/Core/MeshIO.cpp
Expand Up @@ -36,6 +36,7 @@
#include <Base/FileInfo.h>
#include <Base/Sequencer.h>
#include <Base/Stream.h>
#include <Base/Placement.h>
#include <zipios++/gzipoutputstream.h>

#include <math.h>
Expand Down Expand Up @@ -1431,6 +1432,9 @@ bool MeshOutput::SaveAny(const char* FileName, MeshIO::Format format) const
else if (fi.hasExtension("iv")) {
fileformat = MeshIO::IV;
}
else if (fi.hasExtension("x3d")) {
fileformat = MeshIO::X3D;
}
else if (fi.hasExtension("py")) {
fileformat = MeshIO::PY;
}
Expand Down Expand Up @@ -1497,6 +1501,11 @@ bool MeshOutput::SaveAny(const char* FileName, MeshIO::Format format) const
if (!SaveInventor(str))
throw Base::FileException("Export of Inventor mesh failed",FileName);
}
else if (fileformat == MeshIO::X3D) {
// write file
if (!SaveX3D(str))
throw Base::FileException("Export of X3D failed",FileName);
}
else if (fileformat == MeshIO::PY) {
// write file
if (!SavePython(str))
Expand Down Expand Up @@ -2039,6 +2048,76 @@ bool MeshOutput::SaveInventor (std::ostream &rstrOut) const
return true;
}

/** Writes an X3D file. */
bool MeshOutput::SaveX3D (std::ostream &out) const
{
if ((!out) || (out.bad() == true) || (_rclMesh.CountFacets() == 0))
return false;

const MeshPointArray& pts = _rclMesh.GetPoints();
const MeshFacetArray& fts = _rclMesh.GetFacets();

Base::SequencerLauncher seq("Saving...", _rclMesh.CountFacets() + 1);
out.precision(6);
out.setf(std::ios::fixed | std::ios::showpoint);

// Header info
out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
out << "<X3D profile=\"Immersive\" version=\"3.2\" xmlns:xsd="
<< "\"http://www.w3.org/2001/XMLSchema-instance\" xsd:noNamespaceSchemaLocation="
<< "\"http://www.web3d.org/specifications/x3d-3.2.xsd\">" << std::endl;
out << " <head>" << std::endl
<< " <meta name=\"generator\" content=\"FreeCAD\"/>" << std::endl
<< " <meta name=\"author\" content=\"\"/> " << std::endl
<< " <meta name=\"company\" content=\"\"/>" << std::endl
<< " </head>" << std::endl;

// Beginning
out << " <Scene>" << std::endl;
if (apply_transform) {
Base::Placement p(_transform);
const Base::Vector3d& v = p.getPosition();
const Base::Rotation& r = p.getRotation();
Base::Vector3d axis; double angle;
r.getValue(axis, angle);
out << " <Transform "
<< "translation='"
<< v.x << " "
<< v.y << " "
<< v.z << "' "
<< "rotation='"
<< axis.x << " "
<< axis.y << " "
<< axis.z << " "
<< angle << "'>" << std::endl;
}
else {
out << " <Transform>" << std::endl;
}
out << " <Shape>" << std::endl;

out << " <IndexedFaceSet solid=\"false\" coordIndex=\"";
for (MeshFacetArray::_TConstIterator it = fts.begin(); it != fts.end(); ++it) {
out << it->_aulPoints[0] << " " << it->_aulPoints[1] << " " << it->_aulPoints[2] << " -1 ";
}
out << "\">" << std::endl;

out << " <Coordinate point=\"";
for (MeshPointArray::_TConstIterator it = pts.begin(); it != pts.end(); ++it) {
out << it->x << " " << it->y << " " << it->z << ", ";
}
out << "\"/>" << std::endl;

// End
out << " </IndexedFaceSet>" << std::endl
<< " </Shape>" << std::endl
<< " </Transform>" << std::endl
<< " </Scene>" << std::endl
<< "</X3D>" << std::endl;

return true;
}

/** Writes a Nastran file. */
bool MeshOutput::SaveNastran (std::ostream &rstrOut) const
{
Expand Down
3 changes: 3 additions & 0 deletions src/Mod/Mesh/App/Core/MeshIO.h
Expand Up @@ -47,6 +47,7 @@ namespace MeshIO {
OBJ,
OFF,
IV,
X3D,
VRML,
WRZ,
NAS,
Expand Down Expand Up @@ -153,6 +154,8 @@ class MeshExport MeshOutput
bool SaveMeshNode (std::ostream &rstrIn);
/** Writes an OpenInventor file. */
bool SaveInventor (std::ostream &rstrOut) const;
/** Writes an X3D file. */
bool SaveX3D (std::ostream &rstrOut) const;
/** Writes a VRML file. */
bool SaveVRML (std::ostream &rstrOut, const App::Material &rclMat) const;
/** Writes a Nastran file. */
Expand Down
1 change: 1 addition & 0 deletions src/Mod/Mesh/App/MeshPyImp.cpp
Expand Up @@ -161,6 +161,7 @@ PyObject* MeshPy::write(PyObject *args)
ext["OBJ" ] = MeshCore::MeshIO::OBJ;
ext["OFF" ] = MeshCore::MeshIO::OFF;
ext["IV" ] = MeshCore::MeshIO::IV;
ext["X3D" ] = MeshCore::MeshIO::X3D;
ext["VRML"] = MeshCore::MeshIO::VRML;
ext["WRL" ] = MeshCore::MeshIO::VRML;
ext["WRZ" ] = MeshCore::MeshIO::WRZ;
Expand Down
1 change: 1 addition & 0 deletions src/Mod/Mesh/Gui/Command.cpp
Expand Up @@ -356,6 +356,7 @@ void CmdMeshExport::activated(int iMsg)
ext << qMakePair<QString, QByteArray>(QObject::tr("Alias Mesh (*.obj)"), "OBJ");
ext << qMakePair<QString, QByteArray>(QObject::tr("Object File Format (*.off)"), "OFF");
ext << qMakePair<QString, QByteArray>(QObject::tr("Inventor V2.1 ascii (*.iv)"), "IV");
ext << qMakePair<QString, QByteArray>(QObject::tr("X3D Extensible 3D(*.x3d)"), "X3D");
ext << qMakePair<QString, QByteArray>(QObject::tr("Standford Polygon (*.ply)"), "PLY");
ext << qMakePair<QString, QByteArray>(QObject::tr("VRML V2.0 (*.wrl *.vrml)"), "VRML");
ext << qMakePair<QString, QByteArray>(QObject::tr("Compressed VRML 2.0 (*.wrz)"), "WRZ");
Expand Down

0 comments on commit b7a9b77

Please sign in to comment.