Salome HOME
3bd64910c877776803eceff05277c89e9ef5f975
[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 <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>
16
17 #include <GeomAPI_Edge.h>
18 #include <GeomAPI_Lin.h>
19
20 #define FACE 4
21 #define EDGE 6
22 #define _LATERAL_TAG 1
23 #define _FROM_TAG 2
24 #define _TO_TAG 3
25
26 //=================================================================================================
27 FeaturesPlugin_Revolution::FeaturesPlugin_Revolution()
28 {
29 }
30
31 //=================================================================================================
32 void FeaturesPlugin_Revolution::initAttributes()
33 {
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");
39
40   data()->addAttribute(AXIS_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
41
42   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
43
44   data()->addAttribute(TO_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
45   data()->addAttribute(FROM_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
46
47   data()->addAttribute(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
48   data()->addAttribute(TO_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
49
50   data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
51   data()->addAttribute(FROM_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
52
53   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
54   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
55 }
56
57 //=================================================================================================
58 void FeaturesPlugin_Revolution::execute()
59 {
60   AttributeSelectionListPtr aFaceRefs = selectionList(LIST_ID());
61
62   //Getting axis.
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());
70   }
71   if(anEdge) {
72     anAxis = std::make_shared<GeomAPI_Ax1>(anEdge->line()->location(), anEdge->line()->direction());
73   }
74
75   // Getting angles.
76   double aToAngle = real(TO_ANGLE_ID())->value();
77   double aFromAngle = real(FROM_ANGLE_ID())->value();
78
79   if(string(CREATION_METHOD())->value() == "ByAngles") {
80     aToAngle = real(TO_ANGLE_ID())->value();
81     aFromAngle =  real(FROM_ANGLE_ID())->value();
82   } else {
83     aToAngle = real(TO_OFFSET_ID())->value();
84     aFromAngle =  real(FROM_OFFSET_ID())->value();
85   }
86
87   // Getting bounding planes.
88   std::shared_ptr<GeomAPI_Shape> aToShape;
89   std::shared_ptr<GeomAPI_Shape> aFromShape;
90
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();
97       }
98     }
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();
104       }
105     }
106   }
107
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);
117       break;
118     }
119
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();
127       } else {
128         static const std::string aFaceError = "Can not find basis for extrusion";
129         setError(aFaceError);
130         break;
131       }
132     }
133
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;
139       } else {
140         aBaseShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
141       }
142
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);
147         break;
148       }
149
150       // Check if shape is valid
151       if(aFeature.shape()->isNull()) {
152         static const std::string aShapeError = "Resulting shape is Null";
153         setError(aShapeError);
154         break;
155       }
156       if(!aFeature.isValid()) {
157         std::string aFeatureError = "Warning: resulting shape is not valid";
158         setError(aFeatureError);
159         break;
160       }
161       //LoadNamingDS
162       LoadNamingDS(aFeature, aResultBody, aBaseShape, aContext);
163
164       setResult(aResultBody, aResultIndex);
165       aResultIndex++;
166
167       if (aFacesNum == -1)
168         break;
169     }
170   }
171   // remove the rest results if there were produced in the previous pass
172   removeResults(aResultIndex);
173 }
174
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)
180 {
181   //load result
182   if(theBasis->isEqual(theContext))
183     theResultBody->store(theFeature.shape());
184   else
185     theResultBody->storeGenerated(theContext, theFeature.shape());
186
187   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theFeature.mapOfShapes();
188
189   std::string aGeneratedName = "LateralFace";
190   theResultBody->loadAndOrientGeneratedShapes(theFeature.makeShape().get(), theBasis, EDGE,_LATERAL_TAG, aGeneratedName, *aSubShapes);
191
192   //Insert from face
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);
198     }
199     theResultBody->generated(aBottomFace, aBotName, _FROM_TAG);
200   }
201
202   //Insert to face
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);
208     }
209     theResultBody->generated(aTopFace, aTopName, _TO_TAG);
210   }
211
212 }