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_ShapeIterator.h>
19 #include <GeomAPI_Trsf.h>
21 #include <ModelAPI_AttributeDouble.h>
22 #include <ModelAPI_AttributeInteger.h>
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_AttributeString.h>
25 #include <ModelAPI_ResultBody.h>
26 #include <ModelAPI_ResultPart.h>
31 //=================================================================================================
32 FeaturesPlugin_MultiRotation::FeaturesPlugin_MultiRotation()
36 //=================================================================================================
37 void FeaturesPlugin_MultiRotation::initAttributes()
39 AttributeSelectionListPtr aSelection =
40 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
41 FeaturesPlugin_MultiRotation::OBJECTS_LIST_ID(),
42 ModelAPI_AttributeSelectionList::typeId()));
44 data()->addAttribute(FeaturesPlugin_MultiRotation::AXIS_ANGULAR_ID(),
45 ModelAPI_AttributeSelection::typeId());
46 data()->addAttribute(FeaturesPlugin_MultiRotation::USE_ANGULAR_STEP_ID(),
47 ModelAPI_AttributeString::typeId());
48 data()->addAttribute(FeaturesPlugin_MultiRotation::STEP_ANGULAR_ID(),
49 ModelAPI_AttributeDouble::typeId());
50 data()->addAttribute(FeaturesPlugin_MultiRotation::NB_COPIES_ANGULAR_ID(),
51 ModelAPI_AttributeInteger::typeId());
53 /*data()->addAttribute(FeaturesPlugin_MultiRotation::USE_RADIAL_DIR_ID(),
54 ModelAPI_AttributeString::typeId());
55 data()->addAttribute(FeaturesPlugin_MultiRotation::STEP_RADIAL_ID(),
56 ModelAPI_AttributeDouble::typeId());
57 data()->addAttribute(FeaturesPlugin_MultiRotation::NB_COPIES_RADIAL_ID(),
58 ModelAPI_AttributeInteger::typeId());*/
61 //=================================================================================================
62 void FeaturesPlugin_MultiRotation::execute()
64 /*std::string useRadialDir = string(FeaturesPlugin_MultiRotation::USE_RADIAL_DIR_ID())->value();
65 if (useRadialDir.empty()) {
73 //=================================================================================================
74 void FeaturesPlugin_MultiRotation::performRotation1D()
77 ListOfShape anObjects;
78 std::list<ResultPtr> aContextes;
79 AttributeSelectionListPtr anObjectsSelList =
80 selectionList(FeaturesPlugin_MultiRotation::OBJECTS_LIST_ID());
81 if (anObjectsSelList->size() == 0) {
84 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
85 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
86 anObjectsSelList->value(anObjectsIndex);
87 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
88 if(!anObject.get()) { // may be for not-activated parts
91 anObjects.push_back(anObject);
92 aContextes.push_back(anObjectAttr->context());
96 static const std::string aSelectionError = "Error: The axis shape selection is bad.";
97 AttributeSelectionPtr anObjRef = selection(AXIS_ANGULAR_ID());
98 GeomShapePtr aShape = anObjRef->value();
100 if (anObjRef->context().get()) {
101 aShape = anObjRef->context()->shape();
105 setError(aSelectionError);
110 if (aShape->isEdge())
112 anEdge = aShape->edge();
114 else if (aShape->isCompound())
116 GeomAPI_ShapeIterator anIt(aShape);
117 anEdge = anIt.current()->edge();
121 setError(aSelectionError);
127 setError(aSelectionError);
131 std::shared_ptr<GeomAPI_Ax1> anAxis(new GeomAPI_Ax1(anEdge->line()->location(),
132 anEdge->line()->direction()));
134 // Getting number of copies.
136 integer(FeaturesPlugin_MultiRotation::NB_COPIES_ANGULAR_ID())->value();
139 std::string aFeatureError = "Multirotation builder ";
140 aFeatureError+=":: the number of copies for the angular direction is null or negative.";
141 setError(aFeatureError);
147 std::string useAngularStep =
148 string(FeaturesPlugin_MultiRotation::USE_ANGULAR_STEP_ID())->value();
149 if (!useAngularStep.empty()) {
150 anAngle = real(FeaturesPlugin_MultiRotation::STEP_ANGULAR_ID())->value();
152 anAngle = 360./nbCopies;
155 // Moving each object.
156 int aResultIndex = 0;
157 std::list<ResultPtr>::iterator aContext = aContextes.begin();
158 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
159 anObjectsIt++, aContext++) {
160 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
161 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
165 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
166 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
167 for (int i=0; i<nbCopies; i++) {
168 aTrsf->setRotation(anAxis, i*anAngle);
169 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
170 aResultPart->setTrsf(*aContext, aTrsf);
171 setResult(aResultPart, aResultIndex);
175 ListOfShape aListOfShape;
176 std::list<std::shared_ptr<GeomAlgoAPI_Rotation> > aListOfRotationAlgo;
178 for (int i=0; i<nbCopies; i++) {
179 std::shared_ptr<GeomAlgoAPI_Rotation> aRotationnAlgo(
180 new GeomAlgoAPI_Rotation(aBaseShape, anAxis, i*anAngle));
182 if (!aRotationnAlgo->check()) {
183 setError(aRotationnAlgo->getError());
187 aRotationnAlgo->build();
189 // Checking that the algorithm worked properly.
190 if (!aRotationnAlgo->isDone()) {
191 static const std::string aFeatureError = "Error : Multitranslation algorithm failed.";
192 setError(aFeatureError);
195 if (aRotationnAlgo->shape()->isNull()) {
196 static const std::string aShapeError = "Error : Resulting shape is null.";
197 setError(aShapeError);
200 if (!aRotationnAlgo->isValid()) {
201 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
202 setError(aFeatureError);
205 aListOfShape.push_back(aRotationnAlgo->shape());
206 aListOfRotationAlgo.push_back(aRotationnAlgo);
208 std::shared_ptr<GeomAPI_Shape> aCompound =
209 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
210 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
211 aResultBody->storeModified(aBaseShape, aCompound);
212 loadNamingDS(aListOfRotationAlgo, aResultBody, aBaseShape);
214 setResult(aResultBody, aResultIndex);
219 // Remove the rest results if there were produced in the previous pass.
220 removeResults(aResultIndex);
223 //=================================================================================================
224 void FeaturesPlugin_MultiRotation::performRotation2D()
227 ListOfShape anObjects;
228 std::list<ResultPtr> aContextes;
229 AttributeSelectionListPtr anObjectsSelList =
230 selectionList(FeaturesPlugin_MultiRotation::OBJECTS_LIST_ID());
231 if (anObjectsSelList->size() == 0) {
234 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
235 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
236 anObjectsSelList->value(anObjectsIndex);
237 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
238 if(!anObject.get()) { // may be for not-activated parts
241 anObjects.push_back(anObject);
242 aContextes.push_back(anObjectAttr->context());
246 std::shared_ptr<GeomAPI_Ax1> anAxis;
247 std::shared_ptr<GeomAPI_Edge> anEdge;
248 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
249 selection(FeaturesPlugin_MultiRotation::AXIS_ANGULAR_ID());
250 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
251 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
252 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
253 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
254 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
257 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
258 anEdge->line()->direction()));
261 // Getting number of copies int he angular direction.
263 integer(FeaturesPlugin_MultiRotation::NB_COPIES_ANGULAR_ID())->value();
266 std::string aFeatureError = "Multirotation builder ";
267 aFeatureError+=":: the number of copies for the angular direction is null or negative.";
268 setError(aFeatureError);
272 // Getting number of copies int he radial direction.
274 integer(FeaturesPlugin_MultiRotation::NB_COPIES_RADIAL_ID())->value();
277 std::string aFeatureError = "Multirotation builder ";
278 aFeatureError+=":: the number of copies for the radial direction is null or negative.";
279 setError(aFeatureError);
285 std::string useAngularStep =
286 string(FeaturesPlugin_MultiRotation::USE_ANGULAR_STEP_ID())->value();
287 if (!useAngularStep.empty()) {
288 anAngle = real(FeaturesPlugin_MultiRotation::STEP_ANGULAR_ID())->value();
290 anAngle = 360./nbAngular;
294 double aStep = real(FeaturesPlugin_MultiRotation::STEP_RADIAL_ID())->value();
296 // Moving each object.
297 int aResultIndex = 0;
298 std::list<ResultPtr>::iterator aContext = aContextes.begin();
299 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
300 anObjectsIt++, aContext++) {
301 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
302 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
304 std::shared_ptr<GeomAPI_Dir> aDir =
305 GeomAlgoAPI_ShapeTools::buildDirFromAxisAndShape(aBaseShape, anAxis);
306 double x = aDir->x();
307 double y = aDir->y();
308 double z = aDir->z();
309 double norm = sqrt(x*x+y*y+z*z);
313 /*ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
314 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
315 for (int j=0; j<aSecondNbCopies; j++) {
316 for (int i=0; i<aFirstNbCopies; i++) {
317 double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
318 double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
319 double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
320 aTrsf->setTranslation(dx, dy, dz);
321 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
322 aResultPart->setTrsf(*aContext, aTrsf);
323 setResult(aResultPart, aResultIndex);
328 ListOfShape aListOfShape;
329 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > aListOfTranslationAlgo;
330 std::list<std::shared_ptr<GeomAlgoAPI_Rotation> > aListOfRotationAlgo;
331 for (int j=0; j<nbRadial; j++) {
333 double dx = j*aStep*x/norm;
334 double dy = j*aStep*y/norm;
335 double dz = j*aStep*z/norm;
336 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
337 new GeomAlgoAPI_Translation(aBaseShape, dx, dy, dz));
339 if (!aTranslationAlgo->check()) {
340 setError(aTranslationAlgo->getError());
344 aTranslationAlgo->build();
346 // Checking that the algorithm worked properly.
347 if (!aTranslationAlgo->isDone()) {
348 static const std::string aFeatureError = "Error : Multirotation algorithm failed.";
349 setError(aFeatureError);
352 if (aTranslationAlgo->shape()->isNull()) {
353 static const std::string aShapeError = "Error : Resulting shape is null.";
354 setError(aShapeError);
357 if (!aTranslationAlgo->isValid()) {
358 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
359 setError(aFeatureError);
362 aListOfShape.push_back(aTranslationAlgo->shape());
363 aListOfTranslationAlgo.push_back(aTranslationAlgo);
364 for (int i=1; i<nbAngular; i++) {
365 std::shared_ptr<GeomAlgoAPI_Rotation> aRotationnAlgo(
366 new GeomAlgoAPI_Rotation(aTranslationAlgo->shape(), anAxis, i*anAngle));
367 if (!aRotationnAlgo->check()) {
368 setError(aTranslationAlgo->getError());
371 aRotationnAlgo->build();// Checking that the algorithm worked properly.
372 if (!aRotationnAlgo->isDone()) {
373 static const std::string aFeatureError = "Error : Multirotation algorithm failed.";
374 setError(aFeatureError);
377 if (aRotationnAlgo->shape()->isNull()) {
378 static const std::string aShapeError = "Error : Resulting shape is null.";
379 setError(aShapeError);
382 if (!aRotationnAlgo->isValid()) {
383 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
384 setError(aFeatureError);
387 aListOfShape.push_back(aRotationnAlgo->shape());
388 aListOfRotationAlgo.push_back(aRotationnAlgo);
391 std::shared_ptr<GeomAPI_Shape> aCompound =
392 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
393 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
394 aResultBody->storeModified(aBaseShape, aCompound);
396 loadNamingDS2(aListOfTranslationAlgo, aResultBody, aBaseShape);
397 loadNamingDS3(aListOfRotationAlgo, aResultBody, aBaseShape, nbRadial);
398 setResult(aResultBody, aResultIndex);
403 // Remove the rest results if there were produced in the previous pass.
404 removeResults(aResultIndex);
407 //=================================================================================================
408 void FeaturesPlugin_MultiRotation::loadNamingDS2(
409 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > theListOfTranslationAlgo,
410 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
411 std::shared_ptr<GeomAPI_Shape> theBaseShape)
413 for (std::list<std::shared_ptr<GeomAlgoAPI_Translation> >::const_iterator anIt =
414 theListOfTranslationAlgo.begin(); anIt != theListOfTranslationAlgo.cend(); ++anIt) {
416 theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::FACE, "Rotated_Face");
419 theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::EDGE, "Rotated_Edge");
422 theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::VERTEX, "Rotated_Vertex");
426 //=================================================================================================
427 void FeaturesPlugin_MultiRotation::loadNamingDS3(
428 std::list<std::shared_ptr<GeomAlgoAPI_Rotation> > theListOfRotationAlgo,
429 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
430 std::shared_ptr<GeomAPI_Shape> theBaseShape, int nb)
433 std::string aRotatedName;
435 for (std::list<std::shared_ptr<GeomAlgoAPI_Rotation> >::const_iterator anIt =
436 theListOfRotationAlgo.begin(); anIt != theListOfRotationAlgo.cend(); ++anIt) {
440 GeomAPI_ShapeExplorer anExp((*anIt)->shape(), GeomAPI_Shape::FACE);
441 for(; anExp.more(); anExp.next()) {
442 aRotatedName = "Rotated_Face_" + std::to_string((long long) anIndex);
443 aRotatedName = aRotatedName + "_" + std::to_string((long long) numFace);
444 theResultBody->generated(anExp.current(), aRotatedName);
451 //=================================================================================================
452 void FeaturesPlugin_MultiRotation::loadNamingDS(
453 std::list<std::shared_ptr<GeomAlgoAPI_Rotation> > theListOfRotationAlgo,
454 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
455 std::shared_ptr<GeomAPI_Shape> theBaseShape)
457 for (std::list<std::shared_ptr<GeomAlgoAPI_Rotation> >::const_iterator anIt =
458 theListOfRotationAlgo.begin(); anIt != theListOfRotationAlgo.cend(); ++anIt) {
460 theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::FACE, "Rotated_Face");
463 theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::EDGE, "Rotated_Edge");
466 theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::VERTEX, "Rotated_Vertex");