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 <kadet1090@gmail.com>

* Update src/Gui/CommandStd.cpp

Co-authored-by: Florian Foinant-Willig <FlachyJoe@users.noreply.github.com>

---------

Co-authored-by: Kacper Donat <kadet1090@gmail.com>
Co-authored-by: Florian Foinant-Willig <FlachyJoe@users.noreply.github.com>
This commit is contained in:
Max Wilfinger
2025-11-25 11:47:05 +01:00
committed by GitHub
parent e70c516774
commit 972ade948c
2 changed files with 80 additions and 1 deletions

View File

@@ -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<App::DocumentObject*>(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<App::GeoFeature*>(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<App::AnnotationLabel*>(obj);
label->LabelText.setValue(std::vector<std::string> {"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

View File

@@ -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"