From 7dca20f979674d7dc478ff086bf217dde9a9efe2 Mon Sep 17 00:00:00 2001 From: Joona Okkonen Date: Fri, 24 Jan 2025 16:04:20 +0200 Subject: [PATCH] Backport version. Sketcher: Fixed mapping error when attempting to create a sketch inside a group. (#19215) * Sketcher mapping error fix Fixed an issue in the Sketcher where attempting to create a sketch while a group is selected caused a mapping error. Now, when a sketch is created with a group selected, the sketch will be placed inside the group. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Joona Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- src/Mod/Sketcher/Gui/Command.cpp | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/Mod/Sketcher/Gui/Command.cpp b/src/Mod/Sketcher/Gui/Command.cpp index c1cea15ffc..95f135b1c9 100644 --- a/src/Mod/Sketcher/Gui/Command.cpp +++ b/src/Mod/Sketcher/Gui/Command.cpp @@ -163,8 +163,23 @@ void CmdSketcherNewSketch::activated(int iMsg) { Q_UNUSED(iMsg); Attacher::eMapMode mapmode = Attacher::mmDeactivated; + std::string groupName; bool bAttach = false; - if (Gui::Selection().hasSelection()) { + bool groupSelected = false; + if (Gui::Selection().countObjectsOfType(App::DocumentObjectGroup::getClassTypeId()) > 0) { + auto selection = Gui::Selection().getSelection(); + if (selection.size() > 1) { + Gui::TranslatedUserWarning( + getActiveGuiDocument(), + QObject::tr("Invalid selection"), + QObject::tr("Too many objects selected")); + return; + } + + groupName = selection[0].FeatName; + groupSelected = true; + } + else if (Gui::Selection().hasSelection()) { Attacher::SuggestResult::eSuggestResult msgid = Attacher::SuggestResult::srOK; QString msg_str; std::vector validModes; @@ -270,9 +285,18 @@ void CmdSketcherNewSketch::activated(int iMsg) std::string FeatName = getUniqueObjectName("Sketch"); openCommand(QT_TRANSLATE_NOOP("Command", "Create a new sketch")); - doCommand(Doc, + if (groupSelected) { + doCommand(Doc, + "App.activeDocument().getObject('%s').addObject(App.activeDocument().addObject('Sketcher::SketchObject', '%s'))", + groupName.c_str(), + FeatName.c_str()); + } + else { + doCommand(Doc, "App.activeDocument().addObject('Sketcher::SketchObject', '%s')", FeatName.c_str()); + } + doCommand(Doc, "App.activeDocument().%s.Placement = App.Placement(App.Vector(%f, %f, %f), " "App.Rotation(%f, %f, %f, %f))",