Salome HOME
Feature #524: 4.01. Revolution feature (not complete!)
[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 _LATERAL_TAG 1
25 #define _FIRST_TAG 2
26 #define _LAST_TAG 3
27 #define EDGE 6
28
29 //=================================================================================================
30 FeaturesPlugin_Revolution::FeaturesPlugin_Revolution()
31 {
32 }
33
34 //=================================================================================================
35 void FeaturesPlugin_Revolution::initAttributes()
36 {
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");
42
43   data()->addAttribute(FeaturesPlugin_Revolution::AXIS_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
44
45   data()->addAttribute(FeaturesPlugin_Revolution::FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
46   data()->addAttribute(FeaturesPlugin_Revolution::FROM_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
47
48   data()->addAttribute(FeaturesPlugin_Revolution::TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
49   data()->addAttribute(FeaturesPlugin_Revolution::TO_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
50
51   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FeaturesPlugin_Revolution::FROM_OBJECT_ID());
52   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FeaturesPlugin_Revolution::TO_OBJECT_ID());
53 }
54
55 //=================================================================================================
56 void FeaturesPlugin_Revolution::execute()
57 {
58   AttributeSelectionListPtr aFaceRefs = selectionList(FeaturesPlugin_Revolution::LIST_ID());
59
60   //Getting axe.
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()));
66   }
67   if(anEdge) {
68     anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(), anEdge->line()->direction()));
69   }
70
71   // Getting bounding planes.
72   std::shared_ptr<GeomAPI_Shape> aFromShape(new GeomAPI_Shape());
73   std::shared_ptr<GeomAPI_Shape> aToShape(new GeomAPI_Shape());
74
75   anObjRef = selection(FeaturesPlugin_Revolution::FROM_OBJECT_ID());
76   if(anObjRef) {
77     aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
78   }
79   anObjRef = selection(FeaturesPlugin_Revolution::TO_OBJECT_ID());
80   if(anObjRef) {
81     aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
82   }
83
84   // Getting angles.
85   double aFromAngle = real(FeaturesPlugin_Revolution::FROM_ANGLE_ID())->value();
86   double aToAngle = real(FeaturesPlugin_Revolution::TO_ANGLE_ID())->value();
87
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);
97       break;
98     }
99
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();
107       } else {
108         static const std::string aFaceError = "Can not find basis for extrusion";
109         setError(aFaceError);
110         break;
111       }
112     }
113
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;
119       } else {
120         aBaseShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
121       }
122
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);
127         break;
128       }
129
130       // Check if shape is valid
131       if(aFeature.shape()->isNull()) {
132         static const std::string aShapeError = "Resulting shape is Null";
133         setError(aShapeError);
134         break;
135       }
136       if(!aFeature.isValid()) {
137         std::string aFeatureError = "Warning: resulting shape is not valid";
138         setError(aFeatureError);
139         break;
140       }
141       //LoadNamingDS
142       LoadNamingDS(aFeature, aResultBody, aBaseShape, aContext);
143
144       setResult(aResultBody, aResultIndex);
145       aResultIndex++;
146
147       if (aFacesNum == -1)
148         break;
149     }
150   }
151   // remove the rest results if there were produced in the previous pass
152   removeResults(aResultIndex);
153 }
154
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)
160 {
161   //TODO: Fix naming
162   theResultBody->store(theFeature.shape());
163   return;
164
165   //load result
166   if(theBasis->isEqual(theContext))
167     theResultBody->store(theFeature.shape());
168   else
169     theResultBody->storeGenerated(theContext, theFeature.shape());
170
171   GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
172   theFeature.mapOfShapes(*aSubShapes);
173
174   //Insert lateral face : Face from Edge
175   std::string aLatName = "LateralFace";
176   theResultBody->loadAndOrientGeneratedShapes(theFeature.makeShape(), theBasis, EDGE,_LATERAL_TAG, aLatName, *aSubShapes);
177
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);
184   //  }
185   //  theResultBody->generated(aBottomFace, aBotName, _FIRST_TAG);
186   //}
187
188   ////Insert last face
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);
194   //  }
195   //  theResultBody->generated(aTopFace, aTopName, _LAST_TAG);
196   //}
197 }