From 6bccd52cd0ce3c850bec26b84e6126863dfed2f8 Mon Sep 17 00:00:00 2001 From: wmayer Date: Wed, 14 Dec 2022 15:41:49 +0100 Subject: [PATCH] App: handle sub-components in PropertyPlacement --- src/App/PropertyGeo.cpp | 132 +++++++++++++++++++++++++++++++--------- 1 file changed, 103 insertions(+), 29 deletions(-) diff --git a/src/App/PropertyGeo.cpp b/src/App/PropertyGeo.cpp index 8c2377b293..9776d70796 100644 --- a/src/App/PropertyGeo.cpp +++ b/src/App/PropertyGeo.cpp @@ -558,38 +558,75 @@ void PropertyPlacement::getPaths(std::vector &paths) const << ObjectIdentifier::SimpleComponent(ObjectIdentifier::String("z"))); } +namespace { +double toDouble(const boost::any &value) +{ + double avalue{}; + + if (value.type() == typeid(Base::Quantity)) + avalue = boost::any_cast(value).getValue(); + else if (value.type() == typeid(double)) + avalue = boost::any_cast(value); + else if (value.type() == typeid(int)) + avalue = boost::any_cast(value); + else if (value.type() == typeid(unsigned int)) + avalue = boost::any_cast(value); + else if (value.type() == typeid(short)) + avalue = boost::any_cast(value); + else if (value.type() == typeid(unsigned short)) + avalue = boost::any_cast(value); + else if (value.type() == typeid(long)) + avalue = boost::any_cast(value); + else if (value.type() == typeid(unsigned long)) + avalue = boost::any_cast(value); + else + throw std::bad_cast(); + return avalue; +} +} + void PropertyPlacement::setPathValue(const ObjectIdentifier &path, const boost::any &value) { - if (path.getSubPathStr() == ".Rotation.Angle") { - double avalue; - - if (value.type() == typeid(Base::Quantity)) - avalue = boost::any_cast(value).getValue(); - else if (value.type() == typeid(double)) - avalue = boost::any_cast(value); - else if (value.type() == typeid(int)) - avalue = boost::any_cast(value); - else if (value.type() == typeid(unsigned int)) - avalue = boost::any_cast(value); - else if (value.type() == typeid(short)) - avalue = boost::any_cast(value); - else if (value.type() == typeid(unsigned short)) - avalue = boost::any_cast(value); - else if (value.type() == typeid(long)) - avalue = boost::any_cast(value); - else if (value.type() == typeid(unsigned long)) - avalue = boost::any_cast(value); - else - throw std::bad_cast(); + auto updateAxis = [=](int index, double coord) { + Base::Vector3d axis; + double angle; + Base::Vector3d base = _cPos.getPosition(); + Base::Rotation rot = _cPos.getRotation(); + rot.getRawValue(axis, angle); + axis[index] = coord; + rot.setValue(axis, angle); + Base::Placement plm(base, rot); + setValue(plm); + }; + std::string subpath = path.getSubPathStr(); + if (subpath == ".Rotation.Angle") { + double avalue = toDouble(value); Property::setPathValue(path, Base::toRadians(avalue)); } - else + else if (subpath == ".Rotation.Axis.x") { + updateAxis(0, toDouble(value)); + } + else if (subpath == ".Rotation.Axis.y") { + updateAxis(1, toDouble(value)); + } + else if (subpath == ".Rotation.Axis.z") { + updateAxis(2, toDouble(value)); + } + else { Property::setPathValue(path, value); + } } const boost::any PropertyPlacement::getPathValue(const ObjectIdentifier &path) const { + auto getAxis = [](const Base::Placement& plm) { + Base::Vector3d axis; + double angle; + const Base::Rotation& rot = plm.getRotation(); + rot.getRawValue(axis, angle); + return axis; + }; std::string p = path.getSubPathStr(); if (p == ".Rotation.Angle") { @@ -600,26 +637,63 @@ const boost::any PropertyPlacement::getPathValue(const ObjectIdentifier &path) c // Convert double to quantity return Base::Quantity(boost::any_cast(Property::getPathValue(path)), Unit::Length); } - else + else if (p == ".Rotation.Axis.x") { + return getAxis(_cPos).x; + } + else if (p == ".Rotation.Axis.y") { + return getAxis(_cPos).y; + } + else if (p == ".Rotation.Axis.z") { + return getAxis(_cPos).z; + } + else { return Property::getPathValue(path); + } } bool PropertyPlacement::getPyPathValue(const ObjectIdentifier &path, Py::Object &res) const { + auto getAxis = [](const Base::Placement& plm) { + Base::Vector3d axis; + double angle; + const Base::Rotation& rot = plm.getRotation(); + rot.getRawValue(axis, angle); + return axis; + }; + std::string p = path.getSubPathStr(); if (p == ".Rotation.Angle") { Base::Vector3d axis; double angle; _cPos.getRotation().getValue(axis,angle); res = Py::asObject(new QuantityPy(new Quantity(Base::toDegrees(angle),Unit::Angle))); - } else if (p == ".Base.x") { + return true; + } + else if (p == ".Base.x") { res = Py::asObject(new QuantityPy(new Quantity(_cPos.getPosition().x,Unit::Length))); - } else if (p == ".Base.y") { + return true; + } + else if (p == ".Base.y") { res = Py::asObject(new QuantityPy(new Quantity(_cPos.getPosition().y,Unit::Length))); - } else if (p == ".Base.z") { + return true; + } + else if (p == ".Base.z") { res = Py::asObject(new QuantityPy(new Quantity(_cPos.getPosition().z,Unit::Length))); - } else - return false; - return true; + return true; + } + else if (p == ".Rotation.Axis.x") { + res = Py::Float(getAxis(_cPos).x); + return true; + } + else if (p == ".Rotation.Axis.y") { + res = Py::Float(getAxis(_cPos).y); + return true; + } + else if (p == ".Rotation.Axis.z") { + res = Py::Float(getAxis(_cPos).z); + return true; + } + + return false; } PyObject *PropertyPlacement::getPyObject()