]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Extrusion.cpp
Salome HOME
16333c306bb5e8fe98aa113255f0dc5c7e80bd9d
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Extrusion.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        FeaturesPlugin_Extrusion.cpp
4 // Created:     30 May 2014
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "FeaturesPlugin_Extrusion.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 #include <GeomAPI_Dir.h>
18 #include <GeomAPI_Edge.h>
19 #include <GeomAPI_Lin.h>
20
21 //=================================================================================================
22 FeaturesPlugin_Extrusion::FeaturesPlugin_Extrusion()
23 {
24 }
25
26 //=================================================================================================
27 void FeaturesPlugin_Extrusion::initAttributes()
28 {
29   initCompositeSketchAttribtues(InitBaseObjectsList);
30
31   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
32
33   data()->addAttribute(TO_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
34   data()->addAttribute(FROM_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
35
36   data()->addAttribute(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
37   data()->addAttribute(TO_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
38
39   data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
40   data()->addAttribute(FROM_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
41
42   data()->addAttribute(DIRECTION_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
43
44   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
45   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
46   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), DIRECTION_OBJECT_ID());
47
48   initCompositeSketchAttribtues(InitSketchLauncher);
49 }
50
51 //=================================================================================================
52 void FeaturesPlugin_Extrusion::execute()
53 {
54   ListOfShape aBaseShapesList;
55   ListOfMakeShape aMakeShapesList;
56
57   // Make extrusions.
58   if(!makeExtrusions(aBaseShapesList, aMakeShapesList)) {
59     return;
60   }
61
62   // Store results.
63   int aResultIndex = 0;
64   ListOfShape::const_iterator aBaseIt = aBaseShapesList.cbegin();
65   ListOfMakeShape::const_iterator anAlgoIt = aMakeShapesList.cbegin();
66   for(; aBaseIt != aBaseShapesList.cend() && anAlgoIt != aMakeShapesList.cend(); ++aBaseIt, ++anAlgoIt) {
67     storeResult(*aBaseIt, *anAlgoIt, aResultIndex++);
68   }
69
70   removeResults(aResultIndex);
71 }
72
73 //=================================================================================================
74 bool FeaturesPlugin_Extrusion::makeExtrusions(ListOfShape& theBaseShapes,
75                                               ListOfMakeShape& theMakeShapes)
76 {
77   theMakeShapes.clear();
78
79   /// Sub-feature of the composite should be set in the base list.
80   setSketchObjectToList();
81
82   // Getting base shapes.
83   getBaseShapes(theBaseShapes);
84
85   //Getting direction.
86   std::shared_ptr<GeomAPI_Dir> aDir;
87   std::shared_ptr<GeomAPI_Edge> anEdge;
88   AttributeSelectionPtr aSelection = selection(DIRECTION_OBJECT_ID());
89   if(aSelection.get() && aSelection->value().get() && aSelection->value()->isEdge()) {
90     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aSelection->value()));
91   } else if(aSelection->context().get() &&
92             aSelection->context()->shape().get() &&
93             aSelection->context()->shape()->isEdge()) {
94     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aSelection->context()->shape()));
95   }
96   if(anEdge.get()) {
97     if(anEdge->isLine()) {
98       aDir = anEdge->line()->direction();
99     }
100   }
101
102   // Getting sizes.
103   double aToSize = 0.0;
104   double aFromSize = 0.0;
105
106   if(string(CREATION_METHOD())->value() == "BySizes") {
107     aToSize = real(TO_SIZE_ID())->value();
108     aFromSize = real(FROM_SIZE_ID())->value();
109   } else {
110     aToSize = real(TO_OFFSET_ID())->value();
111     aFromSize = real(FROM_OFFSET_ID())->value();
112   }
113
114   // Getting bounding planes.
115   GeomShapePtr aToShape;
116   GeomShapePtr aFromShape;
117
118   if(string(CREATION_METHOD())->value() == "ByPlanesAndOffsets") {
119     aSelection = selection(TO_OBJECT_ID());
120     if(aSelection.get()) {
121       aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aSelection->value());
122       if(!aToShape.get() && aSelection->context().get()) {
123         aToShape = aSelection->context()->shape();
124       }
125     }
126     aSelection = selection(FROM_OBJECT_ID());
127     if(aSelection.get()) {
128       aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aSelection->value());
129       if(!aFromShape.get() && aSelection->context().get()) {
130         aFromShape = aSelection->context()->shape();
131       }
132     }
133   }
134
135   // Generating result for each base shape.
136   for(ListOfShape::const_iterator anIter = theBaseShapes.cbegin(); anIter != theBaseShapes.cend(); anIter++) {
137     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anIter;
138
139     std::shared_ptr<GeomAlgoAPI_Prism> aPrismAlgo(new GeomAlgoAPI_Prism(aBaseShape, aDir,
140                                                                         aToShape, aToSize,
141                                                                         aFromShape, aFromSize));
142     if(!isMakeShapeValid(aPrismAlgo)) {
143       return false;
144     }
145
146     theMakeShapes.push_back(aPrismAlgo);
147   }
148
149   return true;
150 }