1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: FeaturesPlugin_Rotation.cpp
4 // Created: 12 May 2015
5 // Author: Dmitry Bobylev
7 #include <FeaturesPlugin_Rotation.h>
9 #include <ModelAPI_AttributeDouble.h>
10 #include <ModelAPI_AttributeSelectionList.h>
11 #include <ModelAPI_AttributeString.h>
12 #include <ModelAPI_ResultBody.h>
13 #include <ModelAPI_ResultPart.h>
15 #include <GeomAlgoAPI_PointBuilder.h>
17 #include <GeomAPI_Edge.h>
18 #include <GeomAPI_Lin.h>
19 #include <GeomAPI_Trsf.h>
21 #include <FeaturesPlugin_Tools.h>
23 //=================================================================================================
24 FeaturesPlugin_Rotation::FeaturesPlugin_Rotation()
28 //=================================================================================================
29 void FeaturesPlugin_Rotation::initAttributes()
31 data()->addAttribute(FeaturesPlugin_Rotation::CREATION_METHOD(),
32 ModelAPI_AttributeString::typeId());
34 AttributeSelectionListPtr aSelection =
35 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
36 FeaturesPlugin_Rotation::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
38 data()->addAttribute(FeaturesPlugin_Rotation::AXIS_OBJECT_ID(),
39 ModelAPI_AttributeSelection::typeId());
40 data()->addAttribute(FeaturesPlugin_Rotation::ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
42 data()->addAttribute(FeaturesPlugin_Rotation::CENTER_POINT_ID(),
43 ModelAPI_AttributeSelection::typeId());
44 data()->addAttribute(FeaturesPlugin_Rotation::START_POINT_ID(),
45 ModelAPI_AttributeSelection::typeId());
46 data()->addAttribute(FeaturesPlugin_Rotation::END_POINT_ID(),
47 ModelAPI_AttributeSelection::typeId());
50 //=================================================================================================
51 void FeaturesPlugin_Rotation::execute()
53 AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Rotation::CREATION_METHOD());
54 std::string aMethodType = aMethodTypeAttr->value();
56 if (aMethodType == CREATION_METHOD_BY_ANGLE()) {
57 performTranslationByAxisAndAngle();
60 if (aMethodType == CREATION_METHOD_BY_THREE_POINTS()) {
61 performTranslationByThreePoints();
65 //=================================================================================================
66 void FeaturesPlugin_Rotation::performTranslationByAxisAndAngle()
69 ListOfShape anObjects;
70 std::list<ResultPtr> aContextes;
71 AttributeSelectionListPtr anObjectsSelList =
72 selectionList(FeaturesPlugin_Rotation::OBJECTS_LIST_ID());
73 if (anObjectsSelList->size() == 0) {
76 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
77 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
78 anObjectsSelList->value(anObjectsIndex);
79 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
83 anObjects.push_back(anObject);
84 aContextes.push_back(anObjectAttr->context());
88 std::shared_ptr<GeomAPI_Ax1> anAxis;
89 std::shared_ptr<GeomAPI_Edge> anEdge;
90 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
91 selection(FeaturesPlugin_Rotation::AXIS_OBJECT_ID());
92 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
93 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
94 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
95 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
96 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
99 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
100 anEdge->line()->direction()));
104 double anAngle = real(FeaturesPlugin_Rotation::ANGLE_ID())->value();
106 // Rotating each object.
107 int aResultIndex = 0;
108 std::list<ResultPtr>::iterator aContext = aContextes.begin();
109 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
110 anObjectsIt++, aContext++) {
111 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
112 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
116 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
117 aTrsf->setRotation(anAxis, anAngle);
118 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
119 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
120 aResultPart->setTrsf(*aContext, aTrsf);
121 setResult(aResultPart, aResultIndex);
123 GeomAlgoAPI_Rotation aRotationAlgo(aBaseShape, anAxis, anAngle);
125 if (!aRotationAlgo.check()) {
126 setError(aRotationAlgo.getError());
130 aRotationAlgo.build();
132 // Checking that the algorithm worked properly.
133 if(!aRotationAlgo.isDone()) {
134 static const std::string aFeatureError = "Error: Rotation algorithm failed.";
135 setError(aFeatureError);
138 if(aRotationAlgo.shape()->isNull()) {
139 static const std::string aShapeError = "Error: Resulting shape is Null.";
140 setError(aShapeError);
143 if(!aRotationAlgo.isValid()) {
144 std::string aFeatureError = "Error: Resulting shape is not valid.";
145 setError(aFeatureError);
149 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
150 loadNamingDS(aRotationAlgo, aResultBody, aBaseShape);
151 setResult(aResultBody, aResultIndex);
156 // Remove the rest results if there were produced in the previous pass.
157 removeResults(aResultIndex);
160 //=================================================================================================
161 void FeaturesPlugin_Rotation::performTranslationByThreePoints()
164 ListOfShape anObjects;
165 std::list<ResultPtr> aContextes;
166 AttributeSelectionListPtr anObjectsSelList =
167 selectionList(FeaturesPlugin_Rotation::OBJECTS_LIST_ID());
168 if (anObjectsSelList->size() == 0) {
171 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
172 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
173 anObjectsSelList->value(anObjectsIndex);
174 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
175 if(!anObject.get()) {
178 anObjects.push_back(anObject);
179 aContextes.push_back(anObjectAttr->context());
182 // Getting the center point and two points (start and end)
183 std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
184 std::shared_ptr<GeomAPI_Pnt> aStartPoint;
185 std::shared_ptr<GeomAPI_Pnt> anEndPoint;
186 std::shared_ptr<ModelAPI_AttributeSelection> aCenterRef =
187 selection(FeaturesPlugin_Rotation::CENTER_POINT_ID());
188 std::shared_ptr<ModelAPI_AttributeSelection> aStartPointRef =
189 selection(FeaturesPlugin_Rotation::START_POINT_ID());
190 std::shared_ptr<ModelAPI_AttributeSelection> anEndPointRef =
191 selection(FeaturesPlugin_Rotation::END_POINT_ID());
192 if ((aCenterRef.get() != NULL) && (aStartPointRef.get() != NULL)
193 && (anEndPointRef.get() != NULL)) {
194 GeomShapePtr aCenterShape = aCenterRef->value();
195 if (!aCenterShape.get())
196 aCenterShape = aCenterRef->context()->shape();
197 GeomShapePtr aStartShape = aStartPointRef->value();
198 if (!aStartShape.get())
199 aStartShape = aStartPointRef->context()->shape();
200 GeomShapePtr anEndShape = anEndPointRef->value();
201 if (!anEndShape.get())
202 anEndShape = anEndPointRef->context()->shape();
203 if (aStartShape && anEndShape && aCenterShape) {
204 aCenterPoint = GeomAlgoAPI_PointBuilder::point(aCenterShape);
205 aStartPoint = GeomAlgoAPI_PointBuilder::point(aStartShape);
206 anEndPoint = GeomAlgoAPI_PointBuilder::point(anEndShape);
210 // Rotating each object.
211 int aResultIndex = 0;
212 std::list<ResultPtr>::iterator aContext = aContextes.begin();
213 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
214 anObjectsIt++, aContext++) {
215 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
216 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
220 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
221 aTrsf->setRotation(aCenterPoint, aStartPoint, anEndPoint);
222 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
223 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
224 aResultPart->setTrsf(*aContext, aTrsf);
225 setResult(aResultPart, aResultIndex);
227 GeomAlgoAPI_Rotation aRotationAlgo(aBaseShape, aCenterPoint, aStartPoint, anEndPoint);
229 if (!aRotationAlgo.check()) {
230 setError(aRotationAlgo.getError());
234 aRotationAlgo.build();
236 // Checking that the algorithm worked properly.
237 if(!aRotationAlgo.isDone()) {
238 static const std::string aFeatureError = "Error: Rotation algorithm failed.";
239 setError(aFeatureError);
242 if(aRotationAlgo.shape()->isNull()) {
243 static const std::string aShapeError = "Error : Resulting shape is Null.";
244 setError(aShapeError);
247 if(!aRotationAlgo.isValid()) {
248 std::string aFeatureError = "Error: Resulting shape is not valid.";
249 setError(aFeatureError);
253 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
254 loadNamingDS(aRotationAlgo, aResultBody, aBaseShape);
255 setResult(aResultBody, aResultIndex);
261 //=================================================================================================
262 void FeaturesPlugin_Rotation::loadNamingDS(GeomAlgoAPI_Rotation& theRotaionAlgo,
263 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
264 std::shared_ptr<GeomAPI_Shape> theBaseShape)
267 theResultBody->storeModified(theBaseShape, theRotaionAlgo.shape());
269 std::string aRotatedName = "Rotated";
270 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theRotaionAlgo.mapOfSubShapes();
272 FeaturesPlugin_Tools::storeModifiedShapes(theRotaionAlgo, theResultBody,
273 theBaseShape, 1, 2, 3, aRotatedName,