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