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 <GeomAPI_Edge.h>
19 #include <GeomAPI_Lin.h>
23 #define _LATERAL_TAG 1
27 //=================================================================================================
28 FeaturesPlugin_Revolution::FeaturesPlugin_Revolution()
32 //=================================================================================================
33 void FeaturesPlugin_Revolution::initAttributes()
35 AttributeSelectionListPtr aSelection =
36 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
37 LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
38 // revolution works with faces always
39 aSelection->setSelectionType("FACE");
41 data()->addAttribute(AXIS_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
43 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
45 data()->addAttribute(TO_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
46 data()->addAttribute(FROM_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
48 data()->addAttribute(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
49 data()->addAttribute(TO_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
51 data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
52 data()->addAttribute(FROM_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
54 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
55 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
58 //=================================================================================================
59 void FeaturesPlugin_Revolution::execute()
61 AttributeSelectionListPtr aFaceRefs = selectionList(LIST_ID());
64 std::shared_ptr<GeomAPI_Ax1> anAxis;
65 std::shared_ptr<GeomAPI_Edge> anEdge;
66 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(AXIS_OBJECT_ID());
67 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
68 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
69 } else if(anObjRef->context() && anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
70 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
73 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(), anEdge->line()->direction()));
77 double aToAngle = real(TO_ANGLE_ID())->value();
78 double aFromAngle = real(FROM_ANGLE_ID())->value();
80 if(string(CREATION_METHOD())->value() == "ByAngles") {
81 aToAngle = real(TO_ANGLE_ID())->value();
82 aFromAngle = real(FROM_ANGLE_ID())->value();
84 aToAngle = real(TO_OFFSET_ID())->value();
85 aFromAngle = real(FROM_OFFSET_ID())->value();
88 // Getting bounding planes.
89 std::shared_ptr<GeomAPI_Shape> aToShape;
90 std::shared_ptr<GeomAPI_Shape> aFromShape;
92 if(string(CREATION_METHOD())->value() == "ByPlanesAndOffsets") {
93 anObjRef = selection(TO_OBJECT_ID());
94 if(anObjRef.get() != NULL) {
95 aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
96 if(aToShape.get() == NULL && anObjRef->context().get() != NULL) {
97 aToShape = anObjRef->context()->shape();
100 anObjRef = selection(FROM_OBJECT_ID());
101 if(anObjRef.get() != NULL) {
102 aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
103 if(aFromShape.get() == NULL && anObjRef->context().get() != NULL) {
104 aFromShape = anObjRef->context()->shape();
109 // for each selected face generate a result
110 int anIndex = 0, aResultIndex = 0;
111 for(; anIndex < aFaceRefs->size(); anIndex++) {
112 std::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = aFaceRefs->value(anIndex);
113 ResultPtr aContextRes = aFaceRef->context();
114 std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
115 if (!aContext.get()) {
116 static const std::string aContextError = "The selection context is bad";
117 setError(aContextError);
121 std::shared_ptr<GeomAPI_Shape> aValueFace = aFaceRef->value();
122 int aFacesNum = -1; // this mean that "aFace" is used
123 ResultConstructionPtr aConstruction =
124 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes);
125 if (!aValueFace.get()) { // this may be the whole sketch result selected, check and get faces
126 if (aConstruction.get()) {
127 aFacesNum = aConstruction->facesNum();
129 static const std::string aFaceError = "Can not find basis for extrusion";
130 setError(aFaceError);
135 for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
136 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
137 std::shared_ptr<GeomAPI_Shape> aBaseShape;
138 if (aFacesNum == -1) {
139 aBaseShape = aValueFace;
141 aBaseShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
144 GeomAlgoAPI_Revolution aFeature(aBaseShape, anAxis, aToShape, aToAngle, aFromShape, aFromAngle);
145 if(!aFeature.isDone()) {
146 static const std::string aFeatureError = "Revolution algorithm failed";
147 setError(aFeatureError);
151 // Check if shape is valid
152 if(aFeature.shape()->isNull()) {
153 static const std::string aShapeError = "Resulting shape is Null";
154 setError(aShapeError);
157 if(!aFeature.isValid()) {
158 std::string aFeatureError = "Warning: resulting shape is not valid";
159 setError(aFeatureError);
163 LoadNamingDS(aFeature, aResultBody, aBaseShape, aContext);
165 setResult(aResultBody, aResultIndex);
172 // remove the rest results if there were produced in the previous pass
173 removeResults(aResultIndex);
176 //=================================================================================================
177 void FeaturesPlugin_Revolution::LoadNamingDS(GeomAlgoAPI_Revolution& theFeature,
178 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
179 std::shared_ptr<GeomAPI_Shape> theBasis,
180 std::shared_ptr<GeomAPI_Shape> theContext)
182 if(theBasis->isEqual(theContext))
183 theResultBody->store(theFeature.shape());
185 theResultBody->storeGenerated(theContext, theFeature.shape());
187 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theFeature.mapOfShapes();
189 std::string aGeneratedName = "LateralFace";
190 theResultBody->loadAndOrientGeneratedShapes(theFeature.makeShape().get(), theBasis, EDGE,_LATERAL_TAG, aGeneratedName, *aSubShapes);
193 std::string aBotName = "FromFace";
194 std::shared_ptr<GeomAPI_Shape> aBottomFace = theFeature.firstShape();
195 if(!aBottomFace->isNull()) {
196 if(aSubShapes->isBound(aBottomFace)) {
197 aBottomFace = aSubShapes->find(aBottomFace);
199 theResultBody->generated(aBottomFace, aBotName, _FROM_TAG);
203 std::string aTopName = "ToFace";
204 std::shared_ptr<GeomAPI_Shape> aTopFace = theFeature.lastShape();
205 if (!aTopFace->isNull()) {
206 if (aSubShapes->isBound(aTopFace)) {
207 aTopFace = aSubShapes->find(aTopFace);
209 theResultBody->generated(aTopFace, aTopName, _TO_TAG);