Salome HOME
Task 2.1. Creation of ellipses and arcs of ellipse.
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Box.cpp
index c8b7baa94a4273f21ab94c8c786890f51c575a28..1e51608a3fa09909d390332859ae3fc71a43e398 100644 (file)
@@ -20,19 +20,57 @@ GeomAlgoAPI_Box::GeomAlgoAPI_Box(const double theDx, const double theDy, const d
   myDx = theDx;
   myDy = theDy;
   myDz = theDz;
+  myMethodType = MethodType::BOX_DIM;
+}
+
+//=================================================================================================
+GeomAlgoAPI_Box::GeomAlgoAPI_Box(std::shared_ptr<GeomAPI_Pnt> theFirstPoint,
+                                 std::shared_ptr<GeomAPI_Pnt> theSecondPoint)
+{
+  myFirstPoint = theFirstPoint;
+  mySecondPoint = theSecondPoint;
+  myMethodType = MethodType::BOX_POINTS;
 }
 
 //=================================================================================================
 bool GeomAlgoAPI_Box::check()
 {
-  if (myDx < Precision::Confusion()) {
-    myError = "Box builder with dimensions  :: Dx is null.";
-    return false;
-  } else if (myDy < Precision::Confusion()) {
-    myError = "Box builder with dimensions  :: Dy is null.";
-    return false;
-  } else if (myDz < Precision::Confusion()) {
-    myError = "Box builder with dimensions  :: Dz is null.";
+  if (myMethodType == MethodType::BOX_DIM) {
+    if (myDx < Precision::Confusion()) {
+      myError = "Box builder with dimensions :: Dx is null or negative.";
+      return false;
+    } else if (myDy < Precision::Confusion()) {
+      myError = "Box builder with dimensions :: Dy is null or negative.";
+      return false;
+    } else if (myDz < Precision::Confusion()) {
+      myError = "Box builder with dimensions :: Dz is null or negative.";
+      return false;
+    }
+  } else if (myMethodType == MethodType::BOX_POINTS) {
+    if (!myFirstPoint.get()) {
+      myError = "Box builder with points :: the first point is not valid.";
+      return false;
+    }
+    if (!mySecondPoint.get()) {
+      myError = "Box builder with points :: the second point is not valid.";
+      return false;
+    }
+    if (myFirstPoint->distance(mySecondPoint) < Precision::Confusion()) {
+      myError = "Box builder with points :: the distance between the two points is null.";
+      return false;
+    }
+    double aDiffX = myFirstPoint->x() - mySecondPoint->x();
+    double aDiffY = myFirstPoint->y() - mySecondPoint->y();
+    double aDiffZ = myFirstPoint->z() - mySecondPoint->z();
+    if (fabs(aDiffX)  < Precision::Confusion() ||
+        fabs(aDiffY)  < Precision::Confusion() ||
+        fabs(aDiffZ)  < Precision::Confusion()) {
+      myError =
+        "Box builder with points :: the points belong both to one of the OXY, OYZ or OZX planes.";
+      return false;
+    }
+  } else {
+    myError = "Box builder :: Method not implemented.";
     return false;
   }
   return true;
@@ -40,6 +78,19 @@ bool GeomAlgoAPI_Box::check()
 
 //=================================================================================================
 void GeomAlgoAPI_Box::build()
+{
+  if (myMethodType == MethodType::BOX_DIM) {
+    buildWithDimensions();
+  } else if (myMethodType == MethodType::BOX_POINTS) {
+    buildWithPoints();
+  } else {
+    myError = "Box builder :: Method not implemented.";
+    return;
+  }
+}
+
+//=================================================================================================
+void GeomAlgoAPI_Box::buildWithDimensions()
 {
   myCreatedFaces.clear();
 
@@ -69,6 +120,41 @@ void GeomAlgoAPI_Box::build()
   setDone(true);
 }
 
+//=================================================================================================
+void GeomAlgoAPI_Box::buildWithPoints()
+{
+  myCreatedFaces.clear();
+
+  const gp_Pnt& aFirstPoint = myFirstPoint->impl<gp_Pnt>();
+  const gp_Pnt& aSecondPoint = mySecondPoint->impl<gp_Pnt>();
+
+  // Construct the box
+  BRepPrimAPI_MakeBox *aBoxMaker = new  BRepPrimAPI_MakeBox(aFirstPoint, aSecondPoint);
+  aBoxMaker->Build();
+
+  // Test the algorithm
+  if(!aBoxMaker->IsDone()) {
+    myError = "Box builder with two points  :: algorithm failed.";
+    return;
+  }
+
+  TopoDS_Shape aResult = aBoxMaker->Shape();
+
+  std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
+  aShape->setImpl(new TopoDS_Shape(aResult));
+  setShape(aShape);
+
+  // Tests on the shape
+  if (!aShape.get() || aShape->isNull()) {
+    myError = "Box builder with two points  :: resulting shape is null.";
+    return;
+  }
+
+  setImpl(aBoxMaker);
+
+  setDone(true);
+}
+
 //=================================================================================================
 void GeomAlgoAPI_Box::prepareNamingFaces()
 {