From 909727ade856e31ecb20ee281fe81cc4cfa21e53 Mon Sep 17 00:00:00 2001 From: Eivind Kvedalen Date: Mon, 28 Mar 2016 00:38:35 +0200 Subject: [PATCH] Expressions: Fixed parsing of unit expressions similar to 1/unit. --- src/App/Expression.cpp | 14 ++++++++++++++ src/App/Expression.h | 6 ++++++ 2 files changed, 20 insertions(+) diff --git a/src/App/Expression.cpp b/src/App/Expression.cpp index eee2b4a58..15e7e7d0e 100644 --- a/src/App/Expression.cpp +++ b/src/App/Expression.cpp @@ -1833,6 +1833,20 @@ UnitExpression * ExpressionParser::parseUnit(const App::DocumentObject *owner, c // Simplify expression Expression * simplified = ScanResult->simplify(); + + if (!unitExpression) { + OperatorExpression * fraction = freecad_dynamic_cast(ScanResult); + + if (fraction && fraction->getOperator() == OperatorExpression::DIV) { + NumberExpression * nom = freecad_dynamic_cast(fraction->getLeft()); + UnitExpression * denom = freecad_dynamic_cast(fraction->getRight()); + const double epsilon = std::numeric_limits::epsilon(); + + // If not initially a unit expression, but value is equal to 1, it means the expression is something like 1/unit + if (denom && nom && essentiallyEqual(nom->getValue(), 1.0, epsilon)) + unitExpression = true; + } + } delete ScanResult; if (unitExpression) { diff --git a/src/App/Expression.h b/src/App/Expression.h index 1f78daf68..e4528e223 100644 --- a/src/App/Expression.h +++ b/src/App/Expression.h @@ -246,6 +246,12 @@ public: virtual void visit(ExpressionVisitor & v); + Operator getOperator() const { return op; } + + Expression * getLeft() const { return left; } + + Expression * getRight() const { return right; } + protected: Operator op; /**< Operator working on left and right */ Expression * left; /**< Left operand */