diff --git a/src/Gui/CommandStructure.cpp b/src/Gui/CommandStructure.cpp index b465c4055c..1fd797e675 100644 --- a/src/Gui/CommandStructure.cpp +++ b/src/Gui/CommandStructure.cpp @@ -32,6 +32,7 @@ #include "ActiveObjectList.h" #include "Application.h" #include "Document.h" +#include "MDIView.h" #include "ViewProviderDocumentObject.h" #include "Selection.h" @@ -121,9 +122,27 @@ void StdCmdGroup::activated(int iMsg) std::string GroupName; GroupName = getUniqueObjectName("Group"); QString label = QApplication::translate("Std_Group", "Group"); - doCommand(Doc,"App.activeDocument().Tip = App.activeDocument().addObject('App::DocumentObjectGroup','%s')",GroupName.c_str()); - doCommand(Doc,"App.activeDocument().%s.Label = '%s'", GroupName.c_str(), - label.toUtf8().data()); + + // create a group + doCommand(Doc,"group = App.activeDocument().addObject('App::DocumentObjectGroup','%s')",GroupName.c_str()); + doCommand(Doc,"group.Label = '%s'", label.toUtf8().data()); + doCommand(Doc,"App.activeDocument().Tip = group"); + + // try to add the group to any active object that supports grouping (has GroupExtension) + if (auto* activeDoc = Gui::Application::Instance->activeDocument()) { + if (auto* activeView = activeDoc->getActiveView()) { + // find the first active object with GroupExtension + if (auto* activeObj = activeView->getActiveObjectWithExtension( + App::GroupExtension::getExtensionClassTypeId())) { + doCommand(Doc, + "active_obj = App.activeDocument().getObject('%s')\n" + "if active_obj and active_obj.allowObject(group):\n" + " active_obj.Group += [group]", + activeObj->getNameInDocument()); + } + } + } // if we have no active object, group will be added to root doc + commitCommand(); Gui::Document* gui = Application::Instance->activeDocument();