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