PartDesign: Add new need active body dialog with option to choose body (#4949)

* [PD] Add new need active body dialog

Intended for use wherever an active body is needed but none exists. The dialog
contains a list with the bodies present in the document, with an extra option to
make a new body. Custom text can be provided if needed.

This commit also replaces the warnings used by "new sketch" and "primitive"
command with this new dialog.

Addresses issue #4288.
This commit is contained in:
Ajinkya Dahale
2021-09-16 13:27:46 -04:00
committed by GitHub
parent c123bc2bf8
commit 28d8d808a8
8 changed files with 324 additions and 42 deletions

View File

@@ -52,6 +52,8 @@
#include "ReferenceSelection.h"
#include "Utils.h"
#include "WorkflowManager.h"
#include "DlgActiveBody.h"
FC_LOG_LEVEL_INIT("PartDesignGui",true,true)
@@ -108,44 +110,30 @@ PartDesign::Body *getBody(bool messageIfNot, bool autoActivate, bool assertModer
Gui::MDIView *activeView = Gui::Application::Instance->activeView();
if (activeView) {
bool singleBodyDocument = activeView->getAppDocument()->
countObjectsOfType(PartDesign::Body::getClassTypeId()) == 1;
if (assertModern && PartDesignGui::assureModernWorkflow ( activeView->getAppDocument() ) ) {
auto doc = activeView->getAppDocument();
bool singleBodyDocument = doc->countObjectsOfType(PartDesign::Body::getClassTypeId()) == 1;
if (assertModern && PartDesignGui::assureModernWorkflow (doc) ) {
activeBody = activeView->getActiveObject<PartDesign::Body*>(PDBODYKEY,topParent,subname);
if (!activeBody && singleBodyDocument && autoActivate) {
auto doc = activeView->getAppDocument();
auto bodies = doc->getObjectsOfType(PartDesign::Body::getClassTypeId());
App::DocumentObject *parent = 0;
App::DocumentObject *body = 0;
std::string sub;
if(bodies.size()==1) {
body = bodies[0];
for(auto &v : body->getParents()) {
if(v.first->getDocument()!=doc)
continue;
if(parent) {
body = 0;
break;
}
parent = v.first;
sub = v.second;
}
}
if(body) {
auto doc = parent?parent->getDocument():body->getDocument();
_FCMD_DOC_CMD(Gui,doc,"ActiveView.setActiveObject('" << PDBODYKEY << "',"
<< Gui::Command::getObjectCmd(parent?parent:body) << ",'" << sub << "')");
return activeView->getActiveObject<PartDesign::Body*>(PDBODYKEY,topParent,subname);
activeBody = makeBodyActive(body, doc, topParent, subname);
}
}
if (!activeBody && messageIfNot) {
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("No active Body"),
DlgActiveBody dia(
Gui::getMainWindow(),
doc,
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.\n\nIf you have a legacy document "
"with PartDesign objects without Body, use the migrate function in "
"PartDesign to put them into a Body."
));
"Please make one active (double click) or create one."
"\n\nIf you have a legacy document with PartDesign objects without Body, "
"use the migrate function in PartDesign to put them into a Body."
));
if (dia.exec() == QDialog::DialogCode::Accepted)
activeBody = dia.getActiveBody();
}
}
}
@@ -153,6 +141,36 @@ PartDesign::Body *getBody(bool messageIfNot, bool autoActivate, bool assertModer
return activeBody;
}
PartDesign::Body * makeBodyActive(App::DocumentObject *body, App::Document *doc,
App::DocumentObject **topParent,
std::string *subname)
{
App::DocumentObject *parent = 0;
std::string sub;
for(auto &v : body->getParents()) {
if(v.first->getDocument()!=doc)
continue;
if(parent) {
body = 0;
break;
}
parent = v.first;
sub = v.second;
}
if(body) {
auto _doc = parent?parent->getDocument():body->getDocument();
_FCMD_DOC_CMD(Gui, _doc, "ActiveView.setActiveObject('" << PDBODYKEY
<< "'," << Gui::Command::getObjectCmd(parent?parent:body)
<< ",'" << sub << "')");
return Gui::Application::Instance->activeView()->
getActiveObject<PartDesign::Body*>(PDBODYKEY,topParent,subname);
}
return dynamic_cast<PartDesign::Body*>(body);
}
void needActiveBodyError(void)
{
QMessageBox::warning( Gui::getMainWindow(),
@@ -170,13 +188,8 @@ PartDesign::Body * makeBody(App::Document *doc)
"App.getDocument('%s').addObject('PartDesign::Body','%s')",
doc->getName(), bodyName.c_str() );
auto body = dynamic_cast<PartDesign::Body*>(doc->getObject(bodyName.c_str()));
if(body) {
auto vp = Gui::Application::Instance->getViewProvider(body);
if(vp) {
// make the new body active
vp->doubleClicked();
}
}
if(body)
makeBodyActive(body, doc);
return body;
}