Salome HOME
473d5964a3cedece2e8b960c44fbc0155dfe64f9
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Rotation.cpp
1 // Copyright (C) 2014-2021  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <FeaturesPlugin_Rotation.h>
21
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>
28
29 #include <GeomAlgoAPI_MakeShapeList.h>
30 #include <GeomAlgoAPI_PointBuilder.h>
31 #include <GeomAlgoAPI_Rotation.h>
32 #include <GeomAlgoAPI_Tools.h>
33
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>
40
41 #include <FeaturesPlugin_Tools.h>
42
43 static const std::string ROTATION_VERSION_1("v9.5");
44
45 //=================================================================================================
46 FeaturesPlugin_Rotation::FeaturesPlugin_Rotation()
47 {
48 }
49
50 //=================================================================================================
51 void FeaturesPlugin_Rotation::initAttributes()
52 {
53   data()->addAttribute(FeaturesPlugin_Rotation::CREATION_METHOD(),
54                        ModelAPI_AttributeString::typeId());
55
56   AttributeSelectionListPtr aSelection =
57     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
58     FeaturesPlugin_Rotation::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
59
60   data()->addAttribute(FeaturesPlugin_Rotation::AXIS_OBJECT_ID(),
61                        ModelAPI_AttributeSelection::typeId());
62   data()->addAttribute(FeaturesPlugin_Rotation::ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
63
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());
70
71   if (!aSelection->isInitialized()) {
72     // new feature, not read from file
73     data()->setVersion(ROTATION_VERSION_1);
74   }
75 }
76
77 //=================================================================================================
78 void FeaturesPlugin_Rotation::execute()
79 {
80   AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Rotation::CREATION_METHOD());
81   std::string aMethodType = aMethodTypeAttr->value();
82
83   GeomTrsfPtr aTrsf;
84   if (aMethodType == CREATION_METHOD_BY_ANGLE())
85     aTrsf = rotationByAxisAndAngle();
86   else if (aMethodType == CREATION_METHOD_BY_THREE_POINTS())
87     aTrsf = rotationByThreePoints();
88
89   performRotation(aTrsf);
90 }
91
92 //=================================================================================================
93 GeomTrsfPtr FeaturesPlugin_Rotation::rotationByAxisAndAngle()
94 {
95   //Getting axis.
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();
99   if (!aShape.get()) {
100     if (anObjRef->context().get()) {
101       aShape = anObjRef->context()->shape();
102     }
103   }
104   if (!aShape.get()) {
105     setError(aSelectionError);
106     return GeomTrsfPtr();
107   }
108
109   GeomEdgePtr anEdge;
110   if (aShape->isEdge())
111   {
112     anEdge = aShape->edge();
113   }
114   else if (aShape->isCompound())
115   {
116     GeomAPI_ShapeIterator anIt(aShape);
117     anEdge = anIt.current()->edge();
118   }
119
120   if (!anEdge.get())
121   {
122     setError(aSelectionError);
123     return GeomTrsfPtr();
124   }
125
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();
129
130   GeomTrsfPtr aTrsf(new GeomAPI_Trsf());
131   aTrsf->setRotation(anAxis, anAngle);
132   return aTrsf;
133 }
134
135 //=================================================================================================
136 GeomTrsfPtr FeaturesPlugin_Rotation::rotationByThreePoints()
137 {
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);
164     }
165
166     if (!aCenterPoint || !aStartPoint || !anEndPoint)
167       return GeomTrsfPtr();
168   }
169
170   GeomTrsfPtr aTrsf(new GeomAPI_Trsf());
171   aTrsf->setRotation(aCenterPoint, aStartPoint, anEndPoint);
172   return aTrsf;
173 }
174
175 //=================================================================================================
176 void FeaturesPlugin_Rotation::performRotation(const GeomTrsfPtr& theTrsf)
177 {
178   if (!theTrsf) {
179     setError("Invalid transformation.");
180     return;
181   }
182
183   bool isKeepSubShapes = data()->version() == ROTATION_VERSION_1;
184
185   // Getting objects.
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))
192     return;
193
194   std::string anError;
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++);
202   }
203
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));
211
212     // Checking that the algorithm worked properly.
213     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aRotationAlgo, getKind(), anError)) {
214       setError(anError);
215       break;
216     }
217
218     anObjects.markModified(aBaseShape, aRotationAlgo->shape());
219     aMakeShapeList->appendAlgo(aRotationAlgo);
220   }
221
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++);
233   }
234
235   // Remove the rest results if there were produced in the previous pass.
236   removeResults(aResultIndex);
237 }