Salome HOME
updated copyright message
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Box.cpp
index c8b7baa94a4273f21ab94c8c786890f51c575a28..1a2149ee7be806d1b44b9ee294a3b5c139ea99d7 100644 (file)
@@ -1,8 +1,21 @@
-// Copyright (C) 2014-2016 CEA/DEN, EDF R&D
-
-// File:        GeomAlgoAPI_Box.cpp
-// Created:     10 Mar 2016
-// Author:      Clarisse Genrault (CEA)
+// Copyright (C) 2014-2023  CEA, EDF
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 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
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
 #include <GeomAlgoAPI_Box.h>
 
@@ -20,19 +33,73 @@ GeomAlgoAPI_Box::GeomAlgoAPI_Box(const double theDx, const double theDy, const d
   myDx = theDx;
   myDy = theDy;
   myDz = theDz;
+  myMethodType = MethodType::BOX_DIM;
+  headError = "Box builder with dimensions";
+}
+
+//=================================================================================================
+GeomAlgoAPI_Box::GeomAlgoAPI_Box(std::shared_ptr<GeomAPI_Pnt> theFirstPoint,
+                                 std::shared_ptr<GeomAPI_Pnt> theSecondPoint)
+{
+  myFirstPoint = theFirstPoint;
+  mySecondPoint = theSecondPoint;
+  myMethodType = MethodType::BOX_POINTS;
+  headError = "Box builder with two points";
+}
+
+//=================================================================================================
+GeomAlgoAPI_Box::GeomAlgoAPI_Box(const double theOx, const double theOy, const double theOz,
+                                 const double theDx, const double theDy, const double theDz)
+{
+  myOx = theOx;
+  myOy = theOy;
+  myOz = theOz;
+  myDx = theDx;
+  myDy = theDy;
+  myDz = theDz;
+  myMethodType = MethodType::BOX_POINT_DIMS;
+  headError = "Box builder with coordinates and dimensions";
 }
 
 //=================================================================================================
 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 || myMethodType == MethodType::BOX_POINT_DIMS) {
+    if (myDx < Precision::Confusion()) {
+      myError = headError + " :: Dx is null or negative.";
+      return false;
+    } else if (myDy < Precision::Confusion()) {
+      myError = headError + " :: Dy is null or negative.";
+      return false;
+    } else if (myDz < Precision::Confusion()) {
+      myError = headError + " :: Dz is null or negative.";
+      return false;
+    }
+  } else if (myMethodType == MethodType::BOX_POINTS) {
+    if (!myFirstPoint.get()) {
+      myError = headError + " :: the first point is not valid.";
+      return false;
+    }
+    if (!mySecondPoint.get()) {
+      myError = headError + " :: the second point is not valid.";
+      return false;
+    }
+    if (myFirstPoint->distance(mySecondPoint) < Precision::Confusion()) {
+      myError = headError + " :: 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 =
+        headError + " :: 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 +107,21 @@ bool GeomAlgoAPI_Box::check()
 
 //=================================================================================================
 void GeomAlgoAPI_Box::build()
+{
+  if (myMethodType == MethodType::BOX_DIM) {
+    buildWithDimensions();
+  } else if (myMethodType == MethodType::BOX_POINTS) {
+    buildWithPoints();
+  } else if (myMethodType == MethodType::BOX_POINT_DIMS) {
+    buildWithPointAndDims();
+  } else {
+    myError = "Box builder :: Method not implemented.";
+    return;
+  }
+}
+
+//=================================================================================================
+void GeomAlgoAPI_Box::buildWithDimensions()
 {
   myCreatedFaces.clear();
 
@@ -49,7 +131,7 @@ void GeomAlgoAPI_Box::build()
 
   // Test the algorithm
   if (!aBoxMaker->IsDone()) {
-    myError = "Box builder with dimensions  :: algorithm failed.";
+    myError = headError + " :: algorithm failed.";
     return;
   }
 
@@ -60,7 +142,42 @@ void GeomAlgoAPI_Box::build()
 
   // Test on the shapes
   if (!aShape.get() || aShape->isNull()) {
-    myError = "Box builder with dimensions  :: resulting shape is null.";
+    myError = headError + " :: resulting shape is null.";
+    return;
+  }
+
+  setImpl(aBoxMaker);
+
+  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 = headError + " :: 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 = headError + " :: resulting shape is null.";
     return;
   }
 
@@ -69,6 +186,18 @@ void GeomAlgoAPI_Box::build()
   setDone(true);
 }
 
+//=================================================================================================
+void GeomAlgoAPI_Box::buildWithPointAndDims()
+{
+  // Construct points from cordinates and dimensions to use the method with two points
+  myFirstPoint =
+    std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(myOx - myDx, myOy - myDy, myOz - myDz));
+  mySecondPoint =
+    std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(myOx + myDx, myOy + myDy, myOz + myDz));
+
+  buildWithPoints();
+}
+
 //=================================================================================================
 void GeomAlgoAPI_Box::prepareNamingFaces()
 {