From ce36dcc379b030eeb08640a43f852007673edd96 Mon Sep 17 00:00:00 2001 From: hlorus Date: Sun, 14 Jan 2024 21:12:04 +0100 Subject: [PATCH] [Part] Move VectorAdapter into own file in PartGui --- src/Mod/Part/App/CMakeLists.txt | 2 + src/Mod/Part/App/VectorAdapter.cpp | 156 +++++++++++++++++++++++++++++ src/Mod/Part/App/VectorAdapter.h | 90 +++++++++++++++++ src/Mod/Part/Gui/TaskDimension.cpp | 94 +---------------- src/Mod/Part/Gui/TaskDimension.h | 43 +------- 5 files changed, 252 insertions(+), 133 deletions(-) create mode 100644 src/Mod/Part/App/VectorAdapter.cpp create mode 100644 src/Mod/Part/App/VectorAdapter.h diff --git a/src/Mod/Part/App/CMakeLists.txt b/src/Mod/Part/App/CMakeLists.txt index 0e6e3f3637..2c0267a77f 100644 --- a/src/Mod/Part/App/CMakeLists.txt +++ b/src/Mod/Part/App/CMakeLists.txt @@ -219,6 +219,8 @@ SET(Features_SRCS AttachExtension.cpp PrismExtension.cpp PrismExtension.h + VectorAdapter.cpp + VectorAdapter.h ) SOURCE_GROUP("Features" FILES ${Features_SRCS}) diff --git a/src/Mod/Part/App/VectorAdapter.cpp b/src/Mod/Part/App/VectorAdapter.cpp new file mode 100644 index 0000000000..a2f9cdec58 --- /dev/null +++ b/src/Mod/Part/App/VectorAdapter.cpp @@ -0,0 +1,156 @@ +/*************************************************************************** + * Copyright (c) 2022 Werner Mayer * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307, USA * + * * + ***************************************************************************/ + + +#include "PreCompiled.h" + +#include +#include +#include "VectorAdapter.h" +#include "Base/Console.h" +#include + +#include "PrimitiveFeature.h" +#include "PartFeature.h" + +#include +#include +#include +#include +#include +#include +#include +#include + + +namespace Part { + + +VectorAdapter::VectorAdapter() : status(false), vector() +{ +} + +VectorAdapter::VectorAdapter(const TopoDS_Face &faceIn, const gp_Vec &pickedPointIn) : + status(false), vector(), origin(pickedPointIn) +{ + Handle(Geom_Surface) surface = BRep_Tool::Surface(faceIn); + if (surface->IsKind(STANDARD_TYPE(Geom_ElementarySurface))) + { + Handle(Geom_ElementarySurface) eSurface = Handle(Geom_ElementarySurface)::DownCast(surface); + gp_Dir direction = eSurface->Axis().Direction(); + vector = direction; + vector.Normalize(); + if (faceIn.Orientation() == TopAbs_REVERSED) { + vector.Reverse(); + } + if (surface->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) || + surface->IsKind(STANDARD_TYPE(Geom_SphericalSurface)) + ) + { + origin = eSurface->Axis().Location().XYZ(); + projectOriginOntoVector(pickedPointIn); + } + else { + origin = pickedPointIn + vector; + } + status = true; + } +} + +VectorAdapter::VectorAdapter(const TopoDS_Edge &edgeIn, const gp_Vec &pickedPointIn) : + status(false), vector(), origin(pickedPointIn) +{ + TopoDS_Vertex firstVertex = TopExp::FirstVertex(edgeIn, Standard_True); + TopoDS_Vertex lastVertex = TopExp::LastVertex(edgeIn, Standard_True); + vector = convert(lastVertex) - convert(firstVertex); + if (vector.Magnitude() < Precision::Confusion()) { + return; + } + vector.Normalize(); + + status = true; + projectOriginOntoVector(pickedPointIn); +} + +VectorAdapter::VectorAdapter(const TopoDS_Vertex &vertex1In, const TopoDS_Vertex &vertex2In) : + status(false), vector(), origin() +{ + vector = convert(vertex2In) - convert(vertex1In); + vector.Normalize(); + + //build origin half way. + gp_Vec tempVector = (convert(vertex2In) - convert(vertex1In)); + double mag = tempVector.Magnitude(); + tempVector.Normalize(); + tempVector *= (mag / 2.0); + origin = tempVector + convert(vertex1In); + + status = true; +} + +VectorAdapter::VectorAdapter(const gp_Vec &vector1, const gp_Vec &vector2) : + status(false), vector(), origin() +{ + vector = vector2- vector1; + vector.Normalize(); + + //build origin half way. + gp_Vec tempVector = vector2 - vector1; + double mag = tempVector.Magnitude(); + tempVector.Normalize(); + tempVector *= (mag / 2.0); + origin = tempVector + vector1; + + status = true; +} + +void VectorAdapter::projectOriginOntoVector(const gp_Vec &pickedPointIn) +{ + Handle(Geom_Curve) heapLine = new Geom_Line(origin.XYZ(), vector.XYZ()); + gp_Pnt tempPoint(pickedPointIn.XYZ()); + GeomAPI_ProjectPointOnCurve projection(tempPoint, heapLine); + if (projection.NbPoints() < 1) { + return; + } + origin.SetXYZ(projection.Point(1).XYZ()); +} + +VectorAdapter::operator gp_Lin() const +{ + gp_Pnt tempOrigin; + tempOrigin.SetXYZ(origin.XYZ()); + return gp_Lin(tempOrigin, gp_Dir(vector)); +} + + +/*convert a vertex to vector*/ +gp_Vec VectorAdapter::convert(const TopoDS_Vertex &vertex) +{ + gp_Pnt point = BRep_Tool::Pnt(vertex); + gp_Vec out(point.X(), point.Y(), point.Z()); + return out; +} + + + +} + diff --git a/src/Mod/Part/App/VectorAdapter.h b/src/Mod/Part/App/VectorAdapter.h new file mode 100644 index 0000000000..2196401305 --- /dev/null +++ b/src/Mod/Part/App/VectorAdapter.h @@ -0,0 +1,90 @@ +/*************************************************************************** + * Copyright (c) 2022 Werner Mayer * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307, USA * + * * + ***************************************************************************/ + +#ifndef PART_VECTORADAPTER_H +#define PART_VECTORADAPTER_H + +#include + +#include +#include +#include +#include +#include +#include + +namespace Part +{ + + +/*! @brief Convert to vector + * + * Used to construct a vector from various input types + */ +class VectorAdapter +{ +public: + /*!default construction isValid is set to false*/ + VectorAdapter(); + /*!Build a vector from a faceIn + * @param faceIn vector will be normal to plane and equal to cylindrical axis. + * @param pickedPointIn location of pick. straight conversion from sbvec. not accurate.*/ + VectorAdapter(const TopoDS_Face &faceIn, const gp_Vec &pickedPointIn); + /*!Build a vector from an edgeIn + * @param edgeIn vector will be lastPoint - firstPoint. + * @param pickedPointIn location of pick. straight conversion from sbvec. not accurate.*/ + VectorAdapter(const TopoDS_Edge &edgeIn, const gp_Vec &pickedPointIn); + /*!Build a vector From 2 vertices. + *vector will be equal to @param vertex2In - @param vertex1In.*/ + VectorAdapter(const TopoDS_Vertex &vertex1In, const TopoDS_Vertex &vertex2In); + /*!Build a vector From 2 vectors. + *vector will be equal to @param vector2 - @param vector1.*/ + VectorAdapter(const gp_Vec &vector1, const gp_Vec &vector2); + + /*!make sure no errors in vector construction. + * @return true = vector is good. false = vector is NOT good.*/ + bool isValid() const {return status;} + /*!get the calculated vector. + * @return the vector. use isValid to ensure correct results.*/ + operator gp_Vec() const {return vector;}//explicit bombs + /*!build occ line used for extrema calculation*/ + operator gp_Lin() const;//explicit bombs + gp_Vec getPickPoint() const {return origin;} + + operator Base::Vector3d() const { + return Base::Vector3d(vector.X(), vector.Y(), vector.Z()); + } + + + static gp_Vec convert(const TopoDS_Vertex& vertex); + + private: + void projectOriginOntoVector(const gp_Vec &pickedPointIn); + bool status; + gp_Vec vector; + gp_Vec origin; +}; + + +} //namespace Part + +#endif diff --git a/src/Mod/Part/Gui/TaskDimension.cpp b/src/Mod/Part/Gui/TaskDimension.cpp index 8812d4dbb9..aba9cff004 100644 --- a/src/Mod/Part/Gui/TaskDimension.cpp +++ b/src/Mod/Part/Gui/TaskDimension.cpp @@ -737,98 +737,6 @@ void PartGui::TaskMeasureLinear::clearAllSlot(bool) PartGui::eraseAllDimensions(); } -PartGui::VectorAdapter::VectorAdapter() : status(false), vector() -{ -} - -PartGui::VectorAdapter::VectorAdapter(const TopoDS_Face &faceIn, const gp_Vec &pickedPointIn) : - status(false), vector(), origin(pickedPointIn) -{ - Handle(Geom_Surface) surface = BRep_Tool::Surface(faceIn); - if (surface->IsKind(STANDARD_TYPE(Geom_ElementarySurface))) - { - Handle(Geom_ElementarySurface) eSurface = Handle(Geom_ElementarySurface)::DownCast(surface); - gp_Dir direction = eSurface->Axis().Direction(); - vector = direction; - vector.Normalize(); - if (faceIn.Orientation() == TopAbs_REVERSED) - vector.Reverse(); - if (surface->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) || - surface->IsKind(STANDARD_TYPE(Geom_SphericalSurface)) - ) - { - origin = eSurface->Axis().Location().XYZ(); - projectOriginOntoVector(pickedPointIn); - } - else - origin = pickedPointIn + vector; - status = true; - } -} - -PartGui::VectorAdapter::VectorAdapter(const TopoDS_Edge &edgeIn, const gp_Vec &pickedPointIn) : - status(false), vector(), origin(pickedPointIn) -{ - TopoDS_Vertex firstVertex = TopExp::FirstVertex(edgeIn, Standard_True); - TopoDS_Vertex lastVertex = TopExp::LastVertex(edgeIn, Standard_True); - vector = PartGui::convert(lastVertex) - PartGui::convert(firstVertex); - if (vector.Magnitude() < Precision::Confusion()) - return; - vector.Normalize(); - - status = true; - projectOriginOntoVector(pickedPointIn); -} - -PartGui::VectorAdapter::VectorAdapter(const TopoDS_Vertex &vertex1In, const TopoDS_Vertex &vertex2In) : - status(false), vector(), origin() -{ - vector = PartGui::convert(vertex2In) - PartGui::convert(vertex1In); - vector.Normalize(); - - //build origin half way. - gp_Vec tempVector = (PartGui::convert(vertex2In) - PartGui::convert(vertex1In)); - double mag = tempVector.Magnitude(); - tempVector.Normalize(); - tempVector *= (mag / 2.0); - origin = tempVector + PartGui::convert(vertex1In); - - status = true; -} - -PartGui::VectorAdapter::VectorAdapter(const gp_Vec &vector1, const gp_Vec &vector2) : - status(false), vector(), origin() -{ - vector = vector2- vector1; - vector.Normalize(); - - //build origin half way. - gp_Vec tempVector = vector2 - vector1; - double mag = tempVector.Magnitude(); - tempVector.Normalize(); - tempVector *= (mag / 2.0); - origin = tempVector + vector1; - - status = true; -} - -void PartGui::VectorAdapter::projectOriginOntoVector(const gp_Vec &pickedPointIn) -{ - Handle(Geom_Curve) heapLine = new Geom_Line(origin.XYZ(), vector.XYZ()); - gp_Pnt tempPoint(pickedPointIn.XYZ()); - GeomAPI_ProjectPointOnCurve projection(tempPoint, heapLine); - if (projection.NbPoints() < 1) - return; - origin.SetXYZ(projection.Point(1).XYZ()); -} - -PartGui::VectorAdapter::operator gp_Lin() const -{ - gp_Pnt tempOrigin; - tempOrigin.SetXYZ(origin.XYZ()); - return gp_Lin(tempOrigin, gp_Dir(vector)); -} - gp_Vec PartGui::convert(const TopoDS_Vertex &vertex) { gp_Pnt point = BRep_Tool::Pnt(vertex); @@ -1694,7 +1602,7 @@ void PartGui::TaskMeasureAngular::selectionClearDelayedSlot() this->blockSelection(false); } -PartGui::VectorAdapter PartGui::TaskMeasureAngular::buildAdapter(const PartGui::DimSelections& selection) +VectorAdapter PartGui::TaskMeasureAngular::buildAdapter(const PartGui::DimSelections& selection) { Base::Matrix4D mat; assert(selection.selections.size() > 0 && selection.selections.size() < 3); diff --git a/src/Mod/Part/Gui/TaskDimension.h b/src/Mod/Part/Gui/TaskDimension.h index 5a2eb07943..5f17ed8de3 100644 --- a/src/Mod/Part/Gui/TaskDimension.h +++ b/src/Mod/Part/Gui/TaskDimension.h @@ -35,6 +35,7 @@ #include #include #include +#include #include #include @@ -52,6 +53,8 @@ class QPushButton; class QPixmap; class QLabel; +using namespace Part; + namespace Gui{class View3dInventorViewer;} namespace PartGui @@ -280,46 +283,6 @@ private: }; -/*! @brief Convert to vector - * - * Used to construct a vector from various input types - */ -class VectorAdapter -{ -public: - /*!default construction isValid is set to false*/ - VectorAdapter(); - /*!Build a vector from a faceIn - * @param faceIn vector will be normal to plane and equal to cylindrical axis. - * @param pickedPointIn location of pick. straight conversion from sbvec. not accurate.*/ - VectorAdapter(const TopoDS_Face &faceIn, const gp_Vec &pickedPointIn); - /*!Build a vector from an edgeIn - * @param edgeIn vector will be lastPoint - firstPoint. - * @param pickedPointIn location of pick. straight conversion from sbvec. not accurate.*/ - VectorAdapter(const TopoDS_Edge &edgeIn, const gp_Vec &pickedPointIn); - /*!Build a vector From 2 vertices. - *vector will be equal to @param vertex2In - @param vertex1In.*/ - VectorAdapter(const TopoDS_Vertex &vertex1In, const TopoDS_Vertex &vertex2In); - /*!Build a vector From 2 vectors. - *vector will be equal to @param vector2 - @param vector1.*/ - VectorAdapter(const gp_Vec &vector1, const gp_Vec &vector2); - - /*!make sure no errors in vector construction. - * @return true = vector is good. false = vector is NOT good.*/ - bool isValid() const {return status;} - /*!get the calculated vector. - * @return the vector. use isValid to ensure correct results.*/ - operator gp_Vec() const {return vector;}//explicit bombs - /*!build occ line used for extrema calculation*/ - operator gp_Lin() const;//explicit bombs - gp_Vec getPickPoint() const {return origin;} - -private: - void projectOriginOntoVector(const gp_Vec &pickedPointIn); - bool status; - gp_Vec vector; - gp_Vec origin; -}; /*!angular dialog class*/ class TaskMeasureAngular : public Gui::TaskView::TaskDialog, public Gui::SelectionObserver