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
107 theSourceShapes.push_back(anObject);
108 theSourceResults.push_back(anObjectAttr->context());
113 //=================================================================================================
114 void FeaturesPlugin_Symmetry::performSymmetryByPoint()
117 ListOfShape anObjects;
118 std::list<ResultPtr> aContextes;
119 if (!collectSourceObjects(anObjects, aContextes))
123 std::shared_ptr<GeomAPI_Pnt> aPoint;
124 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
125 selection(FeaturesPlugin_Symmetry::POINT_OBJECT_ID());
126 if (anObjRef.get() != NULL) {
127 GeomShapePtr aShape1 = anObjRef->value();
128 if (!aShape1.get()) {
129 aShape1 = anObjRef->context()->shape();
132 aPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
136 // Moving each object.
137 int aResultIndex = 0;
138 std::list<ResultPtr>::iterator aContext = aContextes.begin();
139 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
140 anObjectsIt++, aContext++) {
141 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
142 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
146 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
147 aTrsf->setSymmetry(aPoint);
148 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
149 buildResult(anOrigin, aTrsf, aResultIndex);
152 std::shared_ptr<GeomAlgoAPI_Symmetry> aSymmetryAlgo(
153 new GeomAlgoAPI_Symmetry(aBaseShape, aPoint));
155 if (!aSymmetryAlgo->check()) {
156 setError(aSymmetryAlgo->getError());
160 aSymmetryAlgo->build();
162 // Checking that the algorithm worked properly.
163 if(!aSymmetryAlgo->isDone()) {
164 static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
165 setError(aFeatureError);
168 if(aSymmetryAlgo->shape()->isNull()) {
169 static const std::string aShapeError = "Error: Resulting shape is Null.";
170 setError(aShapeError);
173 if(!aSymmetryAlgo->isValid()) {
174 std::string aFeatureError = "Error: Resulting shape is not valid.";
175 setError(aFeatureError);
179 buildResult(aSymmetryAlgo, aBaseShape, aResultIndex);
184 // Remove the rest results if there were produced in the previous pass.
185 removeResults(aResultIndex);
188 //=================================================================================================
189 void FeaturesPlugin_Symmetry::performSymmetryByAxis()
192 ListOfShape anObjects;
193 std::list<ResultPtr> aContextes;
194 if (!collectSourceObjects(anObjects, aContextes))
198 static const std::string aSelectionError = "Error: The axis shape selection is bad.";
199 AttributeSelectionPtr anObjRef = selection(AXIS_OBJECT_ID());
200 GeomShapePtr aShape = anObjRef->value();
202 if (anObjRef->context().get()) {
203 aShape = anObjRef->context()->shape();
207 setError(aSelectionError);
212 if (aShape->isEdge())
214 anEdge = aShape->edge();
216 else if (aShape->isCompound())
218 GeomAPI_ShapeIterator anIt(aShape);
219 anEdge = anIt.current()->edge();
224 setError(aSelectionError);
228 std::shared_ptr<GeomAPI_Ax1> anAxis (new GeomAPI_Ax1(anEdge->line()->location(),
229 anEdge->line()->direction()));
232 // Moving each object.
233 int aResultIndex = 0;
234 std::list<ResultPtr>::iterator aContext = aContextes.begin();
235 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
236 anObjectsIt++, aContext++) {
237 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
238 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
242 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
243 aTrsf->setSymmetry(anAxis);
244 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
245 buildResult(anOrigin, aTrsf, aResultIndex);
248 std::shared_ptr<GeomAlgoAPI_Symmetry> aSymmetryAlgo(
249 new GeomAlgoAPI_Symmetry(aBaseShape, anAxis));
251 if (!aSymmetryAlgo->check()) {
252 setError(aSymmetryAlgo->getError());
256 aSymmetryAlgo->build();
258 // Checking that the algorithm worked properly.
259 if(!aSymmetryAlgo->isDone()) {
260 static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
261 setError(aFeatureError);
264 if(aSymmetryAlgo->shape()->isNull()) {
265 static const std::string aShapeError = "Error: Resulting shape is Null.";
266 setError(aShapeError);
269 if(!aSymmetryAlgo->isValid()) {
270 std::string aFeatureError = "Error: Resulting shape is not valid.";
271 setError(aFeatureError);
275 buildResult(aSymmetryAlgo, aBaseShape, aResultIndex);
280 // Remove the rest results if there were produced in the previous pass.
281 removeResults(aResultIndex);
284 //=================================================================================================
285 void FeaturesPlugin_Symmetry::performSymmetryByPlane()
288 ListOfShape anObjects;
289 std::list<ResultPtr> aContextes;
290 if (!collectSourceObjects(anObjects, aContextes))
294 static const std::string aSelectionError = "Error: The plane shape selection is bad.";
295 AttributeSelectionPtr anObjRef = selection(PLANE_OBJECT_ID());
296 GeomShapePtr aShape = anObjRef->value();
298 if (anObjRef->context().get()) {
299 aShape = anObjRef->context()->shape();
303 setError(aSelectionError);
308 if (aShape->isFace())
310 aFace = aShape->face();
312 else if (aShape->isCompound())
314 GeomAPI_ShapeIterator anIt(aShape);
315 aFace = anIt.current()->face();
320 setError(aSelectionError);
324 std::shared_ptr<GeomAPI_Ax2> aPlane(new GeomAPI_Ax2(aFace->getPlane()->location(),
325 aFace->getPlane()->direction()));
328 // Moving each object.
329 int aResultIndex = 0;
330 std::list<ResultPtr>::iterator aContext = aContextes.begin();
331 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
332 anObjectsIt++, aContext++) {
333 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
334 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
338 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
339 aTrsf->setSymmetry(aPlane);
340 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
341 buildResult(anOrigin, aTrsf, aResultIndex);
343 std::shared_ptr<GeomAlgoAPI_Symmetry> aSymmetryAlgo(
344 new GeomAlgoAPI_Symmetry(aBaseShape, aPlane));
346 if (!aSymmetryAlgo->check()) {
347 setError(aSymmetryAlgo->getError());
351 aSymmetryAlgo->build();
353 // Checking that the algorithm worked properly.
354 if(!aSymmetryAlgo->isDone()) {
355 static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
356 setError(aFeatureError);
359 if(aSymmetryAlgo->shape()->isNull()) {
360 static const std::string aShapeError = "Error: Resulting shape is Null.";
361 setError(aShapeError);
364 if(!aSymmetryAlgo->isValid()) {
365 std::string aFeatureError = "Error: Resulting shape is not valid.";
366 setError(aFeatureError);
370 buildResult(aSymmetryAlgo, aBaseShape, aResultIndex);
375 // Remove the rest results if there were produced in the previous pass.
376 removeResults(aResultIndex);
379 //=================================================================================================
380 void FeaturesPlugin_Symmetry::buildResult(
381 std::shared_ptr<GeomAlgoAPI_Symmetry>& theSymmetryAlgo,
382 std::shared_ptr<GeomAPI_Shape> theBaseShape, int theResultIndex)
384 std::shared_ptr<GeomAlgoAPI_MakeShapeList> anAlgoList(new GeomAlgoAPI_MakeShapeList());
385 anAlgoList->appendAlgo(theSymmetryAlgo);
386 // Compose source shape and the result of symmetry.
387 GeomShapePtr aCompound;
388 if (boolean(KEEP_ORIGINAL_RESULT())->value()) {
390 // add a copy of a base shape otherwise selection of this base shape is bad (2592)
391 std::shared_ptr<GeomAlgoAPI_Copy> aCopyAlgo(new GeomAlgoAPI_Copy(theBaseShape));
392 aShapes.push_back(aCopyAlgo->shape());
393 anAlgoList->appendAlgo(aCopyAlgo);
395 aShapes.push_back(theSymmetryAlgo->shape());
396 aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
398 aCompound = theSymmetryAlgo->shape();
400 // Store and name the result.
401 ResultBodyPtr aResultBody = document()->createBody(data(), theResultIndex);
402 aResultBody->storeModified(theBaseShape, aCompound);
403 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, theBaseShape, anAlgoList, "Symmetried");
404 setResult(aResultBody, theResultIndex);
407 //=================================================================================================
408 void FeaturesPlugin_Symmetry::buildResult(ResultPartPtr theOriginal,
409 std::shared_ptr<GeomAPI_Trsf> theTrsf,
412 if (boolean(KEEP_ORIGINAL_RESULT())->value()) {
413 std::shared_ptr<GeomAPI_Trsf> anIdentity(new GeomAPI_Trsf());
414 ResultPartPtr aCopy = document()->copyPart(theOriginal, data(), theResultIndex);
415 aCopy->setTrsf(theOriginal, anIdentity);
416 setResult(aCopy, theResultIndex);
420 ResultPartPtr aResultPart = document()->copyPart(theOriginal, data(), theResultIndex);
421 aResultPart->setTrsf(theOriginal, theTrsf);
422 setResult(aResultPart, theResultIndex);