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 case GeomAPI_Shape::WIRE: {
283 //std::shared_ptr<GeomAPI_Vertex> aV1, aV2;
284 //GeomAlgoAPI_ShapeTools::findBounds(theBaseShape, aV1, aV2);
285 //ListOfShape aV1History, aV2History;
286 //theMakeShape->generated(aV1, aV1History);
287 //theMakeShape->generated(aV2, aV2History);
288 //if(!aV1History.empty()) {
289 // theResultBody->generated(aV1, aV1History.front(), aGenName + "Edge_1", theTag++);
291 //if(!aV2History.empty()) {
292 // theResultBody->generated(aV2, aV2History.front(), aGenName + "Edge_2", theTag++);
294 aShapeTypeToExplode = GeomAPI_Shape::VERTEX;
297 case GeomAPI_Shape::FACE:
298 case GeomAPI_Shape::SHELL: {
299 aShapeTypeToExplode = GeomAPI_Shape::EDGE;
302 case GeomAPI_Shape::COMPOUND: {
303 aShapeTypeToExplode = GeomAPI_Shape::COMPOUND;
307 if(aShapeTypeToExplode == GeomAPI_Shape::VERTEX ||
308 aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
309 theResultBody->loadAndOrientGeneratedShapes(theMakeShape.get(), theBaseShape,
310 GeomAPI_Shape::VERTEX,
311 theTag++, aGenName + "Edge",
312 *aMapOfSubShapes.get());
314 if(aShapeTypeToExplode == GeomAPI_Shape::EDGE ||
315 aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
316 theResultBody->loadAndOrientGeneratedShapes(theMakeShape.get(),
317 theBaseShape, GeomAPI_Shape::EDGE,
318 theTag++, aGenName + "Face",
319 *aMapOfSubShapes.get());
322 std::shared_ptr<GeomAlgoAPI_MakeSweep> aMakeSweep =
323 std::dynamic_pointer_cast<GeomAlgoAPI_MakeSweep>(theMakeShape);
324 if(aMakeSweep.get()) {
325 // Store from shapes.
326 storeShapes(theResultBody, aBaseShapeType, aMapOfSubShapes,
327 aMakeSweep->fromShapes(), "From_", theTag);
330 storeShapes(theResultBody, aBaseShapeType, aMapOfSubShapes,
331 aMakeSweep->toShapes(), "To_", theTag);
335 //=================================================================================================
336 void FeaturesPlugin_CompositeSketch::storeShapes(ResultBodyPtr theResultBody,
337 const GeomAPI_Shape::ShapeType theBaseShapeType,
338 const std::shared_ptr<GeomAPI_DataMapOfShapeShape> theMapOfSubShapes,
339 const ListOfShape& theShapes,
340 const std::string theName,
343 GeomAPI_Shape::ShapeType aShapeTypeToExplore = GeomAPI_Shape::FACE;
344 std::string aShapeTypeStr = "Face";
345 switch(theBaseShapeType) {
346 case GeomAPI_Shape::VERTEX: {
347 aShapeTypeToExplore = GeomAPI_Shape::VERTEX;
348 aShapeTypeStr = "Vertex";
351 case GeomAPI_Shape::EDGE:
352 case GeomAPI_Shape::WIRE: {
353 aShapeTypeToExplore = GeomAPI_Shape::EDGE;
354 aShapeTypeStr = "Edge";
357 case GeomAPI_Shape::FACE:
358 case GeomAPI_Shape::SHELL: {
359 aShapeTypeToExplore = GeomAPI_Shape::FACE;
360 aShapeTypeStr = "Face";
363 case GeomAPI_Shape::COMPOUND: {
364 aShapeTypeToExplore = GeomAPI_Shape::COMPOUND;
372 for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
373 GeomShapePtr aShape = *anIt;
375 if(aShapeTypeToExplore == GeomAPI_Shape::COMPOUND) {
376 std::string aName = theName + (aShape->shapeType() == GeomAPI_Shape::EDGE ? "Edge" : "Face");
377 storeSubShape(theResultBody,
382 aShape->shapeType() == GeomAPI_Shape::EDGE ? aShapeIndex : aFaceIndex,
385 std::string aName = theName + aShapeTypeStr;
386 storeSubShape(theResultBody, aShape, aShapeTypeToExplore,
387 theMapOfSubShapes, aName, aShapeIndex, theTag);
392 void storeSubShape(ResultBodyPtr theResultBody,
393 const GeomShapePtr theShape,
394 const GeomAPI_Shape::ShapeType theType,
395 const std::shared_ptr<GeomAPI_DataMapOfShapeShape> theMapOfSubShapes,
396 const std::string theName,
400 for(GeomAPI_ShapeExplorer anExp(theShape, theType); anExp.more(); anExp.next()) {
401 GeomShapePtr aSubShape = anExp.current();
402 if(theMapOfSubShapes->isBound(aSubShape)) {
403 aSubShape = theMapOfSubShapes->find(aSubShape);
405 std::ostringstream aStr;
406 aStr << theName << "_" << theShapeIndex++;
407 theResultBody->generated(aSubShape, aStr.str(), theTag++);