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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <FeaturesPlugin_Rotation.h>
23 #include <ModelAPI_AttributeDouble.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_AttributeString.h>
26 #include <ModelAPI_ResultBody.h>
27 #include <ModelAPI_ResultPart.h>
29 #include <GeomAlgoAPI_PointBuilder.h>
31 #include <GeomAPI_Edge.h>
32 #include <GeomAPI_Lin.h>
33 #include <GeomAPI_Trsf.h>
35 #include <FeaturesPlugin_Tools.h>
37 //=================================================================================================
38 FeaturesPlugin_Rotation::FeaturesPlugin_Rotation()
42 //=================================================================================================
43 void FeaturesPlugin_Rotation::initAttributes()
45 data()->addAttribute(FeaturesPlugin_Rotation::CREATION_METHOD(),
46 ModelAPI_AttributeString::typeId());
48 AttributeSelectionListPtr aSelection =
49 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
50 FeaturesPlugin_Rotation::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
52 data()->addAttribute(FeaturesPlugin_Rotation::AXIS_OBJECT_ID(),
53 ModelAPI_AttributeSelection::typeId());
54 data()->addAttribute(FeaturesPlugin_Rotation::ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
56 data()->addAttribute(FeaturesPlugin_Rotation::CENTER_POINT_ID(),
57 ModelAPI_AttributeSelection::typeId());
58 data()->addAttribute(FeaturesPlugin_Rotation::START_POINT_ID(),
59 ModelAPI_AttributeSelection::typeId());
60 data()->addAttribute(FeaturesPlugin_Rotation::END_POINT_ID(),
61 ModelAPI_AttributeSelection::typeId());
64 //=================================================================================================
65 void FeaturesPlugin_Rotation::execute()
67 AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Rotation::CREATION_METHOD());
68 std::string aMethodType = aMethodTypeAttr->value();
70 if (aMethodType == CREATION_METHOD_BY_ANGLE()) {
71 performTranslationByAxisAndAngle();
74 if (aMethodType == CREATION_METHOD_BY_THREE_POINTS()) {
75 performTranslationByThreePoints();
79 //=================================================================================================
80 void FeaturesPlugin_Rotation::performTranslationByAxisAndAngle()
83 ListOfShape anObjects;
84 std::list<ResultPtr> aContextes;
85 AttributeSelectionListPtr anObjectsSelList =
86 selectionList(FeaturesPlugin_Rotation::OBJECTS_LIST_ID());
87 if (anObjectsSelList->size() == 0) {
90 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
91 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
92 anObjectsSelList->value(anObjectsIndex);
93 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
97 anObjects.push_back(anObject);
98 aContextes.push_back(anObjectAttr->context());
102 std::shared_ptr<GeomAPI_Ax1> anAxis;
103 std::shared_ptr<GeomAPI_Edge> anEdge;
104 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
105 selection(FeaturesPlugin_Rotation::AXIS_OBJECT_ID());
106 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
107 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
108 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
109 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
110 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
113 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
114 anEdge->line()->direction()));
118 double anAngle = real(FeaturesPlugin_Rotation::ANGLE_ID())->value();
120 // Rotating each object.
121 int aResultIndex = 0;
122 std::list<ResultPtr>::iterator aContext = aContextes.begin();
123 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
124 anObjectsIt++, aContext++) {
125 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
126 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
130 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
131 aTrsf->setRotation(anAxis, anAngle);
132 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
133 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
134 aResultPart->setTrsf(*aContext, aTrsf);
135 setResult(aResultPart, aResultIndex);
137 GeomAlgoAPI_Rotation aRotationAlgo(aBaseShape, anAxis, anAngle);
139 if (!aRotationAlgo.check()) {
140 setError(aRotationAlgo.getError());
144 aRotationAlgo.build();
146 // Checking that the algorithm worked properly.
147 if(!aRotationAlgo.isDone()) {
148 static const std::string aFeatureError = "Error: Rotation algorithm failed.";
149 setError(aFeatureError);
152 if(aRotationAlgo.shape()->isNull()) {
153 static const std::string aShapeError = "Error: Resulting shape is Null.";
154 setError(aShapeError);
157 if(!aRotationAlgo.isValid()) {
158 std::string aFeatureError = "Error: Resulting shape is not valid.";
159 setError(aFeatureError);
163 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
164 loadNamingDS(aRotationAlgo, aResultBody, aBaseShape);
165 setResult(aResultBody, aResultIndex);
170 // Remove the rest results if there were produced in the previous pass.
171 removeResults(aResultIndex);
174 //=================================================================================================
175 void FeaturesPlugin_Rotation::performTranslationByThreePoints()
178 ListOfShape anObjects;
179 std::list<ResultPtr> aContextes;
180 AttributeSelectionListPtr anObjectsSelList =
181 selectionList(FeaturesPlugin_Rotation::OBJECTS_LIST_ID());
182 if (anObjectsSelList->size() == 0) {
185 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
186 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
187 anObjectsSelList->value(anObjectsIndex);
188 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
189 if(!anObject.get()) {
192 anObjects.push_back(anObject);
193 aContextes.push_back(anObjectAttr->context());
196 // Getting the center point and two points (start and end)
197 std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
198 std::shared_ptr<GeomAPI_Pnt> aStartPoint;
199 std::shared_ptr<GeomAPI_Pnt> anEndPoint;
200 std::shared_ptr<ModelAPI_AttributeSelection> aCenterRef =
201 selection(FeaturesPlugin_Rotation::CENTER_POINT_ID());
202 std::shared_ptr<ModelAPI_AttributeSelection> aStartPointRef =
203 selection(FeaturesPlugin_Rotation::START_POINT_ID());
204 std::shared_ptr<ModelAPI_AttributeSelection> anEndPointRef =
205 selection(FeaturesPlugin_Rotation::END_POINT_ID());
206 if ((aCenterRef.get() != NULL) && (aStartPointRef.get() != NULL)
207 && (anEndPointRef.get() != NULL)) {
208 GeomShapePtr aCenterShape = aCenterRef->value();
209 if (!aCenterShape.get())
210 aCenterShape = aCenterRef->context()->shape();
211 GeomShapePtr aStartShape = aStartPointRef->value();
212 if (!aStartShape.get())
213 aStartShape = aStartPointRef->context()->shape();
214 GeomShapePtr anEndShape = anEndPointRef->value();
215 if (!anEndShape.get())
216 anEndShape = anEndPointRef->context()->shape();
217 if (aStartShape && anEndShape && aCenterShape) {
218 aCenterPoint = GeomAlgoAPI_PointBuilder::point(aCenterShape);
219 aStartPoint = GeomAlgoAPI_PointBuilder::point(aStartShape);
220 anEndPoint = GeomAlgoAPI_PointBuilder::point(anEndShape);
224 // Rotating each object.
225 int aResultIndex = 0;
226 std::list<ResultPtr>::iterator aContext = aContextes.begin();
227 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
228 anObjectsIt++, aContext++) {
229 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
230 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
234 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
235 aTrsf->setRotation(aCenterPoint, aStartPoint, anEndPoint);
236 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
237 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
238 aResultPart->setTrsf(*aContext, aTrsf);
239 setResult(aResultPart, aResultIndex);
241 GeomAlgoAPI_Rotation aRotationAlgo(aBaseShape, aCenterPoint, aStartPoint, anEndPoint);
243 if (!aRotationAlgo.check()) {
244 setError(aRotationAlgo.getError());
248 aRotationAlgo.build();
250 // Checking that the algorithm worked properly.
251 if(!aRotationAlgo.isDone()) {
252 static const std::string aFeatureError = "Error: Rotation algorithm failed.";
253 setError(aFeatureError);
256 if(aRotationAlgo.shape()->isNull()) {
257 static const std::string aShapeError = "Error : Resulting shape is Null.";
258 setError(aShapeError);
261 if(!aRotationAlgo.isValid()) {
262 std::string aFeatureError = "Error: Resulting shape is not valid.";
263 setError(aFeatureError);
267 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
268 loadNamingDS(aRotationAlgo, aResultBody, aBaseShape);
269 setResult(aResultBody, aResultIndex);
275 //=================================================================================================
276 void FeaturesPlugin_Rotation::loadNamingDS(GeomAlgoAPI_Rotation& theRotaionAlgo,
277 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
278 std::shared_ptr<GeomAPI_Shape> theBaseShape)
281 theResultBody->storeModified(theBaseShape, theRotaionAlgo.shape());
283 std::string aRotatedName = "Rotated";
284 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theRotaionAlgo.mapOfSubShapes();
286 FeaturesPlugin_Tools::storeModifiedShapes(theRotaionAlgo, theResultBody,
287 theBaseShape, 1, 2, 3, aRotatedName,