Salome HOME
ExtrusionSketch and RevolutionSketch features.
[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 }
89
90 //=================================================================================================
91 void FeaturesPlugin_CompositeSketch::erase()
92 {
93   if (data().get() && data()->isValid()) { // on abort of sketch of this composite it may be invalid
94     FeaturePtr aSketch =
95       std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
96     if (aSketch.get() && aSketch->data()->isValid()) {
97       document()->removeFeature(aSketch);
98     }
99   }
100   ModelAPI_CompositeFeature::erase();
101 }
102
103
104 //=================================================================================================
105 void FeaturesPlugin_CompositeSketch::execute()
106 {
107   // Getting faces to create solids.
108   std::shared_ptr<ModelAPI_Feature> aSketchFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(
109                                                      reference(SKETCH_OBJECT_ID())->value());
110   if(!aSketchFeature || aSketchFeature->results().empty()) {
111     return;
112   }
113   ResultPtr aSketchRes = aSketchFeature->results().front();
114   ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
115   if(!aConstruction.get()) {
116     return;
117   }
118   selection(SKETCH_SELECTION_ID())->setValue(aSketchRes, std::shared_ptr<GeomAPI_Shape>());
119   int aSketchFacesNum = aConstruction->facesNum();
120   if(aSketchFacesNum == 0) {
121     return;
122   }
123   ListOfShape aFacesList;
124   for(int aFaceIndex = 0; aFaceIndex < aSketchFacesNum; aFaceIndex++) {
125     std::shared_ptr<GeomAPI_Shape> aFace = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
126     aFacesList.push_back(aFace);
127   }
128
129   // Searching faces with common edges.
130   ListOfShape aShells;
131   ListOfShape aFreeFaces;
132   std::shared_ptr<GeomAPI_Shape> aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aFacesList);
133   GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL, aShells, aFreeFaces);
134   aShells.insert(aShells.end(), aFreeFaces.begin(), aFreeFaces.end());
135
136   // Generating result for each shell and face.
137   int aErrorsNum = 0;
138   int aResultIndex = 0;
139   for(ListOfShape::const_iterator anIter = aShells.cbegin(); anIter != aShells.cend(); anIter++) {
140     std::shared_ptr<GeomAPI_Shape> aResult;
141     ListOfShape aFromFaces, aToFaces;
142     std::shared_ptr<GeomAlgoAPI_MakeShape> aMakeShape;
143     std::shared_ptr<GeomAPI_DataMapOfShapeShape> aDataMap;
144
145     std::shared_ptr<GeomAPI_Shape> aBaseFace = *anIter;
146     makeSolid(aBaseFace, aResult, aFromFaces, aToFaces, aMakeShape, aDataMap);
147     if(!aResult.get()) {
148       aErrorsNum++;
149       continue;
150     }
151
152     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
153     loadNamingDS(aResultBody, aBaseFace, aResult, aFromFaces, aToFaces, aMakeShape, aDataMap);
154     setResult(aResultBody, aResultIndex);
155     aResultIndex++;
156   }
157
158   if(aErrorsNum > 0) {
159     std::ostringstream aStringStream;
160     aStringStream << "Warning: could not create solid(s) from " << aErrorsNum << " face(s).";
161     setError(aStringStream.str());
162   }
163
164   // Remove the rest results if there were produced in the previous pass.
165   removeResults(aResultIndex);
166 }
167
168 //=================================================================================================
169 void FeaturesPlugin_CompositeSketch::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
170                                                   const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
171                                                   const std::shared_ptr<GeomAPI_Shape>& theResult,
172                                                   const ListOfShape& theFromFaces,
173                                                   const ListOfShape& theToFaces,
174                                                   const std::shared_ptr<GeomAlgoAPI_MakeShape>& theMakeShape,
175                                                   const std::shared_ptr<GeomAPI_DataMapOfShapeShape>& theDataMap)
176 {
177   //load result
178   theResultBody->storeGenerated(theBaseShape, theResult);
179
180   //Insert lateral face : Face from Edge
181   const std::string aLatName = "LateralFace";
182   const int aLatTag = 1;
183   theResultBody->loadAndOrientGeneratedShapes(theMakeShape.get(), theBaseShape, GeomAPI_Shape::EDGE, aLatTag, aLatName, *theDataMap);
184
185   //Insert to faces
186   const std::string aToName = "ToFace";
187   int aToTag = 2;
188   for(ListOfShape::const_iterator anIt = theToFaces.cbegin(); anIt != theToFaces.cend(); anIt++) {
189     std::shared_ptr<GeomAPI_Shape> aToFace = *anIt;
190     if(theDataMap->isBound(aToFace)) {
191       aToFace = theDataMap->find(aToFace);
192     }
193     theResultBody->generated(aToFace, aToName, aToTag++);
194   }
195
196   //Insert from faces
197   const std::string aFromName = "FromFace";
198   int aFromTag = aToTag > 10000 ? aToTag : 10000;
199   for(ListOfShape::const_iterator anIt = theFromFaces.cbegin(); anIt != theFromFaces.cend(); anIt++) {
200     std::shared_ptr<GeomAPI_Shape> aFromFace = *anIt;
201     if(theDataMap->isBound(aFromFace)) {
202       aFromFace = theDataMap->find(aFromFace);
203     }
204     theResultBody->generated(aFromFace, aFromName, aFromTag++);
205   }
206 }