]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_CompositeSketch.cpp
Salome HOME
Issue #1343 Improvement of Extrusion and Revolution operations: revolution/revolution...
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_CompositeSketch.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        FeaturesPlugin_CompositeSketch.cpp
4 // Created:     11 September 2015
5 // Author:      Dmitry Bobylev
6
7 #include <FeaturesPlugin_CompositeSketch.h>
8
9 #include <ModelAPI_AttributeSelectionList.h>
10 #include <ModelAPI_AttributeReference.h>
11 #include <ModelAPI_BodyBuilder.h>
12 #include <ModelAPI_ResultBody.h>
13 #include <ModelAPI_ResultConstruction.h>
14 #include <ModelAPI_Session.h>
15 #include <ModelAPI_Validator.h>
16
17 #include <GeomAlgoAPI_CompoundBuilder.h>
18 #include <GeomAlgoAPI_Prism.h>
19 #include <GeomAlgoAPI_Revolution.h>
20 #include <GeomAlgoAPI_ShapeTools.h>
21
22 #include <sstream>
23
24 //=================================================================================================
25 void FeaturesPlugin_CompositeSketch::initAttributes()
26 {
27   data()->addAttribute(SKETCH_OBJECT_ID(), ModelAPI_AttributeReference::typeId());
28   data()->addAttribute(SKETCH_SELECTION_ID(), ModelAPI_AttributeSelection::typeId());
29   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SKETCH_SELECTION_ID());
30
31   //initMakeSolidsAttributes();
32 }
33
34 //=================================================================================================
35 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeSketch::addFeature(std::string theID)
36 {
37   std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
38   if (aNew) {
39     data()->reference(SKETCH_OBJECT_ID())->setValue(aNew);
40   }
41   // set as current also after it becomes sub to set correctly enabled for other sketch subs
42   document()->setCurrentFeature(aNew, false);
43   return aNew;
44 }
45
46 //=================================================================================================
47 int FeaturesPlugin_CompositeSketch::numberOfSubs(bool forTree) const
48 {
49   ObjectPtr aObj = data()->reference(SKETCH_OBJECT_ID())->value();
50   return aObj.get()? 1 : 0;
51 }
52
53 //=================================================================================================
54 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeSketch::subFeature(const int theIndex, bool forTree)
55 {
56   if (theIndex == 0)
57     return std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
58   return std::shared_ptr<ModelAPI_Feature>();
59 }
60
61 //=================================================================================================
62 int FeaturesPlugin_CompositeSketch::subFeatureId(const int theIndex) const
63 {
64   if (theIndex == 0) {
65     FeaturePtr aFeature = 
66       std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
67     if (aFeature.get())
68       return aFeature->data()->featureId();
69   }
70   return -1;
71 }
72
73 //=================================================================================================
74 bool FeaturesPlugin_CompositeSketch::isSub(ObjectPtr theObject) const
75 {
76   // check is this feature of result
77   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
78   if (!aFeature)
79     return false;
80  
81   ObjectPtr aSub = data()->reference(SKETCH_OBJECT_ID())->value();
82   return aSub == theObject;
83 }
84
85 //=================================================================================================
86 void FeaturesPlugin_CompositeSketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
87 {
88   AttributeSelectionListPtr aFacesSelectionList = selectionList(LIST_ID());
89   if (aFacesSelectionList.get() && aFacesSelectionList->size() > 0)
90     aFacesSelectionList->clear();
91 }
92
93 //=================================================================================================
94 void FeaturesPlugin_CompositeSketch::erase()
95 {
96   if (data().get() && data()->isValid()) { // on abort of sketch of this composite it may be invalid
97     FeaturePtr aSketch =
98       std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
99     if (aSketch.get() && aSketch->data()->isValid()) {
100       document()->removeFeature(aSketch);
101     }
102   }
103   ModelAPI_CompositeFeature::erase();
104 }
105
106
107 //=================================================================================================
108 void FeaturesPlugin_CompositeSketch::execute()
109 {
110   // Getting faces to create solids.
111   std::shared_ptr<ModelAPI_Feature> aSketchFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(
112                                                      reference(SKETCH_OBJECT_ID())->value());
113   if(!aSketchFeature || aSketchFeature->results().empty()) {
114     return;
115   }
116   ResultPtr aSketchRes = aSketchFeature->results().front();
117   ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
118   if(!aConstruction.get()) {
119     return;
120   }
121
122   /// feature extrusion does not have the next attribute
123   if (data()->attribute(SKETCH_SELECTION_ID()).get()) {
124     if (!selection(SKETCH_SELECTION_ID())->isInitialized() || selection(SKETCH_SELECTION_ID())->context() != aSketchRes) {
125       selection(SKETCH_SELECTION_ID())->setValue(aSketchRes, std::shared_ptr<GeomAPI_Shape>());
126     }
127   }
128   int aSketchFacesNum = aConstruction->facesNum();
129   if(aSketchFacesNum == 0) {
130     return;
131   }
132   ListOfShape aFacesList;
133   for(int aFaceIndex = 0; aFaceIndex < aSketchFacesNum; aFaceIndex++) {
134     std::shared_ptr<GeomAPI_Shape> aFace = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
135     aFacesList.push_back(aFace);
136   }
137
138   // Searching faces with common edges.
139   ListOfShape aShells;
140   ListOfShape aFreeFaces;
141   std::shared_ptr<GeomAPI_Shape> aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aFacesList);
142   GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL, aShells, aFreeFaces);
143   aShells.insert(aShells.end(), aFreeFaces.begin(), aFreeFaces.end());
144
145   // Generating result for each shell and face.
146   int aErrorsNum = 0;
147   int aResultIndex = 0;
148   for(ListOfShape::const_iterator anIter = aShells.cbegin(); anIter != aShells.cend(); anIter++) {
149     std::shared_ptr<GeomAlgoAPI_MakeShape> aMakeShape;
150
151     std::shared_ptr<GeomAPI_Shape> aBaseFace = *anIter;
152     makeSolid(aBaseFace, aMakeShape);
153     if(!aMakeShape.get()) {
154       aErrorsNum++;
155       continue;
156     }
157
158     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
159     loadNamingDS(aResultBody, aBaseFace, aMakeShape);
160     setResult(aResultBody, aResultIndex);
161     aResultIndex++;
162   }
163
164   if(aErrorsNum > 0) {
165     std::ostringstream aStringStream;
166     aStringStream << "Error: Could not create solid(s) from " << aErrorsNum << " face(s).";
167     setError(aStringStream.str());
168   }
169
170   // Remove the rest results if there were produced in the previous pass.
171   removeResults(aResultIndex);
172 }
173
174 //=================================================================================================
175 void FeaturesPlugin_CompositeSketch::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
176                                                   const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
177                                                   const std::shared_ptr<GeomAlgoAPI_MakeShape>& theMakeShape)
178 {
179   //load result
180   theResultBody->storeGenerated(theBaseShape, theMakeShape->shape());
181
182   //Insert lateral face : Face from Edge
183   const std::string aLatName = "LateralFace";
184   const int aLatTag = 1;
185   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aDataMap = theMakeShape->mapOfSubShapes();
186   theResultBody->loadAndOrientGeneratedShapes(theMakeShape.get(), theBaseShape, GeomAPI_Shape::EDGE, aLatTag, aLatName, *aDataMap.get());
187
188   std::shared_ptr<GeomAlgoAPI_MakeSweep> aSweepAlgo = std::dynamic_pointer_cast<GeomAlgoAPI_MakeSweep>(theMakeShape);
189   if(aSweepAlgo.get()) {
190     //Insert to faces
191     int aToFaceIndex = 1;
192     const std::string aToName = "ToFace";
193     int aToTag = 2;
194     const ListOfShape& aToFaces = aSweepAlgo->toShapes();
195     for(ListOfShape::const_iterator anIt = aToFaces.cbegin(); anIt != aToFaces.cend(); anIt++) {
196       std::shared_ptr<GeomAPI_Shape> aToFace = *anIt;
197       if(aDataMap->isBound(aToFace)) {
198         aToFace = aDataMap->find(aToFace);
199       }
200       std::ostringstream aStr;
201       aStr << aToName << "_" << aToFaceIndex++;
202       theResultBody->generated(aToFace, aStr.str(), aToTag++);
203     }
204
205     //Insert from faces
206     int aFromFaceIndex = 1;
207     const std::string aFromName = "FromFace";
208     int aFromTag = aToTag > 10000 ? aToTag : 10000;
209     const ListOfShape& aFromFaces = aSweepAlgo->fromShapes();
210     for(ListOfShape::const_iterator anIt = aFromFaces.cbegin(); anIt != aFromFaces.cend(); anIt++) {
211       std::shared_ptr<GeomAPI_Shape> aFromFace = *anIt;
212       if(aDataMap->isBound(aFromFace)) {
213         aFromFace = aDataMap->find(aFromFace);
214       }
215       std::ostringstream aStr;
216       aStr << aFromName << "_" << aFromFaceIndex++;
217       theResultBody->generated(aFromFace, aStr.str(), aFromTag++);
218     }
219   }
220 }
221 //=================================================================================================
222 void FeaturesPlugin_CompositeSketch::setSketchObjectToList()
223 {
224   std::shared_ptr<ModelAPI_Feature> aSketchFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(
225                                                        reference(SKETCH_OBJECT_ID())->value());
226
227   if(aSketchFeature.get() && !aSketchFeature->results().empty()) {
228     ResultPtr aSketchRes = aSketchFeature->results().front();
229     ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
230     if(aConstruction.get()) {
231       AttributeSelectionListPtr aFacesSelectionList = selectionList(LIST_ID());
232       if (aFacesSelectionList.get() && aFacesSelectionList->size() == 0)
233         aFacesSelectionList->append(aSketchRes, std::shared_ptr<GeomAPI_Shape>());
234     }
235   }
236 }
237