From e9fc6fb89ac50ae165652d041a883beaa8bf08e2 Mon Sep 17 00:00:00 2001 From: Bas Ruigrok Date: Wed, 28 Aug 2024 19:37:44 +0200 Subject: [PATCH] Move getGlobalPlacement from AssemblyObject to GeoFeature --- src/App/GeoFeature.cpp | 60 +++++++++++++++++++ src/App/GeoFeature.h | 4 ++ src/Mod/Assembly/App/AssemblyObject.cpp | 60 +------------------ src/Mod/Assembly/App/AssemblyObject.h | 7 --- src/Mod/Assembly/Gui/ViewProviderAssembly.cpp | 10 ++-- 5 files changed, 70 insertions(+), 71 deletions(-) diff --git a/src/App/GeoFeature.cpp b/src/App/GeoFeature.cpp index 03f3ccfe4f..383196629a 100644 --- a/src/App/GeoFeature.cpp +++ b/src/App/GeoFeature.cpp @@ -25,6 +25,8 @@ #include +#include + #include "ComplexGeoData.h" #include "Document.h" #include "GeoFeature.h" @@ -270,3 +272,61 @@ GeoFeature::getHigherElements(const char *element, bool silent) const return {}; return prop->getComplexData()->getHigherElements(element, silent); } + +Base::Placement GeoFeature::getPlacementFromProp(App::DocumentObject* obj, const char* propName) +{ + Base::Placement plc = Base::Placement(); + auto* propPlacement = dynamic_cast(obj->getPropertyByName(propName)); + if (propPlacement) { + plc = propPlacement->getValue(); + } + return plc; +} + +Base::Placement GeoFeature::getGlobalPlacement(App::DocumentObject* targetObj, + App::DocumentObject* rootObj, + const std::string& sub) +{ + if (!targetObj || !rootObj || sub.empty()) { + return Base::Placement(); + } + std::vector names = Base::Tools::splitSubName(sub); + + App::Document* doc = rootObj->getDocument(); + Base::Placement plc = getPlacementFromProp(rootObj, "Placement"); + + for (auto& name : names) { + App::DocumentObject* obj = doc->getObject(name.c_str()); + if (!obj) { + return Base::Placement(); + } + + plc = plc * getPlacementFromProp(obj, "Placement"); + + if (obj == targetObj) { + return plc; + } + if (obj->isDerivedFrom()) { + // Update doc in case its an external link. + doc = obj->getLinkedObject()->getDocument(); + } + } + + // If targetObj has not been found there's a problem + return Base::Placement(); +} + +Base::Placement GeoFeature::getGlobalPlacement(App::DocumentObject* targetObj, + App::PropertyXLinkSub* prop) +{ + if (!targetObj || !prop) { + return Base::Placement(); + } + + std::vector subs = prop->getSubValues(); + if (subs.empty()) { + return Base::Placement(); + } + + return getGlobalPlacement(targetObj, prop->getValue(), subs[0]); +} diff --git a/src/App/GeoFeature.h b/src/App/GeoFeature.h index 9c0443611f..216f741dcd 100644 --- a/src/App/GeoFeature.h +++ b/src/App/GeoFeature.h @@ -179,6 +179,10 @@ public: /// Return the higher level element names of the given element virtual std::vector getHigherElements(const char *name, bool silent=false) const; + static Base::Placement getPlacementFromProp(DocumentObject* obj, const char* propName); + static Base::Placement getGlobalPlacement(DocumentObject* targetObj, DocumentObject* rootObj, const std::string& sub); + static Base::Placement getGlobalPlacement(DocumentObject* targetObj, PropertyXLinkSub* prop); + protected: void onChanged(const Property* prop) override; // void onDocumentRestored() override; diff --git a/src/Mod/Assembly/App/AssemblyObject.cpp b/src/Mod/Assembly/App/AssemblyObject.cpp index 07438b18f1..3959be3e5c 100644 --- a/src/Mod/Assembly/App/AssemblyObject.cpp +++ b/src/Mod/Assembly/App/AssemblyObject.cpp @@ -2078,6 +2078,7 @@ void AssemblyObject::setJointActivated(App::DocumentObject* joint, bool val) propActivated->setValue(val); } } + bool AssemblyObject::getJointActivated(App::DocumentObject* joint) { auto* propActivated = dynamic_cast(joint->getPropertyByName("Activated")); @@ -2087,65 +2088,6 @@ bool AssemblyObject::getJointActivated(App::DocumentObject* joint) return false; } -Base::Placement AssemblyObject::getPlacementFromProp(App::DocumentObject* obj, const char* propName) -{ - Base::Placement plc = Base::Placement(); - auto* propPlacement = dynamic_cast(obj->getPropertyByName(propName)); - if (propPlacement) { - plc = propPlacement->getValue(); - } - return plc; -} - - -Base::Placement AssemblyObject::getGlobalPlacement(App::DocumentObject* targetObj, - App::DocumentObject* rootObj, - const std::string& sub) -{ - if (!targetObj || !rootObj || sub == "") { - return Base::Placement(); - } - std::vector names = Base::Tools::splitSubName(sub); - - App::Document* doc = rootObj->getDocument(); - Base::Placement plc = getPlacementFromProp(rootObj, "Placement"); - - for (auto& name : names) { - App::DocumentObject* obj = doc->getObject(name.c_str()); - if (!obj) { - return Base::Placement(); - } - - plc = plc * getPlacementFromProp(obj, "Placement"); - - if (obj == targetObj) { - return plc; - } - if (isLink(obj)) { - // Update doc in case its an external link. - doc = obj->getLinkedObject()->getDocument(); - } - } - - // If targetObj has not been found there's a problem - return Base::Placement(); -} - -Base::Placement AssemblyObject::getGlobalPlacement(App::DocumentObject* targetObj, - App::PropertyXLinkSub* prop) -{ - if (!targetObj || !prop) { - return Base::Placement(); - } - - std::vector subs = prop->getSubValues(); - if (subs.empty()) { - return Base::Placement(); - } - - return getGlobalPlacement(targetObj, prop->getValue(), subs[0]); -} - double AssemblyObject::getJointDistance(App::DocumentObject* joint) { double distance = 0.0; diff --git a/src/Mod/Assembly/App/AssemblyObject.h b/src/Mod/Assembly/App/AssemblyObject.h index 247e7d0939..4fc2185460 100644 --- a/src/Mod/Assembly/App/AssemblyObject.h +++ b/src/Mod/Assembly/App/AssemblyObject.h @@ -280,13 +280,6 @@ public: const char* propName); static std::vector getSubAsList(App::PropertyXLinkSub* prop); static std::vector getSubAsList(App::DocumentObject* joint, const char* propName); - static Base::Placement getPlacementFromProp(App::DocumentObject* obj, const char* propName); - - static Base::Placement getGlobalPlacement(App::DocumentObject* targetObj, - App::DocumentObject* rootObj, - const std::string& sub); - static Base::Placement getGlobalPlacement(App::DocumentObject* targetObj, - App::PropertyXLinkSub* prop); }; // using AssemblyObjectPython = App::FeaturePythonT; diff --git a/src/Mod/Assembly/Gui/ViewProviderAssembly.cpp b/src/Mod/Assembly/Gui/ViewProviderAssembly.cpp index ecfb467001..45abfeffd8 100644 --- a/src/Mod/Assembly/Gui/ViewProviderAssembly.cpp +++ b/src/Mod/Assembly/Gui/ViewProviderAssembly.cpp @@ -766,7 +766,7 @@ ViewProviderAssembly::DragMode ViewProviderAssembly::findDragMode() const char* plcPropName = (pName == "Reference1") ? "Placement1" : "Placement2"; // jcsPlc is relative to the Object - jcsPlc = AssemblyObject::getPlacementFromProp(movingJoint, plcPropName); + jcsPlc = App::GeoFeature::getPlacementFromProp(movingJoint, plcPropName); // Make jcsGlobalPlc relative to the origin of the doc auto* ref = @@ -775,7 +775,7 @@ ViewProviderAssembly::DragMode ViewProviderAssembly::findDragMode() return DragMode::Translation; } auto* obj = assemblyPart->getObjFromRef(movingJoint, pName.c_str()); - Base::Placement global_plc = AssemblyObject::getGlobalPlacement(obj, ref); + Base::Placement global_plc = App::GeoFeature::getGlobalPlacement(obj, ref); jcsGlobalPlc = global_plc * jcsPlc; // Add downstream parts so that they move together @@ -922,7 +922,7 @@ void ViewProviderAssembly::initMoveDragger() App::DocumentObject* part = docsToMove[0].obj; draggerInitPlc = - AssemblyObject::getGlobalPlacement(part, docsToMove[0].rootObj, docsToMove[0].sub); + App::GeoFeature::getGlobalPlacement(part, docsToMove[0].rootObj, docsToMove[0].sub); std::vector listOfObjs; std::vector listOfRefs; for (auto& movingObj : docsToMove) { @@ -1132,11 +1132,11 @@ ViewProviderAssembly::getCenterOfBoundingBox(const std::vector& mo // bboxCenter does not take into account obj global placement Base::Placement plc(bboxCenter, Base::Rotation()); // Change plc to be relative to the object placement. - Base::Placement objPlc = AssemblyObject::getPlacementFromProp(movingObj.obj, "Placement"); + Base::Placement objPlc = App::GeoFeature::getPlacementFromProp(movingObj.obj, "Placement"); plc = objPlc.inverse() * plc; // Change plc to be relative to the origin of the document. Base::Placement global_plc = - AssemblyObject::getGlobalPlacement(movingObj.obj, movingObj.rootObj, movingObj.sub); + App::GeoFeature::getGlobalPlacement(movingObj.obj, movingObj.rootObj, movingObj.sub); plc = global_plc * plc; bboxCenter = plc.getPosition();