From 2c2a977e04525fa64c8905e6455eaf42e7d6af10 Mon Sep 17 00:00:00 2001 From: Paddle Date: Mon, 18 Sep 2023 18:54:34 +0200 Subject: [PATCH] Add setter/getter for constraint expression. --- src/Mod/Sketcher/App/SketchObject.cpp | 39 +++++++++++++++++++++++++++ src/Mod/Sketcher/App/SketchObject.h | 7 +++++ 2 files changed, 46 insertions(+) diff --git a/src/Mod/Sketcher/App/SketchObject.cpp b/src/Mod/Sketcher/App/SketchObject.cpp index 32918af027..59bdb450d6 100644 --- a/src/Mod/Sketcher/App/SketchObject.cpp +++ b/src/Mod/Sketcher/App/SketchObject.cpp @@ -660,6 +660,45 @@ int SketchObject::moveDatumsToEnd() return 0; } +bool SketchObject::constraintHasExpression(int constNum) +{ + App::ObjectIdentifier path = Constraints.createPath(constNum); + auto info = getExpression(path); + if (info.expression) { + return true; + } + return false; +} + +std::string SketchObject::getConstraintExpression(int constNum) +{ + App::ObjectIdentifier path = Constraints.createPath(constNum); + auto info = getExpression(path); + if (info.expression) { + std::string expression = info.expression->toString(); + return expression; + } + + return ""; +} + +void SketchObject::setConstraintExpression(int constNum, std::string& newExpression) +{ + App::ObjectIdentifier path = Constraints.createPath(constNum); + auto info = getExpression(path); + if (info.expression) { + try { + std::shared_ptr expr(App::Expression::parse(this, newExpression)); + // there is a bug in the SketchObject API because setExpression() is protected but public in DocumentObject + App::DocumentObject* base = this; + base->setExpression(path, expr); + } + catch (const Base::Exception&) { + Base::Console().Error("Failed to set constraint expression."); + } + } +} + int SketchObject::setVirtualSpace(int ConstrId, bool isinvirtualspace) { // no need to check input data validity as this is an sketchobject managed operation. diff --git a/src/Mod/Sketcher/App/SketchObject.h b/src/Mod/Sketcher/App/SketchObject.h index 4274a27c57..b33a1b21e0 100644 --- a/src/Mod/Sketcher/App/SketchObject.h +++ b/src/Mod/Sketcher/App/SketchObject.h @@ -269,6 +269,13 @@ public: /// Move Dimensional constraints at the end of the properties array int moveDatumsToEnd(); + // Check if a constraint has an expression associated. + bool constraintHasExpression(int constNum); + // Get a constraint associated expression + std::string getConstraintExpression(int constNum); + // Set a constraint associated expression + void setConstraintExpression(int constNum, std::string& newExpression); + /// set the driving status of this constraint and solve int setVirtualSpace(int ConstrId, bool isinvirtualspace); /// set the driving status of a group of constraints at once