Add setter/getter for constraint expression.

This commit is contained in:
Paddle
2023-09-18 18:54:34 +02:00
parent 346d0d7231
commit 2c2a977e04
2 changed files with 46 additions and 0 deletions

View File

@@ -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<App::Expression> 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.

View File

@@ -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