Salome HOME
Improvement #635: Move maximum functionality to API class
[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_Session.h>
12 #include <ModelAPI_Validator.h>
13
14 #include <GeomAlgoAPI_Revolution.h>
15 #include <GeomAPI_Edge.h>
16 #include <GeomAPI_Lin.h>
17
18 //=================================================================================================
19 void FeaturesPlugin_RevolutionBoolean::initMakeSolidsAttributes()
20 {
21   data()->addAttribute(FeaturesPlugin_RevolutionBoolean::AXIS_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
22
23   data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
24   data()->addAttribute(FROM_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
25
26   data()->addAttribute(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
27   data()->addAttribute(TO_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
28
29   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
30   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
31 }
32
33 //=================================================================================================
34 ListOfShape FeaturesPlugin_RevolutionBoolean::MakeSolids(const ListOfShape& theFaces)
35 {
36   //Getting axis.
37   std::shared_ptr<GeomAPI_Ax1> anAxis;
38   std::shared_ptr<GeomAPI_Edge> anEdge;
39   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(FeaturesPlugin_RevolutionBoolean::AXIS_OBJECT_ID());
40   if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
41     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
42   }
43   if(anEdge) {
44     anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(), anEdge->line()->direction()));
45   }
46
47   // Getting revolution bounding planes.
48   std::shared_ptr<GeomAPI_Shape> aFromShape;
49   std::shared_ptr<GeomAPI_Shape> aToShape;
50   anObjRef = selection(FROM_OBJECT_ID());
51   if(anObjRef.get() != NULL) {
52     aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
53     if(aFromShape.get() == NULL && anObjRef->context().get() != NULL) {
54       aFromShape = anObjRef->context()->shape();
55     }
56   }
57   anObjRef = selection(TO_OBJECT_ID());
58   if(anObjRef.get() != NULL) {
59     aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
60     if(aToShape.get() == NULL && anObjRef->context().get() != NULL) {
61       aToShape =  anObjRef->context()->shape();
62     }
63   }
64
65   // Getting revolution angles.
66   double aFromAngle = real(FROM_ANGLE_ID())->value();
67   double aToAngle = real(TO_ANGLE_ID())->value();
68
69   // Revol faces.
70   ListOfShape aRevolutionList;
71   for(ListOfShape::const_iterator aFacesIt = theFaces.begin(); aFacesIt != theFaces.end(); aFacesIt++) {
72     std::shared_ptr<GeomAPI_Shape> aBaseShape = *aFacesIt;
73     GeomAlgoAPI_Revolution aRevolAlgo(aBaseShape, anAxis, aFromShape, aFromAngle, aToShape, aToAngle);
74
75     // Checking that the algorithm worked properly.
76     if(!aRevolAlgo.isDone() || aRevolAlgo.shape()->isNull() || !aRevolAlgo.isValid()) {
77       setError("Revolution algorithm failed");
78       return ListOfShape();
79     }
80     aRevolutionList.push_back(aRevolAlgo.shape());
81   }
82
83   return aRevolutionList;
84 }