Mesh: [skip ci] support of asymptote export

This commit is contained in:
wmayer
2020-08-27 09:51:35 +02:00
parent 6ca3a56c47
commit 7ae2d7e495
4 changed files with 84 additions and 1 deletions

View File

@@ -1774,6 +1774,7 @@ std::vector<std::string> MeshOutput::supportedMeshFormats()
fmt.emplace_back("wrl");
fmt.emplace_back("wrz");
fmt.emplace_back("amf");
fmt.emplace_back("asy");
return fmt;
}
@@ -1828,6 +1829,9 @@ MeshIO::Format MeshOutput::GetFormat(const char* FileName)
else if (file.hasExtension("smf")) {
return MeshIO::SMF;
}
else if (file.hasExtension("asy")) {
return MeshIO::ASY;
}
else {
return MeshIO::Undefined;
}
@@ -1946,6 +1950,11 @@ bool MeshOutput::SaveAny(const char* FileName, MeshIO::Format format) const
if (!SaveNastran(str))
throw Base::FileException("Export of NASTRAN mesh failed",FileName);
}
else if (fileformat == MeshIO::ASY) {
// write file
if (!SaveAsymptote(str))
throw Base::FileException("Export of ASY mesh failed",FileName);
}
else {
throw Base::FileException("File format not supported", FileName);
}
@@ -1990,6 +1999,8 @@ bool MeshOutput::SaveFormat(std::ostream &str, MeshIO::Format fmt) const
return SaveAsciiPLY(str);
case MeshIO::PY:
return SavePython(str);
case MeshIO::ASY:
return SaveAsymptote(str);
default:
throw Base::FileException("Unsupported file format");
}
@@ -2361,6 +2372,72 @@ bool MeshOutput::SaveSMF (std::ostream &out) const
return true;
}
/** Saves an Asymptote file. */
bool MeshOutput::SaveAsymptote(std::ostream &out) const
{
out << "/*\n"
" * Created by FreeCAD <http://www.freecadweb.org>\n"
" */\n\n";
out << "import three;\n\n";
out << "size(500);\n\n";
Base::BoundBox3f bbox = _rclMesh.GetBoundBox();
Base::Vector3f camera(bbox.GetCenter());
camera.x += bbox.LengthX();
Base::Vector3f target(bbox.GetCenter());
Base::Vector3f upvec(0.0f, 0.0f, 1.0f);
out << "// CA:Camera, OB:Camera\n"
<< "currentprojection = perspective(camera = (" << camera.x << ", "
<< camera.y << ", "
<< camera.z << "),\n"
<< " target = (" << target.x << ", "
<< target.y << ", "
<< target.z << "),\n"
" autoadjust = false,\n"
" showtarget = false,\n"
" up = (" << upvec.x << ", "
<< upvec.y << ", "
<< upvec.z << "));\n\n";
out << "// LA:Spot, OB:Lamp\n"
<< "// WO:World\n"
<< "currentlight = light(diffuse = rgb(1, 1, 1),\n"
" specular = rgb(1, 1, 1),\n"
" background = rgb(0.078281, 0.16041, 0.25),\n"
" 0.56639, 0.21839, 0.79467);\n\n";
out << "// ME:Mesh, OB:Mesh\n";
MeshFacetIterator clIter(_rclMesh), clEnd(_rclMesh);
clIter.Transform(this->_transform);
clIter.Begin();
clEnd.End();
const MeshGeomFacet *pclFacet;
while (clIter < clEnd) {
pclFacet = &(*clIter);
out << "draw(surface(";
// vertices
for (int i = 0; i < 3; i++) {
out << '(' << pclFacet->_aclPoints[i].x << ", "
<< pclFacet->_aclPoints[i].y << ", "
<< pclFacet->_aclPoints[i].z << ")--";
}
out << "cycle),\n";
out << " rgb(0.8, 0.8, 0.8));\n";
++clIter;
}
return true;
}
/** Saves an OFF file. */
bool MeshOutput::SaveOFF (std::ostream &out) const
{

View File

@@ -57,7 +57,8 @@ namespace MeshIO {
APLY,
PY,
AMF,
SMF
SMF,
ASY
};
enum Binding {
OVERALL,
@@ -186,6 +187,8 @@ public:
bool SaveBinaryPLY (std::ostream &rstrOut) const;
/** Saves the mesh object into an ASCII PLY file. */
bool SaveAsciiPLY (std::ostream &rstrOut) const;
/** Saves the mesh object into an asymptote file. */
bool SaveAsymptote (std::ostream &rstrOut) const;
/** Saves the mesh object into an XML file. */
void SaveXML (Base::Writer &writer) const;
/** Saves a node to an OpenInventor file. */

View File

@@ -177,6 +177,7 @@ PyObject* MeshPy::read(PyObject *args, PyObject *kwds)
ext["PLY" ] = MeshCore::MeshIO::PLY;
ext["APLY"] = MeshCore::MeshIO::APLY;
ext["PY" ] = MeshCore::MeshIO::PY;
ext["ASY" ] = MeshCore::MeshIO::ASY;
PyObject* input;
char* Ext;
@@ -228,6 +229,7 @@ PyObject* MeshPy::write(PyObject *args, PyObject *kwds)
ext["PLY" ] = MeshCore::MeshIO::PLY;
ext["APLY"] = MeshCore::MeshIO::APLY;
ext["PY" ] = MeshCore::MeshIO::PY;
ext["ASY" ] = MeshCore::MeshIO::ASY;
static char* keywords_path[] = {"Filename","Format","Name","Material",NULL};
if (PyArg_ParseTupleAndKeywords(args, kwds, "et|ssO", keywords_path, "utf-8",

View File

@@ -521,6 +521,7 @@ void CmdMeshExport::activated(int)
ext << qMakePair<QString, QByteArray>(QString::fromLatin1("%1 (*.wrz)").arg(QObject::tr("Compressed VRML 2.0")), "WRZ");
ext << qMakePair<QString, QByteArray>(QString::fromLatin1("%1 (*.nas *.bdf)").arg(QObject::tr("Nastran")), "NAS");
ext << qMakePair<QString, QByteArray>(QString::fromLatin1("%1 (*.py)").arg(QObject::tr("Python module def")), "PY");
ext << qMakePair<QString, QByteArray>(QString::fromLatin1("%1 (*.asy)").arg(QObject::tr("Asymptote Format")), "ASY");
ext << qMakePair<QString, QByteArray>(QString::fromLatin1("%1 (*.*)").arg(QObject::tr("All Files")), ""); // Undefined
QStringList filter;
for (QList<QPair<QString, QByteArray> >::iterator it = ext.begin(); it != ext.end(); ++it)