1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
20 #include <FeaturesPlugin_Rotation.h>
22 #include <ModelAPI_AttributeDouble.h>
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_AttributeString.h>
25 #include <ModelAPI_ResultBody.h>
26 #include <ModelAPI_ResultPart.h>
28 #include <GeomAlgoAPI_PointBuilder.h>
30 #include <GeomAPI_Edge.h>
31 #include <GeomAPI_Lin.h>
32 #include <GeomAPI_Trsf.h>
34 #include <FeaturesPlugin_Tools.h>
36 //=================================================================================================
37 FeaturesPlugin_Rotation::FeaturesPlugin_Rotation()
41 //=================================================================================================
42 void FeaturesPlugin_Rotation::initAttributes()
44 data()->addAttribute(FeaturesPlugin_Rotation::CREATION_METHOD(),
45 ModelAPI_AttributeString::typeId());
47 AttributeSelectionListPtr aSelection =
48 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
49 FeaturesPlugin_Rotation::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
51 data()->addAttribute(FeaturesPlugin_Rotation::AXIS_OBJECT_ID(),
52 ModelAPI_AttributeSelection::typeId());
53 data()->addAttribute(FeaturesPlugin_Rotation::ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
55 data()->addAttribute(FeaturesPlugin_Rotation::CENTER_POINT_ID(),
56 ModelAPI_AttributeSelection::typeId());
57 data()->addAttribute(FeaturesPlugin_Rotation::START_POINT_ID(),
58 ModelAPI_AttributeSelection::typeId());
59 data()->addAttribute(FeaturesPlugin_Rotation::END_POINT_ID(),
60 ModelAPI_AttributeSelection::typeId());
63 //=================================================================================================
64 void FeaturesPlugin_Rotation::execute()
66 AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Rotation::CREATION_METHOD());
67 std::string aMethodType = aMethodTypeAttr->value();
69 if (aMethodType == CREATION_METHOD_BY_ANGLE()) {
70 performTranslationByAxisAndAngle();
73 if (aMethodType == CREATION_METHOD_BY_THREE_POINTS()) {
74 performTranslationByThreePoints();
78 //=================================================================================================
79 void FeaturesPlugin_Rotation::performTranslationByAxisAndAngle()
82 ListOfShape anObjects;
83 std::list<ResultPtr> aContextes;
84 AttributeSelectionListPtr anObjectsSelList =
85 selectionList(FeaturesPlugin_Rotation::OBJECTS_LIST_ID());
86 if (anObjectsSelList->size() == 0) {
89 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
90 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
91 anObjectsSelList->value(anObjectsIndex);
92 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
96 anObjects.push_back(anObject);
97 aContextes.push_back(anObjectAttr->context());
101 std::shared_ptr<GeomAPI_Ax1> anAxis;
102 std::shared_ptr<GeomAPI_Edge> anEdge;
103 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
104 selection(FeaturesPlugin_Rotation::AXIS_OBJECT_ID());
105 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
106 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
107 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
108 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
109 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
112 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
113 anEdge->line()->direction()));
117 double anAngle = real(FeaturesPlugin_Rotation::ANGLE_ID())->value();
119 // Rotating each object.
120 int aResultIndex = 0;
121 std::list<ResultPtr>::iterator aContext = aContextes.begin();
122 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
123 anObjectsIt++, aContext++) {
124 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
125 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
129 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
130 aTrsf->setRotation(anAxis, anAngle);
131 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
132 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
133 aResultPart->setTrsf(*aContext, aTrsf);
134 setResult(aResultPart, aResultIndex);
136 GeomAlgoAPI_Rotation aRotationAlgo(aBaseShape, anAxis, anAngle);
138 if (!aRotationAlgo.check()) {
139 setError(aRotationAlgo.getError());
143 aRotationAlgo.build();
145 // Checking that the algorithm worked properly.
146 if(!aRotationAlgo.isDone()) {
147 static const std::string aFeatureError = "Error: Rotation algorithm failed.";
148 setError(aFeatureError);
151 if(aRotationAlgo.shape()->isNull()) {
152 static const std::string aShapeError = "Error: Resulting shape is Null.";
153 setError(aShapeError);
156 if(!aRotationAlgo.isValid()) {
157 std::string aFeatureError = "Error: Resulting shape is not valid.";
158 setError(aFeatureError);
162 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
163 loadNamingDS(aRotationAlgo, aResultBody, aBaseShape);
164 setResult(aResultBody, aResultIndex);
169 // Remove the rest results if there were produced in the previous pass.
170 removeResults(aResultIndex);
173 //=================================================================================================
174 void FeaturesPlugin_Rotation::performTranslationByThreePoints()
177 ListOfShape anObjects;
178 std::list<ResultPtr> aContextes;
179 AttributeSelectionListPtr anObjectsSelList =
180 selectionList(FeaturesPlugin_Rotation::OBJECTS_LIST_ID());
181 if (anObjectsSelList->size() == 0) {
184 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
185 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
186 anObjectsSelList->value(anObjectsIndex);
187 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
188 if(!anObject.get()) {
191 anObjects.push_back(anObject);
192 aContextes.push_back(anObjectAttr->context());
195 // Getting the center point and two points (start and end)
196 std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
197 std::shared_ptr<GeomAPI_Pnt> aStartPoint;
198 std::shared_ptr<GeomAPI_Pnt> anEndPoint;
199 std::shared_ptr<ModelAPI_AttributeSelection> aCenterRef =
200 selection(FeaturesPlugin_Rotation::CENTER_POINT_ID());
201 std::shared_ptr<ModelAPI_AttributeSelection> aStartPointRef =
202 selection(FeaturesPlugin_Rotation::START_POINT_ID());
203 std::shared_ptr<ModelAPI_AttributeSelection> anEndPointRef =
204 selection(FeaturesPlugin_Rotation::END_POINT_ID());
205 if ((aCenterRef.get() != NULL) && (aStartPointRef.get() != NULL)
206 && (anEndPointRef.get() != NULL)) {
207 GeomShapePtr aCenterShape = aCenterRef->value();
208 if (!aCenterShape.get())
209 aCenterShape = aCenterRef->context()->shape();
210 GeomShapePtr aStartShape = aStartPointRef->value();
211 if (!aStartShape.get())
212 aStartShape = aStartPointRef->context()->shape();
213 GeomShapePtr anEndShape = anEndPointRef->value();
214 if (!anEndShape.get())
215 anEndShape = anEndPointRef->context()->shape();
216 if (aStartShape && anEndShape && aCenterShape) {
217 aCenterPoint = GeomAlgoAPI_PointBuilder::point(aCenterShape);
218 aStartPoint = GeomAlgoAPI_PointBuilder::point(aStartShape);
219 anEndPoint = GeomAlgoAPI_PointBuilder::point(anEndShape);
223 // Rotating each object.
224 int aResultIndex = 0;
225 std::list<ResultPtr>::iterator aContext = aContextes.begin();
226 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
227 anObjectsIt++, aContext++) {
228 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
229 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
233 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
234 aTrsf->setRotation(aCenterPoint, aStartPoint, anEndPoint);
235 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
236 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
237 aResultPart->setTrsf(*aContext, aTrsf);
238 setResult(aResultPart, aResultIndex);
240 GeomAlgoAPI_Rotation aRotationAlgo(aBaseShape, aCenterPoint, aStartPoint, anEndPoint);
242 if (!aRotationAlgo.check()) {
243 setError(aRotationAlgo.getError());
247 aRotationAlgo.build();
249 // Checking that the algorithm worked properly.
250 if(!aRotationAlgo.isDone()) {
251 static const std::string aFeatureError = "Error: Rotation algorithm failed.";
252 setError(aFeatureError);
255 if(aRotationAlgo.shape()->isNull()) {
256 static const std::string aShapeError = "Error : Resulting shape is Null.";
257 setError(aShapeError);
260 if(!aRotationAlgo.isValid()) {
261 std::string aFeatureError = "Error: Resulting shape is not valid.";
262 setError(aFeatureError);
266 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
267 loadNamingDS(aRotationAlgo, aResultBody, aBaseShape);
268 setResult(aResultBody, aResultIndex);
274 //=================================================================================================
275 void FeaturesPlugin_Rotation::loadNamingDS(GeomAlgoAPI_Rotation& theRotaionAlgo,
276 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
277 std::shared_ptr<GeomAPI_Shape> theBaseShape)
280 theResultBody->storeModified(theBaseShape, theRotaionAlgo.shape());
282 std::string aRotatedName = "Rotated";
283 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theRotaionAlgo.mapOfSubShapes();
285 FeaturesPlugin_Tools::storeModifiedShapes(theRotaionAlgo, theResultBody,
286 theBaseShape, 1, 2, 3, aRotatedName,