1 // Copyright (C) 2014-2019 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
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>
29 #include <GeomAlgoAPI_Tools.h>
31 #include <GeomAPI_Edge.h>
32 #include <GeomAPI_Lin.h>
33 #include <GeomAPI_ShapeIterator.h>
34 #include <GeomAPI_Trsf.h>
36 #include <FeaturesPlugin_Tools.h>
38 //=================================================================================================
39 FeaturesPlugin_Rotation::FeaturesPlugin_Rotation()
43 //=================================================================================================
44 void FeaturesPlugin_Rotation::initAttributes()
46 data()->addAttribute(FeaturesPlugin_Rotation::CREATION_METHOD(),
47 ModelAPI_AttributeString::typeId());
49 AttributeSelectionListPtr aSelection =
50 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
51 FeaturesPlugin_Rotation::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
53 data()->addAttribute(FeaturesPlugin_Rotation::AXIS_OBJECT_ID(),
54 ModelAPI_AttributeSelection::typeId());
55 data()->addAttribute(FeaturesPlugin_Rotation::ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
57 data()->addAttribute(FeaturesPlugin_Rotation::CENTER_POINT_ID(),
58 ModelAPI_AttributeSelection::typeId());
59 data()->addAttribute(FeaturesPlugin_Rotation::START_POINT_ID(),
60 ModelAPI_AttributeSelection::typeId());
61 data()->addAttribute(FeaturesPlugin_Rotation::END_POINT_ID(),
62 ModelAPI_AttributeSelection::typeId());
65 //=================================================================================================
66 void FeaturesPlugin_Rotation::execute()
68 AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Rotation::CREATION_METHOD());
69 std::string aMethodType = aMethodTypeAttr->value();
71 if (aMethodType == CREATION_METHOD_BY_ANGLE()) {
72 performTranslationByAxisAndAngle();
75 if (aMethodType == CREATION_METHOD_BY_THREE_POINTS()) {
76 performTranslationByThreePoints();
80 //=================================================================================================
81 void FeaturesPlugin_Rotation::performTranslationByAxisAndAngle()
84 ListOfShape anObjects;
85 std::list<ResultPtr> aContextes;
86 AttributeSelectionListPtr anObjectsSelList =
87 selectionList(FeaturesPlugin_Rotation::OBJECTS_LIST_ID());
88 if (anObjectsSelList->size() == 0) {
91 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
92 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
93 anObjectsSelList->value(anObjectsIndex);
94 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
98 anObjects.push_back(anObject);
99 aContextes.push_back(anObjectAttr->context());
103 static const std::string aSelectionError = "Error: The axis shape selection is bad.";
104 AttributeSelectionPtr anObjRef = selection(AXIS_OBJECT_ID());
105 GeomShapePtr aShape = anObjRef->value();
107 if (anObjRef->context().get()) {
108 aShape = anObjRef->context()->shape();
112 setError(aSelectionError);
117 if (aShape->isEdge())
119 anEdge = aShape->edge();
121 else if (aShape->isCompound())
123 GeomAPI_ShapeIterator anIt(aShape);
124 anEdge = anIt.current()->edge();
129 setError(aSelectionError);
133 std::shared_ptr<GeomAPI_Ax1> anAxis (new GeomAPI_Ax1(anEdge->line()->location(),
134 anEdge->line()->direction()));
137 double anAngle = real(FeaturesPlugin_Rotation::ANGLE_ID())->value();
139 // Rotating each object.
141 int aResultIndex = 0;
142 std::list<ResultPtr>::iterator aContext = aContextes.begin();
143 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
144 anObjectsIt++, aContext++) {
145 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
146 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
150 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
151 aTrsf->setRotation(anAxis, anAngle);
152 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
153 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
154 aResultPart->setTrsf(*aContext, aTrsf);
155 setResult(aResultPart, aResultIndex);
157 std::shared_ptr<GeomAlgoAPI_Rotation> aRotationAlgo(new GeomAlgoAPI_Rotation(aBaseShape,
161 if (!aRotationAlgo->check()) {
162 setError(aRotationAlgo->getError());
166 aRotationAlgo->build();
168 // Checking that the algorithm worked properly.
169 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aRotationAlgo, getKind(), anError)) {
174 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
175 aResultBody->storeModified(aBaseShape, aRotationAlgo->shape());
176 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShape, aRotationAlgo, "Rotated");
177 setResult(aResultBody, aResultIndex);
182 // Remove the rest results if there were produced in the previous pass.
183 removeResults(aResultIndex);
186 //=================================================================================================
187 void FeaturesPlugin_Rotation::performTranslationByThreePoints()
190 ListOfShape anObjects;
191 std::list<ResultPtr> aContextes;
192 AttributeSelectionListPtr anObjectsSelList =
193 selectionList(FeaturesPlugin_Rotation::OBJECTS_LIST_ID());
194 if (anObjectsSelList->size() == 0) {
197 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
198 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
199 anObjectsSelList->value(anObjectsIndex);
200 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
201 if(!anObject.get()) {
204 anObjects.push_back(anObject);
205 aContextes.push_back(anObjectAttr->context());
208 // Getting the center point and two points (start and end)
209 std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
210 std::shared_ptr<GeomAPI_Pnt> aStartPoint;
211 std::shared_ptr<GeomAPI_Pnt> anEndPoint;
212 std::shared_ptr<ModelAPI_AttributeSelection> aCenterRef =
213 selection(FeaturesPlugin_Rotation::CENTER_POINT_ID());
214 std::shared_ptr<ModelAPI_AttributeSelection> aStartPointRef =
215 selection(FeaturesPlugin_Rotation::START_POINT_ID());
216 std::shared_ptr<ModelAPI_AttributeSelection> anEndPointRef =
217 selection(FeaturesPlugin_Rotation::END_POINT_ID());
218 if ((aCenterRef.get() != NULL) &&
219 (aStartPointRef.get() != NULL) &&
220 (anEndPointRef.get() != NULL)) {
221 GeomShapePtr aCenterShape = aCenterRef->value();
222 if (!aCenterShape.get() && aCenterRef->context().get())
223 aCenterShape = aCenterRef->context()->shape();
224 GeomShapePtr aStartShape = aStartPointRef->value();
225 if (!aStartShape.get() && aStartPointRef->context().get())
226 aStartShape = aStartPointRef->context()->shape();
227 GeomShapePtr anEndShape = anEndPointRef->value();
228 if (!anEndShape.get() && anEndPointRef->context().get())
229 anEndShape = anEndPointRef->context()->shape();
230 if (aStartShape && anEndShape && aCenterShape) {
231 aCenterPoint = GeomAlgoAPI_PointBuilder::point(aCenterShape);
232 aStartPoint = GeomAlgoAPI_PointBuilder::point(aStartShape);
233 anEndPoint = GeomAlgoAPI_PointBuilder::point(anEndShape);
237 // Rotating each object.
239 int aResultIndex = 0;
240 std::list<ResultPtr>::iterator aContext = aContextes.begin();
241 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
242 anObjectsIt++, aContext++) {
243 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
244 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
248 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
249 aTrsf->setRotation(aCenterPoint, aStartPoint, anEndPoint);
250 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
251 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
252 aResultPart->setTrsf(*aContext, aTrsf);
253 setResult(aResultPart, aResultIndex);
255 std::shared_ptr<GeomAlgoAPI_Rotation> aRotationAlgo(new GeomAlgoAPI_Rotation(aBaseShape,
260 if (!aRotationAlgo->check()) {
261 setError(aRotationAlgo->getError());
265 aRotationAlgo->build();
267 // Checking that the algorithm worked properly.
268 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aRotationAlgo, getKind(), anError)) {
273 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
274 aResultBody->storeModified(aBaseShape, aRotationAlgo->shape());
275 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShape, aRotationAlgo, "Rotated");
276 setResult(aResultBody, aResultIndex);