Skip to content

Commit

Permalink
Gui: Add alignToSelection() method and command
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexbas committed May 13, 2024
1 parent fb1539f commit 3fae3fa
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Gui/CommandView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
//===========================================================================
Expand Down Expand Up @@ -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());
Expand Down
7 changes: 7 additions & 0 deletions src/Gui/View3DInventor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down
33 changes: 33 additions & 0 deletions src/Gui/View3DInventorViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::string> 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<SbVec3f>(direction));
setCameraOrientation(orientation);
}
}

/**
* @brief Decide if it should be possible to start any animation
*
Expand Down
2 changes: 2 additions & 0 deletions src/Gui/View3DInventorViewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 3fae3fa

Please sign in to comment.