]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_CompositeSketch.cpp
Salome HOME
Fix for the issue #2854 : Group of faces in error after modification of a sketch...
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_CompositeSketch.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_CompositeSketch.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_CompoundBuilder.h>
31 #include <GeomAlgoAPI_Revolution.h>
32 #include <GeomAlgoAPI_ShapeTools.h>
33 #include <GeomAlgoAPI_SketchBuilder.h>
34
35 #include <GeomAPI_PlanarEdges.h>
36 #include <GeomAPI_ShapeExplorer.h>
37
38 #include <map>
39 #include <sstream>
40
41 static void storeSubShape(const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
42                           ResultBodyPtr theResultBody,
43                           const GeomShapePtr theShape,
44                           const GeomAPI_Shape::ShapeType theType,
45                           const std::string& theName);
46
47 //=================================================================================================
48 void FeaturesPlugin_CompositeSketch::initCompositeSketchAttribtues(const int theInitFlags)
49 {
50   // Initialize sketch launcher.
51   if(theInitFlags & InitSketchLauncher) {
52     data()->addAttribute(SKETCH_ID(), ModelAPI_AttributeReference::typeId());
53     ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SKETCH_ID());
54   }
55
56   // Initialize selection list.
57   if(theInitFlags & InitBaseObjectsList) {
58     data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
59   }
60 }
61
62 //=================================================================================================
63 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeSketch::addFeature(std::string theID)
64 {
65   FeaturePtr aNew = document()->addFeature(theID, false);
66   if(aNew) {
67     data()->reference(SKETCH_ID())->setValue(aNew);
68   }
69
70   // Set as current also after it becomes sub to set correctly enabled for other sketch subs.
71   document()->setCurrentFeature(aNew, false);
72   return aNew;
73 }
74
75 //=================================================================================================
76 int FeaturesPlugin_CompositeSketch::numberOfSubs(bool forTree) const
77 {
78   ObjectPtr aObj = data()->reference(SKETCH_ID())->value();
79   return aObj.get() ? 1 : 0;
80 }
81
82 //=================================================================================================
83 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeSketch::subFeature(const int theIndex,
84                                                                              bool forTree)
85 {
86   FeaturePtr aSubFeature;
87   if(theIndex == 0) {
88     aSubFeature =
89         std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_ID())->value());
90   }
91   return aSubFeature;
92 }
93
94 //=================================================================================================
95 int FeaturesPlugin_CompositeSketch::subFeatureId(const int theIndex) const
96 {
97   if(theIndex == 0) {
98     FeaturePtr aFeature =
99       std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_ID())->value());
100     if(aFeature.get()) {
101       return aFeature->data()->featureId();
102     }
103   }
104
105   return -1;
106 }
107
108 //=================================================================================================
109 bool FeaturesPlugin_CompositeSketch::isSub(ObjectPtr theObject) const
110 {
111   bool isSubFeature = false;
112   // Check is this feature of result
113   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
114   if (aFeature.get()) {
115     ObjectPtr aSub = data()->reference(SKETCH_ID())->value();
116     isSubFeature = aSub == theObject;
117   }
118   return isSubFeature;
119 }
120
121 //=================================================================================================
122 void FeaturesPlugin_CompositeSketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
123 {
124   AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
125   if(aBaseObjectsSelectionList.get() && aBaseObjectsSelectionList->size() > 0) {
126     aBaseObjectsSelectionList->clear();
127   }
128
129   reference(SKETCH_ID())->setValue(ObjectPtr());
130 }
131
132 //=================================================================================================
133 void FeaturesPlugin_CompositeSketch::getBaseShapes(ListOfShape& theBaseShapesList,
134                                                    const bool theIsMakeShells)
135 {
136   theBaseShapesList.clear();
137
138   ListOfShape aBaseFacesList;
139   std::map<ResultConstructionPtr, ListOfShape> aSketchWiresMap;
140   AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
141   if(!aBaseObjectsSelectionList.get()) {
142     setError("Error: Could not get base objects selection list.");
143     return;
144   }
145   if(aBaseObjectsSelectionList->size() == 0) {
146     setError("Error: Base objects list is empty.");
147     return;
148   }
149   for(int anIndex = 0; anIndex < aBaseObjectsSelectionList->size(); anIndex++) {
150     AttributeSelectionPtr aBaseObjectSelection = aBaseObjectsSelectionList->value(anIndex);
151     if(!aBaseObjectSelection.get()) {
152       setError("Error: Selected base object is empty.");
153       return;
154     }
155     GeomShapePtr aBaseShape = aBaseObjectSelection->value();
156     if(aBaseShape.get() && !aBaseShape->isNull()) {
157       GeomAPI_Shape::ShapeType aST = aBaseShape->shapeType();
158       if(aST == GeomAPI_Shape::SOLID || aST == GeomAPI_Shape::COMPSOLID) {
159         setError("Error: Selected shapes has unsupported type.");
160         return;
161       }
162       ResultConstructionPtr aConstruction =
163         std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseObjectSelection->context());
164       if(aConstruction.get() && !aBaseShape->isEqual(aConstruction->shape()) &&
165           aST == GeomAPI_Shape::WIRE) {
166         // It is a wire on the sketch, store it to make face later.
167         aSketchWiresMap[aConstruction].push_back(aBaseShape);
168         continue;
169       } else {
170       aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
171                                    theBaseShapesList.push_back(aBaseShape);
172       }
173     } else {
174       // This may be the whole sketch result selected, check and get faces.
175       ResultConstructionPtr aConstruction =
176         std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseObjectSelection->context());
177       if(!aConstruction.get()) {
178         setError("Error: Selected sketches does not have results.");
179         return;
180       }
181       int aFacesNum = aConstruction->facesNum();
182       if(aFacesNum == 0) {
183         // Probably it can be construction.
184         aBaseShape = aConstruction->shape();
185         if(aBaseShape.get() && !aBaseShape->isNull()) {
186           GeomAPI_Shape::ShapeType aST = aBaseShape->shapeType();
187           if(aST != GeomAPI_Shape::VERTEX && aST != GeomAPI_Shape::EDGE &&
188              aST != GeomAPI_Shape::WIRE &&
189              aST != GeomAPI_Shape::FACE && aST != GeomAPI_Shape::SHELL) {
190             setError("Error: Selected shapes has unsupported type.");
191             return;
192           }
193           aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
194                                        theBaseShapesList.push_back(aBaseShape);
195         }
196       } else {
197         for(int aFaceIndex = 0; aFaceIndex < aFacesNum; aFaceIndex++) {
198           GeomShapePtr aBaseFace = aConstruction->face(aFaceIndex);
199           if(!aBaseFace.get() || aBaseFace->isNull()) {
200             setError("Error: One of the faces on selected sketch is null.");
201             return;
202           }
203           aBaseFacesList.push_back(aBaseFace);
204         }
205       }
206     }
207   }
208
209   // Make faces from sketch wires.
210   for(std::map<ResultConstructionPtr, ListOfShape>::const_iterator anIt = aSketchWiresMap.cbegin();
211       anIt != aSketchWiresMap.cend(); ++anIt) {
212     const std::shared_ptr<GeomAPI_PlanarEdges> aSketchPlanarEdges =
213       std::dynamic_pointer_cast<GeomAPI_PlanarEdges>((*anIt).first->shape());
214     const ListOfShape& aWiresList = (*anIt).second;
215     ListOfShape aFaces;
216     GeomAlgoAPI_ShapeTools::makeFacesWithHoles(aSketchPlanarEdges->origin(),
217                                                aSketchPlanarEdges->norm(),
218                                                aWiresList,
219                                                aFaces);
220     aBaseFacesList.insert(aBaseFacesList.end(), aFaces.begin(), aFaces.end());
221   }
222
223   // Searching faces with common edges.
224   if(theIsMakeShells && aBaseFacesList.size() > 1) {
225     GeomShapePtr aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aBaseFacesList);
226     GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL, theBaseShapesList);
227   } else {
228     theBaseShapesList.insert(theBaseShapesList.end(), aBaseFacesList.begin(),
229                              aBaseFacesList.end());
230   }
231 }
232
233 //=================================================================================================
234 void FeaturesPlugin_CompositeSketch::storeResult(const GeomShapePtr theBaseShape,
235                                         const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
236                                         const int theIndex)
237 {
238   // Create result body.
239   ResultBodyPtr aResultBody = document()->createBody(data(), theIndex);
240
241   // Store generated shape.
242   aResultBody->storeGenerated(theBaseShape, theMakeShape->shape());
243
244   // Store generated edges/faces.
245   storeGenerationHistory(aResultBody, theBaseShape, theMakeShape);
246
247   setResult(aResultBody, theIndex);
248 }
249
250 //=================================================================================================
251 void FeaturesPlugin_CompositeSketch::storeGenerationHistory(ResultBodyPtr theResultBody,
252                                         const GeomShapePtr theBaseShape,
253                                         const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
254 {
255   GeomAPI_Shape::ShapeType aBaseShapeType = theBaseShape->shapeType();
256   GeomAPI_Shape::ShapeType aShapeTypeToExplode = GeomAPI_Shape::SHAPE;
257
258   switch(aBaseShapeType) {
259     case GeomAPI_Shape::EDGE: {
260             aShapeTypeToExplode = GeomAPI_Shape::VERTEX;
261       break;
262     }
263     case GeomAPI_Shape::WIRE: {
264       aShapeTypeToExplode = GeomAPI_Shape::COMPOUND;
265       break;
266     }
267     case GeomAPI_Shape::FACE:
268     case GeomAPI_Shape::SHELL: {
269       aShapeTypeToExplode = GeomAPI_Shape::EDGE;
270       break;
271     }
272     case GeomAPI_Shape::COMPOUND: {
273       aShapeTypeToExplode = GeomAPI_Shape::COMPOUND;
274     }
275   }
276
277   int aLateralIndex = 1;
278   int aBaseEdgeIndex = 1;
279   int aVertexIndex = 1;
280   int aBaseVertexIndex = 1;
281
282   if(aShapeTypeToExplode == GeomAPI_Shape::VERTEX ||
283       aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
284     theResultBody->loadGeneratedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::VERTEX);
285   }
286   if(aShapeTypeToExplode == GeomAPI_Shape::EDGE ||
287       aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
288     theResultBody->loadGeneratedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::EDGE);
289   }
290   std::list<std::shared_ptr<GeomAlgoAPI_MakeSweep> > aSweeps; // all sweeps collected
291   std::shared_ptr<GeomAlgoAPI_MakeSweep> aMakeSweep =
292     std::dynamic_pointer_cast<GeomAlgoAPI_MakeSweep>(theMakeShape);
293   if(aMakeSweep.get()) {
294     aSweeps.push_back(aMakeSweep);
295   } else {
296     std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeList =
297       std::dynamic_pointer_cast<GeomAlgoAPI_MakeShapeList>(theMakeShape);
298     if (aMakeList.get()) {
299       ListOfMakeShape::const_iterator anIter = aMakeList->list().cbegin();
300       for(; anIter != aMakeList->list().cend(); anIter++) {
301         std::shared_ptr<GeomAlgoAPI_MakeSweep> aSweep =
302           std::dynamic_pointer_cast<GeomAlgoAPI_MakeSweep>(*anIter);
303         if (aSweep.get())
304           aSweeps.push_back(aSweep);
305       }
306     }
307   }
308   std::list<std::shared_ptr<GeomAlgoAPI_MakeSweep> >::iterator aSweep = aSweeps.begin();
309   for(; aSweep != aSweeps.end(); aSweep++) {
310     // Store from shapes.
311     storeShapes(theMakeShape, theResultBody, aBaseShapeType, (*aSweep)->fromShapes(), "From_");
312
313     // Store to shapes.
314     storeShapes(theMakeShape, theResultBody, aBaseShapeType, (*aSweep)->toShapes(), "To_");
315   }
316 }
317
318 //=================================================================================================
319 void FeaturesPlugin_CompositeSketch::storeShapes(
320   const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
321   ResultBodyPtr theResultBody,
322   const GeomAPI_Shape::ShapeType theBaseShapeType,
323   const ListOfShape& theShapes,
324   const std::string theName)
325 {
326   GeomAPI_Shape::ShapeType aShapeTypeToExplore = GeomAPI_Shape::FACE;
327   std::string aShapeTypeStr = "Face";
328   switch(theBaseShapeType) {
329     case GeomAPI_Shape::VERTEX: {
330       aShapeTypeToExplore = GeomAPI_Shape::VERTEX;
331       aShapeTypeStr = "Vertex";
332       break;
333     }
334     case GeomAPI_Shape::EDGE:
335     case GeomAPI_Shape::WIRE: {
336       aShapeTypeToExplore = GeomAPI_Shape::EDGE;
337       aShapeTypeStr = "Edge";
338       break;
339     }
340     case GeomAPI_Shape::FACE:
341     case GeomAPI_Shape::SHELL: {
342       aShapeTypeToExplore = GeomAPI_Shape::FACE;
343       aShapeTypeStr = "Face";
344       break;
345     }
346     case GeomAPI_Shape::COMPOUND: {
347       aShapeTypeToExplore = GeomAPI_Shape::COMPOUND;
348       break;
349     }
350   }
351
352   // Store shapes.
353   for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
354     GeomShapePtr aShape = *anIt;
355
356     if(aShapeTypeToExplore == GeomAPI_Shape::COMPOUND) {
357       std::string aName = theName + (aShape->shapeType() == GeomAPI_Shape::EDGE ? "Edge" : "Face");
358       storeSubShape(theMakeShape, theResultBody, aShape, aShape->shapeType(), aName);
359     } else {
360       std::string aName = theName + aShapeTypeStr;
361       storeSubShape(theMakeShape, theResultBody, aShape, aShapeTypeToExplore, aName);
362       if (theBaseShapeType == GeomAPI_Shape::WIRE) { // issue 2289: special names also for vertices
363         aName = theName + "Vertex";
364         storeSubShape(theMakeShape, theResultBody, aShape, GeomAPI_Shape::VERTEX, aName);
365       }
366     }
367   }
368 }
369
370 void storeSubShape(
371   const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
372   ResultBodyPtr theResultBody,
373   const GeomShapePtr theShape,
374   const GeomAPI_Shape::ShapeType theType,
375   const std::string& theName)
376 {
377   for(GeomAPI_ShapeExplorer anExp(theShape, theType); anExp.more(); anExp.next()) {
378     GeomShapePtr aSubShape = anExp.current();
379     if (!theResultBody->generated(aSubShape, theName)) {
380       // store from/to shapes as primitives and then store modification of them by the boolean
381       theResultBody->generated(aSubShape, theName, false);
382       theResultBody->loadModifiedShapes(theMakeShape, aSubShape, theType);
383     }
384   }
385 }