From 62d33fc0d5e5bb6925cac708a8bf047ac7e38100 Mon Sep 17 00:00:00 2001 From: Oliver Oxtoby Date: Thu, 2 Aug 2018 14:34:51 +0200 Subject: [PATCH] Base: Make units of PropertyQuantity settable from Python code The previous implementation did not allow the unit to be set after it is initialised by the default constructor - this fix makes it possible to set the unit by explicitly assigning a Unit object. --- src/App/PropertyUnits.cpp | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/App/PropertyUnits.cpp b/src/App/PropertyUnits.cpp index db796da966..6410917441 100644 --- a/src/App/PropertyUnits.cpp +++ b/src/App/PropertyUnits.cpp @@ -40,6 +40,7 @@ #include "PropertyUnits.h" #include #include +#include using namespace App; using namespace Base; @@ -113,18 +114,28 @@ Base::Quantity PropertyQuantity::createQuantityFromPy(PyObject *value) void PropertyQuantity::setPyObject(PyObject *value) { - Base::Quantity quant= createQuantityFromPy(value); - - Unit unit = quant.getUnit(); - if (unit.isEmpty()){ - PropertyFloat::setValue(quant.getValue()); - return; + // Set the unit if Unit object supplied, else check the unit + // and set the value + + if (PyObject_TypeCheck(value, &(UnitPy::Type))) { + Base::UnitPy *pcObject = static_cast(value); + Base::Unit unit = *(pcObject->getUnitPtr()); + _Unit = unit; } + else { + Base::Quantity quant= createQuantityFromPy(value); - if (unit != _Unit) - throw Base::UnitsMismatchError("Not matching Unit!"); + Unit unit = quant.getUnit(); + if (unit.isEmpty()){ + PropertyFloat::setValue(quant.getValue()); + return; + } - PropertyFloat::setValue(quant.getValue()); + if (unit != _Unit) + throw Base::UnitsMismatchError("Not matching Unit!"); + + PropertyFloat::setValue(quant.getValue()); + } } void PropertyQuantity::setPathValue(const ObjectIdentifier & /*path*/, const boost::any &value)