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>
28 #include <GeomAlgoAPI_Tools.h>
30 #include <GeomAPI_Edge.h>
31 #include <GeomAPI_Face.h>
32 #include <GeomAPI_Lin.h>
33 #include <GeomAPI_Pln.h>
34 #include <GeomAPI_ShapeIterator.h>
35 #include <GeomAPI_Trsf.h>
37 #include <ModelAPI_AttributeBoolean.h>
38 #include <ModelAPI_AttributeSelectionList.h>
39 #include <ModelAPI_AttributeString.h>
40 #include <ModelAPI_ResultBody.h>
41 #include <ModelAPI_ResultPart.h>
43 #include <FeaturesPlugin_Tools.h>
45 //=================================================================================================
46 FeaturesPlugin_Symmetry::FeaturesPlugin_Symmetry()
50 //=================================================================================================
51 void FeaturesPlugin_Symmetry::initAttributes()
53 data()->addAttribute(FeaturesPlugin_Symmetry::CREATION_METHOD(),
54 ModelAPI_AttributeString::typeId());
56 AttributeSelectionListPtr aSelection =
57 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
58 FeaturesPlugin_Symmetry::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
60 data()->addAttribute(FeaturesPlugin_Symmetry::POINT_OBJECT_ID(),
61 ModelAPI_AttributeSelection::typeId());
63 data()->addAttribute(FeaturesPlugin_Symmetry::AXIS_OBJECT_ID(),
64 ModelAPI_AttributeSelection::typeId());
66 data()->addAttribute(FeaturesPlugin_Symmetry::PLANE_OBJECT_ID(),
67 ModelAPI_AttributeSelection::typeId());
69 data()->addAttribute(FeaturesPlugin_Symmetry::KEEP_ORIGINAL_RESULT(),
70 ModelAPI_AttributeBoolean::typeId());
73 //=================================================================================================
74 void FeaturesPlugin_Symmetry::execute()
76 AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Symmetry::CREATION_METHOD());
77 std::string aMethodType = aMethodTypeAttr->value();
79 if (aMethodType == CREATION_METHOD_BY_POINT()) {
80 performSymmetryByPoint();
83 if (aMethodType == CREATION_METHOD_BY_AXIS()) {
84 performSymmetryByAxis();
87 if (aMethodType == CREATION_METHOD_BY_PLANE()) {
88 performSymmetryByPlane();
92 //=================================================================================================
93 bool FeaturesPlugin_Symmetry::collectSourceObjects(ListOfShape& theSourceShapes,
94 std::list<ResultPtr>& theSourceResults)
96 AttributeSelectionListPtr anObjectsSelList =
97 selectionList(FeaturesPlugin_Symmetry::OBJECTS_LIST_ID());
98 if (anObjectsSelList->size() == 0) {
101 for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
102 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
103 anObjectsSelList->value(anObjectsIndex);
104 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
105 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.
139 int aResultIndex = 0;
140 std::list<ResultPtr>::iterator aContext = aContextes.begin();
141 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
142 anObjectsIt++, aContext++) {
143 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
144 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
148 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
149 aTrsf->setSymmetry(aPoint);
150 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
151 buildResult(anOrigin, aTrsf, aResultIndex);
154 std::shared_ptr<GeomAlgoAPI_Symmetry> aSymmetryAlgo(
155 new GeomAlgoAPI_Symmetry(aBaseShape, aPoint));
157 if (!aSymmetryAlgo->check()) {
158 setError(aSymmetryAlgo->getError());
162 aSymmetryAlgo->build();
164 // Checking that the algorithm worked properly.
165 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aSymmetryAlgo, getKind(), anError)) {
170 buildResult(aSymmetryAlgo, aBaseShape, aResultIndex);
175 // Remove the rest results if there were produced in the previous pass.
176 removeResults(aResultIndex);
179 //=================================================================================================
180 void FeaturesPlugin_Symmetry::performSymmetryByAxis()
183 ListOfShape anObjects;
184 std::list<ResultPtr> aContextes;
185 if (!collectSourceObjects(anObjects, aContextes))
189 static const std::string aSelectionError = "Error: The axis shape selection is bad.";
190 AttributeSelectionPtr anObjRef = selection(AXIS_OBJECT_ID());
191 GeomShapePtr aShape = anObjRef->value();
193 if (anObjRef->context().get()) {
194 aShape = anObjRef->context()->shape();
198 setError(aSelectionError);
203 if (aShape->isEdge())
205 anEdge = aShape->edge();
207 else if (aShape->isCompound())
209 GeomAPI_ShapeIterator anIt(aShape);
210 anEdge = anIt.current()->edge();
215 setError(aSelectionError);
219 std::shared_ptr<GeomAPI_Ax1> anAxis (new GeomAPI_Ax1(anEdge->line()->location(),
220 anEdge->line()->direction()));
223 // Moving each object.
225 int aResultIndex = 0;
226 std::list<ResultPtr>::iterator aContext = aContextes.begin();
227 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
228 anObjectsIt++, aContext++) {
229 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
230 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
234 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
235 aTrsf->setSymmetry(anAxis);
236 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
237 buildResult(anOrigin, aTrsf, aResultIndex);
240 std::shared_ptr<GeomAlgoAPI_Symmetry> aSymmetryAlgo(
241 new GeomAlgoAPI_Symmetry(aBaseShape, anAxis));
243 if (!aSymmetryAlgo->check()) {
244 setError(aSymmetryAlgo->getError());
248 aSymmetryAlgo->build();
250 // Checking that the algorithm worked properly.
251 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aSymmetryAlgo, getKind(), anError)) {
256 buildResult(aSymmetryAlgo, aBaseShape, aResultIndex);
261 // Remove the rest results if there were produced in the previous pass.
262 removeResults(aResultIndex);
265 //=================================================================================================
266 void FeaturesPlugin_Symmetry::performSymmetryByPlane()
269 ListOfShape anObjects;
270 std::list<ResultPtr> aContextes;
271 if (!collectSourceObjects(anObjects, aContextes))
275 static const std::string aSelectionError = "Error: The plane shape selection is bad.";
276 AttributeSelectionPtr anObjRef = selection(PLANE_OBJECT_ID());
277 GeomShapePtr aShape = anObjRef->value();
279 if (anObjRef->context().get()) {
280 aShape = anObjRef->context()->shape();
284 setError(aSelectionError);
289 if (aShape->isFace())
291 aFace = aShape->face();
293 else if (aShape->isCompound())
295 GeomAPI_ShapeIterator anIt(aShape);
296 aFace = anIt.current()->face();
301 setError(aSelectionError);
305 std::shared_ptr<GeomAPI_Ax2> aPlane(new GeomAPI_Ax2(aFace->getPlane()->location(),
306 aFace->getPlane()->direction()));
309 // Moving each object.
311 int aResultIndex = 0;
312 std::list<ResultPtr>::iterator aContext = aContextes.begin();
313 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
314 anObjectsIt++, aContext++) {
315 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
316 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
320 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
321 aTrsf->setSymmetry(aPlane);
322 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
323 buildResult(anOrigin, aTrsf, aResultIndex);
325 std::shared_ptr<GeomAlgoAPI_Symmetry> aSymmetryAlgo(
326 new GeomAlgoAPI_Symmetry(aBaseShape, aPlane));
328 if (!aSymmetryAlgo->check()) {
329 setError(aSymmetryAlgo->getError());
333 aSymmetryAlgo->build();
335 // Checking that the algorithm worked properly.
336 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aSymmetryAlgo, getKind(), anError)) {
341 buildResult(aSymmetryAlgo, aBaseShape, aResultIndex);
346 // Remove the rest results if there were produced in the previous pass.
347 removeResults(aResultIndex);
350 //=================================================================================================
351 void FeaturesPlugin_Symmetry::buildResult(
352 std::shared_ptr<GeomAlgoAPI_Symmetry>& theSymmetryAlgo,
353 std::shared_ptr<GeomAPI_Shape> theBaseShape, int theResultIndex)
355 std::shared_ptr<GeomAlgoAPI_MakeShapeList> anAlgoList(new GeomAlgoAPI_MakeShapeList());
356 anAlgoList->appendAlgo(theSymmetryAlgo);
357 // Compose source shape and the result of symmetry.
358 GeomShapePtr aCompound;
359 if (boolean(KEEP_ORIGINAL_RESULT())->value()) {
361 // add a copy of a base shape otherwise selection of this base shape is bad (2592)
362 std::shared_ptr<GeomAlgoAPI_Copy> aCopyAlgo(new GeomAlgoAPI_Copy(theBaseShape));
363 aShapes.push_back(aCopyAlgo->shape());
364 anAlgoList->appendAlgo(aCopyAlgo);
366 aShapes.push_back(theSymmetryAlgo->shape());
367 aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
369 aCompound = theSymmetryAlgo->shape();
371 // Store and name the result.
372 ResultBodyPtr aResultBody = document()->createBody(data(), theResultIndex);
373 aResultBody->storeModified(theBaseShape, aCompound);
374 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, theBaseShape, anAlgoList, "Symmetried");
375 setResult(aResultBody, theResultIndex);
378 //=================================================================================================
379 void FeaturesPlugin_Symmetry::buildResult(ResultPartPtr theOriginal,
380 std::shared_ptr<GeomAPI_Trsf> theTrsf,
383 if (boolean(KEEP_ORIGINAL_RESULT())->value()) {
384 std::shared_ptr<GeomAPI_Trsf> anIdentity(new GeomAPI_Trsf());
385 ResultPartPtr aCopy = document()->copyPart(theOriginal, data(), theResultIndex);
386 aCopy->setTrsf(theOriginal, anIdentity);
387 setResult(aCopy, theResultIndex);
391 ResultPartPtr aResultPart = document()->copyPart(theOriginal, data(), theResultIndex);
392 aResultPart->setTrsf(theOriginal, theTrsf);
393 setResult(aResultPart, theResultIndex);