]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAlgoAPI/GeomAlgoAPI_Placement.cpp
Salome HOME
7fb667490ae11f4399a10e0dd3317a7cc187e78f
[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 #define DEB_PLACEMENT 1
19 GeomAlgoAPI_Placement::GeomAlgoAPI_Placement(
20     std::shared_ptr<GeomAPI_Shape> theAttractiveFace,
21     std::shared_ptr<GeomAPI_Pln> theSourcePlane,
22     std::shared_ptr<GeomAPI_Pln> theDestPlane)
23   : myDone(false),
24     myShape(new GeomAPI_Shape())
25 {
26   build(theAttractiveFace, theSourcePlane, theDestPlane);
27 }
28
29 void GeomAlgoAPI_Placement::build(
30     const std::shared_ptr<GeomAPI_Shape>& theAttractiveShape,
31     const std::shared_ptr<GeomAPI_Pln>& theSourcePlane,
32     const std::shared_ptr<GeomAPI_Pln>& theDestPlane)
33 {
34   std::shared_ptr<GeomAPI_Dir> aSourceDir = theSourcePlane->direction();
35   std::shared_ptr<GeomAPI_Pnt> aSourceLoc = theSourcePlane->location();
36   std::shared_ptr<GeomAPI_Dir> aDestDir = theDestPlane->direction();
37   std::shared_ptr<GeomAPI_Pnt> aDestLoc = theDestPlane->location();
38
39   // Calculate transformation
40   gp_Trsf aTrsf;
41   gp_Vec aSrcDir(aSourceDir->x(), aSourceDir->y(), aSourceDir->z());
42   gp_Vec aDstDir(aDestDir->x(), aDestDir->y(), aDestDir->z());
43   gp_Quaternion aRot(aSrcDir, aDstDir);
44   aTrsf.SetRotation(aRot);
45   gp_Vec aSrcCenter(aSourceLoc->x(), aSourceLoc->y(), aSourceLoc->z());
46   aSrcCenter.Transform(aTrsf);
47   gp_Vec aTrans(aDestLoc->x() - aSrcCenter.X(),
48                 aDestLoc->y() - aSrcCenter.Y(),
49                 aDestLoc->z() - aSrcCenter.Z());
50   aTrsf.SetTransformation(aRot, aTrans);
51
52   // Transform the shape with copying it
53   const TopoDS_Shape& aShape = theAttractiveShape->impl<TopoDS_Shape>();
54   BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aShape, aTrsf, true);
55   if(aBuilder) {
56     setImpl(aBuilder);
57     myDone = aBuilder->IsDone() == Standard_True;
58     if (myDone) {
59       TopoDS_Shape aResult = aBuilder->Shape();
60       // fill data map to keep correct orientation of sub-shapes 
61       for (TopExp_Explorer Exp(aResult,TopAbs_FACE); Exp.More(); Exp.Next()) {
62         std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
63         aCurrentShape->setImpl(new TopoDS_Shape(Exp.Current()));
64         myMap.bind(aCurrentShape, aCurrentShape);
65       }
66 #ifdef DEB_PLACEMENT
67           int aNum = myMap.size();
68           cout << "MAP of Oriented shapes =" << aNum <<endl;
69
70 #endif
71
72       myShape->setImpl(new TopoDS_Shape(aResult));
73       myMkShape = new GeomAlgoAPI_MakeShape (aBuilder);
74     }
75   }
76 }
77
78 //============================================================================
79 const bool GeomAlgoAPI_Placement::isValid() const
80 {
81   BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
82   return (aChecker.IsValid() == Standard_True);
83 }
84
85 //============================================================================
86 const bool GeomAlgoAPI_Placement::hasVolume() const
87 {
88   bool hasVolume(false);
89   if(isValid()) {
90     const TopoDS_Shape& aRShape = myShape->impl<TopoDS_Shape>();
91     GProp_GProps aGProp;
92     BRepGProp::VolumeProperties(aRShape, aGProp);
93     if(aGProp.Mass() > Precision::Confusion()) 
94       hasVolume = true; 
95   }
96   return hasVolume;
97 }
98
99 //============================================================================
100 const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Placement::shape () const 
101 {
102   return myShape;
103 }
104
105 //============================================================================
106 void GeomAlgoAPI_Placement::mapOfShapes (GeomAPI_DataMapOfShapeShape& theMap) const
107 {
108   theMap = myMap;
109 }
110
111 //============================================================================
112 GeomAlgoAPI_MakeShape * GeomAlgoAPI_Placement::makeShape() const
113 {
114   return myMkShape;
115 }
116
117 //============================================================================
118 GeomAlgoAPI_Placement::~GeomAlgoAPI_Placement()
119 {
120   if (myImpl)
121     myMap.clear();
122 }