Salome HOME
e877c4016b7f709a7db6a82c3cd2cacfd2b0f57a
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Symmetry.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_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,
207     std::string & theTextureFile)
208 {
209   // Store and name the result.
210   ResultBodyPtr aResultBody = document()->createBody(data(), theResultIndex);
211   FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, theOriginalShapes, ListOfShape(),
212                                            theAlgo, theTargetShape, "Symmetried");
213   aResultBody->setTextureFile(theTextureFile);
214   setResult(aResultBody, theResultIndex++);
215 }
216
217 //=================================================================================================
218 void FeaturesPlugin_Symmetry::buildResult(ResultPartPtr theOriginal,
219                                           std::shared_ptr<GeomAPI_Trsf> theTrsf,
220                                           int& theResultIndex)
221 {
222   if (boolean(KEEP_ORIGINAL_RESULT())->value()) {
223     std::shared_ptr<GeomAPI_Trsf> anIdentity(new GeomAPI_Trsf());
224     ResultPartPtr aCopy = document()->copyPart(theOriginal, data(), theResultIndex);
225     aCopy->setTrsf(theOriginal, anIdentity);
226     setResult(aCopy, theResultIndex++);
227   }
228
229   ResultPartPtr aResultPart = document()->copyPart(theOriginal, data(), theResultIndex);
230   aResultPart->setTrsf(theOriginal, theTrsf);
231   setResult(aResultPart, theResultIndex++);
232 }
233
234 //=================================================================================================
235 static bool performShapeSymmetry(std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theAlgoList,
236                                  GeomAPI_ShapeHierarchy& theHierarchy,
237                                  GeomShapePtr theBaseShape,
238                                  GeomTrsfPtr theTrsf,
239                                  bool isKeepOriginalResult,
240                                  const std::string& theFeatureKind,
241                                  std::string& theError)
242 {
243   std::shared_ptr<GeomAlgoAPI_Transform> aSymmetryAlgo(
244       new GeomAlgoAPI_Transform(theBaseShape, theTrsf));
245
246   // Checking that the algorithm worked properly.
247   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aSymmetryAlgo, theFeatureKind, theError))
248     return false;
249
250   theAlgoList->appendAlgo(aSymmetryAlgo);
251
252   // Compose source shape and the result of symmetry.
253   GeomShapePtr aCompound;
254   if (isKeepOriginalResult) {
255     ListOfShape aShapes;
256     // add a copy of a base shape otherwise selection of this base shape is bad (2592)
257     std::shared_ptr<GeomAlgoAPI_Copy> aCopyAlgo(new GeomAlgoAPI_Copy(theBaseShape));
258     aShapes.push_back(aCopyAlgo->shape());
259     theAlgoList->appendAlgo(aCopyAlgo);
260
261     aShapes.push_back(aSymmetryAlgo->shape());
262     aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
263   }
264   else
265     aCompound = aSymmetryAlgo->shape();
266
267   theHierarchy.markModified(theBaseShape, aCompound);
268   return true;
269 }
270
271 void FeaturesPlugin_Symmetry::performSymmetry(GeomTrsfPtr theTrsf)
272 {
273   if (!theTrsf) {
274     setError("Invalid transformation.");
275     return;
276   }
277
278   bool isKeepOriginal = boolean(KEEP_ORIGINAL_RESULT())->value();
279   bool isKeepSubShapes = data()->version() == SYMMETRY_VERSION_1;
280
281   // Getting objects.
282   GeomAPI_ShapeHierarchy anObjects;
283   std::list<ResultPtr> aParts;
284   std::string theTextureFile;
285   AttributeSelectionListPtr anObjSelList = selectionList(OBJECTS_LIST_ID());
286   if (!FeaturesPlugin_Tools::shapesFromSelectionList(
287        anObjSelList, isKeepSubShapes, anObjects, aParts, theTextureFile))
288     return;
289
290   std::string anError;
291   int aResultIndex = 0;
292   // Symmetrying parts.
293   for (std::list<ResultPtr>::iterator aPRes = aParts.begin(); aPRes != aParts.end(); ++aPRes) {
294     ResultPartPtr anOriginal = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aPRes);
295     buildResult(anOriginal, theTrsf, aResultIndex);
296   }
297
298   // Collect transformations for each object in a part.
299   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList);
300   for (GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
301        anObjectsIt != anObjects.end(); ++anObjectsIt) {
302     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
303     if (!performShapeSymmetry(aMakeShapeList, anObjects, aBaseShape, theTrsf,
304                               isKeepOriginal, getKind(), anError)) {
305       setError(anError);
306       break;
307     }
308   }
309
310   // Build results of the rotation.
311   const ListOfShape& anOriginalShapes = anObjects.objects();
312   ListOfShape aTopLevel;
313   anObjects.topLevelObjects(aTopLevel);
314   for (ListOfShape::iterator anIt = aTopLevel.begin(); anIt != aTopLevel.end(); ++anIt)
315     buildResult(aMakeShapeList, anOriginalShapes, *anIt, aResultIndex, theTextureFile);
316
317   // Remove the rest results if there were produced in the previous pass.
318   removeResults(aResultIndex);
319 }