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 <GeomAlgoAPI_FaceBuilder.h>
10 #include <GeomAlgoAPI_Rotation.h>
11 #include <GeomAPI_Ax3.h>
12 #include <GeomAPI_Edge.h>
13 #include <GeomAPI_Lin.h>
14 #include <GeomAPI_Pln.h>
15 #include <GeomAPI_XYZ.h>
17 #include <ModelAPI_AttributeDouble.h>
18 #include <ModelAPI_AttributeSelectionList.h>
19 #include <ModelAPI_Session.h>
20 #include <ModelAPI_Validator.h>
21 #include <ModelAPI_ResultConstruction.h>
22 #include <ModelAPI_ResultBody.h>
26 #define _LATERAL_TAG 1
30 //=================================================================================================
31 FeaturesPlugin_Revolution::FeaturesPlugin_Revolution()
35 //=================================================================================================
36 void FeaturesPlugin_Revolution::initAttributes()
38 AttributeSelectionListPtr aSelection =
39 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
40 FeaturesPlugin_Revolution::LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
41 // revolution works with faces always
42 aSelection->setSelectionType("FACE");
44 data()->addAttribute(FeaturesPlugin_Revolution::AXIS_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
46 data()->addAttribute(FeaturesPlugin_Revolution::FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
47 data()->addAttribute(FeaturesPlugin_Revolution::FROM_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
49 data()->addAttribute(FeaturesPlugin_Revolution::TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
50 data()->addAttribute(FeaturesPlugin_Revolution::TO_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
52 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FeaturesPlugin_Revolution::FROM_OBJECT_ID());
53 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FeaturesPlugin_Revolution::TO_OBJECT_ID());
56 //=================================================================================================
57 void FeaturesPlugin_Revolution::execute()
59 AttributeSelectionListPtr aFaceRefs = selectionList(FeaturesPlugin_Revolution::LIST_ID());
62 std::shared_ptr<GeomAPI_Ax1> anAxis;
63 std::shared_ptr<GeomAPI_Edge> anEdge;
64 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(FeaturesPlugin_Revolution::AXIS_OBJECT_ID());
65 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
66 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
69 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(), anEdge->line()->direction()));
72 // Getting bounding planes.
73 std::shared_ptr<GeomAPI_Shape> aFromShape(new GeomAPI_Shape());
74 std::shared_ptr<GeomAPI_Shape> aToShape(new GeomAPI_Shape());
76 anObjRef = selection(FeaturesPlugin_Revolution::FROM_OBJECT_ID());
78 aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
80 anObjRef = selection(FeaturesPlugin_Revolution::TO_OBJECT_ID());
82 aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
86 double aFromAngle = real(FeaturesPlugin_Revolution::FROM_ANGLE_ID())->value();
87 double aToAngle = real(FeaturesPlugin_Revolution::TO_ANGLE_ID())->value();
89 // for each selected face generate a result
90 int anIndex = 0, aResultIndex = 0;
91 for(; anIndex < aFaceRefs->size(); anIndex++) {
92 std::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = aFaceRefs->value(anIndex);
93 ResultPtr aContextRes = aFaceRef->context();
94 std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
95 if (!aContext.get()) {
96 static const std::string aContextError = "The selection context is bad";
97 setError(aContextError);
101 std::shared_ptr<GeomAPI_Shape> aValueFace = aFaceRef->value();
102 int aFacesNum = -1; // this mean that "aFace" is used
103 ResultConstructionPtr aConstruction =
104 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes);
105 if (!aValueFace.get()) { // this may be the whole sketch result selected, check and get faces
106 if (aConstruction.get()) {
107 aFacesNum = aConstruction->facesNum();
109 static const std::string aFaceError = "Can not find basis for extrusion";
110 setError(aFaceError);
115 for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
116 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
117 std::shared_ptr<GeomAPI_Shape> aBaseShape;
118 if (aFacesNum == -1) {
119 aBaseShape = aValueFace;
121 aBaseShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
124 GeomAlgoAPI_Revolution aFeature(aBaseShape, anAxis, aFromShape, aFromAngle, aToShape, aToAngle);
125 if(!aFeature.isDone()) {
126 static const std::string aFeatureError = "Revolution algorithm failed";
127 setError(aFeatureError);
131 // Check if shape is valid
132 if(aFeature.shape()->isNull()) {
133 static const std::string aShapeError = "Resulting shape is Null";
134 setError(aShapeError);
137 if(!aFeature.isValid()) {
138 std::string aFeatureError = "Warning: resulting shape is not valid";
139 setError(aFeatureError);
143 LoadNamingDS(aFeature, aResultBody, aBaseShape, aContext);
145 setResult(aResultBody, aResultIndex);
152 // remove the rest results if there were produced in the previous pass
153 removeResults(aResultIndex);
156 //=================================================================================================
157 void FeaturesPlugin_Revolution::LoadNamingDS(GeomAlgoAPI_Revolution& theFeature,
158 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
159 std::shared_ptr<GeomAPI_Shape> theBasis,
160 std::shared_ptr<GeomAPI_Shape> theContext)
163 if(theBasis->isEqual(theContext))
164 theResultBody->store(theFeature.shape());
166 theResultBody->storeGenerated(theContext, theFeature.shape());
168 GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
169 theFeature.mapOfShapes(*aSubShapes);
171 std::string aGeneratedName = "LateralFace";
172 theResultBody->loadAndOrientGeneratedShapes(theFeature.makeShape(), theBasis, EDGE,_LATERAL_TAG, aGeneratedName, *aSubShapes);
175 std::string aBotName = "FromFace";
176 std::shared_ptr<GeomAPI_Shape> aBottomFace = theFeature.firstShape();
177 if(!aBottomFace->isNull()) {
178 if(aSubShapes->isBound(aBottomFace)) {
179 aBottomFace = aSubShapes->find(aBottomFace);
181 theResultBody->generated(aBottomFace, aBotName, _FROM_TAG);
185 std::string aTopName = "ToFace";
186 std::shared_ptr<GeomAPI_Shape> aTopFace = theFeature.lastShape();
187 if (!aTopFace->isNull()) {
188 if (aSubShapes->isBound(aTopFace)) {
189 aTopFace = aSubShapes->find(aTopFace);
191 theResultBody->generated(aTopFace, aTopName, _TO_TAG);