Salome HOME
Merge branch 'Dev_1.1.1' of newgeom:newgeom into Dev_1.2.0
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Prism.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_Prism.h
4 // Created:     5 May 2015
5 // Author:      Dmitry Bobylev
6
7 #include <GeomAlgoAPI_Prism.h>
8
9 #include <GeomAlgoAPI_DFLoader.h>
10
11 #include <BRep_Tool.hxx>
12 #include <BRepCheck_Analyzer.hxx>
13 #include <BRepFeat_MakePrism.hxx>
14 #include <BRepGProp.hxx>
15 #include <Geom_CylindricalSurface.hxx>
16 #include <Geom_Plane.hxx>
17 #include <Geom_RectangularTrimmedSurface.hxx>
18 #include <gp_Cylinder.hxx>
19 #include <gp_Pln.hxx>
20 #include <GProp_GProps.hxx>
21 #include <LocOpe_FindEdgesInFace.hxx>
22 #include <TopExp_Explorer.hxx>
23 #include <TopoDS.hxx>
24
25 GeomAlgoAPI_Prism::GeomAlgoAPI_Prism(std::shared_ptr<GeomAPI_Shape> theBasis,
26                                      std::shared_ptr<GeomAPI_Shape> theFromShape,
27                                      std::shared_ptr<GeomAPI_Shape> theToShape)
28 : myFromShape(theFromShape),
29   myToShape(theToShape),
30   myShape(new GeomAPI_Shape()),
31   myFirst(new GeomAPI_Shape()),myLast(new GeomAPI_Shape())
32 {
33   build(theBasis);
34 }
35
36 //============================================================================
37 void GeomAlgoAPI_Prism::build(const std::shared_ptr<GeomAPI_Shape>& theBasis)
38 {
39   TopoDS_Face aBasis = TopoDS::Face(theBasis->impl<TopoDS_Shape>());
40   Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(
41     BRep_Tool::Surface(aBasis));
42   if(aPlane.IsNull()) { // non-planar shapes is not supported for extrusion yet
43     return;
44   }
45
46   const gp_Dir& aNormal = aPlane->Pln().Axis().Direction();
47   BRepFeat_MakePrism* aBuilder = new BRepFeat_MakePrism(aBasis, aBasis, aBasis, aNormal, 2, Standard_True);
48
49   if(aBuilder) {
50     setImpl(aBuilder);
51     TopoDS_Shape aFromShape = myFromShape->impl<TopoDS_Shape>();
52     TopoDS_Shape aToShape   = myToShape->impl<TopoDS_Shape>();
53     aBuilder->Perform(myFromShape->impl<TopoDS_Shape>(), myToShape->impl<TopoDS_Shape>());
54     myDone = aBuilder->IsDone();
55     if (myDone) {
56       TopoDS_Shape aResult;
57       if(aBuilder->Shape().ShapeType() == TopAbs_COMPOUND) {
58         aResult = GeomAlgoAPI_DFLoader::refineResult(aBuilder->Shape());
59       }
60       else {
61         aResult = aBuilder->Shape();
62       }
63       TopExp_Explorer anExp(aResult, TopAbs_SOLID);
64       if(!anExp.More()) {
65         return;
66       }
67       // fill data map to keep correct orientation of sub-shapes
68       for (TopExp_Explorer Exp(aResult,TopAbs_FACE); Exp.More(); Exp.Next()) {
69         std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
70         aCurrentShape->setImpl(new TopoDS_Shape(Exp.Current()));
71         myMap.bind(aCurrentShape, aCurrentShape);
72       }
73       myShape->setImpl(new TopoDS_Shape(aResult));
74       myFirst->setImpl(new TopoDS_Shape(aBuilder->Modified(aFromShape).First()));
75       myLast->setImpl(new TopoDS_Shape(aBuilder->Modified(aToShape).First()));
76       myMkShape = new GeomAlgoAPI_MakeShape (aBuilder);
77     }
78   }
79 }
80
81 //============================================================================
82 const bool GeomAlgoAPI_Prism::isDone() const
83 {
84   return myDone;
85 }
86
87 //============================================================================
88 const bool GeomAlgoAPI_Prism::isValid() const
89 {
90   BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
91   return (aChecker.IsValid() == Standard_True);
92 }
93
94 //============================================================================
95 const bool GeomAlgoAPI_Prism::hasVolume() const
96 {
97   bool hasVolume(false);
98   if(isValid()) {
99     const TopoDS_Shape& aRShape = myShape->impl<TopoDS_Shape>();
100     GProp_GProps aGProp;
101     BRepGProp::VolumeProperties(aRShape, aGProp);
102     if(aGProp.Mass() > Precision::Confusion())
103       hasVolume = true;
104   }
105   return hasVolume;
106 }
107
108 //============================================================================
109 const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Prism::shape () const
110 {
111   return myShape;
112 }
113
114 //============================================================================
115 const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Prism::firstShape()
116 {
117   return myFirst;
118 }
119
120 //============================================================================
121 const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Prism::lastShape()
122 {
123   return myLast;
124 }
125
126 //============================================================================
127 void GeomAlgoAPI_Prism::mapOfShapes (GeomAPI_DataMapOfShapeShape& theMap) const
128 {
129   theMap = myMap;
130 }
131
132 //============================================================================
133 GeomAlgoAPI_MakeShape* GeomAlgoAPI_Prism::makeShape() const
134 {
135   return myMkShape;
136 }
137
138 //============================================================================
139 GeomAlgoAPI_Prism::~GeomAlgoAPI_Prism()
140 {
141   if (myImpl) {
142     myMap.clear();
143   }
144 }