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