From b441c0d3e863ca2c02f7de590679b3ed11fd556a Mon Sep 17 00:00:00 2001 From: Bernd Hahnebach Date: Fri, 14 Dec 2018 21:27:58 +0100 Subject: [PATCH] FEM: mesh api, import of z88 mesh file with Fem API --- src/Mod/Fem/App/FemMesh.cpp | 39 +++++++++++++++++++++++++++++++++ src/Mod/Fem/App/FemMesh.h | 1 + src/Mod/Fem/App/FemMeshPy.xml | 6 +++-- src/Mod/Fem/Init.py | 4 ++-- src/Mod/Fem/femtest/testmesh.py | 5 ++--- 5 files changed, 48 insertions(+), 7 deletions(-) diff --git a/src/Mod/Fem/App/FemMesh.cpp b/src/Mod/Fem/App/FemMesh.cpp index 1d6629015e..9089a0afaa 100644 --- a/src/Mod/Fem/App/FemMesh.cpp +++ b/src/Mod/Fem/App/FemMesh.cpp @@ -1169,6 +1169,41 @@ void FemMesh::readNastran(const std::string &Filename) } +void FemMesh::readZ88(const std::string &FileName) +{ + Base::TimeInfo Start; + Base::Console().Log("Start: FemMesh::readZ88() =================================\n"); + + /* + Python command to read Z88 mesh file from test suite: + from feminout.importZ88Mesh import read as read_z88 + femmesh = read_z88(FreeCAD.ConfigGet("AppHomePath") + 'Mod/Fem/femtest/testfiles/mesh/tetra10_mesh.z88') + */ + + PyObject* module = PyImport_ImportModule("feminout.importZ88Mesh"); + if (!module) + return; + try { + Py::Module z88mod(module, true); + Py::Callable method(z88mod.getAttr("read")); + Py::Tuple args(1); + args.setItem(0, Py::String(FileName)); + Py::Object mesh(method.apply(args)); + if (PyObject_TypeCheck(mesh.ptr(), &FemMeshPy::Type)) { + FemMeshPy* fempy = static_cast(mesh.ptr()); + FemMesh* fem = fempy->getFemMeshPtr(); + *this = *fem; // the deep copy should be avoided, a pointer swap method could be implemented + // see https://forum.freecadweb.org/viewtopic.php?f=10&t=31999&start=10#p274241 + } + else { + throw Base::FileException("Problems reading file"); + } + } + catch (Py::Exception& e) { + e.clear(); + } + Base::Console().Log(" %f: Done \n",Base::TimeInfo::diffTimeF(Start,Base::TimeInfo())); +} void FemMesh::read(const char *FileName) { @@ -1207,6 +1242,10 @@ void FemMesh::read(const char *FileName) FemVTKTools::readVTKMesh(File.filePath().c_str(), this); } #endif + else if (File.hasExtension("z88") ) { + // read Z88 mesh file + readZ88(File.filePath()); + } else{ throw Base::FileException("Unknown extension"); } diff --git a/src/Mod/Fem/App/FemMesh.h b/src/Mod/Fem/App/FemMesh.h index 87bd2a1740..28654fa4a5 100644 --- a/src/Mod/Fem/App/FemMesh.h +++ b/src/Mod/Fem/App/FemMesh.h @@ -157,6 +157,7 @@ public: private: void copyMeshData(const FemMesh&); void readNastran(const std::string &Filename); + void readZ88(const std::string &Filename); private: /// positioning matrix diff --git a/src/Mod/Fem/App/FemMeshPy.xml b/src/Mod/Fem/App/FemMeshPy.xml index 5605d22390..fcd24c0057 100755 --- a/src/Mod/Fem/App/FemMeshPy.xml +++ b/src/Mod/Fem/App/FemMeshPy.xml @@ -61,14 +61,16 @@ - Read in a DAT, UNV, MED or STL file. + Read in a various FEM mesh file formats. + read(file.endingToExportTo) + supported formats: DAT, MED, STL, UNV, VTK, Z88 Write out various FEM mesh file formats. write(file.endingToExportTo) - supported formats: DAT, INP, MED, STL, UNV, VTK, Z88 + supported formats: BDF, DAT, INP, MED, STL, UNV, VTK, Z88 diff --git a/src/Mod/Fem/Init.py b/src/Mod/Fem/Init.py index f3f7cab34d..c64760c0ca 100644 --- a/src/Mod/Fem/Init.py +++ b/src/Mod/Fem/Init.py @@ -31,7 +31,7 @@ import FreeCAD FreeCAD.addExportType("FEM mesh TetGen (*.poly)", "feminout.convert2TetGen") # see FemMesh::read() and FemMesh::write() methods in src/Mod/Fem/App/FemMesh.cpp -FreeCAD.addImportType("FEM mesh formats (*.unv *.med *.dat *.bdf *.vtk *.vtu)", "Fem") +FreeCAD.addImportType("FEM mesh formats (*.unv *.med *.dat *.bdf *.vtk *.vtu *.z88)", "Fem") FreeCAD.addExportType("FEM mesh formats (*.unv *.med *.stl *.dat *.inp *.vtk *.vtu *.z88)", "Fem") FreeCAD.addImportType("FEM mesh CalculiX/Abaqus (*.inp)", "feminout.importInpMesh") @@ -40,7 +40,7 @@ FreeCAD.addImportType("FEM result CalculiX (*.frd)", "feminout.importCcxFrdResul FreeCAD.addImportType("FEM mesh Fenics (*.xml *.xdmf)", "feminout.importFenicsMesh") FreeCAD.addExportType("FEM mesh Fenics (*.xml *.xdmf)", "feminout.importFenicsMesh") -FreeCAD.addImportType("FEM mesh Z88 (*i1.txt *.z88)", "feminout.importZ88Mesh") +FreeCAD.addImportType("FEM mesh Z88 (*i1.txt)", "feminout.importZ88Mesh") FreeCAD.addExportType("FEM mesh Z88 (*i1.txt)", "feminout.importZ88Mesh") FreeCAD.addImportType("FEM result Z88 displacements (*o2.txt)", "feminout.importZ88O2Results") diff --git a/src/Mod/Fem/femtest/testmesh.py b/src/Mod/Fem/femtest/testmesh.py index 602c65b881..0c80d5fbbe 100644 --- a/src/Mod/Fem/femtest/testmesh.py +++ b/src/Mod/Fem/femtest/testmesh.py @@ -357,9 +357,8 @@ class TestMeshEleTetra10(unittest.TestCase): # fcc_print(outfile) # fcc_print(testfile) self.femmesh.write(outfile) # write the mesh - from feminout.importZ88Mesh import read as read_z88 - femmesh_testfile = read_z88(outfile) # read the mesh from written mesh - femmesh_outfile = read_z88(testfile) # read the mesh from test mesh + femmesh_testfile = Fem.read(outfile) # read the mesh from written mesh + femmesh_outfile = Fem.read(testfile) # read the mesh from test mesh # reading the test mesh self.assertEqual( femmesh_testfile.Nodes,