Part: Auto-add primitives to active Part

Modified DlgPrimitives.cpp to allow auto adding the newly created object to active Std_Part.
Works with: box, cylinder, sphere, cone, torus, tube, primitives.
This commit is contained in:
carlopav
2021-09-26 22:34:32 +02:00
committed by wmayer
parent 57ce37d867
commit 263f2e91da
3 changed files with 44 additions and 0 deletions

View File

@@ -53,6 +53,9 @@ class CommandTube:
tube = FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Tube")
Shapes.TubeFeature(tube)
vp = ViewProviderShapes.ViewProviderTube(tube.ViewObject)
activePart = FreeCADGui.activeView().getActiveObject('part')
if activePart:
activePart.addObject(tube)
FreeCAD.ActiveDocument.recompute()
vp.startDefaultEditMode(tube.ViewObject)

View File

@@ -30,9 +30,29 @@
# include <QLineEdit>
#endif
#include <App/Part.h>
#include <Gui/Application.h>
#include <Gui/Command.h>
#include <Gui/MainWindow.h>
#include <Gui/Document.h>
//===========================================================================
// Utils
//===========================================================================
namespace {
QString getAutoGroupCommandStr()
// Helper function to get the python code to add the newly created object to the active Part object if present
{
App::Part* activePart = Gui::Application::Instance->activeView()->getActiveObject<App::Part*>("part");
if (activePart) {
QString activePartName = QString::fromLatin1(activePart->getNameInDocument());
return QString::fromLatin1("App.ActiveDocument.getObject('%1\')."
"addObject(App.ActiveDocument.ActiveObject)\n")
.arg(activePartName);
}
return QString::fromLatin1("# Object created at document root.");
}
}
//===========================================================================
// Part_Cylinder
@@ -62,6 +82,7 @@ void CmdPartCylinder::activated(int iMsg)
cmd = QString::fromLatin1("App.ActiveDocument.ActiveObject.Label = \"%1\"")
.arg(qApp->translate("CmdPartCylinder","Cylinder"));
runCommand(Doc,cmd.toUtf8());
runCommand(Doc, getAutoGroupCommandStr().toUtf8());
commitCommand();
updateActive();
runCommand(Gui, "Gui.SendMsgToActiveView(\"ViewFit\")");
@@ -103,6 +124,7 @@ void CmdPartBox::activated(int iMsg)
cmd = QString::fromLatin1("App.ActiveDocument.ActiveObject.Label = \"%1\"")
.arg(qApp->translate("CmdPartBox","Cube"));
runCommand(Doc,cmd.toUtf8());
runCommand(Doc, getAutoGroupCommandStr().toUtf8());
commitCommand();
updateActive();
runCommand(Gui, "Gui.SendMsgToActiveView(\"ViewFit\")");
@@ -144,6 +166,7 @@ void CmdPartSphere::activated(int iMsg)
cmd = QString::fromLatin1("App.ActiveDocument.ActiveObject.Label = \"%1\"")
.arg(qApp->translate("CmdPartSphere","Sphere"));
runCommand(Doc,cmd.toUtf8());
runCommand(Doc, getAutoGroupCommandStr().toUtf8());
commitCommand();
updateActive();
runCommand(Gui, "Gui.SendMsgToActiveView(\"ViewFit\")");
@@ -185,6 +208,7 @@ void CmdPartCone::activated(int iMsg)
cmd = QString::fromLatin1("App.ActiveDocument.ActiveObject.Label = \"%1\"")
.arg(qApp->translate("CmdPartCone","Cone"));
runCommand(Doc,cmd.toUtf8());
runCommand(Doc, getAutoGroupCommandStr().toUtf8());
commitCommand();
updateActive();
runCommand(Gui, "Gui.SendMsgToActiveView(\"ViewFit\")");
@@ -226,6 +250,7 @@ void CmdPartTorus::activated(int iMsg)
cmd = QString::fromLatin1("App.ActiveDocument.ActiveObject.Label = \"%1\"")
.arg(qApp->translate("CmdPartTorus","Torus"));
runCommand(Doc,cmd.toUtf8());
runCommand(Doc, getAutoGroupCommandStr().toUtf8());
commitCommand();
updateActive();
runCommand(Gui, "Gui.SendMsgToActiveView(\"ViewFit\")");

View File

@@ -43,6 +43,7 @@
#include <Base/Tools.h>
#include <Base/UnitsApi.h>
#include <App/Application.h>
#include <App/Part.h>
#include <App/Document.h>
#include <Gui/Application.h>
#include <Gui/Document.h>
@@ -64,6 +65,20 @@ using namespace PartGui;
namespace PartGui {
QString getAutoGroupCommandStr(QString objectName)
// Helper function to get the python code to add the newly created object to the active Part object if present
{
App::Part* activePart = Gui::Application::Instance->activeView()->getActiveObject<App::Part*>("part");
if (activePart) {
QString activeObjectName = QString::fromLatin1(activePart->getNameInDocument());
return QString::fromLatin1("App.ActiveDocument.getObject('%1\')."
"addObject(App.ActiveDocument.getObject('%2\'))\n")
.arg(activeObjectName)
.arg(objectName);
}
return QString::fromLatin1("# Object %1 created at document root").arg(objectName);
}
const char* gce_ErrorStatusText(gce_ErrorType et)
{
switch (et)
@@ -1107,6 +1122,7 @@ void DlgPrimitives::createPrimitive(const QString& placement)
QString prim = tr("Create %1").arg(ui->PrimitiveTypeCB->currentText());
Gui::Application::Instance->activeDocument()->openCommand(prim.toUtf8());
Gui::Command::runCommand(Gui::Command::Doc, cmd.toUtf8());
Gui::Command::runCommand(Gui::Command::Doc, getAutoGroupCommandStr(name).toUtf8());
Gui::Application::Instance->activeDocument()->commitCommand();
Gui::Command::runCommand(Gui::Command::Doc, "App.ActiveDocument.recompute()");
Gui::Command::runCommand(Gui::Command::Gui, "Gui.SendMsgToActiveView(\"ViewFit\")");