]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_CompositeSketch.cpp
Salome HOME
b1f78b9fd5265a598cb7156b20350824dd9156a2
[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 #include <FeaturesPlugin_Tools.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_Revolution.h>
31
32 #include <GeomAPI_ShapeExplorer.h>
33
34
35 static void storeSubShape(const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
36                           ResultBodyPtr theResultBody,
37                           const GeomShapePtr theShape,
38                           const GeomAPI_Shape::ShapeType theType,
39                           const std::string& theName);
40
41 //=================================================================================================
42 void FeaturesPlugin_CompositeSketch::initCompositeSketchAttribtues(const int theInitFlags)
43 {
44   // Initialize sketch launcher.
45   if(theInitFlags & InitSketchLauncher) {
46     data()->addAttribute(SKETCH_ID(), ModelAPI_AttributeReference::typeId());
47     ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SKETCH_ID());
48   }
49
50   // Initialize selection list.
51   if(theInitFlags & InitBaseObjectsList) {
52     data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
53     myCurrentSelectionType = selectionList(BASE_OBJECTS_ID())->selectionType();
54   }
55 }
56
57 //=================================================================================================
58 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeSketch::addFeature(std::string theID)
59 {
60   FeaturePtr aNew = document()->addFeature(theID, false);
61   if(aNew) {
62     data()->reference(SKETCH_ID())->setValue(aNew);
63   }
64
65   // Set as current also after it becomes sub to set correctly enabled for other sketch subs.
66   document()->setCurrentFeature(aNew, false);
67   return aNew;
68 }
69
70 //=================================================================================================
71 int FeaturesPlugin_CompositeSketch::numberOfSubs(bool forTree) const
72 {
73   ObjectPtr aObj = data()->reference(SKETCH_ID())->value();
74   return aObj.get() ? 1 : 0;
75 }
76
77 //=================================================================================================
78 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeSketch::subFeature(const int theIndex,
79                                                                              bool forTree)
80 {
81   FeaturePtr aSubFeature;
82   if(theIndex == 0) {
83     aSubFeature =
84         std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_ID())->value());
85   }
86   return aSubFeature;
87 }
88
89 //=================================================================================================
90 int FeaturesPlugin_CompositeSketch::subFeatureId(const int theIndex) const
91 {
92   if(theIndex == 0) {
93     FeaturePtr aFeature =
94       std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_ID())->value());
95     if(aFeature.get()) {
96       return aFeature->data()->featureId();
97     }
98   }
99
100   return -1;
101 }
102
103 //=================================================================================================
104 bool FeaturesPlugin_CompositeSketch::isSub(ObjectPtr theObject) const
105 {
106   bool isSubFeature = false;
107   // Check is this feature of result
108   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
109   if (aFeature.get()) {
110     ObjectPtr aSub = data()->reference(SKETCH_ID())->value();
111     isSubFeature = aSub == theObject;
112   }
113   return isSubFeature;
114 }
115
116 //=================================================================================================
117 void FeaturesPlugin_CompositeSketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
118 {
119   AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
120   if(aBaseObjectsSelectionList.get() && aBaseObjectsSelectionList->size() > 0) {
121     aBaseObjectsSelectionList->clear();
122   }
123
124   reference(SKETCH_ID())->setValue(ObjectPtr());
125 }
126
127 //=================================================================================================
128 void FeaturesPlugin_CompositeSketch::getBaseShapes(ListOfShape& theBaseShapesList,
129                                                    const bool theIsMakeShells)
130 {
131   AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
132   std::string anError;
133   bool isOk = FeaturesPlugin_Tools::getShape(
134       aBaseObjectsSelectionList, theIsMakeShells, theBaseShapesList, anError);
135   if (!isOk)
136     setError(anError);
137 }
138
139 //=================================================================================================
140 void FeaturesPlugin_CompositeSketch::storeResult(const GeomShapePtr theBaseShape,
141                                         const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
142                                         const int theIndex)
143 {
144   // Create result body.
145   ResultBodyPtr aResultBody = document()->createBody(data(), theIndex);
146
147   // Store generated shape.
148   aResultBody->storeGenerated(theBaseShape, theMakeShape->shape());
149
150   // Store generated edges/faces.
151   storeGenerationHistory(aResultBody, theBaseShape, theMakeShape);
152
153   setResult(aResultBody, theIndex);
154 }
155
156 //=================================================================================================
157 void FeaturesPlugin_CompositeSketch::storeGenerationHistory(ResultBodyPtr theResultBody,
158                                         const GeomShapePtr theBaseShape,
159                                         const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
160 {
161   GeomAPI_Shape::ShapeType aBaseShapeType = theBaseShape->shapeType();
162   GeomAPI_Shape::ShapeType aShapeTypeToExplode = GeomAPI_Shape::SHAPE;
163
164   switch(aBaseShapeType) {
165     case GeomAPI_Shape::EDGE: {
166             aShapeTypeToExplode = GeomAPI_Shape::VERTEX;
167       break;
168     }
169     case GeomAPI_Shape::WIRE: {
170       aShapeTypeToExplode = GeomAPI_Shape::COMPOUND;
171       break;
172     }
173     case GeomAPI_Shape::FACE:
174     case GeomAPI_Shape::SHELL: {
175       aShapeTypeToExplode = GeomAPI_Shape::EDGE;
176       break;
177     }
178     case GeomAPI_Shape::COMPOUND: {
179       aShapeTypeToExplode = GeomAPI_Shape::COMPOUND;
180     }
181   }
182
183   int aLateralIndex = 1;
184   int aBaseEdgeIndex = 1;
185   int aVertexIndex = 1;
186   int aBaseVertexIndex = 1;
187
188   if(aShapeTypeToExplode == GeomAPI_Shape::VERTEX ||
189       aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
190     theResultBody->loadGeneratedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::VERTEX);
191   }
192   if(aShapeTypeToExplode == GeomAPI_Shape::EDGE ||
193       aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
194     theResultBody->loadGeneratedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::EDGE);
195   }
196   std::list<std::shared_ptr<GeomAlgoAPI_MakeSweep> > aSweeps; // all sweeps collected
197   std::shared_ptr<GeomAlgoAPI_MakeSweep> aMakeSweep =
198     std::dynamic_pointer_cast<GeomAlgoAPI_MakeSweep>(theMakeShape);
199   if(aMakeSweep.get()) {
200     aSweeps.push_back(aMakeSweep);
201   } else {
202     std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeList =
203       std::dynamic_pointer_cast<GeomAlgoAPI_MakeShapeList>(theMakeShape);
204     if (aMakeList.get()) {
205       ListOfMakeShape::const_iterator anIter = aMakeList->list().cbegin();
206       for(; anIter != aMakeList->list().cend(); anIter++) {
207         std::shared_ptr<GeomAlgoAPI_MakeSweep> aSweep =
208           std::dynamic_pointer_cast<GeomAlgoAPI_MakeSweep>(*anIter);
209         if (aSweep.get())
210           aSweeps.push_back(aSweep);
211       }
212     }
213   }
214   std::list<std::shared_ptr<GeomAlgoAPI_MakeSweep> >::iterator aSweep = aSweeps.begin();
215   for(; aSweep != aSweeps.end(); aSweep++) {
216     // Store from shapes.
217     storeShapes(theMakeShape, theResultBody, aBaseShapeType, (*aSweep)->fromShapes(), "From_");
218
219     // Store to shapes.
220     storeShapes(theMakeShape, theResultBody, aBaseShapeType, (*aSweep)->toShapes(), "To_");
221   }
222 }
223
224 //=================================================================================================
225 void FeaturesPlugin_CompositeSketch::storeShapes(
226   const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
227   ResultBodyPtr theResultBody,
228   const GeomAPI_Shape::ShapeType theBaseShapeType,
229   const ListOfShape& theShapes,
230   const std::string theName)
231 {
232   GeomAPI_Shape::ShapeType aShapeTypeToExplore = GeomAPI_Shape::FACE;
233   std::string aShapeTypeStr = "Face";
234   switch(theBaseShapeType) {
235     case GeomAPI_Shape::VERTEX: {
236       aShapeTypeToExplore = GeomAPI_Shape::VERTEX;
237       aShapeTypeStr = "Vertex";
238       break;
239     }
240     case GeomAPI_Shape::EDGE:
241     case GeomAPI_Shape::WIRE: {
242       aShapeTypeToExplore = GeomAPI_Shape::EDGE;
243       aShapeTypeStr = "Edge";
244       break;
245     }
246     case GeomAPI_Shape::FACE:
247     case GeomAPI_Shape::SHELL: {
248       aShapeTypeToExplore = GeomAPI_Shape::FACE;
249       aShapeTypeStr = "Face";
250       break;
251     }
252     case GeomAPI_Shape::COMPOUND: {
253       aShapeTypeToExplore = GeomAPI_Shape::COMPOUND;
254       break;
255     }
256   }
257
258   // Store shapes.
259   for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
260     GeomShapePtr aShape = *anIt;
261
262     if(aShapeTypeToExplore == GeomAPI_Shape::COMPOUND) {
263       std::string aName = theName + (aShape->shapeType() == GeomAPI_Shape::EDGE ? "Edge" : "Face");
264       storeSubShape(theMakeShape, theResultBody, aShape, aShape->shapeType(), aName);
265     } else {
266       std::string aName = theName + aShapeTypeStr;
267       storeSubShape(theMakeShape, theResultBody, aShape, aShapeTypeToExplore, aName);
268       if (theBaseShapeType == GeomAPI_Shape::WIRE) { // issue 2289: special names also for vertices
269         aName = theName + "Vertex";
270         storeSubShape(theMakeShape, theResultBody, aShape, GeomAPI_Shape::VERTEX, aName);
271       }
272     }
273   }
274 }
275
276 void storeSubShape(
277   const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
278   ResultBodyPtr theResultBody,
279   const GeomShapePtr theShape,
280   const GeomAPI_Shape::ShapeType theType,
281   const std::string& theName)
282 {
283   for(GeomAPI_ShapeExplorer anExp(theShape, theType); anExp.more(); anExp.next()) {
284     GeomShapePtr aSubShape = anExp.current();
285     if (!theResultBody->generated(aSubShape, theName)) {
286       // store from/to shapes as primitives and then store modification of them by the boolean
287       theResultBody->generated(aSubShape, theName, false);
288       theResultBody->loadModifiedShapes(theMakeShape, aSubShape, theType);
289     }
290   }
291 }
292
293 //=================================================================================================
294 void FeaturesPlugin_CompositeSketch::attributeChanged(const std::string& theID)
295 {
296   if (theID == BASE_OBJECTS_ID()) {
297     AttributeSelectionListPtr anObjects = selectionList(BASE_OBJECTS_ID());
298     if (anObjects->size() == 0 || anObjects->selectionType() != myCurrentSelectionType) {
299       myCurrentSelectionType = anObjects->selectionType();
300       removeResults(0); // clear the results
301     }
302   }
303 }