1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <FeaturesPlugin_Symmetry.h>
23 #include <GeomAlgoAPI_CompoundBuilder.h>
24 #include <GeomAlgoAPI_PointBuilder.h>
25 #include <GeomAlgoAPI_FaceBuilder.h>
26 #include <GeomAlgoAPI_Copy.h>
27 #include <GeomAlgoAPI_MakeShapeList.h>
29 #include <GeomAPI_Edge.h>
30 #include <GeomAPI_Face.h>
31 #include <GeomAPI_Lin.h>
32 #include <GeomAPI_Pln.h>
33 #include <GeomAPI_ShapeIterator.h>
34 #include <GeomAPI_Trsf.h>
36 #include <ModelAPI_AttributeBoolean.h>
37 #include <ModelAPI_AttributeSelectionList.h>
38 #include <ModelAPI_AttributeString.h>
39 #include <ModelAPI_ResultBody.h>
40 #include <ModelAPI_ResultPart.h>
42 #include <FeaturesPlugin_Tools.h>
44 //=================================================================================================
45 FeaturesPlugin_Symmetry::FeaturesPlugin_Symmetry()
49 //=================================================================================================
50 void FeaturesPlugin_Symmetry::initAttributes()
52 data()->addAttribute(FeaturesPlugin_Symmetry::CREATION_METHOD(),
53 ModelAPI_AttributeString::typeId());
55 AttributeSelectionListPtr aSelection =
56 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
57 FeaturesPlugin_Symmetry::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
59 data()->addAttribute(FeaturesPlugin_Symmetry::POINT_OBJECT_ID(),
60 ModelAPI_AttributeSelection::typeId());
62 data()->addAttribute(FeaturesPlugin_Symmetry::AXIS_OBJECT_ID(),
63 ModelAPI_AttributeSelection::typeId());
65 data()->addAttribute(FeaturesPlugin_Symmetry::PLANE_OBJECT_ID(),
66 ModelAPI_AttributeSelection::typeId());
68 data()->addAttribute(FeaturesPlugin_Symmetry::KEEP_ORIGINAL_RESULT(),
69 ModelAPI_AttributeBoolean::typeId());
72 //=================================================================================================
73 void FeaturesPlugin_Symmetry::execute()
75 AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Symmetry::CREATION_METHOD());
76 std::string aMethodType = aMethodTypeAttr->value();
78 if (aMethodType == CREATION_METHOD_BY_POINT()) {
79 performSymmetryByPoint();
82 if (aMethodType == CREATION_METHOD_BY_AXIS()) {
83 performSymmetryByAxis();
86 if (aMethodType == CREATION_METHOD_BY_PLANE()) {
87 performSymmetryByPlane();
91 //=================================================================================================
92 bool FeaturesPlugin_Symmetry::collectSourceObjects(ListOfShape& theSourceShapes,
93 std::list<ResultPtr>& theSourceResults)
95 AttributeSelectionListPtr anObjectsSelList =
96 selectionList(FeaturesPlugin_Symmetry::OBJECTS_LIST_ID());
97 if (anObjectsSelList->size() == 0) {
100 for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
101 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
102 anObjectsSelList->value(anObjectsIndex);
103 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
104 if (!anObject.get()) { // may be for not-activated parts
108 theSourceShapes.push_back(anObject);
109 theSourceResults.push_back(anObjectAttr->context());
114 //=================================================================================================
115 void FeaturesPlugin_Symmetry::performSymmetryByPoint()
118 ListOfShape anObjects;
119 std::list<ResultPtr> aContextes;
120 if (!collectSourceObjects(anObjects, aContextes))
124 std::shared_ptr<GeomAPI_Pnt> aPoint;
125 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
126 selection(FeaturesPlugin_Symmetry::POINT_OBJECT_ID());
127 if (anObjRef.get() != NULL) {
128 GeomShapePtr aShape1 = anObjRef->value();
129 if (!aShape1.get()) {
130 aShape1 = anObjRef->context()->shape();
133 aPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
137 // Moving each object.
138 int aResultIndex = 0;
139 std::list<ResultPtr>::iterator aContext = aContextes.begin();
140 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
141 anObjectsIt++, aContext++) {
142 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
143 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
147 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
148 aTrsf->setSymmetry(aPoint);
149 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
150 buildResult(anOrigin, aTrsf, aResultIndex);
153 std::shared_ptr<GeomAlgoAPI_Symmetry> aSymmetryAlgo(
154 new GeomAlgoAPI_Symmetry(aBaseShape, aPoint));
156 if (!aSymmetryAlgo->check()) {
157 setError(aSymmetryAlgo->getError());
161 aSymmetryAlgo->build();
163 // Checking that the algorithm worked properly.
164 if(!aSymmetryAlgo->isDone()) {
165 static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
166 setError(aFeatureError);
169 if(aSymmetryAlgo->shape()->isNull()) {
170 static const std::string aShapeError = "Error: Resulting shape is Null.";
171 setError(aShapeError);
174 if(!aSymmetryAlgo->isValid()) {
175 std::string aFeatureError = "Error: Resulting shape is not valid.";
176 setError(aFeatureError);
180 buildResult(aSymmetryAlgo, aBaseShape, aResultIndex);
185 // Remove the rest results if there were produced in the previous pass.
186 removeResults(aResultIndex);
189 //=================================================================================================
190 void FeaturesPlugin_Symmetry::performSymmetryByAxis()
193 ListOfShape anObjects;
194 std::list<ResultPtr> aContextes;
195 if (!collectSourceObjects(anObjects, aContextes))
199 static const std::string aSelectionError = "Error: The axis shape selection is bad.";
200 AttributeSelectionPtr anObjRef = selection(AXIS_OBJECT_ID());
201 GeomShapePtr aShape = anObjRef->value();
203 if (anObjRef->context().get()) {
204 aShape = anObjRef->context()->shape();
208 setError(aSelectionError);
213 if (aShape->isEdge())
215 anEdge = aShape->edge();
217 else if (aShape->isCompound())
219 GeomAPI_ShapeIterator anIt(aShape);
220 anEdge = anIt.current()->edge();
224 setError(aSelectionError);
230 setError(aSelectionError);
234 std::shared_ptr<GeomAPI_Ax1> anAxis (new GeomAPI_Ax1(anEdge->line()->location(),
235 anEdge->line()->direction()));
238 // Moving each object.
239 int aResultIndex = 0;
240 std::list<ResultPtr>::iterator aContext = aContextes.begin();
241 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
242 anObjectsIt++, aContext++) {
243 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
244 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
248 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
249 aTrsf->setSymmetry(anAxis);
250 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
251 buildResult(anOrigin, aTrsf, aResultIndex);
254 std::shared_ptr<GeomAlgoAPI_Symmetry> aSymmetryAlgo(
255 new GeomAlgoAPI_Symmetry(aBaseShape, anAxis));
257 if (!aSymmetryAlgo->check()) {
258 setError(aSymmetryAlgo->getError());
262 aSymmetryAlgo->build();
264 // Checking that the algorithm worked properly.
265 if(!aSymmetryAlgo->isDone()) {
266 static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
267 setError(aFeatureError);
270 if(aSymmetryAlgo->shape()->isNull()) {
271 static const std::string aShapeError = "Error: Resulting shape is Null.";
272 setError(aShapeError);
275 if(!aSymmetryAlgo->isValid()) {
276 std::string aFeatureError = "Error: Resulting shape is not valid.";
277 setError(aFeatureError);
281 buildResult(aSymmetryAlgo, aBaseShape, aResultIndex);
286 // Remove the rest results if there were produced in the previous pass.
287 removeResults(aResultIndex);
290 //=================================================================================================
291 void FeaturesPlugin_Symmetry::performSymmetryByPlane()
294 ListOfShape anObjects;
295 std::list<ResultPtr> aContextes;
296 if (!collectSourceObjects(anObjects, aContextes))
300 static const std::string aSelectionError = "Error: The plane shape selection is bad.";
301 AttributeSelectionPtr anObjRef = selection(PLANE_OBJECT_ID());
302 GeomShapePtr aShape = anObjRef->value();
304 if (anObjRef->context().get()) {
305 aShape = anObjRef->context()->shape();
309 setError(aSelectionError);
314 if (aShape->isFace())
316 aFace = aShape->face();
318 else if (aShape->isCompound())
320 GeomAPI_ShapeIterator anIt(aShape);
321 aFace = anIt.current()->face();
325 setError(aSelectionError);
331 setError(aSelectionError);
335 std::shared_ptr<GeomAPI_Ax2> aPlane(new GeomAPI_Ax2(aFace->getPlane()->location(),
336 aFace->getPlane()->direction()));
339 // Moving each object.
340 int aResultIndex = 0;
341 std::list<ResultPtr>::iterator aContext = aContextes.begin();
342 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
343 anObjectsIt++, aContext++) {
344 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
345 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
349 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
350 aTrsf->setSymmetry(aPlane);
351 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
352 buildResult(anOrigin, aTrsf, aResultIndex);
354 std::shared_ptr<GeomAlgoAPI_Symmetry> aSymmetryAlgo(
355 new GeomAlgoAPI_Symmetry(aBaseShape, aPlane));
357 if (!aSymmetryAlgo->check()) {
358 setError(aSymmetryAlgo->getError());
362 aSymmetryAlgo->build();
364 // Checking that the algorithm worked properly.
365 if(!aSymmetryAlgo->isDone()) {
366 static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
367 setError(aFeatureError);
370 if(aSymmetryAlgo->shape()->isNull()) {
371 static const std::string aShapeError = "Error: Resulting shape is Null.";
372 setError(aShapeError);
375 if(!aSymmetryAlgo->isValid()) {
376 std::string aFeatureError = "Error: Resulting shape is not valid.";
377 setError(aFeatureError);
381 buildResult(aSymmetryAlgo, aBaseShape, aResultIndex);
386 // Remove the rest results if there were produced in the previous pass.
387 removeResults(aResultIndex);
390 //=================================================================================================
391 void FeaturesPlugin_Symmetry::buildResult(
392 std::shared_ptr<GeomAlgoAPI_Symmetry>& theSymmetryAlgo,
393 std::shared_ptr<GeomAPI_Shape> theBaseShape, int theResultIndex)
395 GeomAlgoAPI_MakeShapeList anAlgoList;
396 anAlgoList.appendAlgo(theSymmetryAlgo);
397 // Compose source shape and the result of symmetry.
398 GeomShapePtr aCompound;
399 if (boolean(KEEP_ORIGINAL_RESULT())->value()) {
401 // add a copy of a base shape otherwise selection of this base shape is bad (2592)
402 std::shared_ptr<GeomAlgoAPI_Copy> aCopyAlgo(new GeomAlgoAPI_Copy(theBaseShape));
403 aShapes.push_back(aCopyAlgo->shape());
404 anAlgoList.appendAlgo(aCopyAlgo);
406 aShapes.push_back(theSymmetryAlgo->shape());
407 aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
409 aCompound = theSymmetryAlgo->shape();
411 // Store and name the result.
412 ResultBodyPtr aResultBody = document()->createBody(data(), theResultIndex);
413 aResultBody->storeModified(theBaseShape, aCompound);
414 loadNamingDS(anAlgoList, aResultBody, theBaseShape);
415 setResult(aResultBody, theResultIndex);
418 //=================================================================================================
419 void FeaturesPlugin_Symmetry::buildResult(ResultPartPtr theOriginal,
420 std::shared_ptr<GeomAPI_Trsf> theTrsf,
423 if (boolean(KEEP_ORIGINAL_RESULT())->value()) {
424 std::shared_ptr<GeomAPI_Trsf> anIdentity(new GeomAPI_Trsf());
425 ResultPartPtr aCopy = document()->copyPart(theOriginal, data(), theResultIndex);
426 aCopy->setTrsf(theOriginal, anIdentity);
427 setResult(aCopy, theResultIndex);
431 ResultPartPtr aResultPart = document()->copyPart(theOriginal, data(), theResultIndex);
432 aResultPart->setTrsf(theOriginal, theTrsf);
433 setResult(aResultPart, theResultIndex);
436 //=================================================================================================
437 void FeaturesPlugin_Symmetry::loadNamingDS(GeomAlgoAPI_MakeShapeList& theAlgo,
438 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
439 std::shared_ptr<GeomAPI_Shape> theBaseShape)
442 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theAlgo.mapOfSubShapes();
443 std::string aReflectedName = "Symmetried";
444 FeaturesPlugin_Tools::storeModifiedShapes(theAlgo, theResultBody,
445 theBaseShape, 1, 2, 3, aReflectedName,