From bf10bf33f64941a2e4eba5c822fa3bb7e7c62f12 Mon Sep 17 00:00:00 2001 From: wmayer Date: Thu, 21 Jan 2016 13:42:08 +0100 Subject: [PATCH] + simplify porting of Fem module to Python3 --- src/Mod/Fem/App/AppFem.cpp | 14 +- src/Mod/Fem/App/AppFemPy.cpp | 575 +++++++------------------------- src/Mod/Fem/Gui/AppFemGui.cpp | 12 +- src/Mod/Fem/Gui/AppFemGuiPy.cpp | 129 ++++--- 4 files changed, 202 insertions(+), 528 deletions(-) diff --git a/src/Mod/Fem/App/AppFem.cpp b/src/Mod/Fem/App/AppFem.cpp index 1a6fc95dd..97f38e44d 100644 --- a/src/Mod/Fem/App/AppFem.cpp +++ b/src/Mod/Fem/App/AppFem.cpp @@ -54,14 +54,12 @@ #include "FemResultObject.h" #include "FemSolverObject.h" -extern struct PyMethodDef Fem_methods[]; - -PyDoc_STRVAR(module_Fem_doc, -"This module is the FEM module."); +namespace Fem { +extern PyObject* initModule(); +} /* Python entry */ -extern "C" { -void AppFemExport initFem() +PyMODINIT_FUNC initFem() { // load dependend module try { @@ -72,7 +70,7 @@ void AppFemExport initFem() PyErr_SetString(PyExc_ImportError, e.what()); return; } - PyObject* femModule = Py_InitModule3("Fem", Fem_methods, module_Fem_doc); /* mod name, table ptr */ + PyObject* femModule = Fem::initModule(); Base::Console().Log("Loading Fem module... done\n"); Fem::StdMeshers_Arithmetic1DPy ::init_type(femModule); @@ -146,5 +144,3 @@ void AppFemExport initFem() Fem::FemSolverObject ::init(); Fem::FemSolverObjectPython ::init(); } - -} // extern "C" diff --git a/src/Mod/Fem/App/AppFemPy.cpp b/src/Mod/Fem/App/AppFemPy.cpp index 0a4a8bbea..22c383298 100644 --- a/src/Mod/Fem/App/AppFemPy.cpp +++ b/src/Mod/Fem/App/AppFemPy.cpp @@ -27,6 +27,9 @@ # include #endif +#include +#include + #include #include #include @@ -66,44 +69,66 @@ #include #include -#include "Base/Vector3D.h" +#include +#include -using namespace Fem; - - -/* module functions */ -static PyObject * read(PyObject *self, PyObject *args) +namespace Fem { +class Module : public Py::ExtensionModule { - char* Name; - if (!PyArg_ParseTuple(args, "et","utf-8",&Name)) - return NULL; - std::string EncodedName = std::string(Name); - PyMem_Free(Name); +public: + Module() : Py::ExtensionModule("Fem") + { + add_varargs_method("open",&Module::open, + "open(string) -- Create a new document and a Mesh::Import feature to load the file into the document." + ); + add_varargs_method("insert",&Module::insert, + "insert(string|mesh,[string]) -- Load or insert a mesh into the given or active document." + ); + add_varargs_method("export",&Module::exporter, + "export(list,string) -- Export a list of objects into a single file." + ); + add_varargs_method("read",&Module::read, + "Read a mesh from a file and returns a Mesh object." + ); + add_varargs_method("show",&Module::show, + "show(shape) -- Add the shape to the active document or create one if no document exists." + ); + initialize("This module is the Fem module."); // register with Python + } - PY_TRY { - std::auto_ptr mesh(new FemMesh); + virtual ~Module() {} + +private: + virtual Py::Object invoke_method_varargs(void *method_def, const Py::Tuple &args) + { try { - mesh->read(EncodedName.c_str()); - return new FemMeshPy(mesh.release()); + return Py::ExtensionModule::invoke_method_varargs(method_def, args); } - catch(...) { - PyErr_SetString(Base::BaseExceptionFreeCADError, "Loading of mesh was aborted"); - return NULL; + catch (const Standard_Failure &e) { + std::string str; + Standard_CString msg = e.GetMessageString(); + str += typeid(e).name(); + str += " "; + if (msg) {str += msg;} + else {str += "No OCCT Exception Message";} + throw Py::Exception(Part::PartExceptionOCCError, str); } - } PY_CATCH; + catch (const Base::Exception &e) { + throw Py::RuntimeError(e.what()); + } + catch (const std::exception &e) { + throw Py::RuntimeError(e.what()); + } + } + Py::Object open(const Py::Tuple& args) + { + char* Name; + if (!PyArg_ParseTuple(args.ptr(), "et","utf-8",&Name)) + throw Py::Exception(); - Py_Return; -} + std::string EncodedName = std::string(Name); + PyMem_Free(Name); -static PyObject * open(PyObject *self, PyObject *args) -{ - char* Name; - if (!PyArg_ParseTuple(args, "et","utf-8",&Name)) - return NULL; - std::string EncodedName = std::string(Name); - PyMem_Free(Name); - - PY_TRY { std::auto_ptr mesh(new FemMesh); mesh->read(EncodedName.c_str()); Base::FileInfo file(EncodedName.c_str()); @@ -115,47 +140,19 @@ static PyObject * open(PyObject *self, PyObject *args) pcFeature->FemMesh.setValuePtr(mesh.get()); (void)mesh.release(); pcFeature->purgeTouched(); - } PY_CATCH; - Py_Return; -} + return Py::None(); + } + Py::Object insert(const Py::Tuple& args) + { + char* Name; + const char* DocName = 0; + if (!PyArg_ParseTuple(args.ptr(), "et|s","utf-8",&Name,&DocName)) + throw Py::Exception(); -static PyObject * -show(PyObject *self, PyObject *args) -{ - PyObject *pcObj; - if (!PyArg_ParseTuple(args, "O!", &(FemMeshPy::Type), &pcObj)) // convert args: Python->C - return NULL; // NULL triggers exception + std::string EncodedName = std::string(Name); + PyMem_Free(Name); - PY_TRY { - App::Document *pcDoc = App::GetApplication().getActiveDocument(); - if (!pcDoc) - pcDoc = App::GetApplication().newDocument(); - - FemMeshPy* pShape = static_cast(pcObj); - Fem::FemMeshObject *pcFeature = (Fem::FemMeshObject *)pcDoc->addObject("Fem::FemMeshObject", "Mesh"); - // copy the data - //TopoShape* shape = new MeshObject(*pShape->getTopoShapeObjectPtr()); - pcFeature->FemMesh.setValue(*(pShape->getFemMeshPtr())); - pcDoc->recompute(); - } PY_CATCH; - - Py_Return; -} - - - - -static PyObject * importer(PyObject *self, PyObject *args) -{ - char* Name; - const char* DocName = 0; - if (!PyArg_ParseTuple(args, "et|s","utf-8",&Name,&DocName)) - return NULL; - std::string EncodedName = std::string(Name); - PyMem_Free(Name); - - PY_TRY { App::Document *pcDoc = 0; if (DocName) pcDoc = App::GetApplication().getDocument(DocName); @@ -176,378 +173,19 @@ static PyObject * importer(PyObject *self, PyObject *args) pcFeature->FemMesh.setValuePtr(mesh.get()); (void)mesh.release(); pcFeature->purgeTouched(); - } PY_CATCH; - Py_Return; -} + return Py::None(); + } + Py::Object exporter(const Py::Tuple& args) + { + PyObject* object; + char* Name; + if (!PyArg_ParseTuple(args.ptr(), "Oet",&object,"utf-8",&Name)) + throw Py::Exception(); + std::string EncodedName = std::string(Name); + PyMem_Free(Name); -//static PyObject * import_NASTRAN(PyObject *self, PyObject *args) -//{ -// const char* filename_input, *filename_output; -// if (!PyArg_ParseTuple(args, "ss",&filename_input,&filename_output)) -// return NULL; -// -// PY_TRY { -// -// std::ifstream inputfile; -// -// //Für Debugoutput -// ofstream anOutput; -// anOutput.open("c:/time_measurement.txt"); -// time_t seconds1,seconds2; -// -// inputfile.open(filename_input); -// if (!inputfile.is_open()) //Exists...? -// { -// std::cerr << "File not found. Exiting..." << std::endl; -// return NULL; -// } -// -// //Return the line pointer to the beginning of the file -// inputfile.seekg(std::ifstream::beg); -// std::string line1,line2,temp; -// std::stringstream astream; -// std::vector tetra_element; -// std::vector element_id; -// element_id.clear(); -// std::vector > all_elements; -// std::vector >::iterator all_element_iterator; -// std::vector::iterator one_element_iterator; -// all_elements.clear(); -// MeshCore::MeshKernel aMesh; -// MeshCore::MeshPointArray vertices; -// vertices.clear(); -// MeshCore::MeshFacetArray faces; -// faces.clear(); -// MeshCore::MeshPoint current_node; -// -// seconds1 = time(NULL); -// do -// { -// std::getline(inputfile,line1); -// if (line1.size() == 0) continue; -// if (line1.find("GRID*")!= std::string::npos) //We found a Grid line -// { -// //Now lets extract the GRID Points = Nodes -// //As each GRID Line consists of two subsequent lines we have to -// //take care of that as well -// std::getline(inputfile,line2); -// //Extract X Value -// current_node.x = float(atof(line1.substr(40,56).c_str())); -// //Extract Y Value -// current_node.y = float(atof(line1.substr(56,72).c_str())); -// //Extract Z Value -// current_node.z = float(atof(line2.substr(8,24).c_str())); -// -// vertices.push_back(current_node); -// } -// else if (line1.find("CTETRA")!= std::string::npos) -// { -// tetra_element.clear(); -// //Lets extract the elements -// //As each Element Line consists of two subsequent lines as well -// //we have to take care of that -// //At a first step we only extract Quadratic Tetrahedral Elements -// std::getline(inputfile,line2); -// element_id.push_back(atoi(line1.substr(8,16).c_str())); -// tetra_element.push_back(atoi(line1.substr(24,32).c_str())); -// tetra_element.push_back(atoi(line1.substr(32,40).c_str())); -// tetra_element.push_back(atoi(line1.substr(40,48).c_str())); -// tetra_element.push_back(atoi(line1.substr(48,56).c_str())); -// tetra_element.push_back(atoi(line1.substr(56,64).c_str())); -// tetra_element.push_back(atoi(line1.substr(64,72).c_str())); -// tetra_element.push_back(atoi(line2.substr(8,16).c_str())); -// tetra_element.push_back(atoi(line2.substr(16,24).c_str())); -// tetra_element.push_back(atoi(line2.substr(24,32).c_str())); -// tetra_element.push_back(atoi(line2.substr(32,40).c_str())); -// -// all_elements.push_back(tetra_element); -// } -// -// } -// while (inputfile.good()); -// inputfile.close(); -// -// seconds2 = time(NULL); -// -// anOutput << seconds2-seconds1 <<" for Parsing the input file"<(2.0*M_PI/360.0);step_size=(2.0*M_PI/it_steps)) -// { -// for(alpha_x=angle_range_min_x;alpha_xCreateMesh(1, true); -// SMESHDS_Mesh* meshds = mesh->GetMeshDS(); -// -// MeshCore::MeshPointIterator anodeiterator(aMesh); -// int j=1; -// for(anodeiterator.Begin(); anodeiterator.More(); anodeiterator.Next()) -// { -// meshds->AddNodeWithID((*anodeiterator).x,(*anodeiterator).y,(*anodeiterator).z,j); -// j++; -// } -// -// for(unsigned int i=0;iAddVolumeWithID( -// meshds->FindNode(all_elements[i][0]), -// meshds->FindNode(all_elements[i][2]), -// meshds->FindNode(all_elements[i][1]), -// meshds->FindNode(all_elements[i][3]), -// meshds->FindNode(all_elements[i][6]), -// meshds->FindNode(all_elements[i][5]), -// meshds->FindNode(all_elements[i][4]), -// meshds->FindNode(all_elements[i][9]), -// meshds->FindNode(all_elements[i][7]), -// meshds->FindNode(all_elements[i][8]), -// element_id[i] -// ); -// } -// -// mesh->ExportUNV(filename_output); -// ////////////////////////////////////////////////////////////////////////////////////////// -// seconds2 = time(NULL); -// anOutput << seconds2-seconds1 << " seconds for the Mesh Export" << std::endl; -// -// //Output also to ABAQUS Input Format -// ofstream anABAQUS_Output; -// anABAQUS_Output.open("d:/abaqus_output.inp"); -// anABAQUS_Output << "*Node , NSET=Nall" << std::endl; -// j=1; -// for(anodeiterator.Begin(); anodeiterator.More(); anodeiterator.Next()) -// { -// anABAQUS_Output << j <<"," -// <<(*anodeiterator).x << "," -// <<(*anodeiterator).y << "," -// <<(*anodeiterator).z << std::endl; -// j++; -// } -// anABAQUS_Output << "*Element, TYPE=C3D10, ELSET=Eall" << std::endl; -// j=1; -// for(unsigned int i=0;iat(4)-1]-vertices[all_element_iterator->at(0)-1]; -// b = vertices[all_element_iterator->at(7)-1]-vertices[all_element_iterator->at(0)-1]; -// c = vertices[all_element_iterator->at(6)-1]-vertices[all_element_iterator->at(0)-1]; -// a_b_product.x = a.y*b.z-b.y*a.z;a_b_product.y = a.z*b.x-b.z*a.x;a_b_product.z = a.x*b.y-b.x*a.y; -// volume += 1.0/6.0 * fabs((a_b_product.x * c.x)+ (a_b_product.y * c.y)+(a_b_product.z * c.z)); -// //5,9,8,7 -// a = vertices[all_element_iterator->at(8)-1]-vertices[all_element_iterator->at(4)-1]; -// b = vertices[all_element_iterator->at(7)-1]-vertices[all_element_iterator->at(4)-1]; -// c = vertices[all_element_iterator->at(6)-1]-vertices[all_element_iterator->at(4)-1]; -// a_b_product.x = a.y*b.z-b.y*a.z;a_b_product.y = a.z*b.x-b.z*a.x;a_b_product.z = a.x*b.y-b.x*a.y; -// volume += 1.0/6.0 * fabs((a_b_product.x * c.x)+ (a_b_product.y * c.y)+(a_b_product.z * c.z)); -// //5,2,9,7 -// a = vertices[all_element_iterator->at(1)-1]-vertices[all_element_iterator->at(4)-1]; -// b = vertices[all_element_iterator->at(8)-1]-vertices[all_element_iterator->at(4)-1]; -// c = vertices[all_element_iterator->at(6)-1]-vertices[all_element_iterator->at(4)-1]; -// a_b_product.x = a.y*b.z-b.y*a.z;a_b_product.y = a.z*b.x-b.z*a.x;a_b_product.z = a.x*b.y-b.x*a.y; -// volume += 1.0/6.0 * fabs((a_b_product.x * c.x)+ (a_b_product.y * c.y)+(a_b_product.z * c.z)); -// //2,6,9,7 -// a = vertices[all_element_iterator->at(5)-1]-vertices[all_element_iterator->at(1)-1]; -// b = vertices[all_element_iterator->at(8)-1]-vertices[all_element_iterator->at(1)-1]; -// c = vertices[all_element_iterator->at(6)-1]-vertices[all_element_iterator->at(1)-1]; -// a_b_product.x = a.y*b.z-b.y*a.z;a_b_product.y = a.z*b.x-b.z*a.x;a_b_product.z = a.x*b.y-b.x*a.y; -// volume += 1.0/6.0 * fabs((a_b_product.x * c.x)+ (a_b_product.y * c.y)+(a_b_product.z * c.z)); -// //9,6,10,7 -// a = vertices[all_element_iterator->at(5)-1]-vertices[all_element_iterator->at(8)-1]; -// b = vertices[all_element_iterator->at(9)-1]-vertices[all_element_iterator->at(8)-1]; -// c = vertices[all_element_iterator->at(6)-1]-vertices[all_element_iterator->at(8)-1]; -// a_b_product.x = a.y*b.z-b.y*a.z;a_b_product.y = a.z*b.x-b.z*a.x;a_b_product.z = a.x*b.y-b.x*a.y; -// volume += 1.0/6.0 * fabs((a_b_product.x * c.x)+ (a_b_product.y * c.y)+(a_b_product.z * c.z)); -// //6,3,10,7 -// a = vertices[all_element_iterator->at(2)-1]-vertices[all_element_iterator->at(5)-1]; -// b = vertices[all_element_iterator->at(9)-1]-vertices[all_element_iterator->at(5)-1]; -// c = vertices[all_element_iterator->at(6)-1]-vertices[all_element_iterator->at(5)-1]; -// a_b_product.x = a.y*b.z-b.y*a.z;a_b_product.y = a.z*b.x-b.z*a.x;a_b_product.z = a.x*b.y-b.x*a.y; -// volume += 1.0/6.0 * fabs((a_b_product.x * c.x)+ (a_b_product.y * c.y)+(a_b_product.z * c.z)); -// //8,9,10,7 -// a = vertices[all_element_iterator->at(8)-1]-vertices[all_element_iterator->at(7)-1]; -// b = vertices[all_element_iterator->at(9)-1]-vertices[all_element_iterator->at(7)-1]; -// c = vertices[all_element_iterator->at(6)-1]-vertices[all_element_iterator->at(7)-1]; -// a_b_product.x = a.y*b.z-b.y*a.z;a_b_product.y = a.z*b.x-b.z*a.x;a_b_product.z = a.x*b.y-b.x*a.y; -// volume += 1.0/6.0 * fabs((a_b_product.x * c.x)+ (a_b_product.y * c.y)+(a_b_product.z * c.z)); -// //8,9,10,4 -// a = vertices[all_element_iterator->at(8)-1]-vertices[all_element_iterator->at(7)-1]; -// b = vertices[all_element_iterator->at(9)-1]-vertices[all_element_iterator->at(7)-1]; -// c = vertices[all_element_iterator->at(3)-1]-vertices[all_element_iterator->at(7)-1]; -// a_b_product.x = a.y*b.z-b.y*a.z;a_b_product.y = a.z*b.x-b.z*a.x;a_b_product.z = a.x*b.y-b.x*a.y; -// volume += 1.0/6.0 * fabs((a_b_product.x * c.x)+ (a_b_product.y * c.y)+(a_b_product.z * c.z)); -// -// } -// seconds2=time(NULL); -// anOutput << seconds2-seconds1 << " seconds for Volume Calculation " << "Volumen " << volume/1000.0/1000.0/1000.0 << " in m^3" << std::endl; -// anOutput << "Volumen der BBox" << min_volumeBBOX/1000.0/1000.0/1000.0 << std::endl; -// anOutput << "Fly to Buy Ratio: " << min_volumeBBOX / volume << std::endl; -// anOutput.close(); -// -// } PY_CATCH; -// -// Py_Return; -//} - - -static PyObject * exporter(PyObject *self, PyObject *args) -{ - PyObject* object; - char* Name; - if (!PyArg_ParseTuple(args, "Oet",&object,"utf-8",&Name)) - return NULL; - std::string EncodedName = std::string(Name); - PyMem_Free(Name); - - PY_TRY { Py::Sequence list(object); Base::Type meshId = Base::Type::fromName("Fem::FemMeshObject"); for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) { @@ -560,29 +198,46 @@ static PyObject * exporter(PyObject *self, PyObject *args) } } } - } PY_CATCH; - Py_Return; + return Py::None(); + } + Py::Object read(const Py::Tuple& args) + { + char* Name; + if (!PyArg_ParseTuple(args.ptr(), "et","utf-8",&Name)) + throw Py::Exception(); + + std::string EncodedName = std::string(Name); + PyMem_Free(Name); + + std::auto_ptr mesh(new FemMesh); + mesh->read(EncodedName.c_str()); + return Py::asObject(new FemMeshPy(mesh.release())); + } + Py::Object show(const Py::Tuple& args) + { + PyObject *pcObj; + if (!PyArg_ParseTuple(args.ptr(), "O!", &(FemMeshPy::Type), &pcObj)) + throw Py::Exception(); + + App::Document *pcDoc = App::GetApplication().getActiveDocument(); + if (!pcDoc) + pcDoc = App::GetApplication().newDocument(); + + FemMeshPy* pShape = static_cast(pcObj); + Fem::FemMeshObject *pcFeature = (Fem::FemMeshObject *)pcDoc->addObject("Fem::FemMeshObject", "Mesh"); + // copy the data + //TopoShape* shape = new MeshObject(*pShape->getTopoShapeObjectPtr()); + pcFeature->FemMesh.setValue(*(pShape->getFemMeshPtr())); + pcDoc->recompute(); + + return Py::None(); + } +}; + +PyObject* initModule() +{ + return (new Module)->module().ptr(); } -// ---------------------------------------------------------------------------- - -PyDoc_STRVAR(open_doc, -"open(string) -- Create a new document and a Mesh::Import feature to load the file into the document."); - -PyDoc_STRVAR(inst_doc, -"insert(string|mesh,[string]) -- Load or insert a mesh into the given or active document."); - -PyDoc_STRVAR(export_doc, -"export(list,string) -- Export a list of objects into a single file."); - -/* registration table */ -struct PyMethodDef Fem_methods[] = { - {"open" ,open , METH_VARARGS, open_doc}, - {"insert" ,importer, METH_VARARGS, inst_doc}, - {"export" ,exporter, METH_VARARGS, export_doc}, - {"read" ,read, Py_NEWARGS, "Read a mesh from a file and returns a Mesh object."}, - {"show" ,show ,METH_VARARGS, - "show(shape) -- Add the shape to the active document or create one if no document exists."}, - {NULL, NULL} /* sentinel */ -}; +} // namespace Fem diff --git a/src/Mod/Fem/Gui/AppFemGui.cpp b/src/Mod/Fem/Gui/AppFemGui.cpp index 85a8603eb..c7491a03f 100644 --- a/src/Mod/Fem/Gui/AppFemGui.cpp +++ b/src/Mod/Fem/Gui/AppFemGui.cpp @@ -63,20 +63,20 @@ void loadFemResource() Gui::Translator::instance()->refresh(); } -/* registration table */ -extern struct PyMethodDef FemGui_Import_methods[]; +namespace FemGui { +extern PyObject* initModule(); +} /* Python entry */ -extern "C" { -void FemGuiExport initFemGui() +PyMODINIT_FUNC initFemGui() { if (!Gui::Application::Instance) { PyErr_SetString(PyExc_ImportError, "Cannot load Gui module in console application."); return; } - (void) Py_InitModule("FemGui", FemGui_Import_methods); /* mod name, table ptr */ + (void) FemGui::initModule(); Base::Console().Log("Loading GUI of Fem module... done\n"); // instantiating the commands @@ -112,5 +112,3 @@ void FemGuiExport initFemGui() // add resources and reloads the translators loadFemResource(); } - -} // extern "C" { diff --git a/src/Mod/Fem/Gui/AppFemGuiPy.cpp b/src/Mod/Fem/Gui/AppFemGuiPy.cpp index 4ad537c18..4d1497127 100755 --- a/src/Mod/Fem/Gui/AppFemGuiPy.cpp +++ b/src/Mod/Fem/Gui/AppFemGuiPy.cpp @@ -27,6 +27,9 @@ # include #endif +#include +#include + #include #include #include @@ -41,50 +44,80 @@ #include "AbaqusHighlighter.h" -/* module functions */ -static PyObject * setActiveAnalysis(PyObject *self, PyObject *args) +namespace FemGui { +class Module : public Py::ExtensionModule { - if (FemGui::ActiveAnalysisObserver::instance()->hasActiveObject()) { - FemGui::ActiveAnalysisObserver::instance()->highlightActiveObject(Gui::Blue,false); - FemGui::ActiveAnalysisObserver::instance()->setActiveObject(0); +public: + Module() : Py::ExtensionModule("FemGui") + { + add_varargs_method("setActiveAnalysis",&Module::setActiveAnalysis, + "setActiveAnalysis(AnalysisObject) -- Set the Analysis object in work." + ); + add_varargs_method("getActiveAnalysis",&Module::getActiveAnalysis, + "getActiveAnalysis() -- Returns the Analysis object in work." + ); + add_varargs_method("open",&Module::open, + "open(string) -- Opens an Abaqus file in a text editor." + ); + add_varargs_method("insert",&Module::open, + "insert(string,string) -- Opens an Abaqus file in a text editor." + ); + initialize("This module is the FemGui module."); // register with Python } - PyObject *object=0; - if (PyArg_ParseTuple(args,"|O!",&(App::DocumentObjectPy::Type), &object)&& object) { - App::DocumentObject* obj = static_cast(object)->getDocumentObjectPtr(); - if (!obj || !obj->getTypeId().isDerivedFrom(Fem::FemAnalysis::getClassTypeId())){ - PyErr_SetString(Base::BaseExceptionFreeCADError, "Active Analysis object have to be of type Fem::FemAnalysis!"); - return 0; + virtual ~Module() {} + +private: + virtual Py::Object invoke_method_varargs(void *method_def, const Py::Tuple &args) + { + try { + return Py::ExtensionModule::invoke_method_varargs(method_def, args); + } + catch (const Base::Exception &e) { + throw Py::RuntimeError(e.what()); + } + catch (const std::exception &e) { + throw Py::RuntimeError(e.what()); + } + } + Py::Object setActiveAnalysis(const Py::Tuple& args) + { + if (FemGui::ActiveAnalysisObserver::instance()->hasActiveObject()) { + FemGui::ActiveAnalysisObserver::instance()->highlightActiveObject(Gui::Blue,false); + FemGui::ActiveAnalysisObserver::instance()->setActiveObject(0); } - // get the gui document of the Analysis Item - FemGui::ActiveAnalysisObserver::instance()->setActiveObject(static_cast(obj)); - FemGui::ActiveAnalysisObserver::instance()->highlightActiveObject(Gui::Blue,true); + PyObject *object=0; + if (PyArg_ParseTuple(args.ptr(),"|O!",&(App::DocumentObjectPy::Type), &object)&& object) { + App::DocumentObject* obj = static_cast(object)->getDocumentObjectPtr(); + if (!obj || !obj->getTypeId().isDerivedFrom(Fem::FemAnalysis::getClassTypeId())){ + throw Py::Exception(Base::BaseExceptionFreeCADError, "Active Analysis object have to be of type Fem::FemAnalysis!"); + } + + // get the gui document of the Analysis Item + FemGui::ActiveAnalysisObserver::instance()->setActiveObject(static_cast(obj)); + FemGui::ActiveAnalysisObserver::instance()->highlightActiveObject(Gui::Blue,true); + } + + return Py::None(); } - - Py_Return; -} - -/* module functions */ -static PyObject * getActiveAnalysis(PyObject *self, PyObject *args) -{ - if (FemGui::ActiveAnalysisObserver::instance()->hasActiveObject()) { - return FemGui::ActiveAnalysisObserver::instance()->getActiveObject()->getPyObject(); + Py::Object getActiveAnalysis(const Py::Tuple& args) + { + if (FemGui::ActiveAnalysisObserver::instance()->hasActiveObject()) { + return Py::asObject(FemGui::ActiveAnalysisObserver::instance()->getActiveObject()->getPyObject()); + } + return Py::None(); } + Py::Object open(const Py::Tuple& args) + { + char* Name; + const char* DocName; + if (!PyArg_ParseTuple(args.ptr(), "et|s","utf-8",&Name,&DocName)) + throw Py::Exception(); - Py_Return; -} + std::string EncodedName = std::string(Name); + PyMem_Free(Name); -/* module functions */ -static PyObject * open(PyObject *self, PyObject *args) -{ - char* Name; - const char* DocName; - if (!PyArg_ParseTuple(args, "et|s","utf-8",&Name,&DocName)) - return NULL; - std::string EncodedName = std::string(Name); - PyMem_Free(Name); - PY_TRY { QString fileName = QString::fromUtf8(EncodedName.c_str()); QFileInfo fi; fi.setFile(fileName); @@ -93,7 +126,7 @@ static PyObject * open(PyObject *self, PyObject *args) for (QList::Iterator it = views.begin(); it != views.end(); ++it) { if ((*it)->fileName() == fileName) { (*it)->setFocus(); - Py_Return; + return Py::None(); } } @@ -111,22 +144,14 @@ static PyObject * open(PyObject *self, PyObject *args) font.setFamily(QString::fromLatin1("Arial")); editor->setFont(font); } - } PY_CATCH; - Py_Return; + return Py::None(); + } +}; + +PyObject* initModule() +{ + return (new Module)->module().ptr(); } - - -/* registration table */ -struct PyMethodDef FemGui_Import_methods[] = { - {"setActiveAnalysis" ,setActiveAnalysis ,METH_VARARGS, - "setActiveAnalysis(AnalysisObject) -- Set the Analysis object in work."}, - {"getActiveAnalysis" ,getActiveAnalysis ,METH_VARARGS, - "getActiveAnalysis() -- Returns the Analysis object in work."}, - {"open" ,open ,METH_VARARGS, - "open(string) -- Opens an Abaqus file in a text editor."}, - {"insert" ,open ,METH_VARARGS, - "insert(string,string) -- Opens an Abaqus file in a text editor."}, - {NULL, NULL} /* end of table marker */ -}; +} // namespace FemGui