1 // Copyright (C) 2014-2016 CEA/DEN, EDF R&D
3 // File: FeaturesPlugin_Symmetry.cpp
4 // Created: 30 Nov 2016
5 // Author: Clarisse Genrault (CEA)
7 #include <FeaturesPlugin_Symmetry.h>
9 #include <GeomAlgoAPI_PointBuilder.h>
11 #include <GeomAPI_Edge.h>
12 #include <GeomAPI_Face.h>
13 #include <GeomAPI_Lin.h>
14 #include <GeomAPI_Pln.h>
16 #include <ModelAPI_AttributeSelectionList.h>
17 #include <ModelAPI_AttributeString.h>
18 #include <ModelAPI_ResultBody.h>
19 #include <ModelAPI_ResultPart.h>
22 #include <GeomAlgoAPI_FaceBuilder.h>
24 //=================================================================================================
25 FeaturesPlugin_Symmetry::FeaturesPlugin_Symmetry()
29 //=================================================================================================
30 void FeaturesPlugin_Symmetry::initAttributes()
32 data()->addAttribute(FeaturesPlugin_Symmetry::CREATION_METHOD(),
33 ModelAPI_AttributeString::typeId());
35 AttributeSelectionListPtr aSelection =
36 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
37 FeaturesPlugin_Symmetry::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
39 data()->addAttribute(FeaturesPlugin_Symmetry::POINT_OBJECT_ID(),
40 ModelAPI_AttributeSelection::typeId());
42 data()->addAttribute(FeaturesPlugin_Symmetry::AXIS_OBJECT_ID(),
43 ModelAPI_AttributeSelection::typeId());
45 data()->addAttribute(FeaturesPlugin_Symmetry::PLANE_OBJECT_ID(),
46 ModelAPI_AttributeSelection::typeId());
49 //=================================================================================================
50 void FeaturesPlugin_Symmetry::execute()
52 AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Symmetry::CREATION_METHOD());
53 std::string aMethodType = aMethodTypeAttr->value();
55 if (aMethodType == CREATION_METHOD_BY_POINT()) {
56 performSymmetryByPoint();
59 if (aMethodType == CREATION_METHOD_BY_AXIS()) {
60 performSymmetryByAxis();
63 if (aMethodType == CREATION_METHOD_BY_PLANE()) {
64 performSymmetryByPlane();
68 //=================================================================================================
69 void FeaturesPlugin_Symmetry::performSymmetryByPoint()
72 ListOfShape anObjects;
73 std::list<ResultPtr> aContextes;
74 AttributeSelectionListPtr anObjectsSelList =
75 selectionList(FeaturesPlugin_Symmetry::OBJECTS_LIST_ID());
76 if (anObjectsSelList->size() == 0) {
79 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
80 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
81 anObjectsSelList->value(anObjectsIndex);
82 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
83 if(!anObject.get()) { // may be for not-activated parts
87 anObjects.push_back(anObject);
88 aContextes.push_back(anObjectAttr->context());
92 std::shared_ptr<GeomAPI_Pnt> aPoint;
93 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
94 selection(FeaturesPlugin_Symmetry::POINT_OBJECT_ID());
95 if (anObjRef.get() != NULL) {
96 GeomShapePtr aShape1 = anObjRef->value();
98 aShape1 = anObjRef->context()->shape();
101 aPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
105 // Moving each object.
106 int aResultIndex = 0;
107 std::list<ResultPtr>::iterator aContext = aContextes.begin();
108 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
109 anObjectsIt++, aContext++) {
110 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
111 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
115 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
116 aTrsf->setSymmetry(aPoint);
117 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
118 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
119 aResultPart->setTrsf(*aContext, aTrsf);
120 setResult(aResultPart, aResultIndex);
122 GeomAlgoAPI_Symmetry aSymmetryAlgo(aBaseShape, aPoint);
124 if (!aSymmetryAlgo.check()) {
125 setError(aSymmetryAlgo.getError());
129 aSymmetryAlgo.build();
131 // Checking that the algorithm worked properly.
132 if(!aSymmetryAlgo.isDone()) {
133 static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
134 setError(aFeatureError);
137 if(aSymmetryAlgo.shape()->isNull()) {
138 static const std::string aShapeError = "Error: Resulting shape is Null.";
139 setError(aShapeError);
142 if(!aSymmetryAlgo.isValid()) {
143 std::string aFeatureError = "Error: Resulting shape is not valid.";
144 setError(aFeatureError);
148 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
149 loadNamingDS(aSymmetryAlgo, aResultBody, aBaseShape);
150 setResult(aResultBody, aResultIndex);
155 // Remove the rest results if there were produced in the previous pass.
156 removeResults(aResultIndex);
159 //=================================================================================================
160 void FeaturesPlugin_Symmetry::performSymmetryByAxis()
163 ListOfShape anObjects;
164 std::list<ResultPtr> aContextes;
165 AttributeSelectionListPtr anObjectsSelList =
166 selectionList(FeaturesPlugin_Symmetry::OBJECTS_LIST_ID());
167 if (anObjectsSelList->size() == 0) {
170 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
171 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
172 anObjectsSelList->value(anObjectsIndex);
173 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
174 if(!anObject.get()) { // may be for not-activated parts
178 anObjects.push_back(anObject);
179 aContextes.push_back(anObjectAttr->context());
183 std::shared_ptr<GeomAPI_Ax1> anAxis;
184 std::shared_ptr<GeomAPI_Edge> anEdge;
185 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
186 selection(FeaturesPlugin_Symmetry::AXIS_OBJECT_ID());
187 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
188 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
189 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
190 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
191 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
194 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
195 anEdge->line()->direction()));
198 // Moving each object.
199 int aResultIndex = 0;
200 std::list<ResultPtr>::iterator aContext = aContextes.begin();
201 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
202 anObjectsIt++, aContext++) {
203 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
204 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
208 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
209 aTrsf->setSymmetry(anAxis);
210 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
211 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
212 aResultPart->setTrsf(*aContext, aTrsf);
213 setResult(aResultPart, aResultIndex);
215 GeomAlgoAPI_Symmetry aSymmetryAlgo(aBaseShape, anAxis);
217 if (!aSymmetryAlgo.check()) {
218 setError(aSymmetryAlgo.getError());
222 aSymmetryAlgo.build();
224 // Checking that the algorithm worked properly.
225 if(!aSymmetryAlgo.isDone()) {
226 static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
227 setError(aFeatureError);
230 if(aSymmetryAlgo.shape()->isNull()) {
231 static const std::string aShapeError = "Error: Resulting shape is Null.";
232 setError(aShapeError);
235 if(!aSymmetryAlgo.isValid()) {
236 std::string aFeatureError = "Error: Resulting shape is not valid.";
237 setError(aFeatureError);
241 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
242 loadNamingDS(aSymmetryAlgo, aResultBody, aBaseShape);
243 setResult(aResultBody, aResultIndex);
248 // Remove the rest results if there were produced in the previous pass.
249 removeResults(aResultIndex);
252 //=================================================================================================
253 void FeaturesPlugin_Symmetry::performSymmetryByPlane()
256 ListOfShape anObjects;
257 std::list<ResultPtr> aContextes;
258 AttributeSelectionListPtr anObjectsSelList =
259 selectionList(FeaturesPlugin_Symmetry::OBJECTS_LIST_ID());
260 if (anObjectsSelList->size() == 0) {
263 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
264 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
265 anObjectsSelList->value(anObjectsIndex);
266 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
267 if(!anObject.get()) { // may be for not-activated parts
271 anObjects.push_back(anObject);
272 aContextes.push_back(anObjectAttr->context());
276 std::shared_ptr<GeomAPI_Ax2> aPlane;
277 std::shared_ptr<GeomAPI_Pln> aPln;
278 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
279 selection(FeaturesPlugin_Symmetry::PLANE_OBJECT_ID());
280 if (anObjRef && anObjRef->value() && anObjRef->value()->isFace()) {
281 aPln = std::shared_ptr<GeomAPI_Face>(new GeomAPI_Face(anObjRef->value()))->getPlane();
283 else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
284 anObjRef->context()->shape() && anObjRef->context()->shape()->isFace()) {
286 std::shared_ptr<GeomAPI_Face>(new GeomAPI_Face(anObjRef->context()->shape()))->getPlane();
289 aPlane = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aPln->location(),
293 // Moving each object.
294 int aResultIndex = 0;
295 std::list<ResultPtr>::iterator aContext = aContextes.begin();
296 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
297 anObjectsIt++, aContext++) {
298 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
299 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
303 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
304 aTrsf->setSymmetry(aPlane);
305 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
306 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
307 aResultPart->setTrsf(*aContext, aTrsf);
308 setResult(aResultPart, aResultIndex);
310 GeomAlgoAPI_Symmetry aSymmetryAlgo(aBaseShape, aPlane);
312 if (!aSymmetryAlgo.check()) {
313 setError(aSymmetryAlgo.getError());
317 aSymmetryAlgo.build();
319 // Checking that the algorithm worked properly.
320 if(!aSymmetryAlgo.isDone()) {
321 static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
322 setError(aFeatureError);
325 if(aSymmetryAlgo.shape()->isNull()) {
326 static const std::string aShapeError = "Error: Resulting shape is Null.";
327 setError(aShapeError);
330 if(!aSymmetryAlgo.isValid()) {
331 std::string aFeatureError = "Error: Resulting shape is not valid.";
332 setError(aFeatureError);
336 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
337 loadNamingDS(aSymmetryAlgo, aResultBody, aBaseShape);
338 setResult(aResultBody, aResultIndex);
343 // Remove the rest results if there were produced in the previous pass.
344 removeResults(aResultIndex);
347 //=================================================================================================
348 void FeaturesPlugin_Symmetry::loadNamingDS(GeomAlgoAPI_Symmetry& theSymmetryAlgo,
349 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
350 std::shared_ptr<GeomAPI_Shape> theBaseShape)
352 // Store and name the result.
353 theResultBody->storeModified(theBaseShape, theSymmetryAlgo.shape());
356 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theSymmetryAlgo.mapOfSubShapes();
357 int aReflectedTag = 1;
358 std::string aReflectedName = "Symmetried";
359 theResultBody->loadAndOrientModifiedShapes(&theSymmetryAlgo,
360 theBaseShape, GeomAPI_Shape::FACE,
361 aReflectedTag, aReflectedName, *aSubShapes.get());