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>
15 #include <GeomAPI_Trsf.h>
17 #include <ModelAPI_AttributeSelectionList.h>
18 #include <ModelAPI_AttributeString.h>
19 #include <ModelAPI_ResultBody.h>
20 #include <ModelAPI_ResultPart.h>
23 #include <GeomAlgoAPI_FaceBuilder.h>
25 //=================================================================================================
26 FeaturesPlugin_Symmetry::FeaturesPlugin_Symmetry()
30 //=================================================================================================
31 void FeaturesPlugin_Symmetry::initAttributes()
33 data()->addAttribute(FeaturesPlugin_Symmetry::CREATION_METHOD(),
34 ModelAPI_AttributeString::typeId());
36 AttributeSelectionListPtr aSelection =
37 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
38 FeaturesPlugin_Symmetry::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
40 data()->addAttribute(FeaturesPlugin_Symmetry::POINT_OBJECT_ID(),
41 ModelAPI_AttributeSelection::typeId());
43 data()->addAttribute(FeaturesPlugin_Symmetry::AXIS_OBJECT_ID(),
44 ModelAPI_AttributeSelection::typeId());
46 data()->addAttribute(FeaturesPlugin_Symmetry::PLANE_OBJECT_ID(),
47 ModelAPI_AttributeSelection::typeId());
50 //=================================================================================================
51 void FeaturesPlugin_Symmetry::execute()
53 AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Symmetry::CREATION_METHOD());
54 std::string aMethodType = aMethodTypeAttr->value();
56 if (aMethodType == CREATION_METHOD_BY_POINT()) {
57 performSymmetryByPoint();
60 if (aMethodType == CREATION_METHOD_BY_AXIS()) {
61 performSymmetryByAxis();
64 if (aMethodType == CREATION_METHOD_BY_PLANE()) {
65 performSymmetryByPlane();
69 //=================================================================================================
70 void FeaturesPlugin_Symmetry::performSymmetryByPoint()
73 ListOfShape anObjects;
74 std::list<ResultPtr> aContextes;
75 AttributeSelectionListPtr anObjectsSelList =
76 selectionList(FeaturesPlugin_Symmetry::OBJECTS_LIST_ID());
77 if (anObjectsSelList->size() == 0) {
80 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
81 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
82 anObjectsSelList->value(anObjectsIndex);
83 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
84 if(!anObject.get()) { // may be for not-activated parts
88 anObjects.push_back(anObject);
89 aContextes.push_back(anObjectAttr->context());
93 std::shared_ptr<GeomAPI_Pnt> aPoint;
94 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
95 selection(FeaturesPlugin_Symmetry::POINT_OBJECT_ID());
96 if (anObjRef.get() != NULL) {
97 GeomShapePtr aShape1 = anObjRef->value();
99 aShape1 = anObjRef->context()->shape();
102 aPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
106 // Moving each object.
107 int aResultIndex = 0;
108 std::list<ResultPtr>::iterator aContext = aContextes.begin();
109 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
110 anObjectsIt++, aContext++) {
111 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
112 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
116 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
117 aTrsf->setSymmetry(aPoint);
118 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
119 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
120 aResultPart->setTrsf(*aContext, aTrsf);
121 setResult(aResultPart, aResultIndex);
123 GeomAlgoAPI_Symmetry aSymmetryAlgo(aBaseShape, aPoint);
125 if (!aSymmetryAlgo.check()) {
126 setError(aSymmetryAlgo.getError());
130 aSymmetryAlgo.build();
132 // Checking that the algorithm worked properly.
133 if(!aSymmetryAlgo.isDone()) {
134 static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
135 setError(aFeatureError);
138 if(aSymmetryAlgo.shape()->isNull()) {
139 static const std::string aShapeError = "Error: Resulting shape is Null.";
140 setError(aShapeError);
143 if(!aSymmetryAlgo.isValid()) {
144 std::string aFeatureError = "Error: Resulting shape is not valid.";
145 setError(aFeatureError);
149 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
150 loadNamingDS(aSymmetryAlgo, aResultBody, aBaseShape);
151 setResult(aResultBody, aResultIndex);
156 // Remove the rest results if there were produced in the previous pass.
157 removeResults(aResultIndex);
160 //=================================================================================================
161 void FeaturesPlugin_Symmetry::performSymmetryByAxis()
164 ListOfShape anObjects;
165 std::list<ResultPtr> aContextes;
166 AttributeSelectionListPtr anObjectsSelList =
167 selectionList(FeaturesPlugin_Symmetry::OBJECTS_LIST_ID());
168 if (anObjectsSelList->size() == 0) {
171 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
172 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
173 anObjectsSelList->value(anObjectsIndex);
174 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
175 if(!anObject.get()) { // may be for not-activated parts
179 anObjects.push_back(anObject);
180 aContextes.push_back(anObjectAttr->context());
184 std::shared_ptr<GeomAPI_Ax1> anAxis;
185 std::shared_ptr<GeomAPI_Edge> anEdge;
186 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
187 selection(FeaturesPlugin_Symmetry::AXIS_OBJECT_ID());
188 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
189 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
190 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
191 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
192 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
195 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
196 anEdge->line()->direction()));
199 // Moving each object.
200 int aResultIndex = 0;
201 std::list<ResultPtr>::iterator aContext = aContextes.begin();
202 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
203 anObjectsIt++, aContext++) {
204 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
205 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
209 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
210 aTrsf->setSymmetry(anAxis);
211 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
212 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
213 aResultPart->setTrsf(*aContext, aTrsf);
214 setResult(aResultPart, aResultIndex);
216 GeomAlgoAPI_Symmetry aSymmetryAlgo(aBaseShape, anAxis);
218 if (!aSymmetryAlgo.check()) {
219 setError(aSymmetryAlgo.getError());
223 aSymmetryAlgo.build();
225 // Checking that the algorithm worked properly.
226 if(!aSymmetryAlgo.isDone()) {
227 static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
228 setError(aFeatureError);
231 if(aSymmetryAlgo.shape()->isNull()) {
232 static const std::string aShapeError = "Error: Resulting shape is Null.";
233 setError(aShapeError);
236 if(!aSymmetryAlgo.isValid()) {
237 std::string aFeatureError = "Error: Resulting shape is not valid.";
238 setError(aFeatureError);
242 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
243 loadNamingDS(aSymmetryAlgo, aResultBody, aBaseShape);
244 setResult(aResultBody, aResultIndex);
249 // Remove the rest results if there were produced in the previous pass.
250 removeResults(aResultIndex);
253 //=================================================================================================
254 void FeaturesPlugin_Symmetry::performSymmetryByPlane()
257 ListOfShape anObjects;
258 std::list<ResultPtr> aContextes;
259 AttributeSelectionListPtr anObjectsSelList =
260 selectionList(FeaturesPlugin_Symmetry::OBJECTS_LIST_ID());
261 if (anObjectsSelList->size() == 0) {
264 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
265 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
266 anObjectsSelList->value(anObjectsIndex);
267 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
268 if(!anObject.get()) { // may be for not-activated parts
272 anObjects.push_back(anObject);
273 aContextes.push_back(anObjectAttr->context());
277 std::shared_ptr<GeomAPI_Ax2> aPlane;
278 std::shared_ptr<GeomAPI_Pln> aPln;
279 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
280 selection(FeaturesPlugin_Symmetry::PLANE_OBJECT_ID());
281 if (anObjRef && anObjRef->value() && anObjRef->value()->isFace()) {
282 aPln = std::shared_ptr<GeomAPI_Face>(new GeomAPI_Face(anObjRef->value()))->getPlane();
284 else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
285 anObjRef->context()->shape() && anObjRef->context()->shape()->isFace()) {
287 std::shared_ptr<GeomAPI_Face>(new GeomAPI_Face(anObjRef->context()->shape()))->getPlane();
290 aPlane = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aPln->location(),
294 // Moving each object.
295 int aResultIndex = 0;
296 std::list<ResultPtr>::iterator aContext = aContextes.begin();
297 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
298 anObjectsIt++, aContext++) {
299 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
300 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
304 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
305 aTrsf->setSymmetry(aPlane);
306 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
307 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
308 aResultPart->setTrsf(*aContext, aTrsf);
309 setResult(aResultPart, aResultIndex);
311 GeomAlgoAPI_Symmetry aSymmetryAlgo(aBaseShape, aPlane);
313 if (!aSymmetryAlgo.check()) {
314 setError(aSymmetryAlgo.getError());
318 aSymmetryAlgo.build();
320 // Checking that the algorithm worked properly.
321 if(!aSymmetryAlgo.isDone()) {
322 static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
323 setError(aFeatureError);
326 if(aSymmetryAlgo.shape()->isNull()) {
327 static const std::string aShapeError = "Error: Resulting shape is Null.";
328 setError(aShapeError);
331 if(!aSymmetryAlgo.isValid()) {
332 std::string aFeatureError = "Error: Resulting shape is not valid.";
333 setError(aFeatureError);
337 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
338 loadNamingDS(aSymmetryAlgo, aResultBody, aBaseShape);
339 setResult(aResultBody, aResultIndex);
344 // Remove the rest results if there were produced in the previous pass.
345 removeResults(aResultIndex);
348 //=================================================================================================
349 void FeaturesPlugin_Symmetry::loadNamingDS(GeomAlgoAPI_Symmetry& theSymmetryAlgo,
350 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
351 std::shared_ptr<GeomAPI_Shape> theBaseShape)
353 // Store and name the result.
354 theResultBody->storeModified(theBaseShape, theSymmetryAlgo.shape());
357 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theSymmetryAlgo.mapOfSubShapes();
358 int aReflectedTag = 1;
359 std::string aReflectedName = "Symmetried";
360 theResultBody->loadAndOrientModifiedShapes(&theSymmetryAlgo,
361 theBaseShape, GeomAPI_Shape::FACE,
362 aReflectedTag, aReflectedName, *aSubShapes.get());