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_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
45 data()->addAttribute(TO_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
47 data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
48 data()->addAttribute(FROM_ANGLE_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()
57 AttributeSelectionListPtr aFaceRefs = selectionList(LIST_ID());
60 std::shared_ptr<GeomAPI_Ax1> anAxis;
61 std::shared_ptr<GeomAPI_Edge> anEdge;
62 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(AXIS_OBJECT_ID());
63 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
64 anEdge = std::make_shared<GeomAPI_Edge>(anObjRef->value());
65 } else if(anObjRef->context() && anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
66 anEdge = std::make_shared<GeomAPI_Edge>(anObjRef->context()->shape());
69 anAxis = std::make_shared<GeomAPI_Ax1>(anEdge->line()->location(), anEdge->line()->direction());
73 double aFromAngle = real(FROM_ANGLE_ID())->value();
74 double aToAngle = real(TO_ANGLE_ID())->value();
76 // Getting bounding planes.
77 std::shared_ptr<GeomAPI_Shape> aFromShape;
78 std::shared_ptr<GeomAPI_Shape> aToShape;
80 if(string(CREATION_METHOD())->value() == "ByPlanesAndOffsets") {
81 anObjRef = selection(FROM_OBJECT_ID());
82 if(anObjRef.get() != NULL) {
83 aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
84 if(aFromShape.get() == NULL && anObjRef->context().get() != NULL) {
85 aFromShape = anObjRef->context()->shape();
88 anObjRef = selection(TO_OBJECT_ID());
89 if(anObjRef.get() != NULL) {
90 aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
91 if(aToShape.get() == NULL && anObjRef->context().get() != NULL) {
92 aToShape = anObjRef->context()->shape();
97 // for each selected face generate a result
98 int anIndex = 0, aResultIndex = 0;
99 for(; anIndex < aFaceRefs->size(); anIndex++) {
100 std::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = aFaceRefs->value(anIndex);
101 ResultPtr aContextRes = aFaceRef->context();
102 std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
103 if (!aContext.get()) {
104 static const std::string aContextError = "The selection context is bad";
105 setError(aContextError);
109 std::shared_ptr<GeomAPI_Shape> aValueFace = aFaceRef->value();
110 int aFacesNum = -1; // this mean that "aFace" is used
111 ResultConstructionPtr aConstruction =
112 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes);
113 if (!aValueFace.get()) { // this may be the whole sketch result selected, check and get faces
114 if (aConstruction.get()) {
115 aFacesNum = aConstruction->facesNum();
117 static const std::string aFaceError = "Can not find basis for extrusion";
118 setError(aFaceError);
123 for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
124 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
125 std::shared_ptr<GeomAPI_Shape> aBaseShape;
126 if (aFacesNum == -1) {
127 aBaseShape = aValueFace;
129 aBaseShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
132 GeomAlgoAPI_Revolution aFeature(aBaseShape, anAxis, aToShape, aToAngle, aFromShape, aFromAngle);
133 if(!aFeature.isDone()) {
134 static const std::string aFeatureError = "Revolution algorithm failed";
135 setError(aFeatureError);
139 // Check if shape is valid
140 if(aFeature.shape()->isNull()) {
141 static const std::string aShapeError = "Resulting shape is Null";
142 setError(aShapeError);
145 if(!aFeature.isValid()) {
146 std::string aFeatureError = "Warning: resulting shape is not valid";
147 setError(aFeatureError);
151 LoadNamingDS(aFeature, aResultBody, aBaseShape, aContext);
153 setResult(aResultBody, aResultIndex);
160 // remove the rest results if there were produced in the previous pass
161 removeResults(aResultIndex);
164 //=================================================================================================
165 void FeaturesPlugin_Revolution::LoadNamingDS(GeomAlgoAPI_Revolution& theFeature,
166 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
167 std::shared_ptr<GeomAPI_Shape> theBasis,
168 std::shared_ptr<GeomAPI_Shape> theContext)
171 if(theBasis->isEqual(theContext))
172 theResultBody->store(theFeature.shape());
174 theResultBody->storeGenerated(theContext, theFeature.shape());
176 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theFeature.mapOfShapes();
178 std::string aGeneratedName = "LateralFace";
179 theResultBody->loadAndOrientGeneratedShapes(theFeature.makeShape().get(), theBasis, EDGE,_LATERAL_TAG, aGeneratedName, *aSubShapes);
182 std::string aBotName = "FromFace";
183 std::shared_ptr<GeomAPI_Shape> aBottomFace = theFeature.firstShape();
184 if(!aBottomFace->isNull()) {
185 if(aSubShapes->isBound(aBottomFace)) {
186 aBottomFace = aSubShapes->find(aBottomFace);
188 theResultBody->generated(aBottomFace, aBotName, _FROM_TAG);
192 std::string aTopName = "ToFace";
193 std::shared_ptr<GeomAPI_Shape> aTopFace = theFeature.lastShape();
194 if (!aTopFace->isNull()) {
195 if (aSubShapes->isBound(aTopFace)) {
196 aTopFace = aSubShapes->find(aTopFace);
198 theResultBody->generated(aTopFace, aTopName, _TO_TAG);