From ffc6cc2f232fad8fa4dbc8b23bec3582e23172a5 Mon Sep 17 00:00:00 2001 From: Alexander Golubev Date: Mon, 20 Jul 2015 11:17:15 +0300 Subject: [PATCH] Share code for finding a body containing a feature --- src/Mod/Part/App/BodyBase.cpp | 2 +- src/Mod/PartDesign/App/Body.cpp | 6 ++++++ src/Mod/PartDesign/App/Body.h | 6 ++++++ src/Mod/PartDesign/App/FeatureBoolean.cpp | 11 +++-------- src/Mod/PartDesign/Gui/Workbench.cpp | 21 ++++++++++----------- src/Mod/PartDesign/Gui/Workbench.h | 8 ++++++-- 6 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/Mod/Part/App/BodyBase.cpp b/src/Mod/Part/App/BodyBase.cpp index 701d0e6a5..3722b0f0c 100644 --- a/src/Mod/Part/App/BodyBase.cpp +++ b/src/Mod/Part/App/BodyBase.cpp @@ -65,7 +65,7 @@ const bool BodyBase::hasFeature(const App::DocumentObject* f) const BodyBase* BodyBase::findBodyOf(const App::DocumentObject* f) { - App::Document* doc = App::GetApplication().getActiveDocument(); + App::Document* doc = f->getDocument(); if (doc != NULL) { std::vector bodies = doc->getObjectsOfType(BodyBase::getClassTypeId()); for (std::vector::const_iterator b = bodies.begin(); b != bodies.end(); b++) { diff --git a/src/Mod/PartDesign/App/Body.cpp b/src/Mod/PartDesign/App/Body.cpp index e34c6d461..1477c5679 100644 --- a/src/Mod/PartDesign/App/Body.cpp +++ b/src/Mod/PartDesign/App/Body.cpp @@ -232,6 +232,12 @@ const bool Body::isAllowed(const App::DocumentObject* f) } +Body* Body::findBodyOf(const App::DocumentObject* feature) +{ + return static_cast(BodyBase::findBodyOf(feature)); +} + + void Body::addFeature(App::DocumentObject *feature) { insertFeature (feature, Tip.getValue(), /*after = */ true); diff --git a/src/Mod/PartDesign/App/Body.h b/src/Mod/PartDesign/App/Body.h index 6707f0ca5..364872efe 100644 --- a/src/Mod/PartDesign/App/Body.h +++ b/src/Mod/PartDesign/App/Body.h @@ -117,6 +117,12 @@ public: */ static const bool isAllowed(const App::DocumentObject* f); + /** + * Return the body which this feature belongs too, or NULL + * The only difference to BodyBase::findBodyOf() is that this one casts value to Body* + */ + static Body *findBodyOf(const App::DocumentObject* feature); + /// Return the bounding box of the Tip Shape, taking into account datum features Base::BoundBox3d getBoundBox(); diff --git a/src/Mod/PartDesign/App/FeatureBoolean.cpp b/src/Mod/PartDesign/App/FeatureBoolean.cpp index 87a68f24c..ea7adc468 100644 --- a/src/Mod/PartDesign/App/FeatureBoolean.cpp +++ b/src/Mod/PartDesign/App/FeatureBoolean.cpp @@ -85,18 +85,13 @@ App::DocumentObjectExecReturn *Boolean::execute(void) if (baseTopShape._Shape.IsNull()) return new App::DocumentObjectExecReturn("Cannot do boolean operation with invalid base shape"); - // TODO: move PartDesignGui::getBodyFor() from Gui to App and use it here //get the body this boolean feature belongs to - PartDesign::Body* baseBody = NULL; - for(PartDesign::Body* b : this->getDocument()->getObjectsOfType()) { - if(b->hasFeature(this)) { - baseBody = b; - break; - } - } + Part::BodyBase* baseBody = Part::BodyBase::findBodyOf(this); + if(!baseBody) return new App::DocumentObjectExecReturn("Cannot do boolean on feature which is not in a body"); + // TODO: share the code snippet with PartDesignGui::getPartFor() //get the part every body should belong to App::Part* part = NULL; for(App::Part* p : this->getDocument()->getObjectsOfType()) { diff --git a/src/Mod/PartDesign/Gui/Workbench.cpp b/src/Mod/PartDesign/Gui/Workbench.cpp index 9094d6558..e056dab24 100644 --- a/src/Mod/PartDesign/Gui/Workbench.cpp +++ b/src/Mod/PartDesign/Gui/Workbench.cpp @@ -77,7 +77,7 @@ PartDesign::Body *getBody(bool messageIfNot) { PartDesign::Body * activeBody = Gui::Application::Instance->activeView()->getActiveObject(PDBODYKEY); - if (!activeBody && messageIfNot){ + if (!activeBody && messageIfNot) { QMessageBox::warning(Gui::getMainWindow(), QObject::tr("No active Body"), QObject::tr("In order to use PartDesign you need an active Body object in the document. " "Please make one active (double click) or create one. If you have a legacy document " @@ -85,24 +85,23 @@ PartDesign::Body *getBody(bool messageIfNot) "PartDesign to put them into a Body." )); } + return activeBody; } -PartDesign::Body *getBodyFor(App::DocumentObject* obj, bool messageIfNot) +PartDesign::Body *getBodyFor(const App::DocumentObject* obj, bool messageIfNot) { if(!obj) return nullptr; - PartDesign::Body * activeBody = Gui::Application::Instance->activeView()->getActiveObject(PDBODYKEY); - if(activeBody && activeBody->hasFeature(obj)) - return activeBody; + PartDesign::Body * rv = getBody( /*messageIfNot =*/ false); + if(rv && rv->hasFeature(obj)) + return rv; - //try to find the part the object is in - for(PartDesign::Body* b : obj->getDocument()->getObjectsOfType()) { - if(b->hasFeature(obj)) { - return b; - } + rv = PartDesign::Body::findBodyOf(obj); + if (rv) { + return rv; } if (messageIfNot){ @@ -113,7 +112,7 @@ PartDesign::Body *getBodyFor(App::DocumentObject* obj, bool messageIfNot) return nullptr; } -App::Part* getPartFor(App::DocumentObject* obj, bool messageIfNot) { +App::Part* getPartFor(const App::DocumentObject* obj, bool messageIfNot) { if(!obj) return nullptr; diff --git a/src/Mod/PartDesign/Gui/Workbench.h b/src/Mod/PartDesign/Gui/Workbench.h index 6f47d0310..c364163ae 100644 --- a/src/Mod/PartDesign/Gui/Workbench.h +++ b/src/Mod/PartDesign/Gui/Workbench.h @@ -56,8 +56,12 @@ namespace PartDesignGui { /// Return active body or show a warning message PartDesign::Body *getBody(bool messageIfNot); -PartDesign::Body *getBodyFor(App::DocumentObject*, bool messageIfNot); -App::Part *getPartFor(App::DocumentObject*, bool messageIfNot); +/** + * Finds a body for the given feature. And shows a message if not found + * Also unlike Body::findBodyFor it checks if the active body has the feature first. + */ +PartDesign::Body *getBodyFor(const App::DocumentObject*, bool messageIfNot); +App::Part *getPartFor(const App::DocumentObject*, bool messageIfNot); /** * @author Werner Mayer