Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Translation.cpp
index b9cc3b690af73a93ab7cd162392a98684f574dc5..574b63d23b74c208f94451a019ee337f19eb3648 100644 (file)
 // File:        GeomAlgoAPI_Translation.cpp
 // Created:     8 June 2015
 // Author:      Dmitry Bobylev
+//
+// Modified by Clarisse Genrault (CEA) : 17 Nov 2016
 
-#include <GeomAlgoAPI_Translation.h>
-
-#include <GeomAlgoAPI_ShapeTools.h>
+#include "GeomAlgoAPI_Translation.h"
 
 #include <BRepBuilderAPI_Transform.hxx>
-#include <BRepCheck_Analyzer.hxx>
-#include <gp_Ax1.hxx>
 #include <Precision.hxx>
-#include <TopExp_Explorer.hxx>
+#include <gp_Ax1.hxx>
 
 //=================================================================================================
 GeomAlgoAPI_Translation::GeomAlgoAPI_Translation(std::shared_ptr<GeomAPI_Shape> theSourceShape,
-                                           std::shared_ptr<GeomAPI_Ax1>   theAxis,
-                                           double                         theDistance,
-                                           bool theSimpleTransform)
-: myDone(false)
+                                                 std::shared_ptr<GeomAPI_Ax1>   theAxis,
+                                                 double                         theDistance)
 {
-  build(theSourceShape, theAxis, theDistance, theSimpleTransform);
+  myMethodType = BY_DISTANCE;
+  mySourceShape = theSourceShape;
+  myAxis = theAxis;
+  myDistance = theDistance;
 }
 
 //=================================================================================================
-void GeomAlgoAPI_Translation::build(std::shared_ptr<GeomAPI_Shape> theSourceShape,
-                                 std::shared_ptr<GeomAPI_Ax1>   theAxis,
-                                 double                         theDistance,
-                                 bool theSimpleTransform)
+GeomAlgoAPI_Translation::GeomAlgoAPI_Translation(std::shared_ptr<GeomAPI_Shape> theSourceShape,
+                                                 double                         theDx,
+                                                 double                         theDy,
+                                                 double                         theDz)
 {
-  if(!theSourceShape || !theAxis) {
-    return;
-  }
-
-  const TopoDS_Shape& aSourceShape = theSourceShape->impl<TopoDS_Shape>();
-  const gp_Ax1& anAxis = theAxis->impl<gp_Ax1>();
-
-  if(aSourceShape.IsNull()) {
-    return;
-  }
-
-  gp_Trsf aTrsf;
-  aTrsf.SetTranslation(gp_Vec(anAxis.Direction()) * theDistance);
-
-  TopoDS_Shape aResult;
-  // Transform the shape with copying it.
-  if (theSimpleTransform) {
-    TopLoc_Location aDelta(aTrsf);
-    aResult = aSourceShape.Moved(aDelta);
-    // store the accumulated information about the result and this delta
-    //myTrsf = std::shared_ptr<GeomAPI_Trsf>(new GeomAPI_Trsf(new gp_Trsf(aTrsf * aSourceShape.Location().Transformation())));
-    myTrsf = std::shared_ptr<GeomAPI_Trsf>(new GeomAPI_Trsf(new gp_Trsf(aTrsf)));
-    myDone = true; // is OK for sure
-  } else {
-    BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, aTrsf, true);
-    if(!aBuilder) {
-      return;
-    }
-    myMkShape.reset(new GeomAlgoAPI_MakeShape(aBuilder));
-
-    myDone = aBuilder->IsDone() == Standard_True;
-
-    if(!myDone) {
-      return;
-    }
-
-    aResult = aBuilder->Shape();
-    // Fill data map to keep correct orientation of sub-shapes.
-    myMap.reset(new GeomAPI_DataMapOfShapeShape());
-    for(TopExp_Explorer anExp(aResult, TopAbs_FACE); anExp.More(); anExp.Next()) {
-      std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
-      aCurrentShape->setImpl(new TopoDS_Shape(anExp.Current()));
-      myMap->bind(aCurrentShape, aCurrentShape);
-    }
-  }
-
-  myShape.reset(new GeomAPI_Shape());
-  myShape->setImpl(new TopoDS_Shape(aResult));
+  myMethodType = BY_DIM;
+  mySourceShape = theSourceShape;
+  myDx = theDx;
+  myDy = theDy;
+  myDz = theDz;
 }
 
 //=================================================================================================
-const bool GeomAlgoAPI_Translation::isValid() const
+GeomAlgoAPI_Translation::GeomAlgoAPI_Translation(std::shared_ptr<GeomAPI_Shape> theSourceShape,
+                                                 std::shared_ptr<GeomAPI_Pnt>   theStartPoint,
+                                                 std::shared_ptr<GeomAPI_Pnt>   theEndPoint)
 {
-  BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
-  return (aChecker.IsValid() == Standard_True);
+  myMethodType = BY_POINTS;
+  mySourceShape = theSourceShape;
+  myStartPoint = theStartPoint;
+  myEndPoint = theEndPoint;
 }
 
 //=================================================================================================
-const bool GeomAlgoAPI_Translation::hasVolume() const
+bool GeomAlgoAPI_Translation::check()
 {
-  bool hasVolume(false);
-  if(isValid() && (GeomAlgoAPI_ShapeTools::volume(myShape) > Precision::Confusion())) {
-    hasVolume = true;
+  switch (myMethodType) {
+    case BY_DISTANCE: {
+      if (!myAxis) {
+        myError = "Translation builder :: axis is invalid.";
+        return false;
+      }
+      if (!mySourceShape) {
+        myError = "Translation builder :: source shape is invalid.";
+        return false;
+      }
+      return true;
+    }
+    case BY_DIM: {
+      if (!mySourceShape) {
+        myError = "Translation builder :: source shape is invalid.";
+        return false;
+      }
+      return true;
+    }
+    case BY_POINTS: {
+      if (!myStartPoint) {
+        myError = "Translation builder :: start point is invalid.";
+        return false;
+      }
+      if (!myEndPoint) {
+        myError = "Translation builder :: start point is invalid.";
+        return false;
+      }
+      if (!mySourceShape) {
+        myError = "Translation builder :: source shape is invalid.";
+        return false;
+      }
+      return true;
+    }
+    default: {
+      myError = "Translation builder :: method not implemented.";
+      return false;
+    }
   }
-  return hasVolume;
 }
 
 //=================================================================================================
-const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Translation::shape() const
+void GeomAlgoAPI_Translation::build()
 {
-  return myShape;
-}
+  gp_Trsf* aTrsf = new gp_Trsf();
 
-//=================================================================================================
-std::shared_ptr<GeomAPI_DataMapOfShapeShape> GeomAlgoAPI_Translation::mapOfShapes() const
-{
-  return myMap;
-}
+  switch (myMethodType) {
+    case BY_DISTANCE: {
+      const gp_Ax1& anAxis = myAxis->impl<gp_Ax1>();
+      aTrsf->SetTranslation(gp_Vec(anAxis.Direction()) * myDistance);
+      break;
+    }
+    case BY_DIM: {
+      aTrsf->SetTranslation(gp_Vec(myDx, myDy, myDz));
+      break;
+    }
+    case BY_POINTS: {
+      const gp_Pnt& aStartPoint = myStartPoint->impl<gp_Pnt>();
+      const gp_Pnt& aEndPoint = myEndPoint->impl<gp_Pnt>();
+      aTrsf->SetTranslation(aStartPoint, aEndPoint);
+      break;
+    }
+    default: {
+      myError = "Translation builder :: method not supported";
+      return;
+    }
+  }
 
-//=================================================================================================
-std::shared_ptr<GeomAlgoAPI_MakeShape> GeomAlgoAPI_Translation::makeShape() const
-{
-  return myMkShape;
-}
+  const TopoDS_Shape& aSourceShape = mySourceShape->impl<TopoDS_Shape>();
 
-//=================================================================================================
-std::shared_ptr<GeomAPI_Trsf> GeomAlgoAPI_Translation::transformation() const
-{
-  return myTrsf;
-}
+  if(aSourceShape.IsNull()) {
+    myError = "Translation builder :: source shape does not contain any actual shape.";
+    return;
+  }
+
+  // Transform the shape while copying it.
+  BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, *aTrsf, true);
+  if(!aBuilder) {
+    myError = "Translation builder :: source shape does not contain any actual shape.";
+    return;
+  }
+
+  setImpl(aBuilder);
+  setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
+
+  if(!aBuilder->IsDone()) {
+    myError = "Translation builder :: source shape does not contain any actual shape.";
+    return;
+  }
+
+  TopoDS_Shape aResult = aBuilder->Shape();
+
+  std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
+  aShape->setImpl(new TopoDS_Shape(aResult));
+  setShape(aShape);
+  setDone(true);
+}
\ No newline at end of file