From 9ce1662ae1cd56ecf9e5e7189c0ac185c0d48d1e Mon Sep 17 00:00:00 2001 From: qingfengxia Date: Mon, 30 Jan 2017 18:22:11 +0100 Subject: [PATCH] Fem: add FemResult export to VTK files for visualization externally --- src/Mod/Fem/App/AppFemPy.cpp | 11 ++---- src/Mod/Fem/App/CMakeLists.txt | 1 + src/Mod/Fem/CMakeLists.txt | 3 ++ src/Mod/Fem/FemResultVTK.py | 67 ++++++++++++++++++++++++++++++++++ src/Mod/Fem/Init.py | 3 +- 5 files changed, 77 insertions(+), 8 deletions(-) create mode 100644 src/Mod/Fem/FemResultVTK.py diff --git a/src/Mod/Fem/App/AppFemPy.cpp b/src/Mod/Fem/App/AppFemPy.cpp index 6a6314e4b7..8c87238ded 100644 --- a/src/Mod/Fem/App/AppFemPy.cpp +++ b/src/Mod/Fem/App/AppFemPy.cpp @@ -281,16 +281,13 @@ private: std::string EncodedName = std::string(fileName); PyMem_Free(fileName); - if (!pcObj) + if (pcObj) { - App::DocumentObjectPy* objpy= static_cast(pcObj); - App::DocumentObject* obj = objpy->getDocumentObjectPtr(); - if (!obj) + if (PyObject_TypeCheck(pcObj, &(App::DocumentObjectPy::Type))) { - App::Document* pcDoc = App::GetApplication().getActiveDocument(); - obj = pcDoc->getActiveObject(); + App::DocumentObject* obj = static_cast(pcObj)->getDocumentObjectPtr(); + FemVTKTools::writeResult(EncodedName.c_str(), obj); } - FemVTKTools::readFluidicResult(EncodedName.c_str(), obj); } else FemVTKTools::writeResult(EncodedName.c_str()); diff --git a/src/Mod/Fem/App/CMakeLists.txt b/src/Mod/Fem/App/CMakeLists.txt index 4f22594a6a..050758b979 100755 --- a/src/Mod/Fem/App/CMakeLists.txt +++ b/src/Mod/Fem/App/CMakeLists.txt @@ -114,6 +114,7 @@ SET(FemScripts_SRCS convert2TetGen.py importInpMesh.py importZ88Mesh.py + FemResultVTK.py Init.py InitGui.py FemAnalysis.py diff --git a/src/Mod/Fem/CMakeLists.txt b/src/Mod/Fem/CMakeLists.txt index 3aa85c5053..83b42eec23 100755 --- a/src/Mod/Fem/CMakeLists.txt +++ b/src/Mod/Fem/CMakeLists.txt @@ -124,6 +124,9 @@ INSTALL( _ViewProviderFemSolverZ88.py _CommandSolverZ88.py + # result related import and export + FemResultVTK.py + DESTINATION Mod/Fem ) diff --git a/src/Mod/Fem/FemResultVTK.py b/src/Mod/Fem/FemResultVTK.py new file mode 100644 index 0000000000..ffbf6d61b6 --- /dev/null +++ b/src/Mod/Fem/FemResultVTK.py @@ -0,0 +1,67 @@ +# *************************************************************************** +# * (c) Qingfeng Xia 2017 * +# * * +# * This file is part of the FreeCAD CAx development system. * +# * * +# * This program is free software; you can redistribute it and/or modify * +# * it under the terms of the GNU Lesser General Public License (LGPL) * +# * as published by the Free Software Foundation; either version 2 of * +# * the License, or (at your option) any later version. * +# * for detail see the LICENCE text file. * +# * * +# * FreeCAD is distributed in the hope that it will be useful, * +# * but WITHOUT ANY WARRANTY; without even the implied warranty of * +# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +# * GNU Lesser General Public License for more details. * +# * * +# * You should have received a copy of the GNU Library General Public * +# * License along with FreeCAD; if not, write to the Free Software * +# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * +# * USA * +# * * +# * Juergen Riegel 2002 * +# ***************************************************************************/ + +__title__ = "FreeCAD Result import and export VTK file library" +__author__ = "Qingfeng Xia" +__url__ = "http://www.freecadweb.org" + + +import os +from math import pow, sqrt +import numpy as np + +import FreeCAD + +if open.__module__ == '__builtin__': + pyopen = open # because we'll redefine open below + +def insert(filename, docname): + "called when freecad wants to import a file" + try: + doc = FreeCAD.getDocument(docname) + except NameError: + doc = FreeCAD.newDocument(docname) + FreeCAD.ActiveDocument = doc + importFemResult(filename) + + +def open(filename): + "called when freecad opens a file" + docname = os.path.splitext(os.path.basename(filename))[0] + insert(filename, docname) + +def importFemResult(filename): + FreeCAD.Console.PrintError("FemResult import is not implemented, actually not necessary\n") + +def export(objectslist, filename): + "called when freecad exports a fem result object" + if len(objectslist) != 1: + FreeCAD.Console.PrintError("This exporter can only export one object at once\n") + return + obj = objectslist[0] + if not obj.isDerivedFrom("Fem::FemResultObject"): + FreeCAD.Console.PrintError("object selcted is not FemResultObject.\n") + return + import Fem + Fem.writeResult(filename, obj) \ No newline at end of file diff --git a/src/Mod/Fem/Init.py b/src/Mod/Fem/Init.py index 865ebb09c5..1d75097192 100644 --- a/src/Mod/Fem/Init.py +++ b/src/Mod/Fem/Init.py @@ -30,9 +30,10 @@ import FreeCAD FreeCAD.addExportType("TetGen file (*.poly)", "convert2TetGen") FreeCAD.addImportType("FEM formats (*.unv *.med *.dat *.bdf)", "Fem") if("BUILD_FEM_VTK" in FreeCAD.__cmake__): - FreeCAD.addImportType("FEM results (*.vtk *.vtp *.vts *.vtr *.vtu *.vti)", "Fem") + #FreeCAD.addImportType("FEM results (*.vtk *.vtp *.vts *.vtr *.vtu *.vti)", "FemResultVTK") # not implemented yet FreeCAD.addImportType("FEM CFD Unstructure Mesh (*.vtk *.vtu)", "Fem") FreeCAD.addExportType("FEM CFD Unstructure Mesh (*.vtk *.vtu)", "Fem") + FreeCAD.addExportType("FEM CFD Result in VTK format (*.vtk *.vtu)", "FemResultVTK") FreeCAD.addExportType("FEM formats (*.unv *.med *.dat *.inp)", "Fem") FreeCAD.addImportType("CalculiX result (*.frd)", "ccxFrdReader")