Salome HOME
d2ec7bd664ad679eb88279426951997be4081d59
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_RevolutionBoolean.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        FeaturesPlugin_RevolutionBoolean.h
4 // Created:     11 June 2015
5 // Author:      Dmitry Bobylev
6
7 #include <FeaturesPlugin_RevolutionBoolean.h>
8
9 #include <ModelAPI_AttributeDouble.h>
10 #include <ModelAPI_AttributeSelection.h>
11 #include <ModelAPI_AttributeString.h>
12 #include <ModelAPI_Session.h>
13 #include <ModelAPI_Validator.h>
14
15 #include <GeomAlgoAPI_Revolution.h>
16 #include <GeomAPI_Edge.h>
17 #include <GeomAPI_Lin.h>
18
19 //=================================================================================================
20 void FeaturesPlugin_RevolutionBoolean::initMakeSolidsAttributes()
21 {
22   data()->addAttribute(FeaturesPlugin_RevolutionBoolean::AXIS_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
23
24   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
25
26   data()->addAttribute(TO_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
27   data()->addAttribute(FROM_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
28
29   data()->addAttribute(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
30   data()->addAttribute(TO_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
31
32   data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
33   data()->addAttribute(FROM_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
34
35   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
36   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
37 }
38
39 //=================================================================================================
40 void FeaturesPlugin_RevolutionBoolean::makeSolids(const ListOfShape& theFaces,
41                                                   ListOfShape& theResults,
42                                                   std::list<std::shared_ptr<GeomAPI_Interface>>& theAlgos)
43 {
44   //Getting axis.
45   std::shared_ptr<GeomAPI_Ax1> anAxis;
46   std::shared_ptr<GeomAPI_Edge> anEdge;
47   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(FeaturesPlugin_RevolutionBoolean::AXIS_OBJECT_ID());
48   if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
49     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
50   } else if(anObjRef->context() && anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
51     anEdge = std::make_shared<GeomAPI_Edge>(anObjRef->context()->shape());
52   }
53   if(anEdge) {
54     anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(), anEdge->line()->direction()));
55   }
56
57   // Getting revolution angles.
58   double aToAngle = real(TO_ANGLE_ID())->value();
59   double aFromAngle = real(FROM_ANGLE_ID())->value();
60
61   if(string(CREATION_METHOD())->value() == "ByAngles") {
62     aToAngle = real(TO_ANGLE_ID())->value();
63     aFromAngle =  real(FROM_ANGLE_ID())->value();
64   } else {
65     aToAngle = real(TO_OFFSET_ID())->value();
66     aFromAngle =  real(FROM_OFFSET_ID())->value();
67   }
68
69   // Getting revolution bounding planes.
70   std::shared_ptr<GeomAPI_Shape> aToShape;
71   std::shared_ptr<GeomAPI_Shape> aFromShape;
72
73   if(string(CREATION_METHOD())->value() == "ByPlanesAndOffsets") {
74     anObjRef = selection(TO_OBJECT_ID());
75     if(anObjRef.get() != NULL) {
76       aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
77       if(aToShape.get() == NULL && anObjRef->context().get() != NULL) {
78         aToShape =  anObjRef->context()->shape();
79       }
80     }
81     anObjRef = selection(FROM_OBJECT_ID());
82     if(anObjRef.get() != NULL) {
83       aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
84       if(aFromShape.get() == NULL && anObjRef->context().get() != NULL) {
85         aFromShape = anObjRef->context()->shape();
86       }
87     }
88   }
89
90   // Revol faces.
91   theResults.clear();
92   for(ListOfShape::const_iterator aFacesIt = theFaces.begin(); aFacesIt != theFaces.end(); aFacesIt++) {
93     std::shared_ptr<GeomAPI_Shape> aBaseShape = *aFacesIt;
94     std::shared_ptr<GeomAlgoAPI_Revolution> aRevolAlgo = std::make_shared<GeomAlgoAPI_Revolution>(aBaseShape, anAxis,
95                                                                                                   aToShape, aToAngle,
96                                                                                                   aFromShape, aFromAngle);
97
98     // Checking that the algorithm worked properly.
99     if(!aRevolAlgo->isDone()  || !aRevolAlgo->shape().get() || aRevolAlgo->shape()->isNull() ||
100        !aRevolAlgo->isValid()) {
101       setError("Revolution algorithm failed");
102       theResults.clear();
103       return;
104     }
105     theResults.push_back(aRevolAlgo->shape());
106     theAlgos.push_back(aRevolAlgo);
107   }
108 }