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);
177 aShapes.push_back(aBaseShape);
178 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
182 aRotationAlgo->shape(),
184 setResult(aResultBody, aResultIndex);
189 // Remove the rest results if there were produced in the previous pass.
190 removeResults(aResultIndex);
193 //=================================================================================================
194 void FeaturesPlugin_Rotation::performTranslationByThreePoints()
197 ListOfShape anObjects;
198 std::list<ResultPtr> aContextes;
199 AttributeSelectionListPtr anObjectsSelList =
200 selectionList(FeaturesPlugin_Rotation::OBJECTS_LIST_ID());
201 if (anObjectsSelList->size() == 0) {
204 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
205 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
206 anObjectsSelList->value(anObjectsIndex);
207 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
208 if(!anObject.get()) {
211 anObjects.push_back(anObject);
212 aContextes.push_back(anObjectAttr->context());
215 // Getting the center point and two points (start and end)
216 std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
217 std::shared_ptr<GeomAPI_Pnt> aStartPoint;
218 std::shared_ptr<GeomAPI_Pnt> anEndPoint;
219 std::shared_ptr<ModelAPI_AttributeSelection> aCenterRef =
220 selection(FeaturesPlugin_Rotation::CENTER_POINT_ID());
221 std::shared_ptr<ModelAPI_AttributeSelection> aStartPointRef =
222 selection(FeaturesPlugin_Rotation::START_POINT_ID());
223 std::shared_ptr<ModelAPI_AttributeSelection> anEndPointRef =
224 selection(FeaturesPlugin_Rotation::END_POINT_ID());
225 if ((aCenterRef.get() != NULL) &&
226 (aStartPointRef.get() != NULL) &&
227 (anEndPointRef.get() != NULL)) {
228 GeomShapePtr aCenterShape = aCenterRef->value();
229 if (!aCenterShape.get() && aCenterRef->context().get())
230 aCenterShape = aCenterRef->context()->shape();
231 GeomShapePtr aStartShape = aStartPointRef->value();
232 if (!aStartShape.get() && aStartPointRef->context().get())
233 aStartShape = aStartPointRef->context()->shape();
234 GeomShapePtr anEndShape = anEndPointRef->value();
235 if (!anEndShape.get() && anEndPointRef->context().get())
236 anEndShape = anEndPointRef->context()->shape();
237 if (aStartShape && anEndShape && aCenterShape) {
238 aCenterPoint = GeomAlgoAPI_PointBuilder::point(aCenterShape);
239 aStartPoint = GeomAlgoAPI_PointBuilder::point(aStartShape);
240 anEndPoint = GeomAlgoAPI_PointBuilder::point(anEndShape);
244 // Rotating each object.
246 int aResultIndex = 0;
247 std::list<ResultPtr>::iterator aContext = aContextes.begin();
248 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
249 anObjectsIt++, aContext++) {
250 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
251 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
255 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
256 aTrsf->setRotation(aCenterPoint, aStartPoint, anEndPoint);
257 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
258 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
259 aResultPart->setTrsf(*aContext, aTrsf);
260 setResult(aResultPart, aResultIndex);
262 std::shared_ptr<GeomAlgoAPI_Rotation> aRotationAlgo(new GeomAlgoAPI_Rotation(aBaseShape,
267 if (!aRotationAlgo->check()) {
268 setError(aRotationAlgo->getError());
272 aRotationAlgo->build();
274 // Checking that the algorithm worked properly.
275 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aRotationAlgo, getKind(), anError)) {
280 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
283 aShapes.push_back(aBaseShape);
284 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
288 aRotationAlgo->shape(),
290 setResult(aResultBody, aResultIndex);