Salome HOME
Fixed naming for primitives
[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_Prism.h>
32 #include <GeomAlgoAPI_Revolution.h>
33 #include <GeomAlgoAPI_ShapeTools.h>
34 #include <GeomAlgoAPI_SketchBuilder.h>
35
36 #include <GeomAPI_PlanarEdges.h>
37 #include <GeomAPI_ShapeExplorer.h>
38
39 #include <map>
40 #include <sstream>
41
42 static void storeSubShape(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   if(theIndex == 0) {
87     return std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_ID())->value());
88   }
89
90   return std::shared_ptr<ModelAPI_Feature>();
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   // Check is this feature of result
111   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
112   if(!aFeature.get()) {
113     return false;
114   }
115
116   ObjectPtr aSub = data()->reference(SKETCH_ID())->value();
117   return aSub == theObject;
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     ListOfShape aShells;
225     ListOfShape aFreeFaces;
226     GeomShapePtr aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aBaseFacesList);
227     GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL,
228                                           aShells, aFreeFaces);
229     theBaseShapesList.insert(theBaseShapesList.end(), aFreeFaces.begin(), aFreeFaces.end());
230     theBaseShapesList.insert(theBaseShapesList.end(), aShells.begin(), aShells.end());
231   } else {
232     theBaseShapesList.insert(theBaseShapesList.end(), aBaseFacesList.begin(),
233                              aBaseFacesList.end());
234   }
235 }
236
237 //=================================================================================================
238 bool FeaturesPlugin_CompositeSketch::isMakeShapeValid(
239   const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
240 {
241   // Check that algo is done.
242   if(!theMakeShape->isDone()) {
243     setError("Error: " + getKind() + " algorithm failed.");
244     return false;
245   }
246
247   // Check if shape is not null.
248   if(!theMakeShape->shape().get() || theMakeShape->shape()->isNull()) {
249     setError("Error: Resulting shape is null.");
250     return false;
251   }
252
253   // Check that resulting shape is valid.
254   if(!theMakeShape->isValid()) {
255     setError("Error: Resulting shape is not valid.");
256     return false;
257   }
258
259   return true;
260 }
261
262 //=================================================================================================
263 void FeaturesPlugin_CompositeSketch::storeResult(const GeomShapePtr theBaseShape,
264                                         const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
265                                         const int theIndex)
266 {
267   // Create result body.
268   ResultBodyPtr aResultBody = document()->createBody(data(), theIndex);
269
270   // Store generated shape.
271   aResultBody->storeGenerated(theBaseShape, theMakeShape->shape());
272
273   // Store generated edges/faces.
274   storeGenerationHistory(aResultBody, theBaseShape, theMakeShape);
275
276   setResult(aResultBody, theIndex);
277 }
278
279 //=================================================================================================
280 void FeaturesPlugin_CompositeSketch::storeGenerationHistory(ResultBodyPtr theResultBody,
281                                         const GeomShapePtr theBaseShape,
282                                         const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
283 {
284   GeomAPI_Shape::ShapeType aBaseShapeType = theBaseShape->shapeType();
285   GeomAPI_Shape::ShapeType aShapeTypeToExplode = GeomAPI_Shape::SHAPE;
286
287   switch(aBaseShapeType) {
288     case GeomAPI_Shape::EDGE: {
289             aShapeTypeToExplode = GeomAPI_Shape::VERTEX;
290       break;
291     }
292     case GeomAPI_Shape::WIRE: {
293       aShapeTypeToExplode = GeomAPI_Shape::COMPOUND;
294       break;
295     }
296     case GeomAPI_Shape::FACE:
297     case GeomAPI_Shape::SHELL: {
298       aShapeTypeToExplode = GeomAPI_Shape::EDGE;
299       break;
300     }
301     case GeomAPI_Shape::COMPOUND: {
302       aShapeTypeToExplode = GeomAPI_Shape::COMPOUND;
303     }
304   }
305
306   int aLateralIndex = 1;
307   int aBaseEdgeIndex = 1;
308   int aVertexIndex = 1;
309   int aBaseVertexIndex = 1;
310
311   if(aShapeTypeToExplode == GeomAPI_Shape::VERTEX ||
312       aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
313     theResultBody->loadGeneratedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::VERTEX);
314   }
315   if(aShapeTypeToExplode == GeomAPI_Shape::EDGE ||
316       aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
317     theResultBody->loadGeneratedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::EDGE);
318   }
319
320   std::shared_ptr<GeomAlgoAPI_MakeSweep> aMakeSweep =
321     std::dynamic_pointer_cast<GeomAlgoAPI_MakeSweep>(theMakeShape);
322   if(aMakeSweep.get()) {
323     // Store from shapes.
324     storeShapes(theResultBody, aBaseShapeType, aMakeSweep->fromShapes(), "From_");
325
326     // Store to shapes.
327     storeShapes(theResultBody, aBaseShapeType, aMakeSweep->toShapes(), "To_");
328   }
329 }
330
331 //=================================================================================================
332 void FeaturesPlugin_CompositeSketch::storeShapes(ResultBodyPtr theResultBody,
333                               const GeomAPI_Shape::ShapeType theBaseShapeType,
334                               const ListOfShape& theShapes,
335                               const std::string theName)
336 {
337   GeomAPI_Shape::ShapeType aShapeTypeToExplore = GeomAPI_Shape::FACE;
338   std::string aShapeTypeStr = "Face";
339   switch(theBaseShapeType) {
340     case GeomAPI_Shape::VERTEX: {
341       aShapeTypeToExplore = GeomAPI_Shape::VERTEX;
342       aShapeTypeStr = "Vertex";
343       break;
344     }
345     case GeomAPI_Shape::EDGE:
346     case GeomAPI_Shape::WIRE: {
347       aShapeTypeToExplore = GeomAPI_Shape::EDGE;
348       aShapeTypeStr = "Edge";
349       break;
350     }
351     case GeomAPI_Shape::FACE:
352     case GeomAPI_Shape::SHELL: {
353       aShapeTypeToExplore = GeomAPI_Shape::FACE;
354       aShapeTypeStr = "Face";
355       break;
356     }
357     case GeomAPI_Shape::COMPOUND: {
358       aShapeTypeToExplore = GeomAPI_Shape::COMPOUND;
359       break;
360     }
361   }
362
363   // Store shapes.
364   for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
365     GeomShapePtr aShape = *anIt;
366
367     if(aShapeTypeToExplore == GeomAPI_Shape::COMPOUND) {
368       std::string aName = theName + (aShape->shapeType() == GeomAPI_Shape::EDGE ? "Edge" : "Face");
369       storeSubShape(theResultBody, aShape, aShape->shapeType(), aName);
370     } else {
371       std::string aName = theName + aShapeTypeStr;
372       storeSubShape(theResultBody, aShape, aShapeTypeToExplore, aName);
373       if (theBaseShapeType == GeomAPI_Shape::WIRE) { // issue 2289: special names also for vertices
374         aName = theName + "Vertex";
375         storeSubShape(theResultBody, aShape, GeomAPI_Shape::VERTEX, aName);
376       }
377     }
378   }
379 }
380
381 void storeSubShape(ResultBodyPtr theResultBody,
382                    const GeomShapePtr theShape,
383                    const GeomAPI_Shape::ShapeType theType,
384                    const std::string& theName)
385 {
386   for(GeomAPI_ShapeExplorer anExp(theShape, theType); anExp.more(); anExp.next()) {
387     GeomShapePtr aSubShape = anExp.current();
388     theResultBody->generated(aSubShape, theName);
389   }
390 }