Salome HOME
Issue #2593: CEA 2018-2 Geometrical Naming
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Extrusion.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "FeaturesPlugin_Extrusion.h"
22
23 #include <ModelAPI_AttributeDouble.h>
24 #include <ModelAPI_AttributeSelection.h>
25 #include <ModelAPI_AttributeString.h>
26 #include <ModelAPI_Session.h>
27 #include <ModelAPI_Validator.h>
28
29 #include <GeomAlgoAPI_Prism.h>
30
31 #include <GeomAPI_Dir.h>
32 #include <GeomAPI_Edge.h>
33 #include <GeomAPI_Lin.h>
34 #include <GeomAPI_ShapeIterator.h>
35
36 //=================================================================================================
37 FeaturesPlugin_Extrusion::FeaturesPlugin_Extrusion()
38 {
39 }
40
41 //=================================================================================================
42 void FeaturesPlugin_Extrusion::initAttributes()
43 {
44   initCompositeSketchAttribtues(InitBaseObjectsList);
45
46   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
47
48   data()->addAttribute(TO_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
49   data()->addAttribute(FROM_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
50
51   data()->addAttribute(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
52   data()->addAttribute(TO_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
53
54   data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
55   data()->addAttribute(FROM_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
56
57   data()->addAttribute(DIRECTION_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
58
59   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
60   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
61   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), DIRECTION_OBJECT_ID());
62
63   initCompositeSketchAttribtues(InitSketchLauncher);
64 }
65
66 //=================================================================================================
67 void FeaturesPlugin_Extrusion::execute()
68 {
69   ListOfShape aBaseShapesList;
70   ListOfMakeShape aMakeShapesList;
71
72   // Make extrusions.
73   if(!makeExtrusions(aBaseShapesList, aMakeShapesList)) {
74     return;
75   }
76
77   // Store results.
78   int aResultIndex = 0;
79   ListOfShape::const_iterator aBaseIt = aBaseShapesList.cbegin();
80   ListOfMakeShape::const_iterator anAlgoIt = aMakeShapesList.cbegin();
81   for(; aBaseIt != aBaseShapesList.cend() && anAlgoIt != aMakeShapesList.cend();
82         ++aBaseIt, ++anAlgoIt) {
83     storeResult(*aBaseIt, *anAlgoIt, aResultIndex++);
84   }
85
86   removeResults(aResultIndex);
87 }
88
89 //=================================================================================================
90 bool FeaturesPlugin_Extrusion::makeExtrusions(ListOfShape& theBaseShapes,
91                                               ListOfMakeShape& theMakeShapes)
92 {
93   theMakeShapes.clear();
94
95   // Getting base shapes.
96   getBaseShapes(theBaseShapes);
97
98   //Getting direction.
99   static const std::string aSelectionError = "Error: The direction shape selection is bad.";
100   AttributeSelectionPtr aSelection = selection(DIRECTION_OBJECT_ID());
101   GeomShapePtr aShape = aSelection->value();
102   if (!aShape.get()) {
103     if (aSelection->context().get()) {
104       aShape = aSelection->context()->shape();
105     }
106   }
107
108   GeomEdgePtr anEdge;
109   if (aShape.get()) {
110     if (aShape->isEdge())
111     {
112       anEdge = aShape->edge();
113     }
114     else if (aShape->isCompound())
115     {
116       GeomAPI_ShapeIterator anIt(aShape);
117       anEdge = anIt.current()->edge();
118     }
119   }
120
121   std::shared_ptr<GeomAPI_Dir> aDir;
122   if(anEdge.get()) {
123     if(anEdge->isLine()) {
124       aDir = anEdge->line()->direction();
125     }
126   }
127
128   // Getting sizes.
129   double aToSize = 0.0;
130   double aFromSize = 0.0;
131
132   if(string(CREATION_METHOD())->value() == CREATION_METHOD_BY_SIZES()) {
133     aToSize = real(TO_SIZE_ID())->value();
134     aFromSize = real(FROM_SIZE_ID())->value();
135   } else {
136     aToSize = real(TO_OFFSET_ID())->value();
137     aFromSize = real(FROM_OFFSET_ID())->value();
138   }
139
140   // Getting bounding planes.
141   GeomShapePtr aToShape;
142   GeomShapePtr aFromShape;
143
144   if(string(CREATION_METHOD())->value() == CREATION_METHOD_BY_PLANES()) {
145     aSelection = selection(TO_OBJECT_ID());
146     if(aSelection.get()) {
147       aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aSelection->value());
148       if(!aToShape.get() && aSelection->context().get()) {
149         aToShape = aSelection->context()->shape();
150       }
151       if (aToShape->isCompound()) {
152         GeomAPI_ShapeIterator anIt(aToShape);
153         aToShape = anIt.current();
154       }
155     }
156     aSelection = selection(FROM_OBJECT_ID());
157     if(aSelection.get()) {
158       aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aSelection->value());
159       if(!aFromShape.get() && aSelection->context().get()) {
160         aFromShape = aSelection->context()->shape();
161       }
162       if (aFromShape->isCompound()) {
163         GeomAPI_ShapeIterator anIt(aFromShape);
164         aFromShape = anIt.current();
165       }
166     }
167   }
168
169   // Generating result for each base shape.
170   for(ListOfShape::const_iterator
171       anIter = theBaseShapes.cbegin(); anIter != theBaseShapes.cend(); anIter++) {
172     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anIter;
173
174     std::shared_ptr<GeomAlgoAPI_Prism> aPrismAlgo(new GeomAlgoAPI_Prism(aBaseShape, aDir,
175                                                                         aToShape, aToSize,
176                                                                         aFromShape, aFromSize));
177     if(!isMakeShapeValid(aPrismAlgo)) {
178       return false;
179     }
180
181     theMakeShapes.push_back(aPrismAlgo);
182   }
183
184   return true;
185 }