Salome HOME
11a0dca227d1fd8d0981685f7ffba80578603d4d
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Translation.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_Translation.h>
21
22 #include <ModelAPI_AttributeDouble.h>
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_AttributeString.h>
25 #include <ModelAPI_BodyBuilder.h>
26 #include <ModelAPI_ResultBody.h>
27 #include <ModelAPI_ResultPart.h>
28 #include <ModelAPI_Session.h>
29
30 #include <GeomAPI_Ax1.h>
31 #include <GeomAPI_Edge.h>
32 #include <GeomAPI_Lin.h>
33 #include <GeomAPI_ShapeIterator.h>
34 #include <GeomAPI_ShapeHierarchy.h>
35 #include <GeomAPI_Trsf.h>
36
37 #include <GeomAlgoAPI_MakeShapeList.h>
38 #include <GeomAlgoAPI_PointBuilder.h>
39 #include <GeomAlgoAPI_Tools.h>
40 #include <GeomAlgoAPI_Transform.h>
41
42 #include <FeaturesPlugin_Tools.h>
43
44 static const std::string TRANSLATION_VERSION_1("v9.5");
45
46 //=================================================================================================
47 FeaturesPlugin_Translation::FeaturesPlugin_Translation()
48 {
49 }
50
51 //=================================================================================================
52 void FeaturesPlugin_Translation::initAttributes()
53 {
54   data()->addAttribute(FeaturesPlugin_Translation::CREATION_METHOD(),
55                        ModelAPI_AttributeString::typeId());
56
57   AttributeSelectionListPtr aSelection =
58     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
59     FeaturesPlugin_Translation::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
60
61   data()->addAttribute(FeaturesPlugin_Translation::AXIS_OBJECT_ID(),
62                        ModelAPI_AttributeSelection::typeId());
63   data()->addAttribute(FeaturesPlugin_Translation::DISTANCE_ID(),
64                        ModelAPI_AttributeDouble::typeId());
65
66   data()->addAttribute(FeaturesPlugin_Translation::DX_ID(),
67                        ModelAPI_AttributeDouble::typeId());
68   data()->addAttribute(FeaturesPlugin_Translation::DY_ID(),
69                        ModelAPI_AttributeDouble::typeId());
70   data()->addAttribute(FeaturesPlugin_Translation::DZ_ID(),
71                        ModelAPI_AttributeDouble::typeId());
72
73   data()->addAttribute(FeaturesPlugin_Translation::START_POINT_ID(),
74                        ModelAPI_AttributeSelection::typeId());
75   data()->addAttribute(FeaturesPlugin_Translation::END_POINT_ID(),
76                        ModelAPI_AttributeSelection::typeId());
77
78   if (!aSelection->isInitialized()) {
79     // new feature, not read from file
80     data()->setVersion(TRANSLATION_VERSION_1);
81   }
82 }
83
84 //=================================================================================================
85 void FeaturesPlugin_Translation::execute()
86 {
87   AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Translation::CREATION_METHOD());
88   std::string aMethodType = aMethodTypeAttr->value();
89
90   GeomTrsfPtr aTrsf;
91   if (aMethodType == CREATION_METHOD_BY_DISTANCE())
92     aTrsf = translationByAxisAndDistance();
93   else if (aMethodType == CREATION_METHOD_BY_DIMENSIONS())
94     aTrsf = translationByDimensions();
95   else if (aMethodType == CREATION_METHOD_BY_TWO_POINTS())
96     aTrsf = translationByTwoPoints();
97
98   performTranslation(aTrsf);
99 }
100
101 //=================================================================================================
102 GeomTrsfPtr FeaturesPlugin_Translation::translationByAxisAndDistance()
103 {
104   //Getting axis.
105   static const std::string aSelectionError = "Error: The axis shape selection is bad.";
106   AttributeSelectionPtr anObjRef = selection(AXIS_OBJECT_ID());
107   GeomShapePtr aShape = anObjRef->value();
108   if (!aShape.get()) {
109     if (anObjRef->context().get()) {
110       aShape = anObjRef->context()->shape();
111     }
112   }
113   if (!aShape.get()) {
114     setError(aSelectionError);
115     return GeomTrsfPtr();
116   }
117
118   GeomEdgePtr anEdge;
119   if (aShape->isEdge())
120   {
121     anEdge = aShape->edge();
122   }
123   else if (aShape->isCompound())
124   {
125     GeomAPI_ShapeIterator anIt(aShape);
126     anEdge = anIt.current()->edge();
127   }
128
129   if (!anEdge.get())
130   {
131     setError(aSelectionError);
132     return GeomTrsfPtr();
133   }
134
135   std::shared_ptr<GeomAPI_Ax1> anAxis(new GeomAPI_Ax1(anEdge->line()->location(),
136                                                       anEdge->line()->direction()));
137
138   // Getting distance.
139   double aDistance = real(FeaturesPlugin_Translation::DISTANCE_ID())->value();
140
141   GeomTrsfPtr aTrsf(new GeomAPI_Trsf);
142   aTrsf->setTranslation(anAxis, aDistance);
143   return aTrsf;
144 }
145
146 //=================================================================================================
147 GeomTrsfPtr FeaturesPlugin_Translation::translationByDimensions()
148 {
149   // Getting dimensions in X, in Y and in Z
150   double aDX = real(FeaturesPlugin_Translation::DX_ID())->value();
151   double aDY = real(FeaturesPlugin_Translation::DY_ID())->value();
152   double aDZ = real(FeaturesPlugin_Translation::DZ_ID())->value();
153
154   GeomTrsfPtr aTrsf(new GeomAPI_Trsf);
155   aTrsf->setTranslation(aDX, aDY, aDZ);
156   return aTrsf;
157 }
158
159 //=================================================================================================
160 GeomTrsfPtr FeaturesPlugin_Translation::translationByTwoPoints()
161 {
162   // Getting the start point and the end point
163   AttributeSelectionPtr aRef1 = data()->selection(FeaturesPlugin_Translation::START_POINT_ID());
164   AttributeSelectionPtr aRef2 = data()->selection(FeaturesPlugin_Translation::END_POINT_ID());
165   std::shared_ptr<GeomAPI_Pnt> aFirstPoint;
166   std::shared_ptr<GeomAPI_Pnt> aSecondPoint;
167   if ((aRef1.get() != NULL) && (aRef2.get() != NULL)) {
168     GeomShapePtr aShape1 = aRef1->value();
169     if (!aShape1.get()) //If we can't get the points directly, try getting them from the context
170       aShape1 = aRef1->context()->shape();
171     GeomShapePtr aShape2 = aRef2->value();
172     if (!aShape2.get())
173       aShape2 = aRef2->context()->shape();
174     if (aShape1 && aShape2) {
175       aFirstPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
176       aSecondPoint = GeomAlgoAPI_PointBuilder::point(aShape2);
177     }
178   }
179
180   GeomTrsfPtr aTrsf(new GeomAPI_Trsf);
181   if (aFirstPoint && aSecondPoint) {
182     aTrsf->setTranslation(aFirstPoint, aSecondPoint);
183   }
184   return aTrsf;
185 }
186
187 //=================================================================================================
188 void FeaturesPlugin_Translation::performTranslation(const GeomTrsfPtr& theTrsf)
189 {
190   if (!theTrsf) {
191     setError("Invalid transformation.");
192     return;
193   }
194
195   bool isKeepSubShapes = data()->version() == TRANSLATION_VERSION_1;
196
197   // Getting objects.
198   GeomAPI_ShapeHierarchy anObjects;
199   std::list<ResultPtr> aParts;
200   std::string theTextureFile;
201   AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECTS_LIST_ID());
202   if (!FeaturesPlugin_Tools::shapesFromSelectionList(
203        anObjectsSelList, isKeepSubShapes, anObjects, aParts, theTextureFile))
204     return;
205
206   std::string anError;
207   int aResultIndex = 0;
208   // Moving each part.
209   for (std::list<ResultPtr>::iterator aPRes = aParts.begin(); aPRes != aParts.end(); ++aPRes) {
210     ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aPRes);
211     ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
212     aResultPart->setTrsf(anOrigin, theTrsf);
213     setResult(aResultPart, aResultIndex++);
214   }
215
216   // Collect transformations for each object in a part.
217   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList);
218
219   for (GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
220     anObjectsIt != anObjects.end(); anObjectsIt++) {
221     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
222     std::shared_ptr<GeomAlgoAPI_Transform> aTransformAlgo(
223         new GeomAlgoAPI_Transform(aBaseShape, theTrsf));
224
225     // Checking that the algorithm worked properly.
226     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aTransformAlgo, getKind(), anError)) {
227       setError(anError);
228       break;
229     }
230
231     anObjects.markModified(aBaseShape, aTransformAlgo->shape());
232     aMakeShapeList->appendAlgo(aTransformAlgo);
233   }
234
235   // Build results of the operation.
236   const ListOfShape& anOriginalShapes = anObjects.objects();
237   ListOfShape aTopLevel;
238   anObjects.topLevelObjects(aTopLevel);
239   for (ListOfShape::iterator anIt = aTopLevel.begin(); anIt != aTopLevel.end(); ++anIt) {
240     //LoadNamingDS
241     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
242     FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, anOriginalShapes, ListOfShape(),
243                                              aMakeShapeList, *anIt, "Translated");
244     aResultBody->setTextureFile(theTextureFile);
245     setResult(aResultBody, aResultIndex++);
246   }
247
248   // Remove the rest results if there were produced in the previous pass.
249   removeResults(aResultIndex);
250 }