1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <FeaturesPlugin_CompositeSketch.h>
21 #include <FeaturesPlugin_Tools.h>
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>
30 #include <GeomAlgoAPI_Revolution.h>
32 #include <GeomAPI_ShapeExplorer.h>
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);
41 //=================================================================================================
42 void FeaturesPlugin_CompositeSketch::initCompositeSketchAttribtues(const int theInitFlags)
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());
50 // Initialize selection list.
51 if(theInitFlags & InitBaseObjectsList) {
52 data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
53 myCurrentSelectionType = selectionList(BASE_OBJECTS_ID())->selectionType();
57 //=================================================================================================
58 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeSketch::addFeature(std::string theID)
60 FeaturePtr aNew = document()->addFeature(theID, false);
62 data()->reference(SKETCH_ID())->setValue(aNew);
65 // Set as current also after it becomes sub to set correctly enabled for other sketch subs.
66 document()->setCurrentFeature(aNew, false);
70 //=================================================================================================
71 int FeaturesPlugin_CompositeSketch::numberOfSubs(bool forTree) const
73 ObjectPtr aObj = data()->reference(SKETCH_ID())->value();
74 return aObj.get() ? 1 : 0;
77 //=================================================================================================
78 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeSketch::subFeature(const int theIndex,
81 FeaturePtr aSubFeature;
84 std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_ID())->value());
89 //=================================================================================================
90 int FeaturesPlugin_CompositeSketch::subFeatureId(const int theIndex) const
94 std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_ID())->value());
96 return aFeature->data()->featureId();
103 //=================================================================================================
104 bool FeaturesPlugin_CompositeSketch::isSub(ObjectPtr theObject) const
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;
116 //=================================================================================================
117 void FeaturesPlugin_CompositeSketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
119 AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
120 if(aBaseObjectsSelectionList.get() && aBaseObjectsSelectionList->size() > 0) {
121 aBaseObjectsSelectionList->clear();
124 reference(SKETCH_ID())->setValue(ObjectPtr());
127 //=================================================================================================
128 void FeaturesPlugin_CompositeSketch::getBaseShapes(ListOfShape& theBaseShapesList,
129 const bool theIsMakeShells)
131 AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
133 bool isOk = FeaturesPlugin_Tools::getShape(
134 aBaseObjectsSelectionList, theIsMakeShells, theBaseShapesList, anError);
139 //=================================================================================================
140 void FeaturesPlugin_CompositeSketch::storeResult(const GeomShapePtr theBaseShape,
141 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
144 // Create result body.
145 ResultBodyPtr aResultBody = document()->createBody(data(), theIndex);
147 // Store generated shape.
148 aResultBody->storeGenerated(theBaseShape, theMakeShape->shape());
150 // Store generated edges/faces.
151 storeGenerationHistory(aResultBody, theBaseShape, theMakeShape);
153 setResult(aResultBody, theIndex);
156 //=================================================================================================
157 void FeaturesPlugin_CompositeSketch::storeGenerationHistory(ResultBodyPtr theResultBody,
158 const GeomShapePtr theBaseShape,
159 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
161 GeomAPI_Shape::ShapeType aBaseShapeType = theBaseShape->shapeType();
162 GeomAPI_Shape::ShapeType aShapeTypeToExplode = GeomAPI_Shape::SHAPE;
164 switch(aBaseShapeType) {
165 case GeomAPI_Shape::EDGE: {
166 aShapeTypeToExplode = GeomAPI_Shape::VERTEX;
169 case GeomAPI_Shape::WIRE: {
170 aShapeTypeToExplode = GeomAPI_Shape::COMPOUND;
173 case GeomAPI_Shape::FACE:
174 case GeomAPI_Shape::SHELL: {
175 aShapeTypeToExplode = GeomAPI_Shape::EDGE;
178 case GeomAPI_Shape::COMPOUND: {
179 aShapeTypeToExplode = GeomAPI_Shape::COMPOUND;
183 int aLateralIndex = 1;
184 int aBaseEdgeIndex = 1;
185 int aVertexIndex = 1;
186 int aBaseVertexIndex = 1;
188 if(aShapeTypeToExplode == GeomAPI_Shape::VERTEX ||
189 aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
190 theResultBody->loadGeneratedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::VERTEX);
192 if(aShapeTypeToExplode == GeomAPI_Shape::EDGE ||
193 aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
194 theResultBody->loadGeneratedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::EDGE);
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);
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);
210 aSweeps.push_back(aSweep);
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_");
220 storeShapes(theMakeShape, theResultBody, aBaseShapeType, (*aSweep)->toShapes(), "To_");
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)
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";
240 case GeomAPI_Shape::EDGE:
241 case GeomAPI_Shape::WIRE: {
242 aShapeTypeToExplore = GeomAPI_Shape::EDGE;
243 aShapeTypeStr = "Edge";
246 case GeomAPI_Shape::FACE:
247 case GeomAPI_Shape::SHELL: {
248 aShapeTypeToExplore = GeomAPI_Shape::FACE;
249 aShapeTypeStr = "Face";
252 case GeomAPI_Shape::COMPOUND: {
253 aShapeTypeToExplore = GeomAPI_Shape::COMPOUND;
259 for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
260 GeomShapePtr aShape = *anIt;
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);
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);
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)
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);
293 //=================================================================================================
294 void FeaturesPlugin_CompositeSketch::attributeChanged(const std::string& theID)
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