1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: FeaturesPlugin_Revolution.cpp
4 // Created: 12 May 2015
5 // Author: Dmitry Bobylev
7 #include "FeaturesPlugin_Revolution.h"
9 #include <ModelAPI_AttributeDouble.h>
10 #include <ModelAPI_AttributeSelectionList.h>
11 #include <ModelAPI_AttributeString.h>
12 #include <ModelAPI_BodyBuilder.h>
13 #include <ModelAPI_Session.h>
14 #include <ModelAPI_Validator.h>
15 #include <ModelAPI_ResultConstruction.h>
16 #include <ModelAPI_ResultBody.h>
18 #include <GeomAlgoAPI_CompoundBuilder.h>
19 #include <GeomAlgoAPI_ShapeTools.h>
20 #include <GeomAPI_Edge.h>
21 #include <GeomAPI_Lin.h>
25 //=================================================================================================
26 FeaturesPlugin_Revolution::FeaturesPlugin_Revolution()
30 //=================================================================================================
31 void FeaturesPlugin_Revolution::initAttributes()
33 AttributeSelectionListPtr aSelection =
34 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
35 LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
36 // revolution works with faces always
37 aSelection->setSelectionType("FACE");
39 data()->addAttribute(AXIS_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
41 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
43 data()->addAttribute(TO_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
44 data()->addAttribute(FROM_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
46 data()->addAttribute(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
47 data()->addAttribute(TO_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
49 data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
50 data()->addAttribute(FROM_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
52 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
53 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
56 //=================================================================================================
57 void FeaturesPlugin_Revolution::execute()
60 ListOfShape aFacesList;
61 AttributeSelectionListPtr aFacesSelectionList = selectionList(LIST_ID());
62 for(int anIndex = 0; anIndex < aFacesSelectionList->size(); anIndex++) {
63 AttributeSelectionPtr aFaceSel = aFacesSelectionList->value(anIndex);
64 std::shared_ptr<GeomAPI_Shape> aFaceShape = aFaceSel->value();
65 if(aFaceShape.get() && !aFaceShape->isNull()) { // Getting face.
66 aFacesList.push_back(aFaceShape);
67 } else { // This may be the whole sketch result selected, check and get faces.
68 ResultPtr aContext = aFaceSel->context();
69 std::shared_ptr<GeomAPI_Shape> aContextShape = aContext->shape();
70 if(!aContextShape.get()) {
71 static const std::string aContextError = "Error: The selection context is bad.";
72 setError(aContextError);
75 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
76 if(!aConstruction.get()) {
77 static const std::string aFaceError = "Error: Can not find basis for revolution.";
81 int aFacesNum = aConstruction->facesNum();
82 for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
83 aFaceShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
84 aFacesList.push_back(aFaceShape);
90 std::shared_ptr<GeomAPI_Ax1> anAxis;
91 std::shared_ptr<GeomAPI_Edge> anEdge;
92 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(AXIS_OBJECT_ID());
93 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
94 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
95 } else if(anObjRef->context() && anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
96 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
99 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(), anEdge->line()->direction()));
103 double aToAngle = real(TO_ANGLE_ID())->value();
104 double aFromAngle = real(FROM_ANGLE_ID())->value();
106 if(string(CREATION_METHOD())->value() == "ByAngles") {
107 aToAngle = real(TO_ANGLE_ID())->value();
108 aFromAngle = real(FROM_ANGLE_ID())->value();
110 aToAngle = real(TO_OFFSET_ID())->value();
111 aFromAngle = real(FROM_OFFSET_ID())->value();
114 // Getting bounding planes.
115 std::shared_ptr<GeomAPI_Shape> aToShape;
116 std::shared_ptr<GeomAPI_Shape> aFromShape;
118 if(string(CREATION_METHOD())->value() == "ByPlanesAndOffsets") {
119 anObjRef = selection(TO_OBJECT_ID());
120 if(anObjRef.get() != NULL) {
121 aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
122 if(aToShape.get() == NULL && anObjRef->context().get() != NULL) {
123 aToShape = anObjRef->context()->shape();
126 anObjRef = selection(FROM_OBJECT_ID());
127 if(anObjRef.get() != NULL) {
128 aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
129 if(aFromShape.get() == NULL && anObjRef->context().get() != NULL) {
130 aFromShape = anObjRef->context()->shape();
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.
143 int aResultIndex = 0;
144 for(ListOfShape::const_iterator anIter = aShells.cbegin(); anIter != aShells.cend(); anIter++) {
145 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anIter;
147 GeomAlgoAPI_Revolution aRevolAlgo(aBaseShape, anAxis, aToShape, aToAngle, aFromShape, aFromAngle);
148 if(!aRevolAlgo.isDone()) {
149 static const std::string aPrismAlgoError = "Error: Revolution algorithm failed.";
150 setError(aPrismAlgoError);
155 // Check if shape is valid
156 if(!aRevolAlgo.shape().get() || aRevolAlgo.shape()->isNull()) {
157 static const std::string aShapeError = "Error: Resulting shape is Null.";
158 setError(aShapeError);
162 if(!aRevolAlgo.isValid()) {
163 std::string aPrismAlgoError = "Error: Resulting shape is not valid.";
164 setError(aPrismAlgoError);
169 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
170 loadNamingDS(aRevolAlgo, aResultBody, aBaseShape);
171 setResult(aResultBody, aResultIndex);
175 removeResults(aResultIndex);
178 //=================================================================================================
179 void FeaturesPlugin_Revolution::loadNamingDS(GeomAlgoAPI_Revolution& theRevolAlgo,
180 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
181 std::shared_ptr<GeomAPI_Shape> theBasis)
184 theResultBody->storeGenerated(theBasis, theRevolAlgo.shape());
186 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theRevolAlgo.mapOfSubShapes();
188 //Insert lateral face : Face from Edge
189 const std::string aLatName = "LateralFace";
190 const int aLatTag = 1;
191 theResultBody->loadAndOrientGeneratedShapes(&theRevolAlgo, theBasis, GeomAPI_Shape::EDGE, aLatTag, aLatName, *aSubShapes);
194 int aToFaceIndex = 1;
195 const std::string aToName = "ToFace";
197 const ListOfShape& aToFaces = theRevolAlgo.toShapes();
198 for(ListOfShape::const_iterator anIt = aToFaces.cbegin(); anIt != aToFaces.cend(); anIt++) {
199 std::shared_ptr<GeomAPI_Shape> aToFace = *anIt;
200 if(aSubShapes->isBound(aToFace)) {
201 aToFace = aSubShapes->find(aToFace);
203 std::ostringstream aStr;
204 aStr << aToName << "_" << aToFaceIndex++;
205 theResultBody->generated(aToFace, aStr.str(), aToTag++);
209 int aFromFaceIndex = 1;
210 const std::string aFromName = "FromFace";
211 int aFromTag = aToTag > 10000 ? aToTag : 10000;
212 const ListOfShape& aFromFaces = theRevolAlgo.fromShapes();
213 for(ListOfShape::const_iterator anIt = aFromFaces.cbegin(); anIt != aFromFaces.cend(); anIt++) {
214 std::shared_ptr<GeomAPI_Shape> aFromFace = *anIt;
215 if(aSubShapes->isBound(aFromFace)) {
216 aFromFace = aSubShapes->find(aFromFace);
218 std::ostringstream aStr;
219 aStr << aFromName << "_" << aFromFaceIndex++;
220 theResultBody->generated(aFromFace, aStr.str(), aFromTag++);