]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Revolution.cpp
Salome HOME
4a7795a06717745094491cfd10e4cb0666b12082
[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_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
45   data()->addAttribute(TO_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
46
47   data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
48   data()->addAttribute(FROM_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
49
50   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
51   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
52 }
53
54 //=================================================================================================
55 void FeaturesPlugin_Revolution::execute()
56 {
57   AttributeSelectionListPtr aFaceRefs = selectionList(LIST_ID());
58
59   //Getting axis.
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());
67   }
68   if(anEdge) {
69     anAxis = std::make_shared<GeomAPI_Ax1>(anEdge->line()->location(), anEdge->line()->direction());
70   }
71
72   // Getting angles.
73   double aFromAngle = real(FROM_ANGLE_ID())->value();
74   double aToAngle = real(TO_ANGLE_ID())->value();
75
76   // Getting bounding planes.
77   std::shared_ptr<GeomAPI_Shape> aFromShape;
78   std::shared_ptr<GeomAPI_Shape> aToShape;
79
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();
86       }
87     }
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();
93       }
94     }
95   }
96
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);
106       break;
107     }
108
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();
116       } else {
117         static const std::string aFaceError = "Can not find basis for extrusion";
118         setError(aFaceError);
119         break;
120       }
121     }
122
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;
128       } else {
129         aBaseShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
130       }
131
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);
136         break;
137       }
138
139       // Check if shape is valid
140       if(aFeature.shape()->isNull()) {
141         static const std::string aShapeError = "Resulting shape is Null";
142         setError(aShapeError);
143         break;
144       }
145       if(!aFeature.isValid()) {
146         std::string aFeatureError = "Warning: resulting shape is not valid";
147         setError(aFeatureError);
148         break;
149       }
150       //LoadNamingDS
151       LoadNamingDS(aFeature, aResultBody, aBaseShape, aContext);
152
153       setResult(aResultBody, aResultIndex);
154       aResultIndex++;
155
156       if (aFacesNum == -1)
157         break;
158     }
159   }
160   // remove the rest results if there were produced in the previous pass
161   removeResults(aResultIndex);
162 }
163
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)
169 {
170   //load result
171   if(theBasis->isEqual(theContext))
172     theResultBody->store(theFeature.shape());
173   else
174     theResultBody->storeGenerated(theContext, theFeature.shape());
175
176   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theFeature.mapOfShapes();
177
178   std::string aGeneratedName = "LateralFace";
179   theResultBody->loadAndOrientGeneratedShapes(theFeature.makeShape().get(), theBasis, EDGE,_LATERAL_TAG, aGeneratedName, *aSubShapes);
180
181   //Insert from face
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);
187     }
188     theResultBody->generated(aBottomFace, aBotName, _FROM_TAG);
189   }
190
191   //Insert to face
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);
197     }
198     theResultBody->generated(aTopFace, aTopName, _TO_TAG);
199   }
200
201 }