From 9154072f469d354a40641043129cfd0d91905a74 Mon Sep 17 00:00:00 2001 From: Bas Ruigrok Date: Wed, 8 May 2024 23:08:09 +0200 Subject: [PATCH] Gui: Add alignToSelection() method and command --- src/Gui/CommandView.cpp | 28 ++++++++++++++++++++ src/Gui/View3DInventor.cpp | 7 +++++ src/Gui/View3DInventorViewer.cpp | 44 ++++++++++++++++++++++++++++++++ src/Gui/View3DInventorViewer.h | 2 ++ 4 files changed, 81 insertions(+) diff --git a/src/Gui/CommandView.cpp b/src/Gui/CommandView.cpp index f4cb50cd8c4e2..6e77adfb1736d 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 937128baf1eeb..7770a71f2ed89 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 ee5313b0819a2..4413bbbfff556 100644 --- a/src/Gui/View3DInventorViewer.cpp +++ b/src/Gui/View3DInventorViewer.cpp @@ -80,6 +80,7 @@ # include #endif +#include #include #include #include @@ -3341,6 +3342,49 @@ 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; + } + + const App::SubObjectT selected(selection[0].pObject, selection[0].SubName); + + Base::PyGILStateLocker lock; + + PyObject* pyObj = nullptr; + Base::Matrix4D matrix; + selected.getObject()->getSubObject(selected.getSubName().c_str(), &pyObj, &matrix); + if (!pyObj) { + return; + } + + if (!PyObject_TypeCheck(pyObj, &Data::ComplexGeoDataPy::Type)) { + return; + } + + const auto geoData = static_cast(pyObj)->getComplexGeoDataPtr(); + + Base::Rotation orientation; + if (geoData->getRotation(orientation)) { + double q0, q1, q2, q3; + orientation.getValue(q0, q1, q2, q3); + setCameraOrientation(SbRotation(q0, q1, q2, q3)); + } +} + /** * @brief Decide if it should be possible to start any animation * diff --git a/src/Gui/View3DInventorViewer.h b/src/Gui/View3DInventorViewer.h index eca7f186b0164..cdcdc5c853e85 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,