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>
30 #include <GeomAlgoAPI_Tools.h>
32 #include <GeomAPI_Edge.h>
33 #include <GeomAPI_Lin.h>
34 #include <GeomAPI_ShapeIterator.h>
35 #include <GeomAPI_Trsf.h>
37 #include <FeaturesPlugin_Tools.h>
39 //=================================================================================================
40 FeaturesPlugin_Rotation::FeaturesPlugin_Rotation()
44 //=================================================================================================
45 void FeaturesPlugin_Rotation::initAttributes()
47 data()->addAttribute(FeaturesPlugin_Rotation::CREATION_METHOD(),
48 ModelAPI_AttributeString::typeId());
50 AttributeSelectionListPtr aSelection =
51 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
52 FeaturesPlugin_Rotation::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
54 data()->addAttribute(FeaturesPlugin_Rotation::AXIS_OBJECT_ID(),
55 ModelAPI_AttributeSelection::typeId());
56 data()->addAttribute(FeaturesPlugin_Rotation::ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
58 data()->addAttribute(FeaturesPlugin_Rotation::CENTER_POINT_ID(),
59 ModelAPI_AttributeSelection::typeId());
60 data()->addAttribute(FeaturesPlugin_Rotation::START_POINT_ID(),
61 ModelAPI_AttributeSelection::typeId());
62 data()->addAttribute(FeaturesPlugin_Rotation::END_POINT_ID(),
63 ModelAPI_AttributeSelection::typeId());
66 //=================================================================================================
67 void FeaturesPlugin_Rotation::execute()
69 AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Rotation::CREATION_METHOD());
70 std::string aMethodType = aMethodTypeAttr->value();
72 if (aMethodType == CREATION_METHOD_BY_ANGLE()) {
73 performTranslationByAxisAndAngle();
76 if (aMethodType == CREATION_METHOD_BY_THREE_POINTS()) {
77 performTranslationByThreePoints();
81 //=================================================================================================
82 void FeaturesPlugin_Rotation::performTranslationByAxisAndAngle()
85 ListOfShape anObjects;
86 std::list<ResultPtr> aContextes;
87 AttributeSelectionListPtr anObjectsSelList =
88 selectionList(FeaturesPlugin_Rotation::OBJECTS_LIST_ID());
89 if (anObjectsSelList->size() == 0) {
92 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
93 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
94 anObjectsSelList->value(anObjectsIndex);
95 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
99 anObjects.push_back(anObject);
100 aContextes.push_back(anObjectAttr->context());
104 static const std::string aSelectionError = "Error: The axis shape selection is bad.";
105 AttributeSelectionPtr anObjRef = selection(AXIS_OBJECT_ID());
106 GeomShapePtr aShape = anObjRef->value();
108 if (anObjRef->context().get()) {
109 aShape = anObjRef->context()->shape();
113 setError(aSelectionError);
118 if (aShape->isEdge())
120 anEdge = aShape->edge();
122 else if (aShape->isCompound())
124 GeomAPI_ShapeIterator anIt(aShape);
125 anEdge = anIt.current()->edge();
130 setError(aSelectionError);
134 std::shared_ptr<GeomAPI_Ax1> anAxis (new GeomAPI_Ax1(anEdge->line()->location(),
135 anEdge->line()->direction()));
138 double anAngle = real(FeaturesPlugin_Rotation::ANGLE_ID())->value();
140 // Rotating each object.
142 int aResultIndex = 0;
143 std::list<ResultPtr>::iterator aContext = aContextes.begin();
144 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
145 anObjectsIt++, aContext++) {
146 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
147 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
151 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
152 aTrsf->setRotation(anAxis, anAngle);
153 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
154 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
155 aResultPart->setTrsf(*aContext, aTrsf);
156 setResult(aResultPart, aResultIndex);
158 std::shared_ptr<GeomAlgoAPI_Rotation> aRotationAlgo(new GeomAlgoAPI_Rotation(aBaseShape,
162 if (!aRotationAlgo->check()) {
163 setError(aRotationAlgo->getError());
167 aRotationAlgo->build();
169 // Checking that the algorithm worked properly.
170 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aRotationAlgo, getKind(), anError)) {
175 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
178 aShapes.push_back(aBaseShape);
179 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
183 aRotationAlgo->shape(),
185 setResult(aResultBody, aResultIndex);
190 // Remove the rest results if there were produced in the previous pass.
191 removeResults(aResultIndex);
194 //=================================================================================================
195 void FeaturesPlugin_Rotation::performTranslationByThreePoints()
198 ListOfShape anObjects;
199 std::list<ResultPtr> aContextes;
200 AttributeSelectionListPtr anObjectsSelList =
201 selectionList(FeaturesPlugin_Rotation::OBJECTS_LIST_ID());
202 if (anObjectsSelList->size() == 0) {
205 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
206 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
207 anObjectsSelList->value(anObjectsIndex);
208 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
209 if(!anObject.get()) {
212 anObjects.push_back(anObject);
213 aContextes.push_back(anObjectAttr->context());
216 // Getting the center point and two points (start and end)
217 std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
218 std::shared_ptr<GeomAPI_Pnt> aStartPoint;
219 std::shared_ptr<GeomAPI_Pnt> anEndPoint;
220 std::shared_ptr<ModelAPI_AttributeSelection> aCenterRef =
221 selection(FeaturesPlugin_Rotation::CENTER_POINT_ID());
222 std::shared_ptr<ModelAPI_AttributeSelection> aStartPointRef =
223 selection(FeaturesPlugin_Rotation::START_POINT_ID());
224 std::shared_ptr<ModelAPI_AttributeSelection> anEndPointRef =
225 selection(FeaturesPlugin_Rotation::END_POINT_ID());
226 if ((aCenterRef.get() != NULL) &&
227 (aStartPointRef.get() != NULL) &&
228 (anEndPointRef.get() != NULL)) {
229 GeomShapePtr aCenterShape = aCenterRef->value();
230 if (!aCenterShape.get() && aCenterRef->context().get())
231 aCenterShape = aCenterRef->context()->shape();
232 GeomShapePtr aStartShape = aStartPointRef->value();
233 if (!aStartShape.get() && aStartPointRef->context().get())
234 aStartShape = aStartPointRef->context()->shape();
235 GeomShapePtr anEndShape = anEndPointRef->value();
236 if (!anEndShape.get() && anEndPointRef->context().get())
237 anEndShape = anEndPointRef->context()->shape();
238 if (aStartShape && anEndShape && aCenterShape) {
239 aCenterPoint = GeomAlgoAPI_PointBuilder::point(aCenterShape);
240 aStartPoint = GeomAlgoAPI_PointBuilder::point(aStartShape);
241 anEndPoint = GeomAlgoAPI_PointBuilder::point(anEndShape);
245 // Rotating each object.
247 int aResultIndex = 0;
248 std::list<ResultPtr>::iterator aContext = aContextes.begin();
249 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
250 anObjectsIt++, aContext++) {
251 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
252 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
256 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
257 aTrsf->setRotation(aCenterPoint, aStartPoint, anEndPoint);
258 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
259 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
260 aResultPart->setTrsf(*aContext, aTrsf);
261 setResult(aResultPart, aResultIndex);
263 std::shared_ptr<GeomAlgoAPI_Rotation> aRotationAlgo(new GeomAlgoAPI_Rotation(aBaseShape,
268 if (!aRotationAlgo->check()) {
269 setError(aRotationAlgo->getError());
273 aRotationAlgo->build();
275 // Checking that the algorithm worked properly.
276 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aRotationAlgo, getKind(), anError)) {
281 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
284 aShapes.push_back(aBaseShape);
285 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
289 aRotationAlgo->shape(),
291 setResult(aResultBody, aResultIndex);