From efa1dd9c63045b79d5266e47e1d6c76d2e0fcece Mon Sep 17 00:00:00 2001 From: wmayer Date: Mon, 5 Dec 2011 15:41:39 +0000 Subject: [PATCH] 0000508: Part: Parameterized Primitive: Plane - new planes with normals x or y are placed wrong git-svn-id: https://free-cad.svn.sourceforge.net/svnroot/free-cad/trunk@5221 e8eeb9e2-ec13-0410-a4a9-efa5cf37419d --- src/Mod/Part/Gui/DlgPrimitives.cpp | 63 +++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/src/Mod/Part/Gui/DlgPrimitives.cpp b/src/Mod/Part/Gui/DlgPrimitives.cpp index aac97e74f..4be9e537c 100644 --- a/src/Mod/Part/Gui/DlgPrimitives.cpp +++ b/src/Mod/Part/Gui/DlgPrimitives.cpp @@ -24,6 +24,9 @@ #include "PreCompiled.h" #ifndef _PreComp_ #include +#include +#include +#include #include #include #endif @@ -127,12 +130,68 @@ DlgPrimitives::~DlgPrimitives() viewer->removeEventCallback(SoMouseButtonEvent::getClassTypeId(), pickCallback,this); } } + +namespace Base { +// Specialization for gp_XYZ +template <> +struct vec_traits { + typedef gp_XYZ vec_type; + typedef double float_type; + vec_traits(const vec_type& v) : v(v){} + inline float_type x() { return v.X(); } + inline float_type y() { return v.Y(); } + inline float_type z() { return v.Z(); } +private: + const vec_type& v; +}; +} QString DlgPrimitives::toPlacement() const { Base::Vector3f d = ui.getDirection(); - Base::Rotation rot(Base::Vector3d(0.0,0.0,1.0), - Base::Vector3d(d.x,d.y,d.z)); + gp_Dir dir = gp_Dir(d.x,d.y,d.z); + gp_Pnt pnt = gp_Pnt(0.0,0.0,0.0); + gp_Ax3 ax3; + + double cosNX = dir.Dot(gp::DX()); + double cosNY = dir.Dot(gp::DY()); + double cosNZ = dir.Dot(gp::DZ()); + std::vector cosXYZ; + cosXYZ.push_back(fabs(cosNX)); + cosXYZ.push_back(fabs(cosNY)); + cosXYZ.push_back(fabs(cosNZ)); + + int pos = std::max_element(cosXYZ.begin(), cosXYZ.end()) - cosXYZ.begin(); + + // +X/-X + if (pos == 0) { + if (cosNX > 0) + ax3 = gp_Ax3(pnt, dir, gp_Dir(0,1,0)); + else + ax3 = gp_Ax3(pnt, dir, gp_Dir(0,-1,0)); + } + // +Y/-Y + else if (pos == 1) { + if (cosNY > 0) + ax3 = gp_Ax3(pnt, dir, gp_Dir(0,0,1)); + else + ax3 = gp_Ax3(pnt, dir, gp_Dir(0,0,-1)); + } + // +Z/-Z + else { + ax3 = gp_Ax3(pnt, dir, gp_Dir(1,0,0)); + } + + gp_Trsf Trf; + Trf.SetTransformation(ax3); + Trf.Invert(); + + gp_XYZ theAxis(0,0,1); + Standard_Real theAngle = 0.0; + Trf.GetRotation(theAxis,theAngle); + + Base::Rotation rot(Base::convertTo(theAxis), theAngle); + return QString::fromAscii("Base.Placement(Base.Vector(%1,%2,%3),Base.Rotation(%4,%5,%6,%7))") .arg(ui.xPos->value(),0,'f',2) .arg(ui.yPos->value(),0,'f',2)