From f0fb8ddabe2e32b947fc459963751433ec5a1f06 Mon Sep 17 00:00:00 2001 From: Joona Okkonen Date: Mon, 20 Jan 2025 19:11:09 +0200 Subject: [PATCH] Sketcher: Fixed mapping error when attempting to create a sketch inside a group. (#18971) * 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. * Update to use new templated countObjectsOfType This commit updates the code to use the new templated version of countObjectsOfType, aligning with recent changes in the main branch. --------- Co-authored-by: Joona --- 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 87882fc4b5..fda6934924 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() > 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))",