1 // Copyright (C) 2014-2022 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>
27 #include <ModelAPI_Tools.h>
29 #include <GeomAlgoAPI_MakeShapeList.h>
30 #include <GeomAlgoAPI_PointBuilder.h>
31 #include <GeomAlgoAPI_Rotation.h>
32 #include <GeomAlgoAPI_Tools.h>
34 #include <GeomAPI_Ax1.h>
35 #include <GeomAPI_Edge.h>
36 #include <GeomAPI_Lin.h>
37 #include <GeomAPI_ShapeIterator.h>
38 #include <GeomAPI_ShapeHierarchy.h>
39 #include <GeomAPI_Trsf.h>
41 #include <FeaturesPlugin_Tools.h>
43 static const std::string ROTATION_VERSION_1("v9.5");
45 //=================================================================================================
46 FeaturesPlugin_Rotation::FeaturesPlugin_Rotation()
50 //=================================================================================================
51 void FeaturesPlugin_Rotation::initAttributes()
53 data()->addAttribute(FeaturesPlugin_Rotation::CREATION_METHOD(),
54 ModelAPI_AttributeString::typeId());
56 AttributeSelectionListPtr aSelection =
57 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
58 FeaturesPlugin_Rotation::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
60 data()->addAttribute(FeaturesPlugin_Rotation::AXIS_OBJECT_ID(),
61 ModelAPI_AttributeSelection::typeId());
62 data()->addAttribute(FeaturesPlugin_Rotation::ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
64 data()->addAttribute(FeaturesPlugin_Rotation::CENTER_POINT_ID(),
65 ModelAPI_AttributeSelection::typeId());
66 data()->addAttribute(FeaturesPlugin_Rotation::START_POINT_ID(),
67 ModelAPI_AttributeSelection::typeId());
68 data()->addAttribute(FeaturesPlugin_Rotation::END_POINT_ID(),
69 ModelAPI_AttributeSelection::typeId());
71 if (!aSelection->isInitialized()) {
72 // new feature, not read from file
73 data()->setVersion(ROTATION_VERSION_1);
77 //=================================================================================================
78 void FeaturesPlugin_Rotation::execute()
80 AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Rotation::CREATION_METHOD());
81 std::string aMethodType = aMethodTypeAttr->value();
84 if (aMethodType == CREATION_METHOD_BY_ANGLE())
85 aTrsf = rotationByAxisAndAngle();
86 else if (aMethodType == CREATION_METHOD_BY_THREE_POINTS())
87 aTrsf = rotationByThreePoints();
89 performRotation(aTrsf);
92 //=================================================================================================
93 GeomTrsfPtr FeaturesPlugin_Rotation::rotationByAxisAndAngle()
96 static const std::string aSelectionError = "Error: The axis shape selection is bad.";
97 AttributeSelectionPtr anObjRef = selection(AXIS_OBJECT_ID());
98 GeomShapePtr aShape = anObjRef->value();
100 if (anObjRef->context().get()) {
101 aShape = anObjRef->context()->shape();
105 setError(aSelectionError);
106 return GeomTrsfPtr();
110 if (aShape->isEdge())
112 anEdge = aShape->edge();
114 else if (aShape->isCompound())
116 GeomAPI_ShapeIterator anIt(aShape);
117 anEdge = anIt.current()->edge();
122 setError(aSelectionError);
123 return GeomTrsfPtr();
126 std::shared_ptr<GeomAPI_Ax1> anAxis (new GeomAPI_Ax1(anEdge->line()->location(),
127 anEdge->line()->direction()));
128 double anAngle = real(FeaturesPlugin_Rotation::ANGLE_ID())->value();
130 GeomTrsfPtr aTrsf(new GeomAPI_Trsf());
131 aTrsf->setRotation(anAxis, anAngle);
135 //=================================================================================================
136 GeomTrsfPtr FeaturesPlugin_Rotation::rotationByThreePoints()
138 // Getting the center point and two points (start and end)
139 std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
140 std::shared_ptr<GeomAPI_Pnt> aStartPoint;
141 std::shared_ptr<GeomAPI_Pnt> anEndPoint;
142 std::shared_ptr<ModelAPI_AttributeSelection> aCenterRef =
143 selection(FeaturesPlugin_Rotation::CENTER_POINT_ID());
144 std::shared_ptr<ModelAPI_AttributeSelection> aStartPointRef =
145 selection(FeaturesPlugin_Rotation::START_POINT_ID());
146 std::shared_ptr<ModelAPI_AttributeSelection> anEndPointRef =
147 selection(FeaturesPlugin_Rotation::END_POINT_ID());
148 if ((aCenterRef.get() != NULL) &&
149 (aStartPointRef.get() != NULL) &&
150 (anEndPointRef.get() != NULL)) {
151 GeomShapePtr aCenterShape = aCenterRef->value();
152 if (!aCenterShape.get() && aCenterRef->context().get())
153 aCenterShape = aCenterRef->context()->shape();
154 GeomShapePtr aStartShape = aStartPointRef->value();
155 if (!aStartShape.get() && aStartPointRef->context().get())
156 aStartShape = aStartPointRef->context()->shape();
157 GeomShapePtr anEndShape = anEndPointRef->value();
158 if (!anEndShape.get() && anEndPointRef->context().get())
159 anEndShape = anEndPointRef->context()->shape();
160 if (aStartShape && anEndShape && aCenterShape) {
161 aCenterPoint = GeomAlgoAPI_PointBuilder::point(aCenterShape);
162 aStartPoint = GeomAlgoAPI_PointBuilder::point(aStartShape);
163 anEndPoint = GeomAlgoAPI_PointBuilder::point(anEndShape);
166 if (!aCenterPoint || !aStartPoint || !anEndPoint)
167 return GeomTrsfPtr();
170 GeomTrsfPtr aTrsf(new GeomAPI_Trsf());
171 aTrsf->setRotation(aCenterPoint, aStartPoint, anEndPoint);
175 //=================================================================================================
176 void FeaturesPlugin_Rotation::performRotation(const GeomTrsfPtr& theTrsf)
179 setError("Invalid transformation.");
183 bool isKeepSubShapes = data()->version() == ROTATION_VERSION_1;
186 GeomAPI_ShapeHierarchy anObjects;
187 std::list<ResultPtr> aParts;
188 ResultPtr aTextureSource;
189 AttributeSelectionListPtr anObjSelList = selectionList(OBJECTS_LIST_ID());
190 if (!FeaturesPlugin_Tools::shapesFromSelectionList(
191 anObjSelList, isKeepSubShapes, anObjects, aParts, aTextureSource))
195 int aResultIndex = 0;
196 // Rotating each part.
197 for (std::list<ResultPtr>::iterator aPRes = aParts.begin(); aPRes != aParts.end(); ++aPRes) {
198 ResultPartPtr anOriginal = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aPRes);
199 ResultPartPtr aResultPart = document()->copyPart(anOriginal, data(), aResultIndex);
200 aResultPart->setTrsf(anOriginal, theTrsf);
201 setResult(aResultPart, aResultIndex++);
204 // Collect transformations for each object in a part.
205 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList);
206 for(GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
207 anObjectsIt != anObjects.end(); ++anObjectsIt) {
208 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
209 std::shared_ptr<GeomAlgoAPI_Transform> aRotationAlgo(
210 new GeomAlgoAPI_Transform(aBaseShape, theTrsf));
212 // Checking that the algorithm worked properly.
213 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aRotationAlgo, getKind(), anError)) {
218 anObjects.markModified(aBaseShape, aRotationAlgo->shape());
219 aMakeShapeList->appendAlgo(aRotationAlgo);
222 // Build results of the rotation.
223 const ListOfShape& anOriginalShapes = anObjects.objects();
224 ListOfShape aTopLevel;
225 anObjects.topLevelObjects(aTopLevel);
226 for (ListOfShape::iterator anIt = aTopLevel.begin(); anIt != aTopLevel.end(); ++anIt) {
227 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
228 ModelAPI_Tools::loadModifiedShapes(aResultBody, anOriginalShapes, ListOfShape(),
229 aMakeShapeList, *anIt, "Rotated");
230 // Copy image data, if any
231 ModelAPI_Tools::copyImageAttribute(aTextureSource, aResultBody);
232 setResult(aResultBody, aResultIndex++);
235 // Remove the rest results if there were produced in the previous pass.
236 removeResults(aResultIndex);