Share code for finding a body containing a feature

This commit is contained in:
Alexander Golubev 2015-07-20 11:17:15 +03:00 committed by Stefan Tröger
parent 16e4ce20f0
commit ffc6cc2f23
6 changed files with 32 additions and 22 deletions

View File

@ -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<App::DocumentObject*> bodies = doc->getObjectsOfType(BodyBase::getClassTypeId());
for (std::vector<App::DocumentObject*>::const_iterator b = bodies.begin(); b != bodies.end(); b++) {

View File

@ -232,6 +232,12 @@ const bool Body::isAllowed(const App::DocumentObject* f)
}
Body* Body::findBodyOf(const App::DocumentObject* feature)
{
return static_cast<Body*>(BodyBase::findBodyOf(feature));
}
void Body::addFeature(App::DocumentObject *feature)
{
insertFeature (feature, Tip.getValue(), /*after = */ true);

View File

@ -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();

View File

@ -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<PartDesign::Body>()) {
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<App::Part>()) {

View File

@ -77,7 +77,7 @@ PartDesign::Body *getBody(bool messageIfNot)
{
PartDesign::Body * activeBody = Gui::Application::Instance->activeView()->getActiveObject<PartDesign::Body*>(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<PartDesign::Body*>(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<PartDesign::Body>()) {
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;

View File

@ -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