From d576ee027f37c7e09675e76c4b4f0461b9f68998 Mon Sep 17 00:00:00 2001 From: marioalexis Date: Fri, 7 Mar 2025 14:30:54 -0300 Subject: [PATCH] Fem: Improve magnetization --- src/Mod/Fem/Gui/Resources/ui/Magnetization.ui | 599 ++++++------------ .../equation_magnetodynamics_elmer.py | 2 +- .../equation_magnetostatics_2D_elmer.py | 4 +- .../femobjects/constraint_magnetization.py | 226 ++++--- .../equations/magnetodynamic2D_writer.py | 10 +- .../elmer/equations/magnetodynamic_writer.py | 14 +- .../task_constraint_magnetization.py | 217 ++++--- 7 files changed, 476 insertions(+), 596 deletions(-) diff --git a/src/Mod/Fem/Gui/Resources/ui/Magnetization.ui b/src/Mod/Fem/Gui/Resources/ui/Magnetization.ui index 4569891b8d..86aa5b930e 100644 --- a/src/Mod/Fem/Gui/Resources/ui/Magnetization.ui +++ b/src/Mod/Fem/Gui/Resources/ui/Magnetization.ui @@ -13,326 +13,197 @@ Analysis feature properties - - - - - Magnetization + + + + + - - - - true - - Real - - - - - - - true - - - Imaginary - - - - - - - true - - - x - - - - - - - true - - - - 100 - 20 - - - Real part of potential x-component - - - true - - - - - - -1000000000000000000000.000000000000000 - - - 1000000000000000000000.000000000000000 - - - 1.000000000000000 - - - 1.000000000000000 - - - - - - - unspecified - - - - - - - - - - true - - - - 100 - 20 - - - - Imaginary part of potential x-component - - - true - - - - - - -1000000000000000000000.000000000000000 - - - 1000000000000000000000.000000000000000 - - - 1.000000000000000 - - - 1.000000000000000 - - - - - - - unspecified - - - - - - - - - - true - - - y - - - - - - - true - - - - 100 - 20 - - - - Real part of potential y-component - - - true - - - - - - -1000000000000000000000.000000000000000 - - - 1000000000000000000000.000000000000000 - - - 1.000000000000000 - - - 1.000000000000000 - - - - - - - unspecified - - - - - - - - - - true - - - - 100 - 20 - - - - Imaginary part of potential y-component - - - true - - - - - - -1000000000000000000000.000000000000000 - - - 1000000000000000000000.000000000000000 - - - 1.000000000000000 - - - 1.000000000000000 - - - - - - - unspecified - - - - - - - - - - true - - - z - - - - - - - true - - - - 100 - 20 - - - - Real part of potential z-component - - - true - - - - - - -1000000000000000000000.000000000000000 - - - 1000000000000000000000.000000000000000 - - - 1.000000000000000 - - - 1.000000000000000 - - - - - - - unspecified - - - - - - - - - - true - - - - 100 - 20 - - - - Imaginary part of potential z-component - - - true - - - - - - -1000000000000000000000.000000000000000 - - - 1000000000000000000000.000000000000000 - - - 1.000000000000000 - - - 1.000000000000000 - - - - - - - unspecified - - - - + Imaginary part is only used for equations +with harmonic/oscillating driving current + + + false + + + + + + true + + + Real + + + + + + + true + + + Imaginary + + + + + + + x + + + + + + + true + + + Real part of magnetization x-component + + + true + + + A/m + + + 1.000000000000000 + + + 0.000000000000000 + + + + + + + true + + + Imaginary part of magnetization x-component + + + true + + + A/m + + + 1.000000000000000 + + + 0.000000000000000 + + + + + + + y + + + + + + + true + + + Real part of magnetization y-component + + + true + + + A/m + + + 1.000000000000000 + + + 0.000000000000000 + + + + + + + true + + + Imaginary part of magnetization y-component + + + true + + + A/m + + + 1.000000000000000 + + + 0.000000000000000 + + + + + + + z + + + + + + + true + + + Real part of magnetization z-component + + + true + + + A/m + + + 1.000000000000000 + + + 0.000000000000000 + + + + + + + true + + + Imaginary part of magnetization z-component + + + true + + + A/m + + + 1.000000000000000 + + + 0.000000000000000 + + + + @@ -345,102 +216,4 @@ - - - reXunspecBox - toggled(bool) - realXQSB - setDisabled(bool) - - - 152 - 61 - - - 80 - 61 - - - - - reYunspecBox - toggled(bool) - realYQSB - setDisabled(bool) - - - 152 - 87 - - - 80 - 87 - - - - - imZunspecBox - toggled(bool) - imagZQSB - setDisabled(bool) - - - 297 - 113 - - - 224 - 113 - - - - - imYunspecBox - toggled(bool) - imagYQSB - setDisabled(bool) - - - 297 - 87 - - - 224 - 87 - - - - - imXunspecBox - toggled(bool) - imagXQSB - setDisabled(bool) - - - 297 - 61 - - - 224 - 61 - - - - - reZunspecBox - toggled(bool) - realZQSB - setDisabled(bool) - - - 152 - 113 - - - 80 - 113 - - - - diff --git a/src/Mod/Fem/femexamples/equation_magnetodynamics_elmer.py b/src/Mod/Fem/femexamples/equation_magnetodynamics_elmer.py index bd5778f875..fe5c592696 100644 --- a/src/Mod/Fem/femexamples/equation_magnetodynamics_elmer.py +++ b/src/Mod/Fem/femexamples/equation_magnetodynamics_elmer.py @@ -194,7 +194,7 @@ def setup(doc=None, solvertype="elmer"): Magnetization.Magnetization_re_1 = "7500.000 A/m" Magnetization.Magnetization_re_2 = "7500.000 A/m" Magnetization.Magnetization_re_3 = "7500.000 A/m" - Magnetization.Magnetization_re_2_Disabled = False + Magnetization.EnableMagnetization_2 = True analysis.addObject(Magnetization) # mesh diff --git a/src/Mod/Fem/femexamples/equation_magnetostatics_2D_elmer.py b/src/Mod/Fem/femexamples/equation_magnetostatics_2D_elmer.py index 15f3f41758..b9d4aeda90 100644 --- a/src/Mod/Fem/femexamples/equation_magnetostatics_2D_elmer.py +++ b/src/Mod/Fem/femexamples/equation_magnetostatics_2D_elmer.py @@ -248,14 +248,14 @@ def setup(doc=None, solvertype="elmer"): Magnetization_lower = ObjectsFem.makeConstraintMagnetization(doc, "Magnetization_Lower_End") Magnetization_lower.References = [(BooleanFragments, "Face1")] Magnetization_lower.Magnetization_re_1 = "-7500.0 A/m" - Magnetization_lower.Magnetization_re_1_Disabled = False + Magnetization_lower.EnableMagnetization_1 = True analysis.addObject(Magnetization_lower) # magnetization upper Magnetization_upper = ObjectsFem.makeConstraintMagnetization(doc, "Magnetization_Upper_End") Magnetization_upper.References = [(BooleanFragments, "Face2")] Magnetization_upper.Magnetization_re_1 = "7500.0 A/m" - Magnetization_upper.Magnetization_re_1_Disabled = False + Magnetization_upper.EnableMagnetization_1 = True analysis.addObject(Magnetization_upper) # mesh diff --git a/src/Mod/Fem/femobjects/constraint_magnetization.py b/src/Mod/Fem/femobjects/constraint_magnetization.py index d978a343d2..f36aef8ebf 100644 --- a/src/Mod/Fem/femobjects/constraint_magnetization.py +++ b/src/Mod/Fem/femobjects/constraint_magnetization.py @@ -1,5 +1,6 @@ # *************************************************************************** # * Copyright (c) 2023 Uwe Stöhr * +# * Copyright (c) 2025 Mario Passaglia * # * * # * This file is part of the FreeCAD CAx development system. * # * * @@ -22,15 +23,19 @@ # *************************************************************************** __title__ = "FreeCAD FEM constraint magnetization document object" -__author__ = "Uwe Stöhr" +__author__ = "Uwe Stöhr, Mario Passaglia" __url__ = "https://www.freecad.org" ## @package constraint_magnetization # \ingroup FEM # \brief constraint magnetization source object +from FreeCAD import Base + from . import base_fempythonobject +_PropHelper = base_fempythonobject._PropHelper + class ConstraintMagnetization(base_fempythonobject.BaseFemPythonObject): @@ -38,101 +43,132 @@ class ConstraintMagnetization(base_fempythonobject.BaseFemPythonObject): def __init__(self, obj): super().__init__(obj) - self.add_properties(obj) + + for prop in self._get_properties(): + prop.add_to_object(obj) + + def _get_properties(self): + prop = [] + + prop.append( + _PropHelper( + type="App::PropertyMagnetization", + name="Magnetization_re_1", + group="Magnetization Vector", + doc="Real part of magnetization x-component", + value="0 A/m", + ) + ) + prop.append( + _PropHelper( + type="App::PropertyMagnetization", + name="Magnetization_re_2", + group="Magnetization Vector", + doc="Real part of magnetization y-component", + value="0 A/m", + ) + ) + prop.append( + _PropHelper( + type="App::PropertyMagnetization", + name="Magnetization_re_3", + group="Magnetization Vector", + doc="Real part of magnetization z-component", + value="0 A/m", + ) + ) + prop.append( + _PropHelper( + type="App::PropertyMagnetization", + name="Magnetization_im_1", + group="Magnetization Vector", + doc="Imaginary part of magnetization x-component", + value="0 A/m", + ) + ) + prop.append( + _PropHelper( + type="App::PropertyMagnetization", + name="Magnetization_im_2", + group="Magnetization Vector", + doc="Imaginary part of magnetization y-component", + value="0 A/m", + ) + ) + prop.append( + _PropHelper( + type="App::PropertyMagnetization", + name="Magnetization_im_3", + group="Magnetization Vector", + doc="Imaginary part of magnetization z-component", + value="0 A/m", + ) + ) + prop.append( + _PropHelper( + type="App::PropertyBool", + name="EnableMagnetization_1", + group="Magnetization Vector", + doc="Enable magnetization x component", + value=False, + ) + ) + prop.append( + _PropHelper( + type="App::PropertyBool", + name="EnableMagnetization_2", + group="Magnetization Vector", + doc="Enable magnetization y component", + value=False, + ) + ) + prop.append( + _PropHelper( + type="App::PropertyBool", + name="EnableMagnetization_3", + group="Magnetization Vector", + doc="Enable magnetization z component", + value=False, + ) + ) + + return prop def onDocumentRestored(self, obj): - self.add_properties(obj) + # update old project with new properties + for prop in self._get_properties(): + try: + obj.getPropertyByName(prop.name) + except Base.PropertyError: + prop.add_to_object(obj) - def add_properties(self, obj): - if not hasattr(obj, "Magnetization_re_1"): - obj.addProperty( - "App::PropertyMagnetization", - "Magnetization_re_1", - "Vector Potential", - "Real part of magnetization x-component", - ) - obj.setPropertyStatus("Magnetization_re_1", "LockDynamic") - obj.Magnetization_re_1 = "0 A/m" - if not hasattr(obj, "Magnetization_re_2"): - obj.addProperty( - "App::PropertyMagnetization", - "Magnetization_re_2", - "Vector Potential", - "Real part of magnetization y-component", - ) - obj.setPropertyStatus("Magnetization_re_2", "LockDynamic") - obj.Magnetization_re_2 = "0 A/m" - if not hasattr(obj, "Magnetization_re_3"): - obj.addProperty( - "App::PropertyMagnetization", - "Magnetization_re_3", - "Vector Potential", - "Real part of magnetization z-component", - ) - obj.setPropertyStatus("Magnetization_re_3", "LockDynamic") - obj.Magnetization_re_3 = "0 A/m" - if not hasattr(obj, "Magnetization_im_1"): - obj.addProperty( - "App::PropertyMagnetization", - "Magnetization_im_1", - "Vector Potential", - "Imaginary part of magnetization x-component", - ) - obj.setPropertyStatus("Magnetization_im_1", "LockDynamic") - obj.Magnetization_im_1 = "0 A/m" - if not hasattr(obj, "Magnetization_im_2"): - obj.addProperty( - "App::PropertyMagnetization", - "Magnetization_im_2", - "Vector Potential", - "Imaginary part of magnetization y-component", - ) - obj.setPropertyStatus("Magnetization_im_2", "LockDynamic") - obj.Magnetization_im_2 = "0 A/m" - if not hasattr(obj, "Magnetization_im_3"): - obj.addProperty( - "App::PropertyMagnetization", - "Magnetization_im_3", - "Vector Potential", - "Imaginary part of magnetization z-component", - ) - obj.setPropertyStatus("Magnetization_im_3", "LockDynamic") - obj.Magnetization_im_3 = "0 A/m" + # enable current density properties from old properties + try: + obj.EnableMagnetization_1 = not obj.getPropertyByName( + "Magnetization_re_1_Disabled" + ) or not obj.getPropertyByName("Magnetization_im_1_Disabled") - # now the enable bools - if not hasattr(obj, "Magnetization_re_1_Disabled"): - obj.addProperty( - "App::PropertyBool", "Magnetization_re_1_Disabled", "Vector Potential", "" - ) - obj.setPropertyStatus("Magnetization_re_1_Disabled", "LockDynamic") - obj.Magnetization_re_1_Disabled = True - if not hasattr(obj, "Magnetization_re_2_Disabled"): - obj.addProperty( - "App::PropertyBool", "Magnetization_re_2_Disabled", "Vector Potential", "" - ) - obj.setPropertyStatus("Magnetization_re_2_Disabled", "LockDynamic") - obj.Magnetization_re_2_Disabled = True - if not hasattr(obj, "Magnetization_re_3_Disabled"): - obj.addProperty( - "App::PropertyBool", "Magnetization_re_3_Disabled", "Vector Potential", "" - ) - obj.setPropertyStatus("Magnetization_re_3_Disabled", "LockDynamic") - obj.Magnetization_re_3_Disabled = True - if not hasattr(obj, "Magnetization_im_1_Disabled"): - obj.addProperty( - "App::PropertyBool", "Magnetization_im_1_Disabled", "Vector Potential", "" - ) - obj.setPropertyStatus("Magnetization_im_1_Disabled", "LockDynamic") - obj.Magnetization_im_1_Disabled = True - if not hasattr(obj, "Magnetization_im_2_Disabled"): - obj.addProperty( - "App::PropertyBool", "Magnetization_im_2_Disabled", "Vector Potential", "" - ) - obj.setPropertyStatus("Magnetization_im_2_Disabled", "LockDynamic") - obj.Magnetization_im_2_Disabled = True - if not hasattr(obj, "Magnetization_im_3_Disabled"): - obj.addProperty( - "App::PropertyBool", "Magnetization_im_3_Disabled", "Vector Potential", "" - ) - obj.setPropertyStatus("Magnetization_im_3_Disabled", "LockDynamic") - obj.Magnetization_im_3_Disabled = True + obj.EnableMagnetization_2 = not obj.getPropertyByName( + "Magnetization_re_2_Disabled" + ) or not obj.getPropertyByName("Magnetization_im_2_Disabled") + + obj.EnableMagnetization_3 = not obj.getPropertyByName( + "Magnetization_re_3_Disabled" + ) or not obj.getPropertyByName("Magnetization_im_3_Disabled") + + # remove old properties + obj.setPropertyStatus("Magnetization_re_1_Disabled", "-LockDynamic") + obj.removeProperty("Magnetization_re_1_Disabled") + obj.setPropertyStatus("Magnetization_re_2_Disabled", "-LockDynamic") + obj.removeProperty("Magnetization_re_2_Disabled") + obj.setPropertyStatus("Magnetization_re_3_Disabled", "-LockDynamic") + obj.removeProperty("Magnetization_re_3_Disabled") + obj.setPropertyStatus("Magnetization_im_1_Disabled", "-LockDynamic") + obj.removeProperty("Magnetization_im_1_Disabled") + obj.setPropertyStatus("Magnetization_im_2_Disabled", "-LockDynamic") + obj.removeProperty("Magnetization_im_2_Disabled") + obj.setPropertyStatus("Magnetization_im_3_Disabled", "-LockDynamic") + obj.removeProperty("Magnetization_im_3_Disabled") + + except Base.PropertyError: + pass diff --git a/src/Mod/Fem/femsolver/elmer/equations/magnetodynamic2D_writer.py b/src/Mod/Fem/femsolver/elmer/equations/magnetodynamic2D_writer.py index 6bc835060b..3212de75c5 100644 --- a/src/Mod/Fem/femsolver/elmer/equations/magnetodynamic2D_writer.py +++ b/src/Mod/Fem/femsolver/elmer/equations/magnetodynamic2D_writer.py @@ -144,20 +144,20 @@ class MgDyn2Dwriter: currentDensity = obj.NormalCurrentDensity_im.getValueAs("A/m^2").Value self.write.bodyForce(name, "Current Density Im", round(currentDensity, 6)) - if hasattr(obj, "Magnetization_re_1"): + if femutils.is_derived_from(obj, "Fem::ConstraintMagnetization"): # output only if magnetization is enabled and needed - if not obj.Magnetization_re_1_Disabled: + if obj.EnableMagnetization_1: magnetization = float(obj.Magnetization_re_1.getValueAs("A/m")) self.write.material(name, "Magnetization 1", round(magnetization, 6)) - if not obj.Magnetization_re_2_Disabled: + if obj.EnableMagnetization_2: magnetization = float(obj.Magnetization_re_2.getValueAs("A/m")) self.write.material(name, "Magnetization 2", round(magnetization, 6)) # imaginaries are only needed for harmonic equation if equation.IsHarmonic: - if not obj.Magnetization_im_1_Disabled: + if obj.EnableMagnetization_1: magnetization = float(obj.Magnetization_im_1.getValueAs("A/m")) self.write.material(name, "Magnetization Im 1", round(magnetization, 6)) - if not obj.Magnetization_im_2_Disabled: + if obj.EnableMagnetization_2: magnetization = float(obj.Magnetization_im_2.getValueAs("A/m")) self.write.material(name, "Magnetization Im 2", round(magnetization, 6)) diff --git a/src/Mod/Fem/femsolver/elmer/equations/magnetodynamic_writer.py b/src/Mod/Fem/femsolver/elmer/equations/magnetodynamic_writer.py index 479cdfcfb9..39d1b9e4eb 100644 --- a/src/Mod/Fem/femsolver/elmer/equations/magnetodynamic_writer.py +++ b/src/Mod/Fem/femsolver/elmer/equations/magnetodynamic_writer.py @@ -185,26 +185,26 @@ class MgDynwriter: currentDensity = float(obj.CurrentDensity_im_3.getValueAs("A/m^2")) self.write.bodyForce(name, "Current Density Im 3", round(currentDensity, 6)) - if hasattr(obj, "Magnetization_im_1"): + if femutils.is_derived_from(obj, "Fem::ConstraintMagnetization"): # output only if magnetization is enabled and needed - if not obj.Magnetization_re_1_Disabled: + if obj.EnableMagnetization_1: magnetization = float(obj.Magnetization_re_1.getValueAs("A/m")) self.write.bodyForce(name, "Magnetization 1", magnetization) - if not obj.Magnetization_re_2_Disabled: + if obj.EnableMagnetization_2: magnetization = float(obj.Magnetization_re_2.getValueAs("A/m")) self.write.bodyForce(name, "Magnetization 2", magnetization) - if not obj.Magnetization_re_3_Disabled: + if obj.EnableMagnetization_3: magnetization = float(obj.Magnetization_re_3.getValueAs("A/m")) self.write.bodyForce(name, "Magnetization 3", magnetization) # imaginaries are only needed for harmonic equation if equation.IsHarmonic: - if not obj.Magnetization_im_1_Disabled: + if obj.EnableMagnetization_1: magnetization = float(obj.Magnetization_im_1.getValueAs("A/m")) self.write.bodyForce(name, "Magnetization Im 1", magnetization) - if not obj.Magnetization_im_2_Disabled: + if obj.EnableMagnetization_2: magnetization = float(obj.Magnetization_im_2.getValueAs("A/m")) self.write.bodyForce(name, "Magnetization Im 2", magnetization) - if not obj.Magnetization_im_3_Disabled: + if obj.EnableMagnetization_3: magnetization = float(obj.Magnetization_im_3.getValueAs("A/m")) self.write.bodyForce(name, "Magnetization Im 3", magnetization) diff --git a/src/Mod/Fem/femtaskpanels/task_constraint_magnetization.py b/src/Mod/Fem/femtaskpanels/task_constraint_magnetization.py index ce80aa23be..e14f6cd499 100644 --- a/src/Mod/Fem/femtaskpanels/task_constraint_magnetization.py +++ b/src/Mod/Fem/femtaskpanels/task_constraint_magnetization.py @@ -1,5 +1,6 @@ # *************************************************************************** # * Copyright (c) 2023 Uwe Stöhr * +# * Copyright (c) 2025 Mario Passaglia * # * * # * This file is part of the FreeCAD CAx development system. * # * * @@ -22,13 +23,15 @@ # *************************************************************************** __title__ = "FreeCAD FEM constraint magnetization task panel for the document object" -__author__ = "Uwe Stöhr" +__author__ = "Uwe Stöhr, Mario Passaglia" __url__ = "https://www.freecad.org" ## @package task_constraint_magnetization # \ingroup FEM # \brief task panel for constraint magnetization object +from PySide import QtCore + import FreeCAD import FreeCADGui @@ -43,19 +46,18 @@ class _TaskPanel(base_femtaskpanel._BaseTaskPanel): def __init__(self, obj): super().__init__(obj) - self._paramWidget = FreeCADGui.PySideUic.loadUi( + self.parameter_widget = FreeCADGui.PySideUic.loadUi( FreeCAD.getHomePath() + "Mod/Fem/Resources/ui/Magnetization.ui" ) - self._initParamWidget() # geometry selection widget # magnetization is always a body force for 3D, therefore only allow solid - self._selectionWidget = selection_widgets.GeometryElementsSelection( + self._selection_widget = selection_widgets.GeometryElementsSelection( obj.References, ["Solid", "Face"], True, False ) # form made from param and selection widget - self.form = [self._paramWidget, self._selectionWidget] + self.form = [self.parameter_widget, self._selection_widget] analysis = obj.getParentGroup() self._mesh = None @@ -67,6 +69,54 @@ class _TaskPanel(base_femtaskpanel._BaseTaskPanel): self._partVisible = None self._meshVisible = None + QtCore.QObject.connect( + self.parameter_widget.ckb_magnetization_1, + QtCore.SIGNAL("toggled(bool)"), + self.magnetization_1_enabled_changed, + ) + QtCore.QObject.connect( + self.parameter_widget.qsb_magnetization_re_1, + QtCore.SIGNAL("valueChanged(Base::Quantity)"), + self.magnetization_re_1_changed, + ) + QtCore.QObject.connect( + self.parameter_widget.ckb_magnetization_2, + QtCore.SIGNAL("toggled(bool)"), + self.magnetization_2_enabled_changed, + ) + QtCore.QObject.connect( + self.parameter_widget.qsb_magnetization_re_2, + QtCore.SIGNAL("valueChanged(Base::Quantity)"), + self.magnetization_re_2_changed, + ) + QtCore.QObject.connect( + self.parameter_widget.ckb_magnetization_3, + QtCore.SIGNAL("toggled(bool)"), + self.magnetization_3_enabled_changed, + ) + QtCore.QObject.connect( + self.parameter_widget.qsb_magnetization_re_3, + QtCore.SIGNAL("valueChanged(Base::Quantity)"), + self.magnetization_re_3_changed, + ) + QtCore.QObject.connect( + self.parameter_widget.qsb_magnetization_im_1, + QtCore.SIGNAL("valueChanged(Base::Quantity)"), + self.magnetization_im_1_changed, + ) + QtCore.QObject.connect( + self.parameter_widget.qsb_magnetization_im_2, + QtCore.SIGNAL("valueChanged(Base::Quantity)"), + self.magnetization_im_2_changed, + ) + QtCore.QObject.connect( + self.parameter_widget.qsb_magnetization_im_3, + QtCore.SIGNAL("valueChanged(Base::Quantity)"), + self.magnetization_im_3_changed, + ) + + self.init_parameter_widget() + def open(self): if self._mesh is not None and self._part is not None: self._meshVisible = self._mesh.ViewObject.isVisible() @@ -76,14 +126,14 @@ class _TaskPanel(base_femtaskpanel._BaseTaskPanel): def reject(self): self._restoreVisibility() - self._selectionWidget.finish_selection() + self._selection_widget.finish_selection() return super().reject() def accept(self): - if self.obj.References != self._selectionWidget.references: - self.obj.References = self._selectionWidget.references - self._applyWidgetChanges() - self._selectionWidget.finish_selection() + if self.obj.References != self._selection_widget.references: + self.obj.References = self._selection_widget.references + self._set_params() + self._selection_widget.finish_selection() self._restoreVisibility() return super().accept() @@ -98,81 +148,102 @@ class _TaskPanel(base_femtaskpanel._BaseTaskPanel): else: self._part.ViewObject.hide() - def _initParamWidget(self): - self._paramWidget.realXQSB.setProperty("value", self.obj.Magnetization_re_1) - FreeCADGui.ExpressionBinding(self._paramWidget.realXQSB).bind( + def _get_params(self): + self.magnetization_re_1 = self.obj.Magnetization_re_1 + self.magnetization_re_2 = self.obj.Magnetization_re_2 + self.magnetization_re_3 = self.obj.Magnetization_re_3 + self.magnetization_im_1 = self.obj.Magnetization_im_1 + self.magnetization_im_2 = self.obj.Magnetization_im_2 + self.magnetization_im_3 = self.obj.Magnetization_im_3 + + self.magnetization_1_enabled = self.obj.EnableMagnetization_1 + self.magnetization_2_enabled = self.obj.EnableMagnetization_2 + self.magnetization_3_enabled = self.obj.EnableMagnetization_3 + + def _set_params(self): + self.obj.Magnetization_re_1 = self.magnetization_re_1 + self.obj.Magnetization_re_2 = self.magnetization_re_2 + self.obj.Magnetization_re_3 = self.magnetization_re_3 + self.obj.Magnetization_im_1 = self.magnetization_im_1 + self.obj.Magnetization_im_2 = self.magnetization_im_2 + self.obj.Magnetization_im_3 = self.magnetization_im_3 + + self.obj.EnableMagnetization_1 = self.magnetization_1_enabled + self.obj.EnableMagnetization_2 = self.magnetization_2_enabled + self.obj.EnableMagnetization_3 = self.magnetization_3_enabled + + def init_parameter_widget(self): + self._get_params() + + self.parameter_widget.qsb_magnetization_re_1.setProperty("value", self.magnetization_re_1) + self.parameter_widget.qsb_magnetization_re_1.setEnabled(self.magnetization_1_enabled) + FreeCADGui.ExpressionBinding(self.parameter_widget.qsb_magnetization_re_1).bind( self.obj, "Magnetization_re_1" ) - self._paramWidget.realYQSB.setProperty("value", self.obj.Magnetization_re_2) - FreeCADGui.ExpressionBinding(self._paramWidget.realYQSB).bind( + + self.parameter_widget.qsb_magnetization_re_2.setProperty("value", self.magnetization_re_2) + self.parameter_widget.qsb_magnetization_re_2.setEnabled(self.magnetization_2_enabled) + FreeCADGui.ExpressionBinding(self.parameter_widget.qsb_magnetization_re_2).bind( self.obj, "Magnetization_re_2" ) - self._paramWidget.realZQSB.setProperty("value", self.obj.Magnetization_re_3) - FreeCADGui.ExpressionBinding(self._paramWidget.realZQSB).bind( + + self.parameter_widget.qsb_magnetization_re_3.setProperty("value", self.magnetization_re_3) + self.parameter_widget.qsb_magnetization_re_3.setEnabled(self.magnetization_3_enabled) + FreeCADGui.ExpressionBinding(self.parameter_widget.qsb_magnetization_re_3).bind( self.obj, "Magnetization_re_3" ) - self._paramWidget.imagXQSB.setProperty("value", self.obj.Magnetization_im_1) - FreeCADGui.ExpressionBinding(self._paramWidget.imagXQSB).bind( + + self.parameter_widget.qsb_magnetization_im_1.setProperty("value", self.magnetization_im_1) + self.parameter_widget.qsb_magnetization_im_1.setEnabled(self.magnetization_1_enabled) + FreeCADGui.ExpressionBinding(self.parameter_widget.qsb_magnetization_im_1).bind( self.obj, "Magnetization_im_1" ) - self._paramWidget.imagYQSB.setProperty("value", self.obj.Magnetization_im_2) - FreeCADGui.ExpressionBinding(self._paramWidget.imagYQSB).bind( + + self.parameter_widget.qsb_magnetization_im_2.setProperty("value", self.magnetization_im_2) + self.parameter_widget.qsb_magnetization_im_2.setEnabled(self.magnetization_2_enabled) + FreeCADGui.ExpressionBinding(self.parameter_widget.qsb_magnetization_im_2).bind( self.obj, "Magnetization_im_2" ) - self._paramWidget.imagZQSB.setProperty("value", self.obj.Magnetization_im_3) - FreeCADGui.ExpressionBinding(self._paramWidget.imagZQSB).bind( + + self.parameter_widget.qsb_magnetization_im_3.setProperty("value", self.magnetization_im_3) + self.parameter_widget.qsb_magnetization_im_3.setEnabled(self.magnetization_3_enabled) + FreeCADGui.ExpressionBinding(self.parameter_widget.qsb_magnetization_im_3).bind( self.obj, "Magnetization_im_3" ) - self._paramWidget.reXunspecBox.setChecked(self.obj.Magnetization_re_1_Disabled) - self._paramWidget.reYunspecBox.setChecked(self.obj.Magnetization_re_2_Disabled) - self._paramWidget.reZunspecBox.setChecked(self.obj.Magnetization_re_3_Disabled) - self._paramWidget.imXunspecBox.setChecked(self.obj.Magnetization_im_1_Disabled) - self._paramWidget.imYunspecBox.setChecked(self.obj.Magnetization_im_2_Disabled) - self._paramWidget.imZunspecBox.setChecked(self.obj.Magnetization_im_3_Disabled) + self.parameter_widget.ckb_magnetization_1.setChecked(self.magnetization_1_enabled) + self.parameter_widget.ckb_magnetization_2.setChecked(self.magnetization_2_enabled) + self.parameter_widget.ckb_magnetization_3.setChecked(self.magnetization_3_enabled) - def _applyMagnetizationChanges(self, enabledBox, magnetizationQSB): - enabled = enabledBox.isChecked() - magnetization = None - try: - magnetization = magnetizationQSB.property("value") - except ValueError: - FreeCAD.Console.PrintMessage( - "Wrong input. Not recognised input: '{}' " - "Magnetization has not been set.\n".format(magnetizationQSB.text()) - ) - magnetization = "0.0 A/m" - return enabled, magnetization + def magnetization_1_enabled_changed(self, value): + self.magnetization_1_enabled = value + self.parameter_widget.qsb_magnetization_re_1.setEnabled(value) + self.parameter_widget.qsb_magnetization_im_1.setEnabled(value) - def _applyWidgetChanges(self): - # apply the magnetizations and their enabled state - self.obj.Magnetization_re_1_Disabled, self.obj.Magnetization_re_1 = ( - self._applyMagnetizationChanges( - self._paramWidget.reXunspecBox, self._paramWidget.realXQSB - ) - ) - self.obj.Magnetization_re_2_Disabled, self.obj.Magnetization_re_2 = ( - self._applyMagnetizationChanges( - self._paramWidget.reYunspecBox, self._paramWidget.realYQSB - ) - ) - self.obj.Magnetization_re_3_Disabled, self.obj.Magnetization_re_3 = ( - self._applyMagnetizationChanges( - self._paramWidget.reZunspecBox, self._paramWidget.realZQSB - ) - ) - self.obj.Magnetization_im_1_Disabled, self.obj.Magnetization_im_1 = ( - self._applyMagnetizationChanges( - self._paramWidget.imXunspecBox, self._paramWidget.imagXQSB - ) - ) - self.obj.Magnetization_im_2_Disabled, self.obj.Magnetization_im_2 = ( - self._applyMagnetizationChanges( - self._paramWidget.imYunspecBox, self._paramWidget.imagYQSB - ) - ) - self.obj.Magnetization_im_3_Disabled, self.obj.Magnetization_im_3 = ( - self._applyMagnetizationChanges( - self._paramWidget.imZunspecBox, self._paramWidget.imagZQSB - ) - ) + def magnetization_2_enabled_changed(self, value): + self.magnetization_2_enabled = value + self.parameter_widget.qsb_magnetization_re_2.setEnabled(value) + self.parameter_widget.qsb_magnetization_im_2.setEnabled(value) + + def magnetization_3_enabled_changed(self, value): + self.magnetization_3_enabled = value + self.parameter_widget.qsb_magnetization_re_3.setEnabled(value) + self.parameter_widget.qsb_magnetization_im_3.setEnabled(value) + + def magnetization_re_1_changed(self, value): + self.magnetization_re_1 = value + + def magnetization_re_2_changed(self, value): + self.magnetization_re_2 = value + + def magnetization_re_3_changed(self, value): + self.magnetization_re_3 = value + + def magnetization_im_1_changed(self, value): + self.magnetization_im_1 = value + + def magnetization_im_2_changed(self, value): + self.magnetization_im_2 = value + + def magnetization_im_3_changed(self, value): + self.magnetization_im_3 = value