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>
23 //=================================================================================================
24 FeaturesPlugin_Revolution::FeaturesPlugin_Revolution()
28 //=================================================================================================
29 void FeaturesPlugin_Revolution::initAttributes()
31 AttributeSelectionListPtr aSelection =
32 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
33 LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
34 // revolution works with faces always
35 aSelection->setSelectionType("FACE");
37 data()->addAttribute(AXIS_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
39 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
41 data()->addAttribute(TO_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
42 data()->addAttribute(FROM_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
44 data()->addAttribute(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
45 data()->addAttribute(TO_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
47 data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
48 data()->addAttribute(FROM_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
50 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
51 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
54 //=================================================================================================
55 void FeaturesPlugin_Revolution::execute()
58 ListOfShape aFacesList;
59 AttributeSelectionListPtr aFacesSelectionList = selectionList(LIST_ID());
60 for(int anIndex = 0; anIndex < aFacesSelectionList->size(); anIndex++) {
61 AttributeSelectionPtr aFaceSel = aFacesSelectionList->value(anIndex);
62 std::shared_ptr<GeomAPI_Shape> aFaceShape = aFaceSel->value();
63 if(aFaceShape.get() && !aFaceShape->isNull()) { // Getting face.
64 aFacesList.push_back(aFaceShape);
65 } else { // This may be the whole sketch result selected, check and get faces.
66 ResultPtr aContext = aFaceSel->context();
67 std::shared_ptr<GeomAPI_Shape> aContextShape = aContext->shape();
68 if(!aContextShape.get()) {
69 static const std::string aContextError = "The selection context is bad";
70 setError(aContextError);
73 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
74 if(!aConstruction.get()) {
75 static const std::string aFaceError = "Can not find basis for revolution";
79 int aFacesNum = aConstruction->facesNum();
80 for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
81 aFaceShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
82 aFacesList.push_back(aFaceShape);
88 std::shared_ptr<GeomAPI_Ax1> anAxis;
89 std::shared_ptr<GeomAPI_Edge> anEdge;
90 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(AXIS_OBJECT_ID());
91 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
92 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
93 } else if(anObjRef->context() && anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
94 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
97 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(), anEdge->line()->direction()));
101 double aToAngle = real(TO_ANGLE_ID())->value();
102 double aFromAngle = real(FROM_ANGLE_ID())->value();
104 if(string(CREATION_METHOD())->value() == "ByAngles") {
105 aToAngle = real(TO_ANGLE_ID())->value();
106 aFromAngle = real(FROM_ANGLE_ID())->value();
108 aToAngle = real(TO_OFFSET_ID())->value();
109 aFromAngle = real(FROM_OFFSET_ID())->value();
112 // Getting bounding planes.
113 std::shared_ptr<GeomAPI_Shape> aToShape;
114 std::shared_ptr<GeomAPI_Shape> aFromShape;
116 if(string(CREATION_METHOD())->value() == "ByPlanesAndOffsets") {
117 anObjRef = selection(TO_OBJECT_ID());
118 if(anObjRef.get() != NULL) {
119 aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
120 if(aToShape.get() == NULL && anObjRef->context().get() != NULL) {
121 aToShape = anObjRef->context()->shape();
124 anObjRef = selection(FROM_OBJECT_ID());
125 if(anObjRef.get() != NULL) {
126 aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
127 if(aFromShape.get() == NULL && anObjRef->context().get() != NULL) {
128 aFromShape = anObjRef->context()->shape();
133 // Searching faces with common edges.
135 ListOfShape aFreeFaces;
136 std::shared_ptr<GeomAPI_Shape> aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aFacesList);
137 GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL, aShells, aFreeFaces);
138 aShells.insert(aShells.end(), aFreeFaces.begin(), aFreeFaces.end());
140 // Generating result for each shell and face.
141 int aResultIndex = 0;
142 for(ListOfShape::const_iterator anIter = aShells.cbegin(); anIter != aShells.cend(); anIter++) {
143 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anIter;
145 GeomAlgoAPI_Revolution aRevolAlgo(aBaseShape, anAxis, aToShape, aToAngle, aFromShape, aFromAngle);
146 if(!aRevolAlgo.isDone()) {
147 static const std::string aPrismAlgoError = "Revolution algorithm failed";
148 setError(aPrismAlgoError);
153 // Check if shape is valid
154 if(!aRevolAlgo.shape().get() || aRevolAlgo.shape()->isNull()) {
155 static const std::string aShapeError = "Resulting shape is Null";
156 setError(aShapeError);
160 if(!aRevolAlgo.isValid()) {
161 std::string aPrismAlgoError = "Warning: resulting shape is not valid";
162 setError(aPrismAlgoError);
167 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
168 loadNamingDS(aRevolAlgo, aResultBody, aBaseShape);
169 setResult(aResultBody, aResultIndex);
173 removeResults(aResultIndex);
176 //=================================================================================================
177 void FeaturesPlugin_Revolution::loadNamingDS(GeomAlgoAPI_Revolution& theRevolAlgo,
178 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
179 std::shared_ptr<GeomAPI_Shape> theBasis)
182 theResultBody->storeGenerated(theBasis, theRevolAlgo.shape());
184 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theRevolAlgo.mapOfSubShapes();
186 //Insert lateral face : Face from Edge
187 const std::string aLatName = "LateralFace";
188 const int aLatTag = 1;
189 theResultBody->loadAndOrientGeneratedShapes(&theRevolAlgo, theBasis, GeomAPI_Shape::EDGE, aLatTag, aLatName, *aSubShapes);
192 const std::string aToName = "ToFace";
194 const ListOfShape& aToFaces = theRevolAlgo.toFaces();
195 for(ListOfShape::const_iterator anIt = aToFaces.cbegin(); anIt != aToFaces.cend(); anIt++) {
196 std::shared_ptr<GeomAPI_Shape> aToFace = *anIt;
197 if(aSubShapes->isBound(aToFace)) {
198 aToFace = aSubShapes->find(aToFace);
200 theResultBody->generated(aToFace, aToName, aToTag++);
204 const std::string aFromName = "FromFace";
205 int aFromTag = aToTag > 10000 ? aToTag : 10000;
206 const ListOfShape& aFromFaces = theRevolAlgo.fromFaces();
207 for(ListOfShape::const_iterator anIt = aFromFaces.cbegin(); anIt != aFromFaces.cend(); anIt++) {
208 std::shared_ptr<GeomAPI_Shape> aFromFace = *anIt;
209 if(aSubShapes->isBound(aFromFace)) {
210 aFromFace = aSubShapes->find(aFromFace);
212 theResultBody->generated(aFromFace, aFromName, aFromTag++);