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_ResultBody.h>
13 #include <ModelAPI_ResultConstruction.h>
14 #include <ModelAPI_Session.h>
15 #include <ModelAPI_Validator.h>
17 #include <GeomAlgoAPI_CompoundBuilder.h>
18 #include <GeomAlgoAPI_Prism.h>
19 #include <GeomAlgoAPI_Revolution.h>
20 #include <GeomAlgoAPI_ShapeTools.h>
24 //=================================================================================================
25 void FeaturesPlugin_CompositeSketch::initAttributes()
27 data()->addAttribute(SKETCH_OBJECT_ID(), ModelAPI_AttributeReference::typeId());
28 data()->addAttribute(SKETCH_SELECTION_ID(), ModelAPI_AttributeSelection::typeId());
29 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SKETCH_SELECTION_ID());
31 initMakeSolidsAttributes();
34 //=================================================================================================
35 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeSketch::addFeature(std::string theID)
37 std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
39 data()->reference(SKETCH_OBJECT_ID())->setValue(aNew);
41 // set as current also after it becomes sub to set correctly enabled for other sketch subs
42 document()->setCurrentFeature(aNew, false);
46 //=================================================================================================
47 int FeaturesPlugin_CompositeSketch::numberOfSubs(bool forTree) const
49 ObjectPtr aObj = data()->reference(SKETCH_OBJECT_ID())->value();
50 return aObj.get()? 1 : 0;
53 //=================================================================================================
54 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeSketch::subFeature(const int theIndex, bool forTree)
57 return std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
58 return std::shared_ptr<ModelAPI_Feature>();
61 //=================================================================================================
62 int FeaturesPlugin_CompositeSketch::subFeatureId(const int theIndex) const
66 std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
68 return aFeature->data()->featureId();
73 //=================================================================================================
74 bool FeaturesPlugin_CompositeSketch::isSub(ObjectPtr theObject) const
76 // check is this feature of result
77 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
81 ObjectPtr aSub = data()->reference(SKETCH_OBJECT_ID())->value();
82 return aSub == theObject;
85 //=================================================================================================
86 void FeaturesPlugin_CompositeSketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
90 //=================================================================================================
91 void FeaturesPlugin_CompositeSketch::erase()
93 if (data().get() && data()->isValid()) { // on abort of sketch of this composite it may be invalid
95 std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
96 if (aSketch.get() && aSketch->data()->isValid()) {
97 document()->removeFeature(aSketch);
100 ModelAPI_CompositeFeature::erase();
104 //=================================================================================================
105 void FeaturesPlugin_CompositeSketch::execute()
107 // Getting faces to create solids.
108 std::shared_ptr<ModelAPI_Feature> aSketchFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(
109 reference(SKETCH_OBJECT_ID())->value());
110 if(!aSketchFeature || aSketchFeature->results().empty()) {
113 ResultPtr aSketchRes = aSketchFeature->results().front();
114 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
115 if(!aConstruction.get()) {
119 /// feature extrusion does not have the next attribute
120 if (data()->attribute(SKETCH_SELECTION_ID()).get()) {
121 if (!selection(SKETCH_SELECTION_ID())->isInitialized() || selection(SKETCH_SELECTION_ID())->context() != aSketchRes) {
122 selection(SKETCH_SELECTION_ID())->setValue(aSketchRes, std::shared_ptr<GeomAPI_Shape>());
125 int aSketchFacesNum = aConstruction->facesNum();
126 if(aSketchFacesNum == 0) {
129 ListOfShape aFacesList;
130 for(int aFaceIndex = 0; aFaceIndex < aSketchFacesNum; aFaceIndex++) {
131 std::shared_ptr<GeomAPI_Shape> aFace = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
132 aFacesList.push_back(aFace);
135 // Searching faces with common edges.
137 ListOfShape aFreeFaces;
138 std::shared_ptr<GeomAPI_Shape> aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aFacesList);
139 GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL, aShells, aFreeFaces);
140 aShells.insert(aShells.end(), aFreeFaces.begin(), aFreeFaces.end());
142 // Generating result for each shell and face.
144 int aResultIndex = 0;
145 for(ListOfShape::const_iterator anIter = aShells.cbegin(); anIter != aShells.cend(); anIter++) {
146 std::shared_ptr<GeomAlgoAPI_MakeShape> aMakeShape;
148 std::shared_ptr<GeomAPI_Shape> aBaseFace = *anIter;
149 makeSolid(aBaseFace, aMakeShape);
150 if(!aMakeShape.get()) {
155 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
156 loadNamingDS(aResultBody, aBaseFace, aMakeShape);
157 setResult(aResultBody, aResultIndex);
162 std::ostringstream aStringStream;
163 aStringStream << "Error: Could not create solid(s) from " << aErrorsNum << " face(s).";
164 setError(aStringStream.str());
167 // Remove the rest results if there were produced in the previous pass.
168 removeResults(aResultIndex);
171 //=================================================================================================
172 void FeaturesPlugin_CompositeSketch::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
173 const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
174 const std::shared_ptr<GeomAlgoAPI_MakeShape>& theMakeShape)
177 theResultBody->storeGenerated(theBaseShape, theMakeShape->shape());
179 //Insert lateral face : Face from Edge
180 const std::string aLatName = "LateralFace";
181 const int aLatTag = 1;
182 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aDataMap = theMakeShape->mapOfSubShapes();
183 theResultBody->loadAndOrientGeneratedShapes(theMakeShape.get(), theBaseShape, GeomAPI_Shape::EDGE, aLatTag, aLatName, *aDataMap.get());
185 std::shared_ptr<GeomAlgoAPI_MakeSweep> aSweepAlgo = std::dynamic_pointer_cast<GeomAlgoAPI_MakeSweep>(theMakeShape);
186 if(aSweepAlgo.get()) {
188 int aToFaceIndex = 1;
189 const std::string aToName = "ToFace";
191 const ListOfShape& aToFaces = aSweepAlgo->toShapes();
192 for(ListOfShape::const_iterator anIt = aToFaces.cbegin(); anIt != aToFaces.cend(); anIt++) {
193 std::shared_ptr<GeomAPI_Shape> aToFace = *anIt;
194 if(aDataMap->isBound(aToFace)) {
195 aToFace = aDataMap->find(aToFace);
197 std::ostringstream aStr;
198 aStr << aToName << "_" << aToFaceIndex++;
199 theResultBody->generated(aToFace, aStr.str(), aToTag++);
203 int aFromFaceIndex = 1;
204 const std::string aFromName = "FromFace";
205 int aFromTag = aToTag > 10000 ? aToTag : 10000;
206 const ListOfShape& aFromFaces = aSweepAlgo->fromShapes();
207 for(ListOfShape::const_iterator anIt = aFromFaces.cbegin(); anIt != aFromFaces.cend(); anIt++) {
208 std::shared_ptr<GeomAPI_Shape> aFromFace = *anIt;
209 if(aDataMap->isBound(aFromFace)) {
210 aFromFace = aDataMap->find(aFromFace);
212 std::ostringstream aStr;
213 aStr << aFromName << "_" << aFromFaceIndex++;
214 theResultBody->generated(aFromFace, aStr.str(), aFromTag++);