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>
24 #define _LATERAL_TAG 1
29 //=================================================================================================
30 FeaturesPlugin_Revolution::FeaturesPlugin_Revolution()
34 //=================================================================================================
35 void FeaturesPlugin_Revolution::initAttributes()
37 AttributeSelectionListPtr aSelection =
38 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
39 FeaturesPlugin_Revolution::LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
40 // revolution works with faces always
41 aSelection->setSelectionType("FACE");
43 data()->addAttribute(FeaturesPlugin_Revolution::AXIS_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
45 data()->addAttribute(FeaturesPlugin_Revolution::FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
46 data()->addAttribute(FeaturesPlugin_Revolution::FROM_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
48 data()->addAttribute(FeaturesPlugin_Revolution::TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
49 data()->addAttribute(FeaturesPlugin_Revolution::TO_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
51 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FeaturesPlugin_Revolution::FROM_OBJECT_ID());
52 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FeaturesPlugin_Revolution::TO_OBJECT_ID());
55 //=================================================================================================
56 void FeaturesPlugin_Revolution::execute()
58 AttributeSelectionListPtr aFaceRefs = selectionList(FeaturesPlugin_Revolution::LIST_ID());
61 std::shared_ptr<GeomAPI_Ax1> anAxis;
62 std::shared_ptr<GeomAPI_Edge> anEdge;
63 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(FeaturesPlugin_Revolution::AXIS_OBJECT_ID());
64 if(anObjRef && anObjRef->value()->isEdge()) {
65 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
68 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(), anEdge->line()->direction()));
71 // Getting bounding planes.
72 std::shared_ptr<GeomAPI_Shape> aFromShape(new GeomAPI_Shape());
73 std::shared_ptr<GeomAPI_Shape> aToShape(new GeomAPI_Shape());
75 anObjRef = selection(FeaturesPlugin_Revolution::FROM_OBJECT_ID());
77 aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
79 anObjRef = selection(FeaturesPlugin_Revolution::TO_OBJECT_ID());
81 aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
85 double aFromAngle = real(FeaturesPlugin_Revolution::FROM_ANGLE_ID())->value();
86 double aToAngle = real(FeaturesPlugin_Revolution::TO_ANGLE_ID())->value();
88 // for each selected face generate a result
89 int anIndex = 0, aResultIndex = 0;
90 for(; anIndex < aFaceRefs->size(); anIndex++) {
91 std::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = aFaceRefs->value(anIndex);
92 ResultPtr aContextRes = aFaceRef->context();
93 std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
94 if (!aContext.get()) {
95 static const std::string aContextError = "The selection context is bad";
96 setError(aContextError);
100 std::shared_ptr<GeomAPI_Shape> aValueFace = aFaceRef->value();
101 int aFacesNum = -1; // this mean that "aFace" is used
102 ResultConstructionPtr aConstruction =
103 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes);
104 if (!aValueFace.get()) { // this may be the whole sketch result selected, check and get faces
105 if (aConstruction.get()) {
106 aFacesNum = aConstruction->facesNum();
108 static const std::string aFaceError = "Can not find basis for extrusion";
109 setError(aFaceError);
114 for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
115 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
116 std::shared_ptr<GeomAPI_Shape> aBaseShape;
117 if (aFacesNum == -1) {
118 aBaseShape = aValueFace;
120 aBaseShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
123 GeomAlgoAPI_Revolution aFeature(aBaseShape, anAxis, aFromShape, aFromAngle, aToShape, aToAngle);
124 if(!aFeature.isDone()) {
125 static const std::string aFeatureError = "Revolution algorithm failed";
126 setError(aFeatureError);
130 // Check if shape is valid
131 if(aFeature.shape()->isNull()) {
132 static const std::string aShapeError = "Resulting shape is Null";
133 setError(aShapeError);
136 if(!aFeature.isValid()) {
137 std::string aFeatureError = "Warning: resulting shape is not valid";
138 setError(aFeatureError);
142 LoadNamingDS(aFeature, aResultBody, aBaseShape, aContext);
144 setResult(aResultBody, aResultIndex);
151 // remove the rest results if there were produced in the previous pass
152 removeResults(aResultIndex);
155 //=================================================================================================
156 void FeaturesPlugin_Revolution::LoadNamingDS(GeomAlgoAPI_Revolution& theFeature,
157 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
158 std::shared_ptr<GeomAPI_Shape> theBasis,
159 std::shared_ptr<GeomAPI_Shape> theContext)
162 theResultBody->store(theFeature.shape());
166 if(theBasis->isEqual(theContext))
167 theResultBody->store(theFeature.shape());
169 theResultBody->storeGenerated(theContext, theFeature.shape());
171 GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
172 theFeature.mapOfShapes(*aSubShapes);
174 //Insert lateral face : Face from Edge
175 std::string aLatName = "LateralFace";
176 theResultBody->loadAndOrientGeneratedShapes(theFeature.makeShape(), theBasis, EDGE,_LATERAL_TAG, aLatName, *aSubShapes);
178 ////Insert first face
179 //std::string aBotName = "FirstFace";
180 //std::shared_ptr<GeomAPI_Shape> aBottomFace = theFeature.firstShape();
181 //if(!aBottomFace->isNull()) {
182 // if(aSubShapes->isBound(aBottomFace)) {
183 // aBottomFace = aSubShapes->find(aBottomFace);
185 // theResultBody->generated(aBottomFace, aBotName, _FIRST_TAG);
189 //std::string aTopName = "LastFace";
190 //std::shared_ptr<GeomAPI_Shape> aTopFace = theFeature.lastShape();
191 //if (!aTopFace->isNull()) {
192 // if (aSubShapes->isBound(aTopFace)) {
193 // aTopFace = aSubShapes->find(aTopFace);
195 // theResultBody->generated(aTopFace, aTopName, _LAST_TAG);