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_Session.h>
13 #include <ModelAPI_Validator.h>
14 #include <ModelAPI_ResultConstruction.h>
15 #include <ModelAPI_ResultBody.h>
17 #include <GeomAPI_Edge.h>
18 #include <GeomAPI_Lin.h>
22 #define _LATERAL_TAG 1
26 //=================================================================================================
27 FeaturesPlugin_Revolution::FeaturesPlugin_Revolution()
31 //=================================================================================================
32 void FeaturesPlugin_Revolution::initAttributes()
34 AttributeSelectionListPtr aSelection =
35 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
36 LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
37 // revolution works with faces always
38 aSelection->setSelectionType("FACE");
40 data()->addAttribute(AXIS_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
42 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
44 data()->addAttribute(TO_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
45 data()->addAttribute(FROM_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
47 data()->addAttribute(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
48 data()->addAttribute(TO_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
50 data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
51 data()->addAttribute(FROM_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
53 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
54 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
57 //=================================================================================================
58 void FeaturesPlugin_Revolution::execute()
60 AttributeSelectionListPtr aFaceRefs = selectionList(LIST_ID());
63 std::shared_ptr<GeomAPI_Ax1> anAxis;
64 std::shared_ptr<GeomAPI_Edge> anEdge;
65 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(AXIS_OBJECT_ID());
66 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
67 anEdge = std::make_shared<GeomAPI_Edge>(anObjRef->value());
68 } else if(anObjRef->context() && anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
69 anEdge = std::make_shared<GeomAPI_Edge>(anObjRef->context()->shape());
72 anAxis = std::make_shared<GeomAPI_Ax1>(anEdge->line()->location(), anEdge->line()->direction());
76 double aToAngle = real(TO_ANGLE_ID())->value();
77 double aFromAngle = real(FROM_ANGLE_ID())->value();
79 if(string(CREATION_METHOD())->value() == "ByAngles") {
80 aToAngle = real(TO_ANGLE_ID())->value();
81 aFromAngle = real(FROM_ANGLE_ID())->value();
83 aToAngle = real(TO_OFFSET_ID())->value();
84 aFromAngle = real(FROM_OFFSET_ID())->value();
87 // Getting bounding planes.
88 std::shared_ptr<GeomAPI_Shape> aToShape;
89 std::shared_ptr<GeomAPI_Shape> aFromShape;
91 if(string(CREATION_METHOD())->value() == "ByPlanesAndOffsets") {
92 anObjRef = selection(TO_OBJECT_ID());
93 if(anObjRef.get() != NULL) {
94 aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
95 if(aToShape.get() == NULL && anObjRef->context().get() != NULL) {
96 aToShape = anObjRef->context()->shape();
99 anObjRef = selection(FROM_OBJECT_ID());
100 if(anObjRef.get() != NULL) {
101 aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
102 if(aFromShape.get() == NULL && anObjRef->context().get() != NULL) {
103 aFromShape = anObjRef->context()->shape();
108 // for each selected face generate a result
109 int anIndex = 0, aResultIndex = 0;
110 for(; anIndex < aFaceRefs->size(); anIndex++) {
111 std::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = aFaceRefs->value(anIndex);
112 ResultPtr aContextRes = aFaceRef->context();
113 std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
114 if (!aContext.get()) {
115 static const std::string aContextError = "The selection context is bad";
116 setError(aContextError);
120 std::shared_ptr<GeomAPI_Shape> aValueFace = aFaceRef->value();
121 int aFacesNum = -1; // this mean that "aFace" is used
122 ResultConstructionPtr aConstruction =
123 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes);
124 if (!aValueFace.get()) { // this may be the whole sketch result selected, check and get faces
125 if (aConstruction.get()) {
126 aFacesNum = aConstruction->facesNum();
128 static const std::string aFaceError = "Can not find basis for extrusion";
129 setError(aFaceError);
134 for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
135 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
136 std::shared_ptr<GeomAPI_Shape> aBaseShape;
137 if (aFacesNum == -1) {
138 aBaseShape = aValueFace;
140 aBaseShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
143 GeomAlgoAPI_Revolution aFeature(aBaseShape, anAxis, aToShape, aToAngle, aFromShape, aFromAngle);
144 if(!aFeature.isDone()) {
145 static const std::string aFeatureError = "Revolution algorithm failed";
146 setError(aFeatureError);
150 // Check if shape is valid
151 if(aFeature.shape()->isNull()) {
152 static const std::string aShapeError = "Resulting shape is Null";
153 setError(aShapeError);
156 if(!aFeature.isValid()) {
157 std::string aFeatureError = "Warning: resulting shape is not valid";
158 setError(aFeatureError);
162 LoadNamingDS(aFeature, aResultBody, aBaseShape, aContext);
164 setResult(aResultBody, aResultIndex);
171 // remove the rest results if there were produced in the previous pass
172 removeResults(aResultIndex);
175 //=================================================================================================
176 void FeaturesPlugin_Revolution::LoadNamingDS(GeomAlgoAPI_Revolution& theFeature,
177 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
178 std::shared_ptr<GeomAPI_Shape> theBasis,
179 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);