[TechDraw] Add ability to align points by rotating view

Fixes #7061
This commit is contained in:
Benjamin Bræstrup Sayoc
2024-09-14 00:20:53 +02:00
committed by WandererFan
parent 516595fbce
commit 4d9e4efc87
12 changed files with 340 additions and 0 deletions

View File

@@ -1,5 +1,6 @@
/***************************************************************************
* Copyright (c) 2016 WandererFan <wandererfan@gmail.com> *
* Copyright (c) 2024 Benjamin Bræstrup Sayoc <benj5378@outlook.com> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
@@ -48,6 +49,7 @@
#include <Base/Exception.h>
#include <Base/Parameter.h>
#include <Base/Tools.h>
#include <Base/Tools2D.h>
#include <Base/Type.h>
#include <Gui/Application.h>
#include <Gui/Command.h>
@@ -74,6 +76,9 @@
#include "DlgPageChooser.h"
#include "DrawGuiUtil.h"
#include "MDIViewPage.h"
#include "QGIEdge.h"
#include "QGIVertex.h"
#include "QGIViewPart.h"
#include "QGSPage.h"
#include "ViewProviderPage.h"
#include "Rez.h"
@@ -776,3 +781,53 @@ QIcon DrawGuiUtil::maskBlackPixels(QIcon itemIcon, QSize iconSize, QColor textCo
return filler;
}
void DrawGuiUtil::rotateToAlign(const QGIEdge* edge, const Base::Vector2d& direction)
{
QGIViewPart* view = static_cast<QGIViewPart*>(edge->parentItem());
DrawViewPart* dvp = static_cast<DrawViewPart*>(view->getViewObject());
BaseGeomPtr bg = dvp->getEdgeGeometry().at(edge->getProjIndex());
std::vector<Base::Vector3d> endPoints = bg->findEndPoints();
Base::Vector3d oldDirection3d = endPoints.at(0) - endPoints.at(1);
Base::Vector2d oldDirection2d(oldDirection3d.x, oldDirection3d.y);
rotateToAlign(dvp, oldDirection2d, direction);
}
//! The view of p1 and p2 will be rotated to make p1 and p2 aligned with direction (for instance horizontalle aligned)
void DrawGuiUtil::rotateToAlign(const QGIVertex* p1, const QGIVertex* p2, const Base::Vector2d& direction)
{
QGIViewPart* view = static_cast<QGIViewPart*>(p1->parentItem());
if(view != static_cast<QGIViewPart*>(p2->parentItem())) {
Base::Console().Error("Vertexes have to be from the same view!");
}
Base::Vector2d oldDirection = p2->vector2dBetweenPoints(p1);
DrawViewPart* dvp = static_cast<DrawViewPart*>(view->getViewObject());
rotateToAlign(dvp, oldDirection, direction);
}
void DrawGuiUtil::rotateToAlign(DrawViewPart* view, const Base::Vector2d& oldDirection, const Base::Vector2d& newDirection)
{
// If pointing counterclockwise, we need to rotate clockwise
// If pointing clockwise, we need to rotate counter clockwise
int cw = 1;
if(newDirection.Angle() > oldDirection.Angle()) {
cw = -1;
}
double toRotate = newDirection.GetAngle(oldDirection);
// Radians to degrees
toRotate = toRotate * 180 / M_PI;
// Rotate least amount possible
if(toRotate > 90) {
// Instead of rotating 145 degrees to match direction
// we only rotate -35 degrees
toRotate = toRotate - 180;
}
else if(toRotate < -90) {
toRotate = toRotate + 180;
}
double oldRotation = view->Rotation.getValue();
view->Rotation.setValue(oldRotation + toRotate * cw);
}