Salome HOME
Revolution tests.
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Revolution.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        FeaturesPlugin_Revolution.cpp
4 // Created:     12 May 2015
5 // Author:      Dmitry Bobylev
6
7 #include <FeaturesPlugin_Revolution.h>
8
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>
16
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>
23
24 #define FACE 4
25 #define EDGE 6
26 #define _LATERAL_TAG 1
27 #define _FROM_TAG 2
28 #define _TO_TAG 3
29
30 //=================================================================================================
31 FeaturesPlugin_Revolution::FeaturesPlugin_Revolution()
32 {
33 }
34
35 //=================================================================================================
36 void FeaturesPlugin_Revolution::initAttributes()
37 {
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");
43
44   data()->addAttribute(FeaturesPlugin_Revolution::AXIS_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
45
46   data()->addAttribute(FeaturesPlugin_Revolution::FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
47   data()->addAttribute(FeaturesPlugin_Revolution::FROM_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
48
49   data()->addAttribute(FeaturesPlugin_Revolution::TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
50   data()->addAttribute(FeaturesPlugin_Revolution::TO_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
51
52   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FeaturesPlugin_Revolution::FROM_OBJECT_ID());
53   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FeaturesPlugin_Revolution::TO_OBJECT_ID());
54 }
55
56 //=================================================================================================
57 void FeaturesPlugin_Revolution::execute()
58 {
59   AttributeSelectionListPtr aFaceRefs = selectionList(FeaturesPlugin_Revolution::LIST_ID());
60
61   //Getting axe.
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()));
67   }
68   if(anEdge) {
69     anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(), anEdge->line()->direction()));
70   }
71
72   // Getting bounding planes.
73   std::shared_ptr<GeomAPI_Shape> aFromShape(new GeomAPI_Shape());
74   std::shared_ptr<GeomAPI_Shape> aToShape(new GeomAPI_Shape());
75
76   anObjRef = selection(FeaturesPlugin_Revolution::FROM_OBJECT_ID());
77   if(anObjRef) {
78     aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
79   }
80   anObjRef = selection(FeaturesPlugin_Revolution::TO_OBJECT_ID());
81   if(anObjRef) {
82     aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
83   }
84
85   // Getting angles.
86   double aFromAngle = real(FeaturesPlugin_Revolution::FROM_ANGLE_ID())->value();
87   double aToAngle = real(FeaturesPlugin_Revolution::TO_ANGLE_ID())->value();
88
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);
98       break;
99     }
100
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();
108       } else {
109         static const std::string aFaceError = "Can not find basis for extrusion";
110         setError(aFaceError);
111         break;
112       }
113     }
114
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;
120       } else {
121         aBaseShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
122       }
123
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);
128         break;
129       }
130
131       // Check if shape is valid
132       if(aFeature.shape()->isNull()) {
133         static const std::string aShapeError = "Resulting shape is Null";
134         setError(aShapeError);
135         break;
136       }
137       if(!aFeature.isValid()) {
138         std::string aFeatureError = "Warning: resulting shape is not valid";
139         setError(aFeatureError);
140         break;
141       }
142       //LoadNamingDS
143       LoadNamingDS(aFeature, aResultBody, aBaseShape, aContext);
144
145       setResult(aResultBody, aResultIndex);
146       aResultIndex++;
147
148       if (aFacesNum == -1)
149         break;
150     }
151   }
152   // remove the rest results if there were produced in the previous pass
153   removeResults(aResultIndex);
154 }
155
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)
161 {
162   //load result
163   if(theBasis->isEqual(theContext))
164     theResultBody->store(theFeature.shape());
165   else
166     theResultBody->storeGenerated(theContext, theFeature.shape());
167
168   GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
169   theFeature.mapOfShapes(*aSubShapes);
170
171   std::string aGeneratedName = "LateralFace";
172   theResultBody->loadAndOrientGeneratedShapes(theFeature.makeShape(), theBasis, EDGE,_LATERAL_TAG, aGeneratedName, *aSubShapes);
173
174   //Insert from face
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);
180     }
181     theResultBody->generated(aBottomFace, aBotName, _FROM_TAG);
182   }
183
184   //Insert to face
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);
190     }
191     theResultBody->generated(aTopFace, aTopName, _TO_TAG);
192   }
193
194 }