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 //=================================================================================================
29 void FeaturesPlugin_CompositeSketch::initCompositeSketchAttribtues(const int theInitFlags)
31 // Initialize sketch launcher.
32 if(theInitFlags & InitSketchLauncher) {
33 data()->addAttribute(SKETCH_ID(), ModelAPI_AttributeReference::typeId());
34 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SKETCH_ID());
37 // Initialize selection list.
38 if(theInitFlags & InitBaseObjectsList) {
39 data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
43 //=================================================================================================
44 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeSketch::addFeature(std::string theID)
46 FeaturePtr aNew = document()->addFeature(theID, false);
48 data()->reference(SKETCH_ID())->setValue(aNew);
51 // Set as current also after it becomes sub to set correctly enabled for other sketch subs.
52 document()->setCurrentFeature(aNew, false);
56 //=================================================================================================
57 int FeaturesPlugin_CompositeSketch::numberOfSubs(bool forTree) const
59 ObjectPtr aObj = data()->reference(SKETCH_ID())->value();
60 return aObj.get() ? 1 : 0;
63 //=================================================================================================
64 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeSketch::subFeature(const int theIndex, bool forTree)
67 return std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_ID())->value());
70 return std::shared_ptr<ModelAPI_Feature>();
73 //=================================================================================================
74 int FeaturesPlugin_CompositeSketch::subFeatureId(const int theIndex) const
78 std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_ID())->value());
80 return aFeature->data()->featureId();
87 //=================================================================================================
88 bool FeaturesPlugin_CompositeSketch::isSub(ObjectPtr theObject) const
90 // Check is this feature of result
91 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
96 ObjectPtr aSub = data()->reference(SKETCH_ID())->value();
97 return aSub == theObject;
100 //=================================================================================================
101 void FeaturesPlugin_CompositeSketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
103 AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
104 if(aBaseObjectsSelectionList.get() && aBaseObjectsSelectionList->size() > 0) {
105 aBaseObjectsSelectionList->clear();
108 reference(SKETCH_ID())->setValue(ObjectPtr());
111 //=================================================================================================
112 void FeaturesPlugin_CompositeSketch::getBaseShapes(ListOfShape& theBaseShapesList,
113 const bool theIsMakeShells)
115 theBaseShapesList.clear();
117 ListOfShape aBaseFacesList;
118 std::map<ResultConstructionPtr, ListOfShape> aSketchWiresMap;
119 AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
120 if(!aBaseObjectsSelectionList.get()) {
121 setError("Error: Could not get base objects selection list.");
124 if(aBaseObjectsSelectionList->size() == 0) {
125 setError("Error: Base objects list is empty.");
128 for(int anIndex = 0; anIndex < aBaseObjectsSelectionList->size(); anIndex++) {
129 AttributeSelectionPtr aBaseObjectSelection = aBaseObjectsSelectionList->value(anIndex);
130 if(!aBaseObjectSelection.get()) {
131 setError("Error: Selected base object is empty.");
134 GeomShapePtr aBaseShape = aBaseObjectSelection->value();
135 if(aBaseShape.get() && !aBaseShape->isNull()) {
136 GeomAPI_Shape::ShapeType aST = aBaseShape->shapeType();
137 if(aST != GeomAPI_Shape::VERTEX && aST != GeomAPI_Shape::EDGE && aST != GeomAPI_Shape::WIRE &&
138 aST != GeomAPI_Shape::FACE && aST != GeomAPI_Shape::SHELL) {
139 setError("Error: Selected shapes has unsupported type.");
142 ResultConstructionPtr aConstruction =
143 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseObjectSelection->context());
144 if(aConstruction.get() && !aBaseShape->isEqual(aConstruction->shape()) && aST == GeomAPI_Shape::WIRE) {
145 // It is a wire on the sketch, store it to make face later.
146 aSketchWiresMap[aConstruction].push_back(aBaseShape);
149 aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
150 theBaseShapesList.push_back(aBaseShape);
153 // This may be the whole sketch result selected, check and get faces.
154 ResultConstructionPtr aConstruction =
155 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseObjectSelection->context());
156 if(!aConstruction.get()) {
157 setError("Error: Selected sketches does not have results.");
160 int aFacesNum = aConstruction->facesNum();
162 // Probably it can be construction.
163 aBaseShape = aConstruction->shape();
164 if(aBaseShape.get() && !aBaseShape->isNull()) {
165 GeomAPI_Shape::ShapeType aST = aBaseShape->shapeType();
166 if(aST != GeomAPI_Shape::VERTEX && aST != GeomAPI_Shape::EDGE && aST != GeomAPI_Shape::WIRE &&
167 aST != GeomAPI_Shape::FACE && aST != GeomAPI_Shape::SHELL) {
168 setError("Error: Selected shapes has unsupported type.");
171 aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
172 theBaseShapesList.push_back(aBaseShape);
175 for(int aFaceIndex = 0; aFaceIndex < aFacesNum; aFaceIndex++) {
176 GeomShapePtr aBaseFace = aConstruction->face(aFaceIndex);
177 if(!aBaseFace.get() || aBaseFace->isNull()) {
178 setError("Error: One of the faces on selected sketch is null.");
181 aBaseFacesList.push_back(aBaseFace);
187 // Make faces from sketch wires.
188 for(std::map<ResultConstructionPtr, ListOfShape>::const_iterator anIt = aSketchWiresMap.cbegin();
189 anIt != aSketchWiresMap.cend(); ++anIt) {
190 const std::shared_ptr<GeomAPI_PlanarEdges> aSketchPlanarEdges =
191 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>((*anIt).first->shape());
192 const ListOfShape& aWiresList = (*anIt).second;
194 GeomAlgoAPI_ShapeTools::makeFacesWithHoles(aSketchPlanarEdges->origin(),
195 aSketchPlanarEdges->norm(),
198 aBaseFacesList.insert(aBaseFacesList.end(), aFaces.begin(), aFaces.end());
201 // Searching faces with common edges.
202 if(theIsMakeShells) {
204 ListOfShape aFreeFaces;
205 GeomShapePtr aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aBaseFacesList);
206 GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL, aShells, aFreeFaces);
207 theBaseShapesList.insert(theBaseShapesList.end(), aFreeFaces.begin(), aFreeFaces.end());
208 theBaseShapesList.insert(theBaseShapesList.end(), aShells.begin(), aShells.end());
210 theBaseShapesList.insert(theBaseShapesList.end(), aBaseFacesList.begin(), aBaseFacesList.end());
214 //=================================================================================================
215 bool FeaturesPlugin_CompositeSketch::isMakeShapeValid(const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
217 // Check that algo is done.
218 if(!theMakeShape->isDone()) {
219 setError("Error: " + getKind() + " algorithm failed.");
223 // Check if shape is not null.
224 if(!theMakeShape->shape().get() || theMakeShape->shape()->isNull()) {
225 setError("Error: Resulting shape is null.");
229 // Check that resulting shape is valid.
230 if(!theMakeShape->isValid()) {
231 setError("Error: Resulting shape is not valid.");
238 //=================================================================================================
239 void FeaturesPlugin_CompositeSketch::storeResult(const GeomShapePtr theBaseShape,
240 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
241 const int theResultIndex)
243 // Create result body.
244 ResultBodyPtr aResultBody = document()->createBody(data(), theResultIndex);
246 // Store generated shape.
247 aResultBody->storeGenerated(theBaseShape, theMakeShape->shape());
249 // Store generated edges/faces.
251 storeGenerationHistory(aResultBody, theBaseShape, theMakeShape, aGenTag);
253 setResult(aResultBody, theResultIndex);
256 //=================================================================================================
257 void FeaturesPlugin_CompositeSketch::storeGenerationHistory(ResultBodyPtr theResultBody,
258 const GeomShapePtr theBaseShape,
259 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
262 GeomAPI_Shape::ShapeType aBaseShapeType = theBaseShape->shapeType();
263 GeomAPI_Shape::ShapeType aShapeTypeToExplode;
264 std::string aGenName = "Generated_";
266 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfSubShapes = theMakeShape->mapOfSubShapes();
267 switch(aBaseShapeType) {
268 case GeomAPI_Shape::VERTEX: {
269 aShapeTypeToExplode = GeomAPI_Shape::VERTEX;
273 case GeomAPI_Shape::EDGE:
274 case GeomAPI_Shape::WIRE: {
275 std::shared_ptr<GeomAPI_Vertex> aV1, aV2;
276 GeomAlgoAPI_ShapeTools::findBounds(theBaseShape, aV1, aV2);
277 ListOfShape aV1History, aV2History;
278 theMakeShape->generated(aV1, aV1History);
279 theMakeShape->generated(aV2, aV2History);
280 if(!aV1History.empty()) {
281 theResultBody->generated(aV1, aV1History.front(), aGenName + "Edge_1", theTag++);
283 if(!aV2History.empty()) {
284 theResultBody->generated(aV2, aV2History.front(), aGenName + "Edge_2", theTag++);
287 case GeomAPI_Shape::FACE:
288 case GeomAPI_Shape::SHELL: {
289 aShapeTypeToExplode = GeomAPI_Shape::EDGE;
294 theResultBody->loadAndOrientGeneratedShapes(theMakeShape.get(), theBaseShape, aShapeTypeToExplode,
295 theTag++, aGenName, *aMapOfSubShapes.get());
297 std::shared_ptr<GeomAlgoAPI_MakeSweep> aMakeSweep = std::dynamic_pointer_cast<GeomAlgoAPI_MakeSweep>(theMakeShape);
298 if(aMakeSweep.get()) {
299 // Store from shapes.
300 storeShapes(theResultBody, aBaseShapeType, aMapOfSubShapes, aMakeSweep->fromShapes(), "From_", theTag);
303 storeShapes(theResultBody, aBaseShapeType, aMapOfSubShapes, aMakeSweep->toShapes(), "To_", theTag);
307 //=================================================================================================
308 void FeaturesPlugin_CompositeSketch::storeShapes(ResultBodyPtr theResultBody,
309 const GeomAPI_Shape::ShapeType theBaseShapeType,
310 const std::shared_ptr<GeomAPI_DataMapOfShapeShape> theMapOfSubShapes,
311 const ListOfShape& theShapes,
312 const std::string theName,
315 GeomAPI_Shape::ShapeType aShapeTypeToExplore = GeomAPI_Shape::FACE;
316 std::string aShapeTypeStr = "Face";
317 switch(theBaseShapeType) {
318 case GeomAPI_Shape::VERTEX: {
319 aShapeTypeToExplore = GeomAPI_Shape::VERTEX;
320 aShapeTypeStr = "Vertex";
323 case GeomAPI_Shape::EDGE:
324 case GeomAPI_Shape::WIRE: {
325 aShapeTypeToExplore = GeomAPI_Shape::EDGE;
326 aShapeTypeStr = "Edge";
329 case GeomAPI_Shape::FACE:
330 case GeomAPI_Shape::SHELL: {
331 aShapeTypeToExplore = GeomAPI_Shape::FACE;
332 aShapeTypeStr = "Face";
339 std::string aName = theName + aShapeTypeStr;
340 for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
341 GeomShapePtr aShape = *anIt;
342 for(GeomAPI_ShapeExplorer anExp(aShape, aShapeTypeToExplore); anExp.more(); anExp.next()) {
343 GeomShapePtr aSubShape = anExp.current();
344 if(theMapOfSubShapes->isBound(aSubShape)) {
345 aSubShape = theMapOfSubShapes->find(aSubShape);
347 std::ostringstream aStr;
348 aStr << aName << "_" << aShapeIndex++;
349 theResultBody->generated(aSubShape, aStr.str(), theTag++);