From 7b225789da20ee810a93470e09d486c91f7dd610 Mon Sep 17 00:00:00 2001 From: wmayer Date: Mon, 13 Jun 2022 16:46:15 +0200 Subject: [PATCH] Fem: do not access the active document but the document the FEM post object is part of --- src/Mod/Fem/Gui/TaskPostBoxes.cpp | 125 ++++++++++++++++++++---------- src/Mod/Fem/Gui/TaskPostBoxes.h | 5 ++ 2 files changed, 87 insertions(+), 43 deletions(-) diff --git a/src/Mod/Fem/Gui/TaskPostBoxes.cpp b/src/Mod/Fem/Gui/TaskPostBoxes.cpp index fc2e2fcbe0..78bb344be2 100644 --- a/src/Mod/Fem/Gui/TaskPostBoxes.cpp +++ b/src/Mod/Fem/Gui/TaskPostBoxes.cpp @@ -40,7 +40,7 @@ #include #include #include -#include +#include #include #include #include @@ -253,7 +253,7 @@ void TaskDlgPost::open() void TaskDlgPost::clicked(int button) { if (button == QDialogButtonBox::Apply) - getView()->getObject()->getDocument()->recompute(); + recompute(); } bool TaskDlgPost::accept() @@ -268,7 +268,7 @@ bool TaskDlgPost::accept() return false; } - Gui::Command::doCommand(Gui::Command::Gui, "Gui.activeDocument().resetEdit()"); + Gui::cmdGuiDocument(getDocumentName(), "resetEdit()"); return true; } @@ -276,11 +276,19 @@ bool TaskDlgPost::reject() { // roll back the done things Gui::Command::abortCommand(); - Gui::Command::doCommand(Gui::Command::Gui, "Gui.activeDocument().resetEdit()"); + Gui::cmdGuiDocument(getDocumentName(), "resetEdit()"); return true; } +void TaskDlgPost::recompute() +{ + Gui::ViewProviderDocumentObject* vp = getView(); + if (vp) { + vp->getObject()->getDocument()->recompute(); + } +} + void TaskDlgPost::modifyStandardButtons(QDialogButtonBox* box) { if (box->button(QDialogButtonBox::Apply)) @@ -305,10 +313,18 @@ bool TaskPostBox::autoApply() { return FemSettings().getPostAutoRecompute(); } +App::Document* TaskPostBox::getDocument() const { + App::DocumentObject* obj = getObject(); + return (obj ? obj->getDocument() : nullptr); +} + void TaskPostBox::recompute() { - if (autoApply()) - App::GetApplication().getActiveDocument()->recompute(); + if (autoApply()) { + App::Document* doc = getDocument(); + if (doc) + doc->recompute(); + } } void TaskPostBox::updateEnumerationList(App::PropertyEnumeration& prop, QComboBox* box) { @@ -460,7 +476,7 @@ void TaskPostClip::applyPythonCode() { void TaskPostClip::collectImplicitFunctions() { std::vector pipelines; - pipelines = App::GetApplication().getActiveDocument()->getObjectsOfType(); + pipelines = getDocument()->getObjectsOfType(); if (!pipelines.empty()) { Fem::FemPostPipeline* pipeline = pipelines.front(); if (pipeline->Functions.getValue() && @@ -505,7 +521,7 @@ void TaskPostClip::on_FunctionBox_currentIndexChanged(int idx) { //set the correct property std::vector pipelines; - pipelines = App::GetApplication().getActiveDocument()->getObjectsOfType(); + pipelines = getDocument()->getObjectsOfType(); if (!pipelines.empty()) { Fem::FemPostPipeline* pipeline = pipelines.front(); if (pipeline->Functions.getValue() && @@ -525,7 +541,7 @@ void TaskPostClip::on_FunctionBox_currentIndexChanged(int idx) { static_cast(getObject())->Function.getValue()); Gui::ViewProvider* view = nullptr; if (fobj) - view = Gui::Application::Instance->activeDocument()->getViewProvider(fobj); + view = Gui::Application::Instance->getViewProvider(fobj); if (fwidget) fwidget->deleteLater(); @@ -655,7 +671,7 @@ static const char* cursor_triangle[] = { void TaskPostDataAlongLine::on_SelectPoints_clicked() { Gui::Command::doCommand(Gui::Command::Doc, ObjectVisible().c_str()); - Gui::Document* doc = Gui::Application::Instance->activeDocument(); + Gui::Document* doc = Gui::Application::Instance->getDocument(getDocument()); Gui::View3DInventor* view = static_cast(doc->getActiveView()); if (view) { Gui::View3DInventorViewer* viewer = view->getViewer(); @@ -722,32 +738,46 @@ void TaskPostDataAlongLine::onChange(double x1, double y1, double z1, double x2, void TaskPostDataAlongLine::point1Changed(double) { - std::string ObjName = getObject()->Label.getValue(); - Gui::Command::doCommand(Gui::Command::Doc, "App.ActiveDocument.%s.Point1 = App.Vector(%f, %f, %f)", ObjName.c_str(), - ui->point1X->value().getValue(), ui->point1Y->value().getValue(), ui->point1Z->value().getValue()); + try { + std::string ObjName = getObject()->Label.getValue(); + Gui::cmdAppDocumentArgs(getDocument(), "%s.Point1 = App.Vector(%f, %f, %f)", ObjName, + ui->point1X->value().getValue(), + ui->point1Y->value().getValue(), + ui->point1Z->value().getValue()); - // recompute the feature to fill all fields with data at this point - getObject()->recomputeFeature(); - // refresh the color bar range - auto currentField = getTypedView()->Field.getValue(); - getTypedView()->Field.setValue(currentField); - // also the axis data must be refreshed to get correct plots - static_cast(getObject())->GetAxisData(); + // recompute the feature to fill all fields with data at this point + getObject()->recomputeFeature(); + // refresh the color bar range + auto currentField = getTypedView()->Field.getValue(); + getTypedView()->Field.setValue(currentField); + // also the axis data must be refreshed to get correct plots + static_cast(getObject())->GetAxisData(); + } + catch (const Base::Exception& e) { + e.ReportException(); + } } void TaskPostDataAlongLine::point2Changed(double) { - std::string ObjName = getObject()->Label.getValue(); - Gui::Command::doCommand(Gui::Command::Doc, "App.ActiveDocument.%s.Point2 = App.Vector(%f, %f, %f)", ObjName.c_str(), - ui->point2X->value().getValue(), ui->point2Y->value().getValue(), ui->point2Z->value().getValue()); + try { + std::string ObjName = getObject()->Label.getValue(); + Gui::cmdAppDocumentArgs(getDocument(), "%s.Point2 = App.Vector(%f, %f, %f)", ObjName, + ui->point2X->value().getValue(), + ui->point2Y->value().getValue(), + ui->point2Z->value().getValue()); - // recompute the feature to fill all fields with data at this point - getObject()->recomputeFeature(); - // refresh the color bar range - auto currentField = getTypedView()->Field.getValue(); - getTypedView()->Field.setValue(currentField); - // also the axis data must be refreshed to get correct plots - static_cast(getObject())->GetAxisData(); + // recompute the feature to fill all fields with data at this point + getObject()->recomputeFeature(); + // refresh the color bar range + auto currentField = getTypedView()->Field.getValue(); + getTypedView()->Field.setValue(currentField); + // also the axis data must be refreshed to get correct plots + static_cast(getObject())->GetAxisData(); + } + catch (const Base::Exception& e) { + e.what(); + } } void TaskPostDataAlongLine::resolutionChanged(int val) { @@ -887,7 +917,9 @@ TaskPostDataAtPoint::TaskPostDataAtPoint(ViewProviderDocumentObject* view, QWidg } TaskPostDataAtPoint::~TaskPostDataAtPoint() { - App::GetApplication().getActiveDocument()->recompute(); + App::Document* doc = getDocument(); + if (doc) + doc->recompute(); } void TaskPostDataAtPoint::applyPythonCode() { @@ -920,7 +952,7 @@ static const char* cursor_star[] = { void TaskPostDataAtPoint::on_SelectPoint_clicked() { Gui::Command::doCommand(Gui::Command::Doc, ObjectVisible().c_str()); - Gui::Document* doc = Gui::Application::Instance->activeDocument(); + Gui::Document* doc = Gui::Application::Instance->getDocument(getDocument()); Gui::View3DInventor* view = static_cast(doc->getActiveView()); if (view) { Gui::View3DInventorViewer* viewer = view->getViewer(); @@ -966,15 +998,22 @@ void TaskPostDataAtPoint::onChange(double x, double y, double z) { void TaskPostDataAtPoint::centerChanged(double) { - std::string ObjName = getObject()->Label.getValue(); - Gui::Command::doCommand(Gui::Command::Doc, "App.ActiveDocument.%s.Center = App.Vector(%f, %f, %f)", ObjName.c_str(), - ui->centerX->value().getValue(), ui->centerY->value().getValue(), ui->centerZ->value().getValue()); + try { + std::string ObjName = getObject()->Label.getValue(); + Gui::cmdAppDocumentArgs(getDocument(), "%s.Center = App.Vector(%f, %f, %f)", ObjName, + ui->centerX->value().getValue(), + ui->centerY->value().getValue(), + ui->centerZ->value().getValue()); - // recompute the feature to fill all fields with data at this point - getObject()->recomputeFeature(); - // show the data dialog by calling on_Field_activated with the field that is currently set - auto currentField = getTypedView()->Field.getValue(); - on_Field_activated(currentField); + // recompute the feature to fill all fields with data at this point + getObject()->recomputeFeature(); + // show the data dialog by calling on_Field_activated with the field that is currently set + auto currentField = getTypedView()->Field.getValue(); + on_Field_activated(currentField); + } + catch (const Base::Exception& e) { + e.ReportException(); + } } void TaskPostDataAtPoint::pointCallback(void* ud, SoEventCallback* n) @@ -1386,7 +1425,7 @@ void TaskPostCut::applyPythonCode() { void TaskPostCut::collectImplicitFunctions() { std::vector pipelines; - pipelines = App::GetApplication().getActiveDocument()->getObjectsOfType(); + pipelines = getDocument()->getObjectsOfType(); if (!pipelines.empty()) { Fem::FemPostPipeline* pipeline = pipelines.front(); if (pipeline->Functions.getValue() && @@ -1431,7 +1470,7 @@ void TaskPostCut::on_FunctionBox_currentIndexChanged(int idx) { //set the correct property std::vector pipelines; - pipelines = App::GetApplication().getActiveDocument()->getObjectsOfType(); + pipelines = getDocument()->getObjectsOfType(); if (!pipelines.empty()) { Fem::FemPostPipeline* pipeline = pipelines.front(); if (pipeline->Functions.getValue() && @@ -1451,7 +1490,7 @@ void TaskPostCut::on_FunctionBox_currentIndexChanged(int idx) { static_cast(getObject())->Function.getValue()); Gui::ViewProvider* view = nullptr; if (fobj) - view = Gui::Application::Instance->activeDocument()->getViewProvider(fobj); + view = Gui::Application::Instance->getViewProvider(fobj); if (fwidget) fwidget->deleteLater(); diff --git a/src/Mod/Fem/Gui/TaskPostBoxes.h b/src/Mod/Fem/Gui/TaskPostBoxes.h index 34e2e948c4..6bc7fc8657 100644 --- a/src/Mod/Fem/Gui/TaskPostBoxes.h +++ b/src/Mod/Fem/Gui/TaskPostBoxes.h @@ -160,6 +160,8 @@ protected: return m_view.get(); } + App::Document* getDocument() const; + bool autoApply(); void recompute(); @@ -202,6 +204,9 @@ public: /// returns for Close and Help button virtual QDialogButtonBox::StandardButtons getStandardButtons(void) const; +protected: + void recompute(); + protected: Gui::ViewProviderWeakPtrT m_view; std::vector m_boxes;