From 6d376dc77aa0c3d4a678cec7d2e19c0ce3dec145 Mon Sep 17 00:00:00 2001 From: marioalexis Date: Mon, 24 Mar 2025 01:23:15 -0300 Subject: [PATCH] Fem: Add method to rename pipeline VTK data arrays --- src/Mod/Fem/App/FemPostPipeline.cpp | 35 ++++++++++++++++++++++-- src/Mod/Fem/App/FemPostPipeline.h | 1 + src/Mod/Fem/App/FemPostPipelinePy.xml | 5 ++++ src/Mod/Fem/App/FemPostPipelinePyImp.cpp | 22 +++++++++++++++ 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/Mod/Fem/App/FemPostPipeline.cpp b/src/Mod/Fem/App/FemPostPipeline.cpp index 4501fca2ca..3af7c03132 100644 --- a/src/Mod/Fem/App/FemPostPipeline.cpp +++ b/src/Mod/Fem/App/FemPostPipeline.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -46,8 +47,6 @@ #endif #include -#include -#include #include "FemMesh.h" #include "FemMeshObject.h" @@ -737,6 +736,38 @@ void FemPostPipeline::onDocumentRestored() } } +void FemPostPipeline::renameArrays(const std::map& names) +{ + std::vector> fields; + auto data = Data.getValue(); + if (!data) { + return; + } + + if (auto dataSet = vtkDataSet::SafeDownCast(data)) { + fields.emplace_back(dataSet); + } + else if (auto blocks = vtkMultiBlockDataSet::SafeDownCast(data)) { + for (unsigned int i = 0; i < blocks->GetNumberOfBlocks(); ++i) { + if (auto dataSet = vtkDataSet::SafeDownCast(blocks->GetBlock(i))) { + fields.emplace_back(dataSet); + } + } + } + + for (auto f : fields) { + auto pointData = f->GetPointData(); + for (const auto& name : names) { + auto array = pointData->GetAbstractArray(name.first.c_str()); + if (array) { + array->SetName(name.second.c_str()); + } + } + } + + Data.touch(); +} + PyObject* FemPostPipeline::getPyObject() { if (PythonObject.is(Py::_None())) { diff --git a/src/Mod/Fem/App/FemPostPipeline.h b/src/Mod/Fem/App/FemPostPipeline.h index c56d37ad01..15d9149705 100644 --- a/src/Mod/Fem/App/FemPostPipeline.h +++ b/src/Mod/Fem/App/FemPostPipeline.h @@ -98,6 +98,7 @@ public: Base::Unit unit, std::string& frame_type); void scale(double s); + void renameArrays(const std::map& names); // load from results void load(FemResultObject* res); diff --git a/src/Mod/Fem/App/FemPostPipelinePy.xml b/src/Mod/Fem/App/FemPostPipelinePy.xml index 9385b805c3..c71981393b 100644 --- a/src/Mod/Fem/App/FemPostPipelinePy.xml +++ b/src/Mod/Fem/App/FemPostPipelinePy.xml @@ -66,5 +66,10 @@ Load a single result object or create a multiframe result by loading multiple re Check if this pipeline holds a given post-processing object + + + Change name of data arrays + + diff --git a/src/Mod/Fem/App/FemPostPipelinePyImp.cpp b/src/Mod/Fem/App/FemPostPipelinePyImp.cpp index f2f7d14bfb..9de89e34cb 100644 --- a/src/Mod/Fem/App/FemPostPipelinePyImp.cpp +++ b/src/Mod/Fem/App/FemPostPipelinePyImp.cpp @@ -285,6 +285,28 @@ PyObject* FemPostPipelinePy::holdsPostObject(PyObject* args) return Py_BuildValue("O", (ok ? Py_True : Py_False)); } +PyObject* FemPostPipelinePy::renameArrays(PyObject* args) +{ + PyObject* pyObj; + if (!PyArg_ParseTuple(args, "O!", &(PyDict_Type), &pyObj)) { + return nullptr; + } + + Py::Dict pyNames {pyObj}; + std::map names {}; + for (auto&& [key, value] : pyNames) { + if (!key.isString() || !value.isString()) { + PyErr_SetString(PyExc_TypeError, "Names must be string objects"); + return nullptr; + } + names.emplace(key.as_string(), static_cast(value).as_string()); + } + + getFemPostPipelinePtr()->renameArrays(names); + + Py_Return; +} + PyObject* FemPostPipelinePy::getCustomAttributes(const char* /*attr*/) const { return nullptr;