Salome HOME
Issue #273: Add copyright string
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Placement.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_Placement.cpp
4 // Created:     2 Dec 2014
5 // Author:      Artem ZHIDKOV
6
7 #include <GeomAlgoAPI_Placement.h>
8 #include <GeomAlgoAPI_DFLoader.h>
9
10 #include <GeomAPI_Pnt.h>
11
12 #include <BRepBuilderAPI_Transform.hxx>
13 #include <gp_Trsf.hxx>
14 #include <gp_Quaternion.hxx>
15 #include <TopExp_Explorer.hxx>
16 #include <BRepCheck_Analyzer.hxx>
17 #include <GProp_GProps.hxx>
18 #include <BRepGProp.hxx>
19 #include <Precision.hxx>
20 #define DEB_PLACEMENT 1
21 GeomAlgoAPI_Placement::GeomAlgoAPI_Placement(
22     std::shared_ptr<GeomAPI_Shape> theAttractiveFace,
23     std::shared_ptr<GeomAPI_Pln> theSourcePlane,
24     std::shared_ptr<GeomAPI_Pln> theDestPlane)
25   : myDone(false),
26     myShape(new GeomAPI_Shape())
27 {
28   build(theAttractiveFace, theSourcePlane, theDestPlane);
29 }
30
31 void GeomAlgoAPI_Placement::build(
32     const std::shared_ptr<GeomAPI_Shape>& theAttractiveShape,
33     const std::shared_ptr<GeomAPI_Pln>& theSourcePlane,
34     const std::shared_ptr<GeomAPI_Pln>& theDestPlane)
35 {
36   std::shared_ptr<GeomAPI_Dir> aSourceDir = theSourcePlane->direction();
37   std::shared_ptr<GeomAPI_Pnt> aSourceLoc = theSourcePlane->location();
38   std::shared_ptr<GeomAPI_Dir> aDestDir = theDestPlane->direction();
39   std::shared_ptr<GeomAPI_Pnt> aDestLoc = theDestPlane->location();
40
41   // Calculate transformation
42   gp_Trsf aTrsf;
43   gp_Vec aSrcDir(aSourceDir->x(), aSourceDir->y(), aSourceDir->z());
44   gp_Vec aDstDir(aDestDir->x(), aDestDir->y(), aDestDir->z());
45   gp_Quaternion aRot(aSrcDir, aDstDir);
46   aTrsf.SetRotation(aRot);
47   gp_Vec aSrcCenter(aSourceLoc->x(), aSourceLoc->y(), aSourceLoc->z());
48   aSrcCenter.Transform(aTrsf);
49   gp_Vec aTrans(aDestLoc->x() - aSrcCenter.X(),
50                 aDestLoc->y() - aSrcCenter.Y(),
51                 aDestLoc->z() - aSrcCenter.Z());
52   aTrsf.SetTransformation(aRot, aTrans);
53
54   // Transform the shape with copying it
55   const TopoDS_Shape& aShape = theAttractiveShape->impl<TopoDS_Shape>();
56   BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aShape, aTrsf, true);
57   if(aBuilder) {
58     setImpl(aBuilder);
59     myDone = aBuilder->IsDone() == Standard_True;
60     if (myDone) {
61       TopoDS_Shape 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 #ifdef DEB_PLACEMENT
69           int aNum = myMap.size();
70           cout << "MAP of Oriented shapes =" << aNum <<endl;
71
72 #endif
73
74       myShape->setImpl(new TopoDS_Shape(aResult));
75       myMkShape = new GeomAlgoAPI_MakeShape (aBuilder);
76     }
77   }
78 }
79
80 //============================================================================
81 const bool GeomAlgoAPI_Placement::isValid() const
82 {
83   BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
84   return (aChecker.IsValid() == Standard_True);
85 }
86
87 //============================================================================
88 const bool GeomAlgoAPI_Placement::hasVolume() const
89 {
90   bool hasVolume(false);
91   if(isValid()) {
92     const TopoDS_Shape& aRShape = myShape->impl<TopoDS_Shape>();
93     GProp_GProps aGProp;
94     BRepGProp::VolumeProperties(aRShape, aGProp);
95     if(aGProp.Mass() > Precision::Confusion()) 
96       hasVolume = true; 
97   }
98   return hasVolume;
99 }
100
101 //============================================================================
102 const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Placement::shape () const 
103 {
104   return myShape;
105 }
106
107 //============================================================================
108 void GeomAlgoAPI_Placement::mapOfShapes (GeomAPI_DataMapOfShapeShape& theMap) const
109 {
110   theMap = myMap;
111 }
112
113 //============================================================================
114 GeomAlgoAPI_MakeShape * GeomAlgoAPI_Placement::makeShape() const
115 {
116   return myMkShape;
117 }
118
119 //============================================================================
120 GeomAlgoAPI_Placement::~GeomAlgoAPI_Placement()
121 {
122   if (myImpl)
123     myMap.clear();
124 }