From 2de933793d12658783cb598a8cfae08be990bc8f Mon Sep 17 00:00:00 2001 From: wwmayer Date: Sun, 12 Oct 2025 20:08:04 +0200 Subject: [PATCH] App: Implement Save() & Restore() for int/float constraint properties Implement the methods Save() and Restore() for PropertyIntegerConstraint and PropertyFloatConstraint. Handle also the case of a user-defined ranges. This fixes https://github.com/FreeCAD/FreeCAD/issues/24571. Hint: For PropertyQuantityConstraint this is not doable because it doesn't support user-defined ranges --- src/App/PropertyStandard.cpp | 96 ++++++++++++++++++++++++++++++++++++ src/App/PropertyStandard.h | 6 +++ 2 files changed, 102 insertions(+) diff --git a/src/App/PropertyStandard.cpp b/src/App/PropertyStandard.cpp index 57f70d9886..fbf7da46d3 100644 --- a/src/App/PropertyStandard.cpp +++ b/src/App/PropertyStandard.cpp @@ -770,6 +770,54 @@ void PropertyIntegerConstraint::setPyObject(PyObject* value) } } +void PropertyIntegerConstraint::Save(Base::Writer& writer) const +{ + writer.Stream() << writer.ind() + << "isDeletable()) { + long minimum = _ConstStruct->LowerBound; + long maximum = _ConstStruct->UpperBound; + long stepsize = _ConstStruct->StepSize; + writer.Stream() << " min=\"" << minimum << "\"" + << " max=\"" << maximum << "\"" + << " step=\"" << stepsize << "\""; + } + writer.Stream() << "/>\n"; +} + +void PropertyIntegerConstraint::Restore(Base::XMLReader& reader) +{ + // read my Element + reader.readElement("Integer"); + // get the value of my Attribute + setValue(reader.getAttributeAsInteger("value")); + + bool createConstraint = false; + long minimum = std::numeric_limits::lowest(); + long maximum = std::numeric_limits::max(); + long stepsize = 1.0; + if (reader.hasAttribute("min")) { + minimum = reader.getAttributeAsInteger("min"); + createConstraint = true; + } + if (reader.hasAttribute("max")) { + maximum = reader.getAttributeAsInteger("max"); + createConstraint = true; + } + if (reader.hasAttribute("step")) { + stepsize = reader.getAttributeAsInteger("step"); + } + + if (createConstraint) { + Constraints* c = new Constraints(); + c->setDeletable(true); + c->LowerBound = minimum; + c->UpperBound = maximum; + c->StepSize = stepsize; + setConstraints(c); + } +} + //************************************************************************** //************************************************************************** // PropertyPercent @@ -1272,6 +1320,54 @@ void PropertyFloatConstraint::setPyObject(PyObject* value) } } +void PropertyFloatConstraint::Save(Base::Writer& writer) const +{ + writer.Stream() << writer.ind() + << "isDeletable()) { + double minimum = _ConstStruct->LowerBound; + double maximum = _ConstStruct->UpperBound; + double stepsize = _ConstStruct->StepSize; + writer.Stream() << " min=\"" << minimum << "\"" + << " max=\"" << maximum << "\"" + << " step=\"" << stepsize << "\""; + } + writer.Stream() << "/>\n"; +} + +void PropertyFloatConstraint::Restore(Base::XMLReader& reader) +{ + // read my Element + reader.readElement("Float"); + // get the value of my Attribute + setValue(reader.getAttributeAsFloat("value")); + + bool createConstraint = false; + double minimum = std::numeric_limits::lowest(); + double maximum = std::numeric_limits::max(); + double stepsize = 1.0; + if (reader.hasAttribute("min")) { + minimum = reader.getAttributeAsFloat("min"); + createConstraint = true; + } + if (reader.hasAttribute("max")) { + maximum = reader.getAttributeAsFloat("max"); + createConstraint = true; + } + if (reader.hasAttribute("step")) { + stepsize = reader.getAttributeAsFloat("step"); + } + + if (createConstraint) { + Constraints* c = new Constraints(); + c->setDeletable(true); + c->LowerBound = minimum; + c->UpperBound = maximum; + c->StepSize = stepsize; + setConstraints(c); + } +} + //************************************************************************** // PropertyPrecision //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ diff --git a/src/App/PropertyStandard.h b/src/App/PropertyStandard.h index b2b0a81d22..3eff182e84 100644 --- a/src/App/PropertyStandard.h +++ b/src/App/PropertyStandard.h @@ -339,6 +339,9 @@ public: } void setPyObject(PyObject* py) override; + void Save(Base::Writer& writer) const override; + void Restore(Base::XMLReader& reader) override; + protected: const Constraints* _ConstStruct {nullptr}; }; @@ -673,6 +676,9 @@ public: void setPyObject(PyObject* py) override; + void Save(Base::Writer& writer) const override; + void Restore(Base::XMLReader& reader) override; + protected: const Constraints* _ConstStruct {nullptr}; };