From 3fae3fac63a4ed04f551435543ad8908e00de27e Mon Sep 17 00:00:00 2001 From: Bas Ruigrok Date: Sun, 12 May 2024 14:45:16 +0200 Subject: [PATCH] Gui: Add alignToSelection() method and command --- src/Gui/CommandView.cpp | 28 +++++++++++++++++++++++++++ src/Gui/View3DInventor.cpp | 7 +++++++ src/Gui/View3DInventorViewer.cpp | 33 ++++++++++++++++++++++++++++++++ src/Gui/View3DInventorViewer.h | 2 ++ 4 files changed, 70 insertions(+) diff --git a/src/Gui/CommandView.cpp b/src/Gui/CommandView.cpp index f4cb50cd8c4e..6e77adfb1736 100644 --- a/src/Gui/CommandView.cpp +++ b/src/Gui/CommandView.cpp @@ -4076,6 +4076,33 @@ bool StdRecallWorkingView::isActive() return view && view->getViewer()->hasHomePosition(); } +//=========================================================================== +// Std_AlignToSelection +//=========================================================================== + +DEF_STD_CMD_A(StdCmdAlignToSelection) + +StdCmdAlignToSelection::StdCmdAlignToSelection() + : Command("Std_AlignToSelection") +{ + sGroup = "Standard-View"; + sMenuText = QT_TR_NOOP("Align to selection"); + sToolTipText = QT_TR_NOOP("Align the view with the selection"); + sWhatsThis = "Std_AlignToSelection"; + eType = Alter3DView; +} + +void StdCmdAlignToSelection::activated(int iMsg) +{ + Q_UNUSED(iMsg); + doCommand(Command::Gui,"Gui.SendMsgToActiveView(\"AlignToSelection\")"); +} + +bool StdCmdAlignToSelection::isActive() +{ + return getGuiApplication()->sendHasMsgToActiveView("AlignToSelection"); +} + //=========================================================================== // Instantiation //=========================================================================== @@ -4107,6 +4134,7 @@ void CreateViewStdCommands() rcCmdMgr.addCommand(new StdStoreWorkingView()); rcCmdMgr.addCommand(new StdRecallWorkingView()); rcCmdMgr.addCommand(new StdCmdViewGroup()); + rcCmdMgr.addCommand(new StdCmdAlignToSelection()); rcCmdMgr.addCommand(new StdCmdViewExample1()); rcCmdMgr.addCommand(new StdCmdViewExample2()); diff --git a/src/Gui/View3DInventor.cpp b/src/Gui/View3DInventor.cpp index 937128baf1ee..7770a71f2ed8 100644 --- a/src/Gui/View3DInventor.cpp +++ b/src/Gui/View3DInventor.cpp @@ -408,6 +408,10 @@ bool View3DInventor::onMsg(const char* pMsg, const char** ppReturn) getGuiDocument()->saveCopy(); return true; } + else if (strcmp("AlignToSelection", pMsg) == 0) { + _viewer->alignToSelection(); + return true; + } else if (strcmp("ZoomIn", pMsg) == 0) { View3DInventorViewer* viewer = getViewer(); viewer->navigationStyle()->zoomIn(); @@ -511,6 +515,9 @@ bool View3DInventor::onHasMsg(const char* pMsg) const else if(strncmp("Dump",pMsg,4) == 0) { return true; } + else if (strcmp("AlignToSelection", pMsg) == 0) { + return true; + } if (strcmp("ZoomIn", pMsg) == 0) { return true; } diff --git a/src/Gui/View3DInventorViewer.cpp b/src/Gui/View3DInventorViewer.cpp index ee5313b0819a..c792191e0c41 100644 --- a/src/Gui/View3DInventorViewer.cpp +++ b/src/Gui/View3DInventorViewer.cpp @@ -3341,6 +3341,39 @@ void View3DInventorViewer::viewSelection() } } +void View3DInventorViewer::alignToSelection() +{ + if (!getCamera()) { + return; + } + + const auto selection = Selection().getSelection(); + + // Empty selection + if (selection.empty()) { + return; + } + + // Too much selections + if (selection.size() > 1) { + return; + } + + // Get the geo feature + App::GeoFeature* geoFeature = nullptr; + std::pair elementName; + App::GeoFeature::resolveElement(selection[0].pObject, selection[0].SubName, elementName, false, App::GeoFeature::ElementNameType::Normal, nullptr, nullptr, &geoFeature); + if (!geoFeature) { + return; + } + + Base::Vector3d direction; + if (geoFeature->getCameraAlignmentDirection(direction, selection[0].SubName)) { + const auto orientation = SbRotation(SbVec3f(0, 0, 1), Base::convertTo(direction)); + setCameraOrientation(orientation); + } +} + /** * @brief Decide if it should be possible to start any animation * diff --git a/src/Gui/View3DInventorViewer.h b/src/Gui/View3DInventorViewer.h index eca7f186b016..cdcdc5c853e8 100644 --- a/src/Gui/View3DInventorViewer.h +++ b/src/Gui/View3DInventorViewer.h @@ -414,6 +414,8 @@ class GuiExport View3DInventorViewer : public Quarter::SoQTQuarterAdaptor, publi */ void viewSelection(); + void alignToSelection(); + void setGradientBackground(Background); Background getGradientBackground() const; void setGradientBackgroundColor(const SbColor& fromColor,