Salome HOME
import image: add texture to transformations
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Symmetry.cpp
1 // Copyright (C) 2014-2020  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_Symmetry.h>
21
22 #include <GeomAlgoAPI_CompoundBuilder.h>
23 #include <GeomAlgoAPI_PointBuilder.h>
24 #include <GeomAlgoAPI_FaceBuilder.h>
25 #include <GeomAlgoAPI_Copy.h>
26 #include <GeomAlgoAPI_MakeShapeList.h>
27 #include <GeomAlgoAPI_Tools.h>
28 #include <GeomAlgoAPI_Transform.h>
29
30 #include <GeomAPI_Ax1.h>
31 #include <GeomAPI_Ax2.h>
32 #include <GeomAPI_Edge.h>
33 #include <GeomAPI_Face.h>
34 #include <GeomAPI_Lin.h>
35 #include <GeomAPI_Pln.h>
36 #include <GeomAPI_ShapeIterator.h>
37 #include <GeomAPI_Trsf.h>
38
39 #include <ModelAPI_AttributeBoolean.h>
40 #include <ModelAPI_AttributeSelectionList.h>
41 #include <ModelAPI_AttributeString.h>
42 #include <ModelAPI_ResultBody.h>
43 #include <ModelAPI_ResultPart.h>
44
45 #include <FeaturesPlugin_Tools.h>
46
47 static const std::string SYMMETRY_VERSION_1("v9.5");
48
49 //=================================================================================================
50 FeaturesPlugin_Symmetry::FeaturesPlugin_Symmetry()
51 {
52 }
53
54 //=================================================================================================
55 void FeaturesPlugin_Symmetry::initAttributes()
56 {
57   data()->addAttribute(FeaturesPlugin_Symmetry::CREATION_METHOD(),
58                        ModelAPI_AttributeString::typeId());
59
60   AttributeSelectionListPtr aSelection =
61     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
62     FeaturesPlugin_Symmetry::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
63
64   data()->addAttribute(FeaturesPlugin_Symmetry::POINT_OBJECT_ID(),
65                        ModelAPI_AttributeSelection::typeId());
66
67   data()->addAttribute(FeaturesPlugin_Symmetry::AXIS_OBJECT_ID(),
68                        ModelAPI_AttributeSelection::typeId());
69
70   data()->addAttribute(FeaturesPlugin_Symmetry::PLANE_OBJECT_ID(),
71                        ModelAPI_AttributeSelection::typeId());
72
73   data()->addAttribute(FeaturesPlugin_Symmetry::KEEP_ORIGINAL_RESULT(),
74                        ModelAPI_AttributeBoolean::typeId());
75
76   if (!aSelection->isInitialized()) {
77     // new feature, not read from file
78     data()->setVersion(SYMMETRY_VERSION_1);
79   }
80 }
81
82 //=================================================================================================
83 void FeaturesPlugin_Symmetry::execute()
84 {
85   AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Symmetry::CREATION_METHOD());
86   std::string aMethodType = aMethodTypeAttr->value();
87
88   GeomTrsfPtr aTrsf;
89   if (aMethodType == CREATION_METHOD_BY_POINT())
90     aTrsf = symmetryByPoint();
91   else if (aMethodType == CREATION_METHOD_BY_AXIS())
92     aTrsf = symmetryByAxis();
93   else if (aMethodType == CREATION_METHOD_BY_PLANE())
94     aTrsf = symmetryByPlane();
95
96   performSymmetry(aTrsf);
97 }
98
99 //=================================================================================================
100 GeomTrsfPtr FeaturesPlugin_Symmetry::symmetryByPoint()
101 {
102   //Getting point.
103   std::shared_ptr<GeomAPI_Pnt> aPoint;
104   AttributeSelectionPtr anObjRef = selection(FeaturesPlugin_Symmetry::POINT_OBJECT_ID());
105   if (anObjRef.get() != NULL) {
106     GeomShapePtr aShape1 = anObjRef->value();
107     if (!aShape1.get()) {
108       aShape1 = anObjRef->context()->shape();
109     }
110     if (aShape1) {
111       aPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
112     }
113   }
114
115   GeomTrsfPtr aTrsf(new GeomAPI_Trsf);
116   aTrsf->setSymmetry(aPoint);
117   return aTrsf;
118 }
119
120 //=================================================================================================
121 GeomTrsfPtr FeaturesPlugin_Symmetry::symmetryByAxis()
122 {
123   //Getting axis.
124   static const std::string aSelectionError = "Error: The axis shape selection is bad.";
125   AttributeSelectionPtr anObjRef = selection(AXIS_OBJECT_ID());
126   GeomShapePtr aShape = anObjRef->value();
127   if (!aShape.get()) {
128     if (anObjRef->context().get()) {
129       aShape = anObjRef->context()->shape();
130     }
131   }
132   if (!aShape.get()) {
133     setError(aSelectionError);
134     return GeomTrsfPtr();
135   }
136
137   GeomEdgePtr anEdge;
138   if (aShape->isEdge())
139   {
140     anEdge = aShape->edge();
141   }
142   else if (aShape->isCompound())
143   {
144     GeomAPI_ShapeIterator anIt(aShape);
145     anEdge = anIt.current()->edge();
146   }
147
148   if (!anEdge.get())
149   {
150     setError(aSelectionError);
151     return GeomTrsfPtr();
152   }
153
154   std::shared_ptr<GeomAPI_Ax1> anAxis (new GeomAPI_Ax1(anEdge->line()->location(),
155                                                        anEdge->line()->direction()));
156   GeomTrsfPtr aTrsf(new GeomAPI_Trsf);
157   aTrsf->setSymmetry(anAxis);
158   return aTrsf;
159 }
160
161 //=================================================================================================
162 GeomTrsfPtr FeaturesPlugin_Symmetry::symmetryByPlane()
163 {
164   //Getting plane.
165   static const std::string aSelectionError = "Error: The plane shape selection is bad.";
166   AttributeSelectionPtr anObjRef = selection(PLANE_OBJECT_ID());
167   GeomShapePtr aShape = anObjRef->value();
168   if (!aShape.get()) {
169     if (anObjRef->context().get()) {
170       aShape = anObjRef->context()->shape();
171     }
172   }
173   if (!aShape.get()) {
174     setError(aSelectionError);
175     return GeomTrsfPtr();
176   }
177
178   GeomFacePtr aFace;
179   if (aShape->isFace())
180   {
181     aFace = aShape->face();
182   }
183   else if (aShape->isCompound())
184   {
185     GeomAPI_ShapeIterator anIt(aShape);
186     aFace = anIt.current()->face();
187   }
188
189   if (!aFace.get())
190   {
191     setError(aSelectionError);
192     return GeomTrsfPtr();
193   }
194
195   std::shared_ptr<GeomAPI_Ax2> aPlane(new GeomAPI_Ax2(aFace->getPlane()->location(),
196                                                       aFace->getPlane()->direction()));
197   GeomTrsfPtr aTrsf(new GeomAPI_Trsf);
198   aTrsf->setSymmetry(aPlane);
199   return aTrsf;
200 }
201
202 //=================================================================================================
203 void FeaturesPlugin_Symmetry::buildResult(
204     const std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theAlgo,
205     const std::list<std::shared_ptr<GeomAPI_Shape> >& theOriginalShapes,
206     std::shared_ptr<GeomAPI_Shape> theTargetShape, int& theResultIndex, std::string & theTextureFile)
207 {
208   // Store and name the result.
209   ResultBodyPtr aResultBody = document()->createBody(data(), theResultIndex);
210   FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, theOriginalShapes, ListOfShape(),
211                                            theAlgo, theTargetShape, "Symmetried");
212   aResultBody->setTextureFile(theTextureFile);
213   setResult(aResultBody, theResultIndex++);
214 }
215
216 //=================================================================================================
217 void FeaturesPlugin_Symmetry::buildResult(ResultPartPtr theOriginal,
218                                           std::shared_ptr<GeomAPI_Trsf> theTrsf,
219                                           int& theResultIndex)
220 {
221   if (boolean(KEEP_ORIGINAL_RESULT())->value()) {
222     std::shared_ptr<GeomAPI_Trsf> anIdentity(new GeomAPI_Trsf());
223     ResultPartPtr aCopy = document()->copyPart(theOriginal, data(), theResultIndex);
224     aCopy->setTrsf(theOriginal, anIdentity);
225     setResult(aCopy, theResultIndex++);
226   }
227
228   ResultPartPtr aResultPart = document()->copyPart(theOriginal, data(), theResultIndex);
229   aResultPart->setTrsf(theOriginal, theTrsf);
230   setResult(aResultPart, theResultIndex++);
231 }
232
233 //=================================================================================================
234 static bool performShapeSymmetry(std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theAlgoList,
235                                  GeomAPI_ShapeHierarchy& theHierarchy,
236                                  GeomShapePtr theBaseShape,
237                                  GeomTrsfPtr theTrsf,
238                                  bool isKeepOriginalResult,
239                                  const std::string& theFeatureKind,
240                                  std::string& theError)
241 {
242   std::shared_ptr<GeomAlgoAPI_Transform> aSymmetryAlgo(
243       new GeomAlgoAPI_Transform(theBaseShape, theTrsf));
244
245   // Checking that the algorithm worked properly.
246   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aSymmetryAlgo, theFeatureKind, theError))
247     return false;
248
249   theAlgoList->appendAlgo(aSymmetryAlgo);
250
251   // Compose source shape and the result of symmetry.
252   GeomShapePtr aCompound;
253   if (isKeepOriginalResult) {
254     ListOfShape aShapes;
255     // add a copy of a base shape otherwise selection of this base shape is bad (2592)
256     std::shared_ptr<GeomAlgoAPI_Copy> aCopyAlgo(new GeomAlgoAPI_Copy(theBaseShape));
257     aShapes.push_back(aCopyAlgo->shape());
258     theAlgoList->appendAlgo(aCopyAlgo);
259
260     aShapes.push_back(aSymmetryAlgo->shape());
261     aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
262   }
263   else
264     aCompound = aSymmetryAlgo->shape();
265
266   theHierarchy.markModified(theBaseShape, aCompound);
267   return true;
268 }
269
270 void FeaturesPlugin_Symmetry::performSymmetry(GeomTrsfPtr theTrsf)
271 {
272   if (!theTrsf) {
273     setError("Invalid transformation.");
274     return;
275   }
276
277   bool isKeepOriginal = boolean(KEEP_ORIGINAL_RESULT())->value();
278   bool isKeepSubShapes = data()->version() == SYMMETRY_VERSION_1;
279
280   // Getting objects.
281   GeomAPI_ShapeHierarchy anObjects;
282   std::list<ResultPtr> aParts;
283   std::string theTextureFile;
284   AttributeSelectionListPtr anObjSelList = selectionList(OBJECTS_LIST_ID());
285   if (!FeaturesPlugin_Tools::shapesFromSelectionList(
286        anObjSelList, isKeepSubShapes, anObjects, aParts, theTextureFile))
287     return;
288
289   std::string anError;
290   int aResultIndex = 0;
291   // Symmetrying parts.
292   for (std::list<ResultPtr>::iterator aPRes = aParts.begin(); aPRes != aParts.end(); ++aPRes) {
293     ResultPartPtr anOriginal = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aPRes);
294     buildResult(anOriginal, theTrsf, aResultIndex);
295   }
296
297   // Collect transformations for each object in a part.
298   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList);
299   for (GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
300        anObjectsIt != anObjects.end(); ++anObjectsIt) {
301     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
302     if (!performShapeSymmetry(aMakeShapeList, anObjects, aBaseShape, theTrsf,
303                               isKeepOriginal, getKind(), anError)) {
304       setError(anError);
305       break;
306     }
307   }
308
309   // Build results of the rotation.
310   const ListOfShape& anOriginalShapes = anObjects.objects();
311   ListOfShape aTopLevel;
312   anObjects.topLevelObjects(aTopLevel);
313   for (ListOfShape::iterator anIt = aTopLevel.begin(); anIt != aTopLevel.end(); ++anIt)
314     buildResult(aMakeShapeList, anOriginalShapes, *anIt, aResultIndex, theTextureFile);
315
316   // Remove the rest results if there were produced in the previous pass.
317   removeResults(aResultIndex);
318 }