Salome HOME
#1118 Crash the SALOME application after export
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Translation.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_Translation.cpp
4 // Created:     8 June 2015
5 // Author:      Dmitry Bobylev
6
7 #include <GeomAlgoAPI_Translation.h>
8
9 #include <GeomAlgoAPI_ShapeTools.h>
10
11 #include <BRepBuilderAPI_Transform.hxx>
12 #include <BRepCheck_Analyzer.hxx>
13 #include <gp_Ax1.hxx>
14 #include <Precision.hxx>
15 #include <TopExp_Explorer.hxx>
16
17 //=================================================================================================
18 GeomAlgoAPI_Translation::GeomAlgoAPI_Translation(std::shared_ptr<GeomAPI_Shape> theSourceShape,
19                                                  std::shared_ptr<GeomAPI_Ax1>   theAxis,
20                                                  double                         theDistance)
21 : myDone(false)
22 {
23   build(theSourceShape, theAxis, theDistance);
24 }
25
26 //=================================================================================================
27 void GeomAlgoAPI_Translation::build(std::shared_ptr<GeomAPI_Shape> theSourceShape,
28                                     std::shared_ptr<GeomAPI_Ax1>   theAxis,
29                                     double                         theDistance)
30 {
31   if(!theSourceShape || !theAxis) {
32     return;
33   }
34
35   const TopoDS_Shape& aSourceShape = theSourceShape->impl<TopoDS_Shape>();
36   const gp_Ax1& anAxis = theAxis->impl<gp_Ax1>();
37
38   if(aSourceShape.IsNull()) {
39     return;
40   }
41
42   gp_Trsf* aTrsf = new gp_Trsf();
43   aTrsf->SetTranslation(gp_Vec(anAxis.Direction()) * theDistance);
44   myTrsf.reset(new GeomAPI_Trsf(aTrsf));
45
46   // Transform the shape with copying it.
47   BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, *aTrsf, true);
48   if(!aBuilder) {
49     return;
50   }
51   myMkShape.reset(new GeomAlgoAPI_MakeShape(aBuilder));
52
53   myDone = aBuilder->IsDone() == Standard_True;
54
55   if(!myDone) {
56     return;
57   }
58
59   TopoDS_Shape aResult = aBuilder->Shape();
60   // Fill data map to keep correct orientation of sub-shapes.
61   myMap.reset(new GeomAPI_DataMapOfShapeShape());
62   for(TopExp_Explorer anExp(aResult, TopAbs_FACE); anExp.More(); anExp.Next()) {
63     std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
64     aCurrentShape->setImpl(new TopoDS_Shape(anExp.Current()));
65     myMap->bind(aCurrentShape, aCurrentShape);
66   }
67
68   myShape.reset(new GeomAPI_Shape());
69   myShape->setImpl(new TopoDS_Shape(aResult));
70 }
71
72 //=================================================================================================
73 const bool GeomAlgoAPI_Translation::isValid() const
74 {
75   BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
76   return (aChecker.IsValid() == Standard_True);
77 }
78
79 //=================================================================================================
80 const bool GeomAlgoAPI_Translation::hasVolume() const
81 {
82   bool hasVolume(false);
83   if(isValid() && (GeomAlgoAPI_ShapeTools::volume(myShape) > Precision::Confusion())) {
84     hasVolume = true;
85   }
86   return hasVolume;
87 }
88
89 //=================================================================================================
90 const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Translation::shape() const
91 {
92   return myShape;
93 }
94
95 //=================================================================================================
96 std::shared_ptr<GeomAPI_DataMapOfShapeShape> GeomAlgoAPI_Translation::mapOfShapes() const
97 {
98   return myMap;
99 }
100
101 //=================================================================================================
102 std::shared_ptr<GeomAlgoAPI_MakeShape> GeomAlgoAPI_Translation::makeShape() const
103 {
104   return myMkShape;
105 }
106
107 //=================================================================================================
108 std::shared_ptr<GeomAPI_Trsf> GeomAlgoAPI_Translation::transformation() const
109 {
110   return myTrsf;
111 }