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,
76 return std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_ID())->value());
79 return std::shared_ptr<ModelAPI_Feature>();
82 //=================================================================================================
83 int FeaturesPlugin_CompositeSketch::subFeatureId(const int theIndex) const
87 std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_ID())->value());
89 return aFeature->data()->featureId();
96 //=================================================================================================
97 bool FeaturesPlugin_CompositeSketch::isSub(ObjectPtr theObject) const
99 // Check is this feature of result
100 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
101 if(!aFeature.get()) {
105 ObjectPtr aSub = data()->reference(SKETCH_ID())->value();
106 return aSub == theObject;
109 //=================================================================================================
110 void FeaturesPlugin_CompositeSketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
112 AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
113 if(aBaseObjectsSelectionList.get() && aBaseObjectsSelectionList->size() > 0) {
114 aBaseObjectsSelectionList->clear();
117 reference(SKETCH_ID())->setValue(ObjectPtr());
120 //=================================================================================================
121 void FeaturesPlugin_CompositeSketch::getBaseShapes(ListOfShape& theBaseShapesList,
122 const bool theIsMakeShells)
124 theBaseShapesList.clear();
126 ListOfShape aBaseFacesList;
127 std::map<ResultConstructionPtr, ListOfShape> aSketchWiresMap;
128 AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
129 if(!aBaseObjectsSelectionList.get()) {
130 setError("Error: Could not get base objects selection list.");
133 if(aBaseObjectsSelectionList->size() == 0) {
134 setError("Error: Base objects list is empty.");
137 for(int anIndex = 0; anIndex < aBaseObjectsSelectionList->size(); anIndex++) {
138 AttributeSelectionPtr aBaseObjectSelection = aBaseObjectsSelectionList->value(anIndex);
139 if(!aBaseObjectSelection.get()) {
140 setError("Error: Selected base object is empty.");
143 GeomShapePtr aBaseShape = aBaseObjectSelection->value();
144 if(aBaseShape.get() && !aBaseShape->isNull()) {
145 GeomAPI_Shape::ShapeType aST = aBaseShape->shapeType();
146 if(aST == GeomAPI_Shape::SOLID || aST == GeomAPI_Shape::COMPSOLID) {
147 setError("Error: Selected shapes has unsupported type.");
150 ResultConstructionPtr aConstruction =
151 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseObjectSelection->context());
152 if(aConstruction.get() && !aBaseShape->isEqual(aConstruction->shape()) &&
153 aST == GeomAPI_Shape::WIRE) {
154 // It is a wire on the sketch, store it to make face later.
155 aSketchWiresMap[aConstruction].push_back(aBaseShape);
158 aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
159 theBaseShapesList.push_back(aBaseShape);
162 // This may be the whole sketch result selected, check and get faces.
163 ResultConstructionPtr aConstruction =
164 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseObjectSelection->context());
165 if(!aConstruction.get()) {
166 setError("Error: Selected sketches does not have results.");
169 int aFacesNum = aConstruction->facesNum();
171 // Probably it can be construction.
172 aBaseShape = aConstruction->shape();
173 if(aBaseShape.get() && !aBaseShape->isNull()) {
174 GeomAPI_Shape::ShapeType aST = aBaseShape->shapeType();
175 if(aST != GeomAPI_Shape::VERTEX && aST != GeomAPI_Shape::EDGE &&
176 aST != GeomAPI_Shape::WIRE &&
177 aST != GeomAPI_Shape::FACE && aST != GeomAPI_Shape::SHELL) {
178 setError("Error: Selected shapes has unsupported type.");
181 aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
182 theBaseShapesList.push_back(aBaseShape);
185 for(int aFaceIndex = 0; aFaceIndex < aFacesNum; aFaceIndex++) {
186 GeomShapePtr aBaseFace = aConstruction->face(aFaceIndex);
187 if(!aBaseFace.get() || aBaseFace->isNull()) {
188 setError("Error: One of the faces on selected sketch is null.");
191 aBaseFacesList.push_back(aBaseFace);
197 // Make faces from sketch wires.
198 for(std::map<ResultConstructionPtr, ListOfShape>::const_iterator anIt = aSketchWiresMap.cbegin();
199 anIt != aSketchWiresMap.cend(); ++anIt) {
200 const std::shared_ptr<GeomAPI_PlanarEdges> aSketchPlanarEdges =
201 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>((*anIt).first->shape());
202 const ListOfShape& aWiresList = (*anIt).second;
204 GeomAlgoAPI_ShapeTools::makeFacesWithHoles(aSketchPlanarEdges->origin(),
205 aSketchPlanarEdges->norm(),
208 aBaseFacesList.insert(aBaseFacesList.end(), aFaces.begin(), aFaces.end());
211 // Searching faces with common edges.
212 if(theIsMakeShells && aBaseFacesList.size() > 1) {
214 ListOfShape aFreeFaces;
215 GeomShapePtr aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aBaseFacesList);
216 GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL,
217 aShells, aFreeFaces);
218 theBaseShapesList.insert(theBaseShapesList.end(), aFreeFaces.begin(), aFreeFaces.end());
219 theBaseShapesList.insert(theBaseShapesList.end(), aShells.begin(), aShells.end());
221 theBaseShapesList.insert(theBaseShapesList.end(), aBaseFacesList.begin(),
222 aBaseFacesList.end());
226 //=================================================================================================
227 bool FeaturesPlugin_CompositeSketch::isMakeShapeValid(
228 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
230 // Check that algo is done.
231 if(!theMakeShape->isDone()) {
232 setError("Error: " + getKind() + " algorithm failed.");
236 // Check if shape is not null.
237 if(!theMakeShape->shape().get() || theMakeShape->shape()->isNull()) {
238 setError("Error: Resulting shape is null.");
242 // Check that resulting shape is valid.
243 if(!theMakeShape->isValid()) {
244 setError("Error: Resulting shape is not valid.");
251 //=================================================================================================
252 void FeaturesPlugin_CompositeSketch::storeResult(const GeomShapePtr theBaseShape,
253 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
256 // Create result body.
257 ResultBodyPtr aResultBody = document()->createBody(data(), theIndex);
259 // Store generated shape.
260 aResultBody->storeGenerated(theBaseShape, theMakeShape->shape());
262 // Store generated edges/faces.
264 storeGenerationHistory(aResultBody, theBaseShape, theMakeShape, aGenTag);
266 setResult(aResultBody, theIndex);
269 //=================================================================================================
270 void FeaturesPlugin_CompositeSketch::storeGenerationHistory(ResultBodyPtr theResultBody,
271 const GeomShapePtr theBaseShape,
272 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
275 GeomAPI_Shape::ShapeType aBaseShapeType = theBaseShape->shapeType();
276 GeomAPI_Shape::ShapeType aShapeTypeToExplode = GeomAPI_Shape::SHAPE;
277 std::string aGenName = "Generated_";
279 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfSubShapes = theMakeShape->mapOfSubShapes();
280 switch(aBaseShapeType) {
281 case GeomAPI_Shape::EDGE: {
282 aShapeTypeToExplode = GeomAPI_Shape::VERTEX;
285 case GeomAPI_Shape::WIRE: {
286 //std::shared_ptr<GeomAPI_Vertex> aV1, aV2;
287 //GeomAlgoAPI_ShapeTools::findBounds(theBaseShape, aV1, aV2);
288 //ListOfShape aV1History, aV2History;
289 //theMakeShape->generated(aV1, aV1History);
290 //theMakeShape->generated(aV2, aV2History);
291 //if(!aV1History.empty()) {
292 // theResultBody->generated(aV1, aV1History.front(), aGenName + "Edge_1", theTag++);
294 //if(!aV2History.empty()) {
295 // theResultBody->generated(aV2, aV2History.front(), aGenName + "Edge_2", theTag++);
297 aShapeTypeToExplode = GeomAPI_Shape::COMPOUND;
300 case GeomAPI_Shape::FACE:
301 case GeomAPI_Shape::SHELL: {
302 aShapeTypeToExplode = GeomAPI_Shape::EDGE;
305 case GeomAPI_Shape::COMPOUND: {
306 aShapeTypeToExplode = GeomAPI_Shape::COMPOUND;
310 if(aShapeTypeToExplode == GeomAPI_Shape::VERTEX ||
311 aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
312 theResultBody->loadAndOrientGeneratedShapes(theMakeShape.get(), theBaseShape,
313 GeomAPI_Shape::VERTEX,
314 theTag++, aGenName + "Edge",
315 *aMapOfSubShapes.get());
317 if(aShapeTypeToExplode == GeomAPI_Shape::EDGE ||
318 aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
319 theResultBody->loadAndOrientGeneratedShapes(theMakeShape.get(),
320 theBaseShape, GeomAPI_Shape::EDGE,
321 theTag++, aGenName + "Face",
322 *aMapOfSubShapes.get());
325 std::shared_ptr<GeomAlgoAPI_MakeSweep> aMakeSweep =
326 std::dynamic_pointer_cast<GeomAlgoAPI_MakeSweep>(theMakeShape);
327 if(aMakeSweep.get()) {
328 // Store from shapes.
329 storeShapes(theResultBody, aBaseShapeType, aMapOfSubShapes,
330 aMakeSweep->fromShapes(), "From_", theTag);
333 storeShapes(theResultBody, aBaseShapeType, aMapOfSubShapes,
334 aMakeSweep->toShapes(), "To_", theTag);
338 //=================================================================================================
339 void FeaturesPlugin_CompositeSketch::storeShapes(ResultBodyPtr theResultBody,
340 const GeomAPI_Shape::ShapeType theBaseShapeType,
341 const std::shared_ptr<GeomAPI_DataMapOfShapeShape> theMapOfSubShapes,
342 const ListOfShape& theShapes,
343 const std::string theName,
346 GeomAPI_Shape::ShapeType aShapeTypeToExplore = GeomAPI_Shape::FACE;
347 std::string aShapeTypeStr = "Face";
348 switch(theBaseShapeType) {
349 case GeomAPI_Shape::VERTEX: {
350 aShapeTypeToExplore = GeomAPI_Shape::VERTEX;
351 aShapeTypeStr = "Vertex";
354 case GeomAPI_Shape::EDGE:
355 case GeomAPI_Shape::WIRE: {
356 aShapeTypeToExplore = GeomAPI_Shape::EDGE;
357 aShapeTypeStr = "Edge";
360 case GeomAPI_Shape::FACE:
361 case GeomAPI_Shape::SHELL: {
362 aShapeTypeToExplore = GeomAPI_Shape::FACE;
363 aShapeTypeStr = "Face";
366 case GeomAPI_Shape::COMPOUND: {
367 aShapeTypeToExplore = GeomAPI_Shape::COMPOUND;
375 for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
376 GeomShapePtr aShape = *anIt;
378 if(aShapeTypeToExplore == GeomAPI_Shape::COMPOUND) {
379 std::string aName = theName + (aShape->shapeType() == GeomAPI_Shape::EDGE ? "Edge" : "Face");
380 storeSubShape(theResultBody,
385 aShape->shapeType() == GeomAPI_Shape::EDGE ? aShapeIndex : aFaceIndex,
388 std::string aName = theName + aShapeTypeStr;
389 storeSubShape(theResultBody, aShape, aShapeTypeToExplore,
390 theMapOfSubShapes, aName, aShapeIndex, theTag);
395 void storeSubShape(ResultBodyPtr theResultBody,
396 const GeomShapePtr theShape,
397 const GeomAPI_Shape::ShapeType theType,
398 const std::shared_ptr<GeomAPI_DataMapOfShapeShape> theMapOfSubShapes,
399 const std::string theName,
403 for(GeomAPI_ShapeExplorer anExp(theShape, theType); anExp.more(); anExp.next()) {
404 GeomShapePtr aSubShape = anExp.current();
405 if(theMapOfSubShapes->isBound(aSubShape)) {
406 aSubShape = theMapOfSubShapes->find(aSubShape);
408 std::ostringstream aStr;
409 aStr << theName << "_" << theShapeIndex++;
410 theResultBody->generated(aSubShape, aStr.str(), theTag++);