diff --git a/src/Mod/Mesh/App/MeshPyImp.cpp b/src/Mod/Mesh/App/MeshPyImp.cpp index bf1e03096c..49d52d4d96 100644 --- a/src/Mod/Mesh/App/MeshPyImp.cpp +++ b/src/Mod/Mesh/App/MeshPyImp.cpp @@ -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 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 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 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 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) diff --git a/src/Mod/Part/App/TopoShapePyImp.cpp b/src/Mod/Part/App/TopoShapePyImp.cpp index 8eccc63e28..f25fc12dbe 100644 --- a/src/Mod/Part/App/TopoShapePyImp.cpp +++ b/src/Mod/Part/App/TopoShapePyImp.cpp @@ -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);