Salome HOME
Separate attributes for Extrusion and Revolution features.
[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 ListOfShape FeaturesPlugin_RevolutionBoolean::MakeSolids(const ListOfShape& theFaces)
41 {
42   //Getting axis.
43   std::shared_ptr<GeomAPI_Ax1> anAxis;
44   std::shared_ptr<GeomAPI_Edge> anEdge;
45   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(FeaturesPlugin_RevolutionBoolean::AXIS_OBJECT_ID());
46   if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
47     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
48   } else if(anObjRef->context() && anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
49     anEdge = std::make_shared<GeomAPI_Edge>(anObjRef->context()->shape());
50   }
51   if(anEdge) {
52     anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(), anEdge->line()->direction()));
53   }
54
55   // Getting revolution angles.
56   double aToAngle = real(TO_ANGLE_ID())->value();
57   double aFromAngle = real(FROM_ANGLE_ID())->value();
58
59   if(string(CREATION_METHOD())->value() == "ByAngles") {
60     aToAngle = real(TO_ANGLE_ID())->value();
61     aFromAngle =  real(FROM_ANGLE_ID())->value();
62   } else {
63     aToAngle = real(TO_OFFSET_ID())->value();
64     aFromAngle =  real(FROM_OFFSET_ID())->value();
65   }
66
67   // Getting revolution bounding planes.
68   std::shared_ptr<GeomAPI_Shape> aToShape;
69   std::shared_ptr<GeomAPI_Shape> aFromShape;
70
71   if(string(CREATION_METHOD())->value() == "ByPlanesAndOffsets") {
72     anObjRef = selection(TO_OBJECT_ID());
73     if(anObjRef.get() != NULL) {
74       aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
75       if(aToShape.get() == NULL && anObjRef->context().get() != NULL) {
76         aToShape =  anObjRef->context()->shape();
77       }
78     }
79     anObjRef = selection(FROM_OBJECT_ID());
80     if(anObjRef.get() != NULL) {
81       aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
82       if(aFromShape.get() == NULL && anObjRef->context().get() != NULL) {
83         aFromShape = anObjRef->context()->shape();
84       }
85     }
86   }
87
88   // Revol faces.
89   ListOfShape aRevolutionList;
90   for(ListOfShape::const_iterator aFacesIt = theFaces.begin(); aFacesIt != theFaces.end(); aFacesIt++) {
91     std::shared_ptr<GeomAPI_Shape> aBaseShape = *aFacesIt;
92     GeomAlgoAPI_Revolution aRevolAlgo(aBaseShape, anAxis, aToShape, aToAngle, aFromShape, aFromAngle);
93
94     // Checking that the algorithm worked properly.
95     if(!aRevolAlgo.isDone() || aRevolAlgo.shape()->isNull() || !aRevolAlgo.isValid()) {
96       setError("Revolution algorithm failed");
97       return ListOfShape();
98     }
99     aRevolutionList.push_back(aRevolAlgo.shape());
100   }
101
102   return aRevolutionList;
103 }