Salome HOME
updated copyright message
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Extrusion.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "FeaturesPlugin_Extrusion.h"
21
22 #include <ModelAPI_AttributeDouble.h>
23 #include <ModelAPI_AttributeSelection.h>
24 #include <ModelAPI_AttributeString.h>
25 #include <ModelAPI_Session.h>
26 #include <ModelAPI_Validator.h>
27 #include <ModelAPI_Tools.h>
28
29 #include <GeomAlgoAPI_Prism.h>
30 #include <GeomAlgoAPI_Tools.h>
31
32 #include <GeomAPI_Dir.h>
33 #include <GeomAPI_Edge.h>
34 #include <GeomAPI_Lin.h>
35 #include <GeomAPI_ShapeIterator.h>
36
37 //=================================================================================================
38 FeaturesPlugin_Extrusion::FeaturesPlugin_Extrusion()
39 {
40 }
41
42 //=================================================================================================
43 void FeaturesPlugin_Extrusion::initAttributes()
44 {
45   initCompositeSketchAttribtues(InitBaseObjectsList);
46
47   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
48
49   data()->addAttribute(TO_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
50   data()->addAttribute(FROM_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
51
52   data()->addAttribute(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
53   data()->addAttribute(TO_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
54
55   data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
56   data()->addAttribute(FROM_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
57
58   data()->addAttribute(DIRECTION_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
59
60   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
61   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
62   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), DIRECTION_OBJECT_ID());
63
64   initCompositeSketchAttribtues(InitSketchLauncher);
65 }
66
67 //=================================================================================================
68 void FeaturesPlugin_Extrusion::execute()
69 {
70   ListOfShape aBaseShapesList, aBoundaryShapes;
71   ListOfMakeShape aMakeShapesList;
72
73   // Make extrusions.
74   if(!makeExtrusions(aBaseShapesList, aBoundaryShapes, aMakeShapesList)) {
75     return;
76   }
77
78   // Store results.
79   int aResultIndex = 0;
80   ListOfShape::const_iterator aBaseIt = aBaseShapesList.cbegin();
81   ListOfMakeShape::const_iterator anAlgoIt = aMakeShapesList.cbegin();
82   for(; aBaseIt != aBaseShapesList.cend() && anAlgoIt != aMakeShapesList.cend();
83         ++aBaseIt, ++anAlgoIt) {
84     if (aBoundaryShapes.empty())
85       storeResult(*aBaseIt, *anAlgoIt, aResultIndex++);
86     else
87       storeResultWithBoundaries(*aBaseIt, aBoundaryShapes, *anAlgoIt, aResultIndex++);
88   }
89
90   removeResults(aResultIndex);
91 }
92
93 //=================================================================================================
94 bool FeaturesPlugin_Extrusion::makeExtrusions(ListOfShape& theBaseShapes,
95                                               ListOfShape& theBoundaryShapes,
96                                               ListOfMakeShape& theMakeShapes)
97 {
98   theMakeShapes.clear();
99
100   // Getting base shapes.
101   getBaseShapes(theBaseShapes);
102
103   //Getting direction.
104   std::shared_ptr<GeomAPI_Dir> aDir;
105   getDirection(aDir);
106
107   // Getting sizes.
108   double aToSize = 0.0;
109   double aFromSize = 0.0;
110   getSizes(aToSize, aFromSize);
111
112   // Getting bounding planes.
113   GeomShapePtr aToShape;
114   GeomShapePtr aFromShape;
115
116   if(string(CREATION_METHOD())->value() == CREATION_METHOD_BY_PLANES()) {
117     AttributeSelectionPtr aSelection = selection(TO_OBJECT_ID());
118     if(aSelection.get()) {
119       aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aSelection->value());
120       if(!aToShape.get() && aSelection->context().get()) {
121         aToShape = aSelection->context()->shape();
122       }
123       if (aToShape.get() && aToShape->isCompound()) {
124         GeomAPI_ShapeIterator anIt(aToShape);
125         aToShape = anIt.current();
126       }
127     }
128     aSelection = selection(FROM_OBJECT_ID());
129     if(aSelection.get()) {
130       aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aSelection->value());
131       if(!aFromShape.get() && aSelection->context().get()) {
132         aFromShape = aSelection->context()->shape();
133       }
134       if (aFromShape.get() && aFromShape->isCompound()) {
135         GeomAPI_ShapeIterator anIt(aFromShape);
136         aFromShape = anIt.current();
137       }
138     }
139   }
140   if (aToShape && !aToShape->isPlanar())
141     theBoundaryShapes.push_back(aToShape);
142   if (aFromShape && !aFromShape->isPlanar())
143     theBoundaryShapes.push_back(aFromShape);
144
145   // Generating result for each base shape.
146   std::string anError;
147   for(ListOfShape::const_iterator
148       anIter = theBaseShapes.cbegin(); anIter != theBaseShapes.cend(); anIter++) {
149     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anIter;
150
151     std::shared_ptr<GeomAlgoAPI_Prism> aPrismAlgo(new GeomAlgoAPI_Prism(aBaseShape, aDir,
152                                                                         aToShape, aToSize,
153                                                                         aFromShape, aFromSize));
154     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aPrismAlgo, getKind(), anError)) {
155       setError(anError);
156       return false;
157     }
158
159     theMakeShapes.push_back(aPrismAlgo);
160   }
161
162   return true;
163 }
164
165 //=================================================================================================
166 void FeaturesPlugin_Extrusion::storeResultWithBoundaries(
167     const GeomShapePtr theBaseShape,
168     const ListOfShape& theBoundaryShapes,
169     const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
170     const int theIndex)
171 {
172   // Create result body.
173   ResultBodyPtr aResultBody = document()->createBody(data(), theIndex);
174
175   // Store modified shapes.
176   ModelAPI_Tools::loadModifiedShapes(aResultBody, theBoundaryShapes, ListOfShape(),
177                                      theMakeShape, theMakeShape->shape());
178
179   // Store generated edges/faces.
180   storeGenerationHistory(aResultBody, theBaseShape, theMakeShape);
181
182   setResult(aResultBody, theIndex);
183 }
184
185 //=================================================================================================
186 void FeaturesPlugin_Extrusion::getDirection(std::shared_ptr<GeomAPI_Dir>& theDir)
187 {
188   static const std::string aSelectionError = "Error: The direction shape selection is bad.";
189   AttributeSelectionPtr aSelection = selection(DIRECTION_OBJECT_ID());
190   GeomShapePtr aShape = aSelection->value();
191   if (!aShape.get()) {
192     if (aSelection->context().get()) {
193       aShape = aSelection->context()->shape();
194     }
195   }
196
197   GeomEdgePtr anEdge;
198   if (aShape.get()) {
199     if (aShape->isEdge())
200     {
201       anEdge = aShape->edge();
202     }
203     else if (aShape->isCompound())
204     {
205       GeomAPI_ShapeIterator anIt(aShape);
206       anEdge = anIt.current()->edge();
207     }
208   }
209
210   if (anEdge.get()) {
211     if (anEdge->isLine()) {
212       theDir = anEdge->line()->direction();
213     }
214   }
215 }
216
217 //=================================================================================================
218 void FeaturesPlugin_Extrusion::getSizes(double& theToSize, double& theFromSize)
219 {
220   if (string(CREATION_METHOD())->value() == CREATION_METHOD_BY_SIZES()) {
221     theToSize = real(TO_SIZE_ID())->value();
222     theFromSize = real(FROM_SIZE_ID())->value();
223   } else if (string(CREATION_METHOD())->value() == CREATION_METHOD_BY_PLANES()) {
224     theToSize = real(TO_OFFSET_ID())->value();
225     theFromSize = real(FROM_OFFSET_ID())->value();
226   } else {
227   }
228 }