Salome HOME
Task #3236: Generalization of extrusion
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_CompositeSketch.cpp
1 // Copyright (C) 2014-2020  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include <FeaturesPlugin_CompositeSketch.h>
21 #include <FeaturesPlugin_Tools.h>
22
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_AttributeReference.h>
25 #include <ModelAPI_BodyBuilder.h>
26 #include <ModelAPI_ResultConstruction.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Validator.h>
29
30 #include <GeomAlgoAPI_Revolution.h>
31
32 #include <GeomAPI_ShapeExplorer.h>
33
34
35 static void storeSubShape(const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
36                           ResultBodyPtr theResultBody,
37                           const GeomShapePtr theShape,
38                           const GeomAPI_Shape::ShapeType theType,
39                           const std::string& theName);
40
41 //=================================================================================================
42 void FeaturesPlugin_CompositeSketch::initCompositeSketchAttribtues(const int theInitFlags)
43 {
44   // Initialize sketch launcher.
45   if(theInitFlags & InitSketchLauncher) {
46     data()->addAttribute(SKETCH_ID(), ModelAPI_AttributeReference::typeId());
47     ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SKETCH_ID());
48   }
49
50   // Initialize selection list.
51   if(theInitFlags & InitBaseObjectsList) {
52     data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
53     myCurrentSelectionType = selectionList(BASE_OBJECTS_ID())->selectionType();
54     selectionList(BASE_OBJECTS_ID())->setWholeResultAllowed(true);
55   }
56 }
57
58 //=================================================================================================
59 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeSketch::addFeature(std::string theID)
60 {
61   FeaturePtr aNew = document()->addFeature(theID, false);
62   if(aNew) {
63     data()->reference(SKETCH_ID())->setValue(aNew);
64   }
65
66   // Set as current also after it becomes sub to set correctly enabled for other sketch subs.
67   document()->setCurrentFeature(aNew, false);
68   return aNew;
69 }
70
71 //=================================================================================================
72 int FeaturesPlugin_CompositeSketch::numberOfSubs(bool /*forTree*/) const
73 {
74   ObjectPtr aObj = data()->reference(SKETCH_ID())->value();
75   return aObj.get() ? 1 : 0;
76 }
77
78 //=================================================================================================
79 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeSketch::subFeature(const int theIndex,
80                                                                              bool /*forTree*/)
81 {
82   FeaturePtr aSubFeature;
83   if(theIndex == 0) {
84     aSubFeature =
85         std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_ID())->value());
86   }
87   return aSubFeature;
88 }
89
90 //=================================================================================================
91 int FeaturesPlugin_CompositeSketch::subFeatureId(const int theIndex) const
92 {
93   if(theIndex == 0) {
94     FeaturePtr aFeature =
95       std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_ID())->value());
96     if(aFeature.get()) {
97       return aFeature->data()->featureId();
98     }
99   }
100
101   return -1;
102 }
103
104 //=================================================================================================
105 bool FeaturesPlugin_CompositeSketch::isSub(ObjectPtr theObject) const
106 {
107   bool isSubFeature = false;
108   // Check is this feature of result
109   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
110   if (aFeature.get()) {
111     ObjectPtr aSub = data()->reference(SKETCH_ID())->value();
112     isSubFeature = aSub == theObject;
113   }
114   return isSubFeature;
115 }
116
117 //=================================================================================================
118 void FeaturesPlugin_CompositeSketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
119 {
120   AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
121   if(aBaseObjectsSelectionList.get() && aBaseObjectsSelectionList->size() > 0) {
122     aBaseObjectsSelectionList->clear();
123   }
124
125   reference(SKETCH_ID())->setValue(ObjectPtr());
126 }
127
128 //=================================================================================================
129 void FeaturesPlugin_CompositeSketch::getBaseShapes(ListOfShape& theBaseShapesList,
130                                                    const bool theIsMakeShells)
131 {
132   AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
133   std::string anError;
134   bool isOk = FeaturesPlugin_Tools::getShape(
135       aBaseObjectsSelectionList, theIsMakeShells, theBaseShapesList, anError);
136   if (!isOk)
137     setError(anError);
138 }
139
140 //=================================================================================================
141 void FeaturesPlugin_CompositeSketch::storeResult(const GeomShapePtr theBaseShape,
142                                         const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
143                                         const int theIndex)
144 {
145   // Create result body.
146   ResultBodyPtr aResultBody = document()->createBody(data(), theIndex);
147
148   // Store generated shape.
149   aResultBody->storeGenerated(theBaseShape, theMakeShape->shape());
150
151   // Store generated edges/faces.
152   storeGenerationHistory(aResultBody, theBaseShape, theMakeShape);
153
154   setResult(aResultBody, theIndex);
155 }
156
157 //=================================================================================================
158 void FeaturesPlugin_CompositeSketch::storeGenerationHistory(ResultBodyPtr theResultBody,
159                                         const GeomShapePtr theBaseShape,
160                                         const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
161 {
162   GeomAPI_Shape::ShapeType aBaseShapeType = theBaseShape->shapeType();
163   GeomAPI_Shape::ShapeType aShapeTypeToExplode = GeomAPI_Shape::SHAPE;
164
165   switch(aBaseShapeType) {
166     case GeomAPI_Shape::EDGE: {
167             aShapeTypeToExplode = GeomAPI_Shape::VERTEX;
168       break;
169     }
170     case GeomAPI_Shape::WIRE: {
171       aShapeTypeToExplode = GeomAPI_Shape::COMPOUND;
172       break;
173     }
174     case GeomAPI_Shape::FACE:
175     case GeomAPI_Shape::SHELL: {
176       aShapeTypeToExplode = GeomAPI_Shape::EDGE;
177       break;
178     }
179     case GeomAPI_Shape::COMPOUND: {
180       aShapeTypeToExplode = GeomAPI_Shape::COMPOUND;
181     }
182     default: // [to avoid compilation warnings]
183       break;
184   }
185
186   if(aShapeTypeToExplode == GeomAPI_Shape::VERTEX ||
187       aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
188     theResultBody->loadGeneratedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::VERTEX);
189   }
190   if(aShapeTypeToExplode == GeomAPI_Shape::EDGE ||
191       aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
192     theResultBody->loadGeneratedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::EDGE);
193   }
194   std::list<std::shared_ptr<GeomAlgoAPI_MakeSweep> > aSweeps; // all sweeps collected
195   std::shared_ptr<GeomAlgoAPI_MakeSweep> aMakeSweep =
196     std::dynamic_pointer_cast<GeomAlgoAPI_MakeSweep>(theMakeShape);
197   if(aMakeSweep.get()) {
198     aSweeps.push_back(aMakeSweep);
199   } else {
200     std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeList =
201       std::dynamic_pointer_cast<GeomAlgoAPI_MakeShapeList>(theMakeShape);
202     if (aMakeList.get()) {
203       ListOfMakeShape::const_iterator anIter = aMakeList->list().cbegin();
204       for(; anIter != aMakeList->list().cend(); anIter++) {
205         std::shared_ptr<GeomAlgoAPI_MakeSweep> aSweep =
206           std::dynamic_pointer_cast<GeomAlgoAPI_MakeSweep>(*anIter);
207         if (aSweep.get())
208           aSweeps.push_back(aSweep);
209       }
210     }
211   }
212   std::list<std::shared_ptr<GeomAlgoAPI_MakeSweep> >::iterator aSweep = aSweeps.begin();
213   for(; aSweep != aSweeps.end(); aSweep++) {
214     // Store from shapes.
215     storeShapes(theMakeShape, theResultBody, aBaseShapeType, (*aSweep)->fromShapes(), "From_");
216
217     // Store to shapes.
218     storeShapes(theMakeShape, theResultBody, aBaseShapeType, (*aSweep)->toShapes(), "To_");
219   }
220 }
221
222 //=================================================================================================
223 void FeaturesPlugin_CompositeSketch::storeShapes(
224   const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
225   ResultBodyPtr theResultBody,
226   const GeomAPI_Shape::ShapeType theBaseShapeType,
227   const ListOfShape& theShapes,
228   const std::string theName)
229 {
230   GeomAPI_Shape::ShapeType aShapeTypeToExplore = GeomAPI_Shape::FACE;
231   std::string aShapeTypeStr = "Face";
232   switch(theBaseShapeType) {
233     case GeomAPI_Shape::VERTEX: {
234       aShapeTypeToExplore = GeomAPI_Shape::VERTEX;
235       aShapeTypeStr = "Vertex";
236       break;
237     }
238     case GeomAPI_Shape::EDGE:
239     case GeomAPI_Shape::WIRE: {
240       aShapeTypeToExplore = GeomAPI_Shape::EDGE;
241       aShapeTypeStr = "Edge";
242       break;
243     }
244     case GeomAPI_Shape::FACE:
245     case GeomAPI_Shape::SHELL: {
246       aShapeTypeToExplore = GeomAPI_Shape::FACE;
247       aShapeTypeStr = "Face";
248       break;
249     }
250     case GeomAPI_Shape::COMPOUND: {
251       aShapeTypeToExplore = GeomAPI_Shape::COMPOUND;
252       break;
253     }
254     default: // [to avoid compilation warnings]
255       break;
256   }
257
258   // Store shapes.
259   for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
260     GeomShapePtr aShape = *anIt;
261
262     if(aShapeTypeToExplore == GeomAPI_Shape::COMPOUND) {
263       std::string aName = theName + (aShape->shapeType() == GeomAPI_Shape::EDGE ? "Edge" : "Face");
264       storeSubShape(theMakeShape, theResultBody, aShape, aShape->shapeType(), aName);
265     } else {
266       std::string aName = theName + aShapeTypeStr;
267       storeSubShape(theMakeShape, theResultBody, aShape, aShapeTypeToExplore, aName);
268       if (theBaseShapeType == GeomAPI_Shape::WIRE) { // issue 2289: special names also for vertices
269         aName = theName + "Vertex";
270         storeSubShape(theMakeShape, theResultBody, aShape, GeomAPI_Shape::VERTEX, aName);
271       }
272     }
273   }
274 }
275
276 void storeSubShape(
277   const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
278   ResultBodyPtr theResultBody,
279   const GeomShapePtr theShape,
280   const GeomAPI_Shape::ShapeType theType,
281   const std::string& theName)
282 {
283   for(GeomAPI_ShapeExplorer anExp(theShape, theType); anExp.more(); anExp.next()) {
284     GeomShapePtr aSubShape = anExp.current();
285     if (!theResultBody->generated(aSubShape, theName)) {
286       int aNbSubs = theResultBody->numberOfSubs();
287       if (aNbSubs > 0) {
288         // check the modified shape is in the result body, don't store it if not
289         ListOfShape aNewShapes;
290         theMakeShape->modified(aSubShape, aNewShapes);
291         for (int i = 0; i < aNbSubs; ++i) {
292           ResultBodyPtr aSubRes = theResultBody->subResult(i);
293           GeomShapePtr aShape = aSubRes->shape();
294           ListOfShape::iterator aNewIt = aNewShapes.begin();
295           for (; aNewIt != aNewShapes.end(); ++aNewIt)
296             if (aShape->isSubShape(*aNewIt, false))
297               break;
298           if (aNewIt != aNewShapes.end()) {
299             // store from/to shapes as primitives and then store modification of them by the boolean
300             aSubRes->generated(aSubShape, theName, false);
301             aSubRes->loadModifiedShapes(theMakeShape, aSubShape, theType);
302           }
303         }
304       }
305       else {
306         // store from/to shapes as primitives and then store modification of them by the boolean
307         theResultBody->generated(aSubShape, theName, false);
308         theResultBody->loadModifiedShapes(theMakeShape, aSubShape, theType);
309       }
310     }
311   }
312 }