+ add overloaded methods of write() and read() to write and read from streams

This commit is contained in:
wmayer 2016-06-11 14:47:01 +02:00
parent 91ba585ee0
commit 10bb9fa5cc
2 changed files with 106 additions and 33 deletions

View File

@ -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)
@ -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())
format = ext[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 (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);
@ -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)

View File

@ -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);