From 263f2e91da962de890ddd4a08f6e321ab7f22f53 Mon Sep 17 00:00:00 2001 From: carlopav Date: Sun, 26 Sep 2021 22:34:32 +0200 Subject: [PATCH] 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. --- src/Mod/Part/BasicShapes/CommandShapes.py | 3 +++ src/Mod/Part/Gui/CommandParametric.cpp | 25 +++++++++++++++++++++++ src/Mod/Part/Gui/DlgPrimitives.cpp | 16 +++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/src/Mod/Part/BasicShapes/CommandShapes.py b/src/Mod/Part/BasicShapes/CommandShapes.py index 1b2601f2f5..6b5639e244 100644 --- a/src/Mod/Part/BasicShapes/CommandShapes.py +++ b/src/Mod/Part/BasicShapes/CommandShapes.py @@ -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) diff --git a/src/Mod/Part/Gui/CommandParametric.cpp b/src/Mod/Part/Gui/CommandParametric.cpp index 7cb297458b..9196add7ae 100644 --- a/src/Mod/Part/Gui/CommandParametric.cpp +++ b/src/Mod/Part/Gui/CommandParametric.cpp @@ -30,9 +30,29 @@ # include #endif +#include #include #include #include +#include + +//=========================================================================== +// 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("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\")"); diff --git a/src/Mod/Part/Gui/DlgPrimitives.cpp b/src/Mod/Part/Gui/DlgPrimitives.cpp index 3e428146ad..ee3193748b 100644 --- a/src/Mod/Part/Gui/DlgPrimitives.cpp +++ b/src/Mod/Part/Gui/DlgPrimitives.cpp @@ -43,6 +43,7 @@ #include #include #include +#include #include #include #include @@ -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("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\")");