Salome HOME
New feature "Placement" was implemented
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Placement.cpp
1 // File:        GeomAlgoAPI_Placement.cpp
2 // Created:     2 Dec 2014
3 // Author:      Artem ZHIDKOV
4
5 #include <GeomAlgoAPI_Placement.h>
6 #include <GeomAlgoAPI_DFLoader.h>
7
8 #include <GeomAPI_Pnt.h>
9
10 #include <BRepBuilderAPI_Transform.hxx>
11 #include <gp_Trsf.hxx>
12 #include <gp_Quaternion.hxx>
13 #include <TopExp_Explorer.hxx>
14 #include <BRepCheck_Analyzer.hxx>
15 #include <GProp_GProps.hxx>
16 #include <BRepGProp.hxx>
17 #include <Precision.hxx>
18
19
20 GeomAlgoAPI_Placement::GeomAlgoAPI_Placement(
21     std::shared_ptr<GeomAPI_Shape> theAttractiveFace,
22     std::shared_ptr<GeomAPI_Pln> theSourcePlane,
23     std::shared_ptr<GeomAPI_Pln> theDestPlane)
24   : myDone(false),
25     myShape(new GeomAPI_Shape())
26 {
27   build(theAttractiveFace, theSourcePlane, theDestPlane);
28 }
29
30 void GeomAlgoAPI_Placement::build(
31     const std::shared_ptr<GeomAPI_Shape>& theAttractiveShape,
32     const std::shared_ptr<GeomAPI_Pln>& theSourcePlane,
33     const std::shared_ptr<GeomAPI_Pln>& theDestPlane)
34 {
35   std::shared_ptr<GeomAPI_Dir> aSourceDir = theSourcePlane->direction();
36   std::shared_ptr<GeomAPI_Pnt> aSourceLoc = theSourcePlane->location();
37   std::shared_ptr<GeomAPI_Dir> aDestDir = theDestPlane->direction();
38   std::shared_ptr<GeomAPI_Pnt> aDestLoc = theDestPlane->location();
39
40   // Calculate transformation
41   gp_Trsf aTrsf;
42   gp_Vec aSrcDir(aSourceDir->x(), aSourceDir->y(), aSourceDir->z());
43   gp_Vec aDstDir(aDestDir->x(), aDestDir->y(), aDestDir->z());
44   gp_Quaternion aRot(aSrcDir, aDstDir);
45   gp_Vec aTrans(aDestLoc->x() - aSourceLoc->x(),
46                 aDestLoc->y() - aSourceLoc->y(),
47                 aDestLoc->z() - aSourceLoc->z());
48   aTrsf.SetTransformation(aRot, aTrans);
49
50   // Transform the shape with copying it
51   const TopoDS_Shape& aShape = theAttractiveShape->impl<TopoDS_Shape>();
52   BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aShape, aTrsf, true);
53   if(aBuilder) {
54     setImpl(aBuilder);
55     myDone = aBuilder->IsDone() == Standard_True;
56     if (myDone) {
57       TopoDS_Shape aResult;
58       if(aBuilder->Shape().ShapeType() == TopAbs_COMPOUND) 
59         aResult = GeomAlgoAPI_DFLoader::refineResult(aBuilder->Shape());
60       else
61         aResult = aBuilder->Shape();
62       // fill data map to keep correct orientation of sub-shapes 
63       for (TopExp_Explorer Exp(aResult,TopAbs_FACE); Exp.More(); Exp.Next()) {
64         std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
65         aCurrentShape->setImpl(new TopoDS_Shape(Exp.Current()));
66         myMap.bind(aCurrentShape, aCurrentShape);
67       }   
68       myShape->setImpl(new TopoDS_Shape(aResult));
69       myMkShape = new GeomAlgoAPI_MakeShape (aBuilder);
70     }
71   }
72 }
73
74 //============================================================================
75 const bool GeomAlgoAPI_Placement::isValid() const
76 {
77   BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
78   return (aChecker.IsValid() == Standard_True);
79 }
80
81 //============================================================================
82 const bool GeomAlgoAPI_Placement::hasVolume() const
83 {
84   bool hasVolume(false);
85   if(isValid()) {
86     const TopoDS_Shape& aRShape = myShape->impl<TopoDS_Shape>();
87     GProp_GProps aGProp;
88     BRepGProp::VolumeProperties(aRShape, aGProp);
89     if(aGProp.Mass() > Precision::Confusion()) 
90       hasVolume = true; 
91   }
92   return hasVolume;
93 }
94
95 //============================================================================
96 const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Placement::shape () const 
97 {
98   return myShape;
99 }
100
101 //============================================================================
102 void GeomAlgoAPI_Placement::mapOfShapes (GeomAPI_DataMapOfShapeShape& theMap) const
103 {
104   theMap = myMap;
105 }
106
107 //============================================================================
108 GeomAlgoAPI_MakeShape * GeomAlgoAPI_Placement::makeShape() const
109 {
110   return myMkShape;
111 }
112
113 //============================================================================
114 GeomAlgoAPI_Placement::~GeomAlgoAPI_Placement()
115 {
116   if (myImpl)
117     myMap.clear();
118 }