]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
Make plane as close as possible to selected shape by its size
authorvsv <vitaly.smetannikov@opencascade.com>
Tue, 16 Dec 2014 16:24:17 +0000 (19:24 +0300)
committervsv <vitaly.smetannikov@opencascade.com>
Tue, 16 Dec 2014 16:24:17 +0000 (19:24 +0300)
src/ConstructionPlugin/ConstructionPlugin_Plane.cpp
src/GeomAPI/GeomAPI_Pnt.cpp
src/GeomAPI/GeomAPI_Pnt.h
src/GeomAPI/GeomAPI_Shape.cpp
src/GeomAPI/GeomAPI_Shape.h
src/GeomAlgoAPI/GeomAlgoAPI_FaceBuilder.cpp
src/GeomAlgoAPI/GeomAlgoAPI_FaceBuilder.h

index 37faed97091646b4f309eda8efca90f12f5ee3e2..5475402437a3a707cf5e5dbb3ef90dc55be4f413 100644 (file)
@@ -11,6 +11,8 @@
 #include <ModelAPI_ResultConstruction.h>
 #include <GeomAlgoAPI_FaceBuilder.h>
 
+#include <GeomAPI_Pnt2d.h>
+
 
 #define PLANE_SIZE 300
 
@@ -39,8 +41,29 @@ void ConstructionPlugin_Plane::execute()
       std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
 
       aOrig->translate(aDir, aDist);
+      std::shared_ptr<GeomAPI_Pln> aNewPln = 
+        std::shared_ptr<GeomAPI_Pln>(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<GeomAPI_Pnt> aPnt1 = 
+        std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aXmin, aYmin, Zmin));
+      std::shared_ptr<GeomAPI_Pnt> aPnt2 = 
+        std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aXmax, aYmax, Zmax));
+
+      std::shared_ptr<GeomAPI_Pnt2d> aPnt2d1 = aPnt1->to2D(aNewPln);
+      std::shared_ptr<GeomAPI_Pnt2d> 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<GeomAPI_Shape> 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);
index d9bc12b6f5704927313f93c22b19f43e20822e22..29fe933a668f87ddfab4985fbf3ff1b910e12c6a 100644 (file)
@@ -8,8 +8,11 @@
 #include<GeomAPI_XYZ.h>
 #include<GeomAPI_Pnt2d.h>
 #include<GeomAPI_Dir.h>
+#include<GeomAPI_Pln.h>
 
 #include<gp_Pnt.hxx>
+#include<gp_Pln.hxx>
+#include<ProjLib.hxx>
 
 #define MY_PNT static_cast<gp_Pnt*>(myImpl)
 
@@ -82,3 +85,13 @@ void GeomAPI_Pnt::translate(const std::shared_ptr<GeomAPI_Dir>& theDir, double t
   aVec.Multiply(theDist);
   MY_PNT->Translate(aVec);
 }
+
+std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Pnt::to2D(const std::shared_ptr<GeomAPI_Pln>& 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<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aRes.X(), aRes.Y()));
+}
index 835e24939a601e84990387ff7e405960d0bd53eb..44f203a7735232d5b8691da10a3b3f2b3b62f44e 100644 (file)
@@ -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<GeomAPI_Dir>& theDirX,
                                         const std::shared_ptr<GeomAPI_Dir>& theDirY);
 
+  /// Projects a point to the plane defined by the origin and 2 axes vectors in this plane
+  std::shared_ptr<GeomAPI_Pnt2d> to2D(const std::shared_ptr<GeomAPI_Pln>& thePln) const;
+
   /// Translates the point along direction theDir on distance theDist
   void translate(const std::shared_ptr<GeomAPI_Dir>& theDir, double theDist);
 };
index 36fa298ef1b2e48dad1c9dcef4bca6136fba4ba4..6cb6f74c4b8b0dcbf7a31ce122f9c0177442deac 100644 (file)
@@ -7,6 +7,8 @@
 #include<GeomAPI_Shape.h>
 
 #include <TopoDS_Shape.hxx>
+#include <BRepBndLib.hxx>
+#include <Bnd_Box.hxx>
 
 #define MY_SHAPE static_cast<TopoDS_Shape*>(myImpl)
 
@@ -47,3 +49,15 @@ bool GeomAPI_Shape::isFace() const
   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
   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<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
+  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
index ced881e65c394f0daf8b5cb15c75309d233a0608..6302f8606de051e68e675b655ea6e3193f4d81ab 100644 (file)
@@ -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
index 9816c160eca4f85ec422f50814da9d5c28d9bb75..2dc527d9d5462e97016396853b31144b6e9f754a 100644 (file)
@@ -52,3 +52,21 @@ std::shared_ptr<GeomAPI_Pln> GeomAlgoAPI_FaceBuilder::plane(
   aResult = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
   return aResult;
 }
+
+
+std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_FaceBuilder::
+  planarFace(std::shared_ptr<GeomAPI_Pln> 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<GeomAPI_Shape> aRes(new GeomAPI_Shape);
+  aRes->setImpl(new TopoDS_Shape(aFaceBuilder.Face()));
+  return aRes;
+}
\ No newline at end of file
index 85badd9d73fd65b4d8dc66495f994c2ab9f02606..7db03a41b72cd57084eef25a5c4182e68756ff0f 100644 (file)
@@ -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<GeomAPI_Pln> plane(std::shared_ptr<GeomAPI_Shape> theFace);
+
+  /// Creates a planar face by given plane, left lower point and size.
+  static std::shared_ptr<GeomAPI_Shape> planarFace(std::shared_ptr<GeomAPI_Pln> thePlane,
+                                                   double theX, double theY,
+                                                   double theWidth, double theHeight);
 };
 
 #endif