From 972ade948c41f789798bd49a9fa3945e7aa6e4a1 Mon Sep 17 00:00:00 2001 From: Max Wilfinger <6246609+maxwxyz@users.noreply.github.com> Date: Tue, 25 Nov 2025 11:47:05 +0100 Subject: [PATCH] Gui: Add command to create an AnnotationLabel (#23673) * Gui: Add command to create an AnnotationLabel * Update src/Gui/CommandStd.cpp Co-authored-by: Kacper Donat * Update src/Gui/CommandStd.cpp Co-authored-by: Florian Foinant-Willig --------- Co-authored-by: Kacper Donat Co-authored-by: Florian Foinant-Willig --- src/Gui/CommandStd.cpp | 78 ++++++++++++++++++++++++++++++++++++++++++ src/Gui/Workbench.cpp | 3 +- 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/src/Gui/CommandStd.cpp b/src/Gui/CommandStd.cpp index bb6f6db683..9f0d32bfb2 100644 --- a/src/Gui/CommandStd.cpp +++ b/src/Gui/CommandStd.cpp @@ -51,6 +51,8 @@ #include "WhatsThis.h" #include "Workbench.h" #include "WorkbenchManager.h" +#include "App/GeoFeature.h" +#include "App/Annotation.h" using Base::Console; @@ -965,6 +967,81 @@ void StdCmdReloadStyleSheet::activated(int) Application::Instance->reloadStyleSheet(); } +// ================================================================== +// Std_AnnotationLabel +// ================================================================== +DEF_STD_CMD_A(StdCmdAnnotationLabel) + +StdCmdAnnotationLabel::StdCmdAnnotationLabel() + : Command("Std_AnnotationLabel") +{ + // Put it under Tools menu + sGroup = QT_TR_NOOP("Tools"); + sMenuText = QT_TR_NOOP("Annotation Label"); + sToolTipText = QT_TR_NOOP("Creates a new annotation label at the picked location in the 3D view"); + sWhatsThis = "Std_AnnotationLabel"; + sStatusTip = sToolTipText; + sPixmap = "Tree_Annotation"; + eType = AlterDoc; +} + +bool StdCmdAnnotationLabel::isActive() +{ + return ( + App::GetApplication().getActiveDocument() != nullptr + && !Gui::Selection().getSelectionEx().empty() + ); +} + +void StdCmdAnnotationLabel::activated(int) +{ + auto* doc = App::GetApplication().getActiveDocument(); + if (!doc) { + return; + } + + auto selEx = Gui::Selection().getSelectionEx(); + if (selEx.empty()) { + return; + } + + // Use first selected item in active 3D view + const auto& sel = selEx.front(); + + const auto* support = sel.getObject(); + const auto* supportObj = freecad_cast(support); + + // Compute base position. Fallback: object placement position, else origin + Base::Vector3d basePos; + const auto& picked = sel.getPickedPoints(); + if (!picked.empty()) { + basePos = picked.front(); + } + else if (auto* gf = freecad_cast(supportObj); supportObj && gf) { + const auto& gp = gf->globalPlacement(); + basePos = gp.getPosition(); + } + + // Create the label object + openCommand(QT_TRANSLATE_NOOP("Command", "Create Annotation Label")); + auto* obj = doc->addObject("App::AnnotationLabel", "Label"); + if (!obj) { + abortCommand(); + return; + } + + // Set basic properties + auto* label = freecad_cast(obj); + label->LabelText.setValue(std::vector {"Annotation"}); + + // BasePosition at anchor, TextPosition slightly offset for visibility + label->BasePosition.setValue(basePos); + const auto textPos = basePos + Base::Vector3d(10, 10, 10); + label->TextPosition.setValue(textPos); + + commitCommand(); +} + namespace Gui { @@ -999,6 +1076,7 @@ void CreateStdCommands() rcCmdMgr.addCommand(new StdCmdDevHandbook()); // rcCmdMgr.addCommand(new StdCmdDownloadOnlineHelp()); // rcCmdMgr.addCommand(new StdCmdDescription()); + rcCmdMgr.addCommand(new StdCmdAnnotationLabel()); } } // namespace Gui diff --git a/src/Gui/Workbench.cpp b/src/Gui/Workbench.cpp index e6d5e07e9c..05964a37fd 100644 --- a/src/Gui/Workbench.cpp +++ b/src/Gui/Workbench.cpp @@ -770,8 +770,9 @@ MenuItem* StdWorkbench::setupMenuBar() const } #endif *tool << "Std_Measure" - << "Std_ClarifySelection" + << "Std_AnnotationLabel" << "Std_UnitsCalculator" + << "Std_ClarifySelection" << "Separator" << "Std_ViewLoadImage" << "Std_ViewScreenShot"