1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: FeaturesPlugin_CompositeSketch.cpp
4 // Created: 11 September 2015
5 // Author: Dmitry Bobylev
7 #include <FeaturesPlugin_CompositeSketch.h>
9 #include <ModelAPI_AttributeSelectionList.h>
10 #include <ModelAPI_AttributeReference.h>
11 #include <ModelAPI_BodyBuilder.h>
12 #include <ModelAPI_ResultConstruction.h>
13 #include <ModelAPI_Session.h>
14 #include <ModelAPI_Validator.h>
16 #include <GeomAlgoAPI_CompoundBuilder.h>
17 #include <GeomAlgoAPI_Prism.h>
18 #include <GeomAlgoAPI_Revolution.h>
19 #include <GeomAlgoAPI_ShapeTools.h>
20 #include <GeomAlgoAPI_SketchBuilder.h>
22 #include <GeomAPI_PlanarEdges.h>
23 #include <GeomAPI_ShapeExplorer.h>
28 static void storeSubShape(ResultBodyPtr theResultBody,
29 const GeomShapePtr theShape,
30 const GeomAPI_Shape::ShapeType theType,
31 const std::shared_ptr<GeomAPI_DataMapOfShapeShape> theMapOfSubShapes,
32 const std::string theName,
36 //=================================================================================================
37 void FeaturesPlugin_CompositeSketch::initCompositeSketchAttribtues(const int theInitFlags)
39 // Initialize sketch launcher.
40 if(theInitFlags & InitSketchLauncher) {
41 data()->addAttribute(SKETCH_ID(), ModelAPI_AttributeReference::typeId());
42 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SKETCH_ID());
45 // Initialize selection list.
46 if(theInitFlags & InitBaseObjectsList) {
47 data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
51 //=================================================================================================
52 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeSketch::addFeature(std::string theID)
54 FeaturePtr aNew = document()->addFeature(theID, false);
56 data()->reference(SKETCH_ID())->setValue(aNew);
59 // Set as current also after it becomes sub to set correctly enabled for other sketch subs.
60 document()->setCurrentFeature(aNew, false);
64 //=================================================================================================
65 int FeaturesPlugin_CompositeSketch::numberOfSubs(bool forTree) const
67 ObjectPtr aObj = data()->reference(SKETCH_ID())->value();
68 return aObj.get() ? 1 : 0;
71 //=================================================================================================
72 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeSketch::subFeature(const int theIndex, bool forTree)
75 return std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_ID())->value());
78 return std::shared_ptr<ModelAPI_Feature>();
81 //=================================================================================================
82 int FeaturesPlugin_CompositeSketch::subFeatureId(const int theIndex) const
86 std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_ID())->value());
88 return aFeature->data()->featureId();
95 //=================================================================================================
96 bool FeaturesPlugin_CompositeSketch::isSub(ObjectPtr theObject) const
98 // Check is this feature of result
99 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
100 if(!aFeature.get()) {
104 ObjectPtr aSub = data()->reference(SKETCH_ID())->value();
105 return aSub == theObject;
108 //=================================================================================================
109 void FeaturesPlugin_CompositeSketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
111 AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
112 if(aBaseObjectsSelectionList.get() && aBaseObjectsSelectionList->size() > 0) {
113 aBaseObjectsSelectionList->clear();
116 reference(SKETCH_ID())->setValue(ObjectPtr());
119 //=================================================================================================
120 void FeaturesPlugin_CompositeSketch::getBaseShapes(ListOfShape& theBaseShapesList,
121 const bool theIsMakeShells)
123 theBaseShapesList.clear();
125 ListOfShape aBaseFacesList;
126 std::map<ResultConstructionPtr, ListOfShape> aSketchWiresMap;
127 AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
128 if(!aBaseObjectsSelectionList.get()) {
129 setError("Error: Could not get base objects selection list.");
132 if(aBaseObjectsSelectionList->size() == 0) {
133 setError("Error: Base objects list is empty.");
136 for(int anIndex = 0; anIndex < aBaseObjectsSelectionList->size(); anIndex++) {
137 AttributeSelectionPtr aBaseObjectSelection = aBaseObjectsSelectionList->value(anIndex);
138 if(!aBaseObjectSelection.get()) {
139 setError("Error: Selected base object is empty.");
142 GeomShapePtr aBaseShape = aBaseObjectSelection->value();
143 if(aBaseShape.get() && !aBaseShape->isNull()) {
144 GeomAPI_Shape::ShapeType aST = aBaseShape->shapeType();
145 if(aST == GeomAPI_Shape::SOLID || aST == GeomAPI_Shape::COMPSOLID) {
146 setError("Error: Selected shapes has unsupported type.");
149 ResultConstructionPtr aConstruction =
150 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseObjectSelection->context());
151 if(aConstruction.get() && !aBaseShape->isEqual(aConstruction->shape()) && aST == GeomAPI_Shape::WIRE) {
152 // It is a wire on the sketch, store it to make face later.
153 aSketchWiresMap[aConstruction].push_back(aBaseShape);
156 aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
157 theBaseShapesList.push_back(aBaseShape);
160 // This may be the whole sketch result selected, check and get faces.
161 ResultConstructionPtr aConstruction =
162 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseObjectSelection->context());
163 if(!aConstruction.get()) {
164 setError("Error: Selected sketches does not have results.");
167 int aFacesNum = aConstruction->facesNum();
169 // Probably it can be construction.
170 aBaseShape = aConstruction->shape();
171 if(aBaseShape.get() && !aBaseShape->isNull()) {
172 GeomAPI_Shape::ShapeType aST = aBaseShape->shapeType();
173 if(aST != GeomAPI_Shape::VERTEX && aST != GeomAPI_Shape::EDGE && aST != GeomAPI_Shape::WIRE &&
174 aST != GeomAPI_Shape::FACE && aST != GeomAPI_Shape::SHELL) {
175 setError("Error: Selected shapes has unsupported type.");
178 aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
179 theBaseShapesList.push_back(aBaseShape);
182 for(int aFaceIndex = 0; aFaceIndex < aFacesNum; aFaceIndex++) {
183 GeomShapePtr aBaseFace = aConstruction->face(aFaceIndex);
184 if(!aBaseFace.get() || aBaseFace->isNull()) {
185 setError("Error: One of the faces on selected sketch is null.");
188 aBaseFacesList.push_back(aBaseFace);
194 // Make faces from sketch wires.
195 for(std::map<ResultConstructionPtr, ListOfShape>::const_iterator anIt = aSketchWiresMap.cbegin();
196 anIt != aSketchWiresMap.cend(); ++anIt) {
197 const std::shared_ptr<GeomAPI_PlanarEdges> aSketchPlanarEdges =
198 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>((*anIt).first->shape());
199 const ListOfShape& aWiresList = (*anIt).second;
201 GeomAlgoAPI_ShapeTools::makeFacesWithHoles(aSketchPlanarEdges->origin(),
202 aSketchPlanarEdges->norm(),
205 aBaseFacesList.insert(aBaseFacesList.end(), aFaces.begin(), aFaces.end());
208 // Searching faces with common edges.
209 if(theIsMakeShells && aBaseFacesList.size() > 1) {
211 ListOfShape aFreeFaces;
212 GeomShapePtr aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aBaseFacesList);
213 GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL, aShells, aFreeFaces);
214 theBaseShapesList.insert(theBaseShapesList.end(), aFreeFaces.begin(), aFreeFaces.end());
215 theBaseShapesList.insert(theBaseShapesList.end(), aShells.begin(), aShells.end());
217 theBaseShapesList.insert(theBaseShapesList.end(), aBaseFacesList.begin(), aBaseFacesList.end());
221 //=================================================================================================
222 bool FeaturesPlugin_CompositeSketch::isMakeShapeValid(const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
224 // Check that algo is done.
225 if(!theMakeShape->isDone()) {
226 setError("Error: " + getKind() + " algorithm failed.");
230 // Check if shape is not null.
231 if(!theMakeShape->shape().get() || theMakeShape->shape()->isNull()) {
232 setError("Error: Resulting shape is null.");
236 // Check that resulting shape is valid.
237 if(!theMakeShape->isValid()) {
238 setError("Error: Resulting shape is not valid.");
245 //=================================================================================================
246 void FeaturesPlugin_CompositeSketch::storeResult(const GeomShapePtr theBaseShape,
247 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
250 // Create result body.
251 ResultBodyPtr aResultBody = document()->createBody(data(), theIndex);
253 // Store generated shape.
254 aResultBody->storeGenerated(theBaseShape, theMakeShape->shape());
256 // Store generated edges/faces.
258 storeGenerationHistory(aResultBody, theBaseShape, theMakeShape, aGenTag);
260 setResult(aResultBody, theIndex);
263 //=================================================================================================
264 void FeaturesPlugin_CompositeSketch::storeGenerationHistory(ResultBodyPtr theResultBody,
265 const GeomShapePtr theBaseShape,
266 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
269 GeomAPI_Shape::ShapeType aBaseShapeType = theBaseShape->shapeType();
270 GeomAPI_Shape::ShapeType aShapeTypeToExplode = GeomAPI_Shape::SHAPE;
271 std::string aGenName = "Generated_";
273 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfSubShapes = theMakeShape->mapOfSubShapes();
274 switch(aBaseShapeType) {
275 case GeomAPI_Shape::EDGE:
276 case GeomAPI_Shape::WIRE: {
277 //std::shared_ptr<GeomAPI_Vertex> aV1, aV2;
278 //GeomAlgoAPI_ShapeTools::findBounds(theBaseShape, aV1, aV2);
279 //ListOfShape aV1History, aV2History;
280 //theMakeShape->generated(aV1, aV1History);
281 //theMakeShape->generated(aV2, aV2History);
282 //if(!aV1History.empty()) {
283 // theResultBody->generated(aV1, aV1History.front(), aGenName + "Edge_1", theTag++);
285 //if(!aV2History.empty()) {
286 // theResultBody->generated(aV2, aV2History.front(), aGenName + "Edge_2", theTag++);
288 aShapeTypeToExplode = GeomAPI_Shape::VERTEX;
291 case GeomAPI_Shape::FACE:
292 case GeomAPI_Shape::SHELL: {
293 aShapeTypeToExplode = GeomAPI_Shape::EDGE;
296 case GeomAPI_Shape::COMPOUND: {
297 aShapeTypeToExplode = GeomAPI_Shape::COMPOUND;
301 if(aShapeTypeToExplode == GeomAPI_Shape::VERTEX || aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
302 theResultBody->loadAndOrientGeneratedShapes(theMakeShape.get(), theBaseShape, GeomAPI_Shape::VERTEX,
303 theTag++, aGenName + "Edge", *aMapOfSubShapes.get());
305 if(aShapeTypeToExplode == GeomAPI_Shape::EDGE || aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
306 theResultBody->loadAndOrientGeneratedShapes(theMakeShape.get(), theBaseShape, GeomAPI_Shape::EDGE,
307 theTag++, aGenName + "Face", *aMapOfSubShapes.get());
310 std::shared_ptr<GeomAlgoAPI_MakeSweep> aMakeSweep = std::dynamic_pointer_cast<GeomAlgoAPI_MakeSweep>(theMakeShape);
311 if(aMakeSweep.get()) {
312 // Store from shapes.
313 storeShapes(theResultBody, aBaseShapeType, aMapOfSubShapes, aMakeSweep->fromShapes(), "From_", theTag);
316 storeShapes(theResultBody, aBaseShapeType, aMapOfSubShapes, aMakeSweep->toShapes(), "To_", theTag);
320 //=================================================================================================
321 void FeaturesPlugin_CompositeSketch::storeShapes(ResultBodyPtr theResultBody,
322 const GeomAPI_Shape::ShapeType theBaseShapeType,
323 const std::shared_ptr<GeomAPI_DataMapOfShapeShape> theMapOfSubShapes,
324 const ListOfShape& theShapes,
325 const std::string theName,
328 GeomAPI_Shape::ShapeType aShapeTypeToExplore = GeomAPI_Shape::FACE;
329 std::string aShapeTypeStr = "Face";
330 switch(theBaseShapeType) {
331 case GeomAPI_Shape::VERTEX: {
332 aShapeTypeToExplore = GeomAPI_Shape::VERTEX;
333 aShapeTypeStr = "Vertex";
336 case GeomAPI_Shape::EDGE:
337 case GeomAPI_Shape::WIRE: {
338 aShapeTypeToExplore = GeomAPI_Shape::EDGE;
339 aShapeTypeStr = "Edge";
342 case GeomAPI_Shape::FACE:
343 case GeomAPI_Shape::SHELL: {
344 aShapeTypeToExplore = GeomAPI_Shape::FACE;
345 aShapeTypeStr = "Face";
348 case GeomAPI_Shape::COMPOUND: {
349 aShapeTypeToExplore = GeomAPI_Shape::COMPOUND;
357 for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
358 GeomShapePtr aShape = *anIt;
360 if(aShapeTypeToExplore == GeomAPI_Shape::COMPOUND) {
361 std::string aName = theName + (aShape->shapeType() == GeomAPI_Shape::EDGE ? "Edge" : "Face");
362 storeSubShape(theResultBody,
367 aShape->shapeType() == GeomAPI_Shape::EDGE ? aShapeIndex : aFaceIndex,
370 std::string aName = theName + aShapeTypeStr;
371 storeSubShape(theResultBody, aShape, aShapeTypeToExplore,
372 theMapOfSubShapes, aName, aShapeIndex, theTag);
377 void storeSubShape(ResultBodyPtr theResultBody,
378 const GeomShapePtr theShape,
379 const GeomAPI_Shape::ShapeType theType,
380 const std::shared_ptr<GeomAPI_DataMapOfShapeShape> theMapOfSubShapes,
381 const std::string theName,
385 for(GeomAPI_ShapeExplorer anExp(theShape, theType); anExp.more(); anExp.next()) {
386 GeomShapePtr aSubShape = anExp.current();
387 if(theMapOfSubShapes->isBound(aSubShape)) {
388 aSubShape = theMapOfSubShapes->find(aSubShape);
390 std::ostringstream aStr;
391 aStr << theName << "_" << theShapeIndex++;
392 theResultBody->generated(aSubShape, aStr.str(), theTag++);