Skip to content

Commit

Permalink
+ add overloaded methods of write() and read() to write and read from…
Browse files Browse the repository at this point in the history
… streams
  • Loading branch information
wwmayer committed Jun 11, 2016
1 parent 91ba585 commit 10bb9fa
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 32 deletions.
135 changes: 104 additions & 31 deletions src/Mod/Mesh/App/MeshPyImp.cpp
Expand Up @@ -136,14 +136,49 @@ PyObject* MeshPy::copy(PyObject *args)
PyObject* MeshPy::read(PyObject *args)
{
const char* Name;
if (!PyArg_ParseTuple(args, "s",&Name))
return NULL;

PY_TRY {
if (PyArg_ParseTuple(args, "s",&Name)) {
getMeshObjectPtr()->load(Name);
} PY_CATCH;

Py_Return;
Py_Return;
}

PyErr_Clear();

MeshCore::MeshIO::Format format = MeshCore::MeshIO::Undefined;
std::map<std::string, MeshCore::MeshIO::Format> ext;
ext["BMS" ] = MeshCore::MeshIO::BMS;
ext["STL" ] = MeshCore::MeshIO::BSTL;
ext["AST" ] = MeshCore::MeshIO::ASTL;
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;
ext["NAS" ] = MeshCore::MeshIO::NAS;
ext["BDF" ] = MeshCore::MeshIO::NAS;
ext["PLY" ] = MeshCore::MeshIO::PLY;
ext["APLY"] = MeshCore::MeshIO::APLY;
ext["PY" ] = MeshCore::MeshIO::PY;

PyObject* input;
char* Ext;
if (PyArg_ParseTuple(args, "Os",&input,&Ext)) {
if (ext.find(Ext) != ext.end()) {
format = ext[Ext];
}

// read mesh
Base::PyStreambuf buf(input);
std::istream str(0);
str.rdbuf(&buf);
getMeshObjectPtr()->load(str, format);

Py_Return;
}

PyErr_SetString(PyExc_TypeError, "expect string or file object");
return NULL;
}

PyObject* MeshPy::write(PyObject *args)
Expand All @@ -152,32 +187,30 @@ PyObject* MeshPy::write(PyObject *args)
char* Ext=0;
char* ObjName=0;
PyObject* List=0;
if (!PyArg_ParseTuple(args, "s|ssO!",&Name,&Ext,&ObjName,&PyList_Type,&List))
return NULL;

MeshCore::MeshIO::Format format = MeshCore::MeshIO::Undefined;
if (Ext) {
std::map<std::string, MeshCore::MeshIO::Format> ext;
ext["BMS" ] = MeshCore::MeshIO::BMS;
ext["STL" ] = MeshCore::MeshIO::BSTL;
ext["AST" ] = MeshCore::MeshIO::ASTL;
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;
ext["NAS" ] = MeshCore::MeshIO::NAS;
ext["BDF" ] = MeshCore::MeshIO::NAS;
ext["PLY" ] = MeshCore::MeshIO::PLY;
ext["APLY"] = MeshCore::MeshIO::APLY;
ext["PY" ] = MeshCore::MeshIO::PY;
if (ext.find(Ext) != ext.end())
std::map<std::string, MeshCore::MeshIO::Format> ext;
ext["BMS" ] = MeshCore::MeshIO::BMS;
ext["STL" ] = MeshCore::MeshIO::BSTL;
ext["AST" ] = MeshCore::MeshIO::ASTL;
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;
ext["NAS" ] = MeshCore::MeshIO::NAS;
ext["BDF" ] = MeshCore::MeshIO::NAS;
ext["PLY" ] = MeshCore::MeshIO::PLY;
ext["APLY"] = MeshCore::MeshIO::APLY;
ext["PY" ] = MeshCore::MeshIO::PY;

if (PyArg_ParseTuple(args, "s|ssO!",&Name,&Ext,&ObjName,&PyList_Type,&List)) {
if (Ext && ext.find(Ext) != ext.end()) {
format = ext[Ext];
};
}

PY_TRY {
if (List) {
MeshCore::Material mat;
Py::List list(List);
Expand All @@ -200,9 +233,49 @@ PyObject* MeshPy::write(PyObject *args)
else {
getMeshObjectPtr()->save(Name, format, 0, ObjName);
}
} PY_CATCH;

Py_Return;
Py_Return;
}

PyErr_Clear();

PyObject* input;
if (PyArg_ParseTuple(args, "Os|sO!", &input,&Ext,&ObjName,&PyList_Type,&List)) {
if (ext.find(Ext) != ext.end()) {
format = ext[Ext];
}

std::auto_ptr<MeshCore::Material> mat;
if (List) {
mat.reset(new MeshCore::Material);
Py::List list(List);
for (Py::List::iterator it = list.begin(); it != list.end(); ++it) {
Py::Tuple t(*it);
float r = (float)Py::Float(t.getItem(0));
float g = (float)Py::Float(t.getItem(1));
float b = (float)Py::Float(t.getItem(2));
mat->diffuseColor.push_back(App::Color(r,g,b));
}

if (mat->diffuseColor.size() == getMeshObjectPtr()->countPoints())
mat->binding = MeshCore::MeshIO::PER_VERTEX;
else if (mat->diffuseColor.size() == getMeshObjectPtr()->countFacets())
mat->binding = MeshCore::MeshIO::PER_FACE;
else
mat->binding = MeshCore::MeshIO::OVERALL;
}

// write mesh
Base::PyStreambuf buf(input);
std::ostream str(0);
str.rdbuf(&buf);
getMeshObjectPtr()->save(str, format, mat.get(), ObjName);

Py_Return;
}

PyErr_SetString(PyExc_TypeError, "expect string or file object");
return NULL;
}

PyObject* MeshPy::writeInventor(PyObject *args)
Expand Down
2 changes: 1 addition & 1 deletion src/Mod/Part/App/TopoShapePyImp.cpp
Expand Up @@ -359,7 +359,7 @@ PyObject* TopoShapePy::exportBrep(PyObject *args)
PyObject* input;
if (PyArg_ParseTuple(args, "O", &input)) {
try {
// read brep
// write brep
Base::PyStreambuf buf(input);
std::ostream str(0);
str.rdbuf(&buf);
Expand Down

0 comments on commit 10bb9fa

Please sign in to comment.