Salome HOME
Separate attributes for Extrusion and Revolution features.
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_ExtrusionBoolean.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        FeaturesPlugin_ExtrusionBoolean.h
4 // Created:     11 June 2015
5 // Author:      Dmitry Bobylev
6
7 #include <FeaturesPlugin_ExtrusionBoolean.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_Prism.h>
16
17 //=================================================================================================
18 void FeaturesPlugin_ExtrusionBoolean::initMakeSolidsAttributes()
19 {
20   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
21
22   data()->addAttribute(TO_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
23   data()->addAttribute(FROM_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
24
25   data()->addAttribute(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
26   data()->addAttribute(TO_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
27
28   data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
29   data()->addAttribute(FROM_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
30
31   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
32   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
33 }
34
35 //=================================================================================================
36 ListOfShape FeaturesPlugin_ExtrusionBoolean::MakeSolids(const ListOfShape& theFaces)
37 {
38   // Getting extrusion sizes.
39   double aToSize = 0.0;
40   double aFromSize = 0.0;
41
42   if(string(CREATION_METHOD())->value() == "BySizes") {
43     aToSize = real(TO_SIZE_ID())->value();
44     aFromSize =  real(FROM_SIZE_ID())->value();
45   } else {
46     aToSize = real(TO_OFFSET_ID())->value();
47     aFromSize =  real(FROM_OFFSET_ID())->value();
48   }
49
50   // Getting extrusion bounding planes.
51   std::shared_ptr<GeomAPI_Shape> aToShape;
52   std::shared_ptr<GeomAPI_Shape> aFromShape;
53
54   if(string(CREATION_METHOD())->value() == "ByPlanesAndOffsets") {
55     std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(TO_OBJECT_ID());
56     if(anObjRef.get() != NULL) {
57       aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
58       if(aToShape.get() == NULL && anObjRef->context().get() != NULL) {
59         aToShape =  anObjRef->context()->shape();
60       }
61     }
62     anObjRef = selection(FROM_OBJECT_ID());
63     if(anObjRef.get() != NULL) {
64       aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
65       if(aFromShape.get() == NULL && anObjRef->context().get() != NULL) {
66         aFromShape = anObjRef->context()->shape();
67       }
68     }
69   }
70
71   // Extrude faces.
72   ListOfShape anExtrusionList;
73   for(ListOfShape::const_iterator aFacesIt = theFaces.begin(); aFacesIt != theFaces.end(); aFacesIt++) {
74     std::shared_ptr<GeomAPI_Shape> aBaseShape = *aFacesIt;
75     GeomAlgoAPI_Prism aPrismAlgo(aBaseShape, aToShape, aToSize, aFromShape, aFromSize);
76
77     // Checking that the algorithm worked properly.
78     if(!aPrismAlgo.isDone() || aPrismAlgo.shape()->isNull() || !aPrismAlgo.isValid()) {
79       setError("Extrusion algorithm failed");
80       return ListOfShape();
81     }
82     anExtrusionList.push_back(aPrismAlgo.shape());
83   }
84
85   return anExtrusionList;
86 }