1 // Copyright (C) 2014-201x CEA/DEN, EDF R&D
3 // File: FeaturesPlugin_MultiRotation.cpp
4 // Created: 30 Jan 2017
5 // Author: Clarisse Genrault (CEA)
7 #include <FeaturesPlugin_MultiRotation.h>
9 #include <GeomAlgoAPI_CompoundBuilder.h>
10 #include <GeomAlgoAPI_ShapeTools.h>
11 #include <GeomAlgoAPI_Translation.h>
13 #include <GeomAPI_ShapeExplorer.h>
15 #include <GeomAPI_Ax1.h>
16 #include <GeomAPI_Edge.h>
17 #include <GeomAPI_Lin.h>
18 #include <GeomAPI_Trsf.h>
20 #include <ModelAPI_AttributeDouble.h>
21 #include <ModelAPI_AttributeInteger.h>
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_AttributeString.h>
24 #include <ModelAPI_ResultBody.h>
25 #include <ModelAPI_ResultPart.h>
30 //=================================================================================================
31 FeaturesPlugin_MultiRotation::FeaturesPlugin_MultiRotation()
35 //=================================================================================================
36 void FeaturesPlugin_MultiRotation::initAttributes()
38 AttributeSelectionListPtr aSelection =
39 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
40 FeaturesPlugin_MultiRotation::OBJECTS_LIST_ID(),
41 ModelAPI_AttributeSelectionList::typeId()));
43 data()->addAttribute(FeaturesPlugin_MultiRotation::AXIS_ANGULAR_ID(),
44 ModelAPI_AttributeSelection::typeId());
45 data()->addAttribute(FeaturesPlugin_MultiRotation::USE_ANGULAR_STEP_ID(),
46 ModelAPI_AttributeString::typeId());
47 data()->addAttribute(FeaturesPlugin_MultiRotation::STEP_ANGULAR_ID(),
48 ModelAPI_AttributeDouble::typeId());
49 data()->addAttribute(FeaturesPlugin_MultiRotation::NB_COPIES_ANGULAR_ID(),
50 ModelAPI_AttributeInteger::typeId());
52 /*data()->addAttribute(FeaturesPlugin_MultiRotation::USE_RADIAL_DIR_ID(),
53 ModelAPI_AttributeString::typeId());
54 data()->addAttribute(FeaturesPlugin_MultiRotation::STEP_RADIAL_ID(),
55 ModelAPI_AttributeDouble::typeId());
56 data()->addAttribute(FeaturesPlugin_MultiRotation::NB_COPIES_RADIAL_ID(),
57 ModelAPI_AttributeInteger::typeId());*/
60 //=================================================================================================
61 void FeaturesPlugin_MultiRotation::execute()
63 /*std::string useRadialDir = string(FeaturesPlugin_MultiRotation::USE_RADIAL_DIR_ID())->value();
64 if (useRadialDir.empty()) {
72 //=================================================================================================
73 void FeaturesPlugin_MultiRotation::performRotation1D()
76 ListOfShape anObjects;
77 std::list<ResultPtr> aContextes;
78 AttributeSelectionListPtr anObjectsSelList =
79 selectionList(FeaturesPlugin_MultiRotation::OBJECTS_LIST_ID());
80 if (anObjectsSelList->size() == 0) {
83 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
84 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
85 anObjectsSelList->value(anObjectsIndex);
86 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
87 if(!anObject.get()) { // may be for not-activated parts
91 anObjects.push_back(anObject);
92 aContextes.push_back(anObjectAttr->context());
96 std::shared_ptr<GeomAPI_Ax1> anAxis;
97 std::shared_ptr<GeomAPI_Edge> anEdge;
98 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
99 selection(FeaturesPlugin_MultiRotation::AXIS_ANGULAR_ID());
100 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
101 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
102 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
103 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
104 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
107 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
108 anEdge->line()->direction()));
111 // Getting number of copies.
113 integer(FeaturesPlugin_MultiRotation::NB_COPIES_ANGULAR_ID())->value();
116 std::string aFeatureError = "Multirotation builder ";
117 aFeatureError+=":: the number of copies for the angular direction is null or negative.";
118 setError(aFeatureError);
124 std::string useAngularStep =
125 string(FeaturesPlugin_MultiRotation::USE_ANGULAR_STEP_ID())->value();
126 if (!useAngularStep.empty()) {
127 anAngle = real(FeaturesPlugin_MultiRotation::STEP_ANGULAR_ID())->value();
129 anAngle = 360./nbCopies;
132 // Moving each object.
133 int aResultIndex = 0;
134 std::list<ResultPtr>::iterator aContext = aContextes.begin();
135 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
136 anObjectsIt++, aContext++) {
137 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
138 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
142 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
143 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
144 for (int i=0; i<nbCopies; i++) {
145 aTrsf->setRotation(anAxis, i*anAngle);
146 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
147 aResultPart->setTrsf(*aContext, aTrsf);
148 setResult(aResultPart, aResultIndex);
152 ListOfShape aListOfShape;
153 std::list<std::shared_ptr<GeomAlgoAPI_Rotation> > aListOfRotationAlgo;
155 for (int i=0; i<nbCopies; i++) {
156 std::shared_ptr<GeomAlgoAPI_Rotation> aRotationnAlgo(
157 new GeomAlgoAPI_Rotation(aBaseShape, anAxis, i*anAngle));
159 if (!aRotationnAlgo->check()) {
160 setError(aRotationnAlgo->getError());
164 aRotationnAlgo->build();
166 // Checking that the algorithm worked properly.
167 if (!aRotationnAlgo->isDone()) {
168 static const std::string aFeatureError = "Error : Multitranslation algorithm failed.";
169 setError(aFeatureError);
172 if (aRotationnAlgo->shape()->isNull()) {
173 static const std::string aShapeError = "Error : Resulting shape is null.";
174 setError(aShapeError);
177 if (!aRotationnAlgo->isValid()) {
178 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
179 setError(aFeatureError);
182 aListOfShape.push_back(aRotationnAlgo->shape());
183 aListOfRotationAlgo.push_back(aRotationnAlgo);
185 std::shared_ptr<GeomAPI_Shape> aCompound =
186 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
187 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
188 aResultBody->storeModified(aBaseShape, aCompound);
189 loadNamingDS(aListOfRotationAlgo, aResultBody, aBaseShape);
191 setResult(aResultBody, aResultIndex);
196 // Remove the rest results if there were produced in the previous pass.
197 removeResults(aResultIndex);
200 //=================================================================================================
201 void FeaturesPlugin_MultiRotation::performRotation2D()
204 ListOfShape anObjects;
205 std::list<ResultPtr> aContextes;
206 AttributeSelectionListPtr anObjectsSelList =
207 selectionList(FeaturesPlugin_MultiRotation::OBJECTS_LIST_ID());
208 if (anObjectsSelList->size() == 0) {
211 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
212 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
213 anObjectsSelList->value(anObjectsIndex);
214 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
215 if(!anObject.get()) { // may be for not-activated parts
219 anObjects.push_back(anObject);
220 aContextes.push_back(anObjectAttr->context());
224 std::shared_ptr<GeomAPI_Ax1> anAxis;
225 std::shared_ptr<GeomAPI_Edge> anEdge;
226 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
227 selection(FeaturesPlugin_MultiRotation::AXIS_ANGULAR_ID());
228 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
229 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
230 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
231 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
232 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
235 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
236 anEdge->line()->direction()));
239 // Getting number of copies int he angular direction.
241 integer(FeaturesPlugin_MultiRotation::NB_COPIES_ANGULAR_ID())->value();
244 std::string aFeatureError = "Multirotation builder ";
245 aFeatureError+=":: the number of copies for the angular direction is null or negative.";
246 setError(aFeatureError);
250 // Getting number of copies int he radial direction.
252 integer(FeaturesPlugin_MultiRotation::NB_COPIES_RADIAL_ID())->value();
255 std::string aFeatureError = "Multirotation builder ";
256 aFeatureError+=":: the number of copies for the radial direction is null or negative.";
257 setError(aFeatureError);
263 std::string useAngularStep =
264 string(FeaturesPlugin_MultiRotation::USE_ANGULAR_STEP_ID())->value();
265 if (!useAngularStep.empty()) {
266 anAngle = real(FeaturesPlugin_MultiRotation::STEP_ANGULAR_ID())->value();
268 anAngle = 360./nbAngular;
272 double aStep = real(FeaturesPlugin_MultiRotation::STEP_RADIAL_ID())->value();
274 // Moving each object.
275 int aResultIndex = 0;
276 std::list<ResultPtr>::iterator aContext = aContextes.begin();
277 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
278 anObjectsIt++, aContext++) {
279 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
280 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
282 std::shared_ptr<GeomAPI_Dir> aDir =
283 GeomAlgoAPI_ShapeTools::buildDirFromAxisAndShape(aBaseShape, anAxis);
284 double x = aDir->x();
285 double y = aDir->y();
286 double z = aDir->z();
287 double norm = sqrt(x*x+y*y+z*z);
291 /*ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
292 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
293 for (int j=0; j<aSecondNbCopies; j++) {
294 for (int i=0; i<aFirstNbCopies; i++) {
295 double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
296 double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
297 double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
298 aTrsf->setTranslation(dx, dy, dz);
299 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
300 aResultPart->setTrsf(*aContext, aTrsf);
301 setResult(aResultPart, aResultIndex);
306 ListOfShape aListOfShape;
307 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > aListOfTranslationAlgo;
308 std::list<std::shared_ptr<GeomAlgoAPI_Rotation> > aListOfRotationAlgo;
309 for (int j=0; j<nbRadial; j++) {
311 double dx = j*aStep*x/norm;
312 double dy = j*aStep*y/norm;
313 double dz = j*aStep*z/norm;
314 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
315 new GeomAlgoAPI_Translation(aBaseShape, dx, dy, dz));
317 if (!aTranslationAlgo->check()) {
318 setError(aTranslationAlgo->getError());
322 aTranslationAlgo->build();
324 // Checking that the algorithm worked properly.
325 if (!aTranslationAlgo->isDone()) {
326 static const std::string aFeatureError = "Error : Multirotation algorithm failed.";
327 setError(aFeatureError);
330 if (aTranslationAlgo->shape()->isNull()) {
331 static const std::string aShapeError = "Error : Resulting shape is null.";
332 setError(aShapeError);
335 if (!aTranslationAlgo->isValid()) {
336 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
337 setError(aFeatureError);
340 aListOfShape.push_back(aTranslationAlgo->shape());
341 aListOfTranslationAlgo.push_back(aTranslationAlgo);
342 for (int i=1; i<nbAngular; i++) {
343 std::shared_ptr<GeomAlgoAPI_Rotation> aRotationnAlgo(
344 new GeomAlgoAPI_Rotation(aTranslationAlgo->shape(), anAxis, i*anAngle));
345 if (!aRotationnAlgo->check()) {
346 setError(aTranslationAlgo->getError());
349 aRotationnAlgo->build();// Checking that the algorithm worked properly.
350 if (!aRotationnAlgo->isDone()) {
351 static const std::string aFeatureError = "Error : Multirotation algorithm failed.";
352 setError(aFeatureError);
355 if (aRotationnAlgo->shape()->isNull()) {
356 static const std::string aShapeError = "Error : Resulting shape is null.";
357 setError(aShapeError);
360 if (!aRotationnAlgo->isValid()) {
361 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
362 setError(aFeatureError);
365 aListOfShape.push_back(aRotationnAlgo->shape());
366 aListOfRotationAlgo.push_back(aRotationnAlgo);
369 std::shared_ptr<GeomAPI_Shape> aCompound =
370 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
371 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
372 aResultBody->storeModified(aBaseShape, aCompound);
374 loadNamingDS2(aListOfTranslationAlgo, aResultBody, aBaseShape);
375 loadNamingDS3(aListOfRotationAlgo, aResultBody, aBaseShape, nbRadial);
376 setResult(aResultBody, aResultIndex);
381 // Remove the rest results if there were produced in the previous pass.
382 removeResults(aResultIndex);
385 //=================================================================================================
386 void FeaturesPlugin_MultiRotation::loadNamingDS2(
387 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > theListOfTranslationAlgo,
388 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
389 std::shared_ptr<GeomAPI_Shape> theBaseShape)
393 std::string aRotatedName;
395 for (std::list<std::shared_ptr<GeomAlgoAPI_Translation> >::const_iterator anIt =
396 theListOfTranslationAlgo.begin(); anIt != theListOfTranslationAlgo.cend(); ++anIt) {
397 std::cout << "LOAD" << std::endl;
398 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = (*anIt)->mapOfSubShapes();
401 aRotatedName = "Rotated_Face_" + std::to_string((long long) anIndex);
402 theResultBody->loadAndOrientModifiedShapes((*anIt).get(), theBaseShape, GeomAPI_Shape::FACE,
403 aTag++, aRotatedName, *aSubShapes.get(),
407 aRotatedName = "Rotated_Edge_" + std::to_string((long long) anIndex);
408 theResultBody->loadAndOrientModifiedShapes((*anIt).get(), theBaseShape, GeomAPI_Shape::EDGE,
409 aTag++, aRotatedName, *aSubShapes.get(),
413 aRotatedName = "Rotated_Vertex_" + std::to_string((long long) anIndex);
414 theResultBody->loadAndOrientModifiedShapes((*anIt).get(), theBaseShape, GeomAPI_Shape::VERTEX,
415 aTag++, aRotatedName, *aSubShapes.get(),
422 //=================================================================================================
423 void FeaturesPlugin_MultiRotation::loadNamingDS3(
424 std::list<std::shared_ptr<GeomAlgoAPI_Rotation> > theListOfRotationAlgo,
425 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
426 std::shared_ptr<GeomAPI_Shape> theBaseShape, int nb)
430 std::string aRotatedName;
432 for (std::list<std::shared_ptr<GeomAlgoAPI_Rotation> >::const_iterator anIt =
433 theListOfRotationAlgo.begin(); anIt != theListOfRotationAlgo.cend(); ++anIt) {
437 GeomAPI_ShapeExplorer anExp((*anIt)->shape(), GeomAPI_Shape::FACE);
438 for(; anExp.more(); anExp.next()) {
439 aRotatedName = "Rotated_Face_" + std::to_string((long long) anIndex);
440 aRotatedName = aRotatedName + "_" + std::to_string((long long) numFace);
441 theResultBody->generated(anExp.current(), aRotatedName, aTag++);
448 //=================================================================================================
449 void FeaturesPlugin_MultiRotation::loadNamingDS(
450 std::list<std::shared_ptr<GeomAlgoAPI_Rotation> > theListOfRotationAlgo,
451 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
452 std::shared_ptr<GeomAPI_Shape> theBaseShape)
456 std::string aRotatedName;
458 for (std::list<std::shared_ptr<GeomAlgoAPI_Rotation> >::const_iterator anIt =
459 theListOfRotationAlgo.begin(); anIt != theListOfRotationAlgo.cend(); ++anIt) {
460 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = (*anIt)->mapOfSubShapes();
463 aRotatedName = "Rotated_Face_" + std::to_string((long long) anIndex);
464 theResultBody->loadAndOrientModifiedShapes((*anIt).get(), theBaseShape, GeomAPI_Shape::FACE,
465 aTag++, aRotatedName, *aSubShapes.get(),
469 aRotatedName = "Rotated_Edge_" + std::to_string((long long) anIndex);
470 theResultBody->loadAndOrientModifiedShapes((*anIt).get(), theBaseShape, GeomAPI_Shape::EDGE,
471 aTag++, aRotatedName, *aSubShapes.get(),
475 aRotatedName = "Rotated_Vertex_" + std::to_string((long long) anIndex);
476 theResultBody->loadAndOrientModifiedShapes((*anIt).get(), theBaseShape, GeomAPI_Shape::VERTEX,
477 aTag++, aRotatedName, *aSubShapes.get(),