Gui: export scene graph to file or buffer

This commit is contained in:
wmayer
2020-06-29 11:54:38 +02:00
parent b457adc0ae
commit 80ad2edfe4
3 changed files with 34 additions and 25 deletions

View File

@@ -231,39 +231,40 @@ FreeCADGui_subgraphFromObject(PyObject * /*self*/, PyObject *args)
}
static PyObject *
FreeCADGui_replaceSwitchNodes(PyObject * /*self*/, PyObject *args)
FreeCADGui_exportSubgraph(PyObject * /*self*/, PyObject *args)
{
const char* format = "VRML";
PyObject* proxy;
if (!PyArg_ParseTuple(args, "O", &proxy))
PyObject* output;
if (!PyArg_ParseTuple(args, "OO|s", &proxy, &output, &format))
return nullptr;
void* ptr = 0;
try {
Base::Interpreter().convertSWIGPointerObj("pivy.coin", "SoNode *", proxy, &ptr, 0);
SoNode* node = reinterpret_cast<SoNode*>(ptr);
SoNode* replace = SoFCDB::replaceSwitches(node);
if (replace) {
replace->ref();
if (node) {
std::string formatStr(format);
std::string buffer;
std::string prefix = "So";
std::string type = replace->getTypeId().getName().getString();
// doesn't start with the prefix 'So'
if (type.rfind("So", 0) != 0) {
type = prefix + type;
if (formatStr == "VRML") {
SoFCDB::writeToVRML(node, buffer);
}
else if (type == "SoFCSelectionRoot") {
type = "SoSeparator";
else if (formatStr == "IV") {
buffer = SoFCDB::writeNodesToString(node);
}
else {
throw Base::ValueError("Unsupported format");
}
type += " *";
PyObject* proxy = 0;
proxy = Base::Interpreter().createSWIGPointerObj("pivy.coin", type.c_str(), (void*)replace, 1);
return Py::new_reference_to(Py::Object(proxy, true));
}
else {
Py_INCREF(Py_None);
return Py_None;
Base::PyStreambuf buf(output);
std::ostream str(0);
str.rdbuf(&buf);
str << buffer;
}
Py_INCREF(Py_None);
return Py_None;
}
catch (const Base::Exception& e) {
PyErr_SetString(PyExc_RuntimeError, e.what());
@@ -334,9 +335,10 @@ struct PyMethodDef FreeCADGui_methods[] = {
{"subgraphFromObject",FreeCADGui_subgraphFromObject,METH_VARARGS,
"subgraphFromObject(object) -> Node\n\n"
"Return the Inventor subgraph to an object"},
{"replaceSwitchNodes",FreeCADGui_replaceSwitchNodes,METH_VARARGS,
"replaceSwitchNodes(Node) -> Node\n\n"
"Replace Switch nodes with Separators"},
{"exportSubgraph",FreeCADGui_exportSubgraph,METH_VARARGS,
"exportSubgraph(Node, File or Buffer, [Format='VRML']) -> None\n\n"
"Exports the sub-graph in the requested format"
"The format string can be VRML or IV"},
{"getSoDBVersion",FreeCADGui_getSoDBVersion,METH_VARARGS,
"getSoDBVersion() -> String\n\n"
"Return a text string containing the name\n"