From 32e6545db090868a3191b68f45a9d092f01b6d9e Mon Sep 17 00:00:00 2001 From: vsv Date: Tue, 16 Dec 2014 19:24:17 +0300 Subject: [PATCH] Make plane as close as possible to selected shape by its size --- .../ConstructionPlugin_Plane.cpp | 25 ++++++++++++++++++- src/GeomAPI/GeomAPI_Pnt.cpp | 13 ++++++++++ src/GeomAPI/GeomAPI_Pnt.h | 4 +++ src/GeomAPI/GeomAPI_Shape.cpp | 14 +++++++++++ src/GeomAPI/GeomAPI_Shape.h | 5 ++++ src/GeomAlgoAPI/GeomAlgoAPI_FaceBuilder.cpp | 18 +++++++++++++ src/GeomAlgoAPI/GeomAlgoAPI_FaceBuilder.h | 5 ++++ 7 files changed, 83 insertions(+), 1 deletion(-) diff --git a/src/ConstructionPlugin/ConstructionPlugin_Plane.cpp b/src/ConstructionPlugin/ConstructionPlugin_Plane.cpp index 37faed970..547540243 100644 --- a/src/ConstructionPlugin/ConstructionPlugin_Plane.cpp +++ b/src/ConstructionPlugin/ConstructionPlugin_Plane.cpp @@ -11,6 +11,8 @@ #include #include +#include + #define PLANE_SIZE 300 @@ -39,8 +41,29 @@ void ConstructionPlugin_Plane::execute() std::shared_ptr aDir = aPln->direction(); aOrig->translate(aDir, aDist); + std::shared_ptr aNewPln = + std::shared_ptr(new GeomAPI_Pln(aOrig, aDir)); + + // Create rectangular face close to the selected + double aXmin, aYmin, Zmin, aXmax, aYmax, Zmax; + aShape->computeSize(aXmin, aYmin, Zmin, aXmax, aYmax, Zmax); + + std::shared_ptr aPnt1 = + std::shared_ptr(new GeomAPI_Pnt(aXmin, aYmin, Zmin)); + std::shared_ptr aPnt2 = + std::shared_ptr(new GeomAPI_Pnt(aXmax, aYmax, Zmax)); + + std::shared_ptr aPnt2d1 = aPnt1->to2D(aNewPln); + std::shared_ptr aPnt2d2 = aPnt2->to2D(aNewPln); + + double aWidth = aPnt2d2->x() - aPnt2d1->x(); + double aHeight = aPnt2d2->y() - aPnt2d1->y(); + double aWgap = aWidth * 0.1; + double aHgap = aHeight * 0.1; + std::shared_ptr aPlane = - GeomAlgoAPI_FaceBuilder::square(aOrig, aDir, PLANE_SIZE); + GeomAlgoAPI_FaceBuilder::planarFace(aNewPln, aPnt2d1->x() - aWgap, aPnt2d1->y() - aHgap, + aWidth + 2 * aWgap, aHeight + 2 * aHgap); ResultConstructionPtr aConstr = document()->createConstruction(data()); aConstr->setShape(aPlane); setResult(aConstr); diff --git a/src/GeomAPI/GeomAPI_Pnt.cpp b/src/GeomAPI/GeomAPI_Pnt.cpp index d9bc12b6f..29fe933a6 100644 --- a/src/GeomAPI/GeomAPI_Pnt.cpp +++ b/src/GeomAPI/GeomAPI_Pnt.cpp @@ -8,8 +8,11 @@ #include #include #include +#include #include +#include +#include #define MY_PNT static_cast(myImpl) @@ -82,3 +85,13 @@ void GeomAPI_Pnt::translate(const std::shared_ptr& theDir, double t aVec.Multiply(theDist); MY_PNT->Translate(aVec); } + +std::shared_ptr GeomAPI_Pnt::to2D(const std::shared_ptr& thePln) const +{ + double aA, aB, aC, aD; + thePln->coefficients(aA, aB, aC, aD); + gp_Pln aPln(aA, aB, aC, aD); + + gp_Pnt2d aRes = ProjLib::Project(aPln, *MY_PNT); + return std::shared_ptr(new GeomAPI_Pnt2d(aRes.X(), aRes.Y())); +} diff --git a/src/GeomAPI/GeomAPI_Pnt.h b/src/GeomAPI/GeomAPI_Pnt.h index 835e24939..44f203a77 100644 --- a/src/GeomAPI/GeomAPI_Pnt.h +++ b/src/GeomAPI/GeomAPI_Pnt.h @@ -13,6 +13,7 @@ class GeomAPI_XYZ; class GeomAPI_Pnt2d; class GeomAPI_Dir; +class GeomAPI_Pln; /**\class GeomAPI_Pnt * \ingroup DataModel @@ -52,6 +53,9 @@ class GEOMAPI_EXPORT GeomAPI_Pnt : public GeomAPI_Interface const std::shared_ptr& theDirX, const std::shared_ptr& theDirY); + /// Projects a point to the plane defined by the origin and 2 axes vectors in this plane + std::shared_ptr to2D(const std::shared_ptr& thePln) const; + /// Translates the point along direction theDir on distance theDist void translate(const std::shared_ptr& theDir, double theDist); }; diff --git a/src/GeomAPI/GeomAPI_Shape.cpp b/src/GeomAPI/GeomAPI_Shape.cpp index 36fa298ef..6cb6f74c4 100644 --- a/src/GeomAPI/GeomAPI_Shape.cpp +++ b/src/GeomAPI/GeomAPI_Shape.cpp @@ -7,6 +7,8 @@ #include #include +#include +#include #define MY_SHAPE static_cast(myImpl) @@ -47,3 +49,15 @@ bool GeomAPI_Shape::isFace() const const TopoDS_Shape& aShape = const_cast(this)->impl(); return aShape.ShapeType() == TopAbs_FACE; } + +bool GeomAPI_Shape::computeSize(double& theXmin, double& theYmin, double& theZmin, + double& theXmax, double& theYmax, double& theZmax) const +{ + const TopoDS_Shape& aShape = const_cast(this)->impl(); + if (aShape.IsNull()) + return false; + Bnd_Box aBndBox; + BRepBndLib::Add(aShape, aBndBox); + aBndBox.Get(theXmin, theYmin, theZmin, theXmax, theYmax, theZmax); + return true; +} \ No newline at end of file diff --git a/src/GeomAPI/GeomAPI_Shape.h b/src/GeomAPI/GeomAPI_Shape.h index ced881e65..6302f8606 100644 --- a/src/GeomAPI/GeomAPI_Shape.h +++ b/src/GeomAPI/GeomAPI_Shape.h @@ -35,6 +35,11 @@ class GEOMAPI_EXPORT GeomAPI_Shape : public GeomAPI_Interface /// Returns whether the shape is a face virtual bool isFace() const; + /// Computes boundary dimensions of the shape + /// Returns False if it is not possible + bool computeSize(double& theXmin, double& theYmin, double& theZmin, + double& theXmax, double& theYmax, double& theZmax) const; + }; //! Pointer on list of shapes diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_FaceBuilder.cpp b/src/GeomAlgoAPI/GeomAlgoAPI_FaceBuilder.cpp index 9816c160e..2dc527d9d 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_FaceBuilder.cpp +++ b/src/GeomAlgoAPI/GeomAlgoAPI_FaceBuilder.cpp @@ -52,3 +52,21 @@ std::shared_ptr GeomAlgoAPI_FaceBuilder::plane( aResult = std::shared_ptr(new GeomAPI_Pln(aA, aB, aC, aD)); return aResult; } + + +std::shared_ptr GeomAlgoAPI_FaceBuilder:: + planarFace(std::shared_ptr thePlane, + double theX, double theY, + double theWidth, double theHeight) +{ + double aA, aB, aC, aD; + thePlane->coefficients(aA, aB, aC, aD); + gp_Pln aPlane(aA, aB, aC, aD); + + // half of the size in each direction from the center + BRepBuilderAPI_MakeFace aFaceBuilder(aPlane, theX, theX + theWidth, + theY, theY + theHeight); + std::shared_ptr aRes(new GeomAPI_Shape); + aRes->setImpl(new TopoDS_Shape(aFaceBuilder.Face())); + return aRes; +} \ No newline at end of file diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_FaceBuilder.h b/src/GeomAlgoAPI/GeomAlgoAPI_FaceBuilder.h index 85badd9d7..7db03a41b 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_FaceBuilder.h +++ b/src/GeomAlgoAPI/GeomAlgoAPI_FaceBuilder.h @@ -30,6 +30,11 @@ class GEOMALGOAPI_EXPORT GeomAlgoAPI_FaceBuilder /// Returns the plane of the planar face. If it is not planar, returns empty ptr. static std::shared_ptr plane(std::shared_ptr theFace); + + /// Creates a planar face by given plane, left lower point and size. + static std::shared_ptr planarFace(std::shared_ptr thePlane, + double theX, double theY, + double theWidth, double theHeight); }; #endif -- 2.39.2