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 #ifdef FEATURE_MULTIROTATION_TWO_DIRECTIONS
54 data()->addAttribute(FeaturesPlugin_MultiRotation::USE_RADIAL_DIR_ID(),
55 ModelAPI_AttributeString::typeId());
56 data()->addAttribute(FeaturesPlugin_MultiRotation::STEP_RADIAL_ID(),
57 ModelAPI_AttributeDouble::typeId());
58 data()->addAttribute(FeaturesPlugin_MultiRotation::NB_COPIES_RADIAL_ID(),
59 ModelAPI_AttributeInteger::typeId());
63 //=================================================================================================
64 void FeaturesPlugin_MultiRotation::execute()
66 #ifdef FEATURE_MULTIROTATION_TWO_DIRECTIONS
67 std::string useRadialDir = string(FeaturesPlugin_MultiRotation::USE_RADIAL_DIR_ID())->value();
68 if (useRadialDir.empty()) {
78 //=================================================================================================
79 void FeaturesPlugin_MultiRotation::performRotation1D()
82 ListOfShape anObjects;
83 std::list<ResultPtr> aContextes;
84 AttributeSelectionListPtr anObjectsSelList =
85 selectionList(FeaturesPlugin_MultiRotation::OBJECTS_LIST_ID());
86 if (anObjectsSelList->size() == 0) {
89 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
90 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
91 anObjectsSelList->value(anObjectsIndex);
92 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
93 if(!anObject.get()) { // may be for not-activated parts
96 anObjects.push_back(anObject);
97 aContextes.push_back(anObjectAttr->context());
101 static const std::string aSelectionError = "Error: The axis shape selection is bad.";
102 AttributeSelectionPtr anObjRef = selection(AXIS_ANGULAR_ID());
103 GeomShapePtr aShape = anObjRef->value();
105 if (anObjRef->context().get()) {
106 aShape = anObjRef->context()->shape();
110 setError(aSelectionError);
115 if (aShape->isEdge())
117 anEdge = aShape->edge();
119 else if (aShape->isCompound())
121 GeomAPI_ShapeIterator anIt(aShape);
122 anEdge = anIt.current()->edge();
126 setError(aSelectionError);
132 setError(aSelectionError);
136 std::shared_ptr<GeomAPI_Ax1> anAxis(new GeomAPI_Ax1(anEdge->line()->location(),
137 anEdge->line()->direction()));
139 // Getting number of copies.
141 integer(FeaturesPlugin_MultiRotation::NB_COPIES_ANGULAR_ID())->value();
144 std::string aFeatureError = "Multirotation builder ";
145 aFeatureError+=":: the number of copies for the angular direction is null or negative.";
146 setError(aFeatureError);
152 std::string useAngularStep =
153 string(FeaturesPlugin_MultiRotation::USE_ANGULAR_STEP_ID())->value();
154 if (!useAngularStep.empty()) {
155 anAngle = real(FeaturesPlugin_MultiRotation::STEP_ANGULAR_ID())->value();
157 anAngle = 360./nbCopies;
160 // Moving each object.
161 int aResultIndex = 0;
162 std::list<ResultPtr>::iterator aContext = aContextes.begin();
163 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
164 anObjectsIt++, aContext++) {
165 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
166 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
170 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
171 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
172 for (int i=0; i<nbCopies; i++) {
173 aTrsf->setRotation(anAxis, i*anAngle);
174 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
175 aResultPart->setTrsf(*aContext, aTrsf);
176 setResult(aResultPart, aResultIndex);
180 ListOfShape aListOfShape;
181 std::list<std::shared_ptr<GeomAlgoAPI_Rotation> > aListOfRotationAlgo;
183 for (int i=0; i<nbCopies; i++) {
184 std::shared_ptr<GeomAlgoAPI_Rotation> aRotationnAlgo(
185 new GeomAlgoAPI_Rotation(aBaseShape, anAxis, i*anAngle));
187 if (!aRotationnAlgo->check()) {
188 setError(aRotationnAlgo->getError());
192 aRotationnAlgo->build();
194 // Checking that the algorithm worked properly.
195 if (!aRotationnAlgo->isDone()) {
196 static const std::string aFeatureError = "Error : Multitranslation algorithm failed.";
197 setError(aFeatureError);
200 if (aRotationnAlgo->shape()->isNull()) {
201 static const std::string aShapeError = "Error : Resulting shape is null.";
202 setError(aShapeError);
205 if (!aRotationnAlgo->isValid()) {
206 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
207 setError(aFeatureError);
210 aListOfShape.push_back(aRotationnAlgo->shape());
211 aListOfRotationAlgo.push_back(aRotationnAlgo);
213 std::shared_ptr<GeomAPI_Shape> aCompound =
214 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
215 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
216 aResultBody->storeModified(aBaseShape, aCompound);
217 loadNamingDS(aListOfRotationAlgo, aResultBody, aBaseShape);
219 setResult(aResultBody, aResultIndex);
224 // Remove the rest results if there were produced in the previous pass.
225 removeResults(aResultIndex);
228 //=================================================================================================
229 #ifdef FEATURE_MULTIROTATION_TWO_DIRECTIONS
230 void FeaturesPlugin_MultiRotation::performRotation2D()
233 ListOfShape anObjects;
234 std::list<ResultPtr> aContextes;
235 AttributeSelectionListPtr anObjectsSelList =
236 selectionList(FeaturesPlugin_MultiRotation::OBJECTS_LIST_ID());
237 if (anObjectsSelList->size() == 0) {
240 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
241 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
242 anObjectsSelList->value(anObjectsIndex);
243 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
244 if(!anObject.get()) { // may be for not-activated parts
247 anObjects.push_back(anObject);
248 aContextes.push_back(anObjectAttr->context());
252 std::shared_ptr<GeomAPI_Ax1> anAxis;
253 std::shared_ptr<GeomAPI_Edge> anEdge;
254 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
255 selection(FeaturesPlugin_MultiRotation::AXIS_ANGULAR_ID());
256 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
257 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
258 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
259 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
260 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
263 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
264 anEdge->line()->direction()));
267 // Getting number of copies int he angular direction.
269 integer(FeaturesPlugin_MultiRotation::NB_COPIES_ANGULAR_ID())->value();
272 std::string aFeatureError = "Multirotation builder ";
273 aFeatureError+=":: the number of copies for the angular direction is null or negative.";
274 setError(aFeatureError);
278 // Getting number of copies int he radial direction.
280 integer(FeaturesPlugin_MultiRotation::NB_COPIES_RADIAL_ID())->value();
283 std::string aFeatureError = "Multirotation builder ";
284 aFeatureError+=":: the number of copies for the radial direction is null or negative.";
285 setError(aFeatureError);
291 std::string useAngularStep =
292 string(FeaturesPlugin_MultiRotation::USE_ANGULAR_STEP_ID())->value();
293 if (!useAngularStep.empty()) {
294 anAngle = real(FeaturesPlugin_MultiRotation::STEP_ANGULAR_ID())->value();
296 anAngle = 360./nbAngular;
300 double aStep = real(FeaturesPlugin_MultiRotation::STEP_RADIAL_ID())->value();
302 // Moving each object.
303 int aResultIndex = 0;
304 std::list<ResultPtr>::iterator aContext = aContextes.begin();
305 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
306 anObjectsIt++, aContext++) {
307 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
308 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
310 std::shared_ptr<GeomAPI_Dir> aDir =
311 GeomAlgoAPI_ShapeTools::buildDirFromAxisAndShape(aBaseShape, anAxis);
312 double x = aDir->x();
313 double y = aDir->y();
314 double z = aDir->z();
315 double norm = sqrt(x*x+y*y+z*z);
319 /*ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
320 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
321 for (int j=0; j<aSecondNbCopies; j++) {
322 for (int i=0; i<aFirstNbCopies; i++) {
323 double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
324 double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
325 double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
326 aTrsf->setTranslation(dx, dy, dz);
327 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
328 aResultPart->setTrsf(*aContext, aTrsf);
329 setResult(aResultPart, aResultIndex);
334 ListOfShape aListOfShape;
335 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > aListOfTranslationAlgo;
336 std::list<std::shared_ptr<GeomAlgoAPI_Rotation> > aListOfRotationAlgo;
337 for (int j=0; j<nbRadial; j++) {
339 double dx = j*aStep*x/norm;
340 double dy = j*aStep*y/norm;
341 double dz = j*aStep*z/norm;
342 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
343 new GeomAlgoAPI_Translation(aBaseShape, dx, dy, dz));
345 if (!aTranslationAlgo->check()) {
346 setError(aTranslationAlgo->getError());
350 aTranslationAlgo->build();
352 // Checking that the algorithm worked properly.
353 if (!aTranslationAlgo->isDone()) {
354 static const std::string aFeatureError = "Error : Multirotation algorithm failed.";
355 setError(aFeatureError);
358 if (aTranslationAlgo->shape()->isNull()) {
359 static const std::string aShapeError = "Error : Resulting shape is null.";
360 setError(aShapeError);
363 if (!aTranslationAlgo->isValid()) {
364 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
365 setError(aFeatureError);
368 aListOfShape.push_back(aTranslationAlgo->shape());
369 aListOfTranslationAlgo.push_back(aTranslationAlgo);
370 for (int i=1; i<nbAngular; i++) {
371 std::shared_ptr<GeomAlgoAPI_Rotation> aRotationnAlgo(
372 new GeomAlgoAPI_Rotation(aTranslationAlgo->shape(), anAxis, i*anAngle));
373 if (!aRotationnAlgo->check()) {
374 setError(aTranslationAlgo->getError());
377 aRotationnAlgo->build();// Checking that the algorithm worked properly.
378 if (!aRotationnAlgo->isDone()) {
379 static const std::string aFeatureError = "Error : Multirotation algorithm failed.";
380 setError(aFeatureError);
383 if (aRotationnAlgo->shape()->isNull()) {
384 static const std::string aShapeError = "Error : Resulting shape is null.";
385 setError(aShapeError);
388 if (!aRotationnAlgo->isValid()) {
389 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
390 setError(aFeatureError);
393 aListOfShape.push_back(aRotationnAlgo->shape());
394 aListOfRotationAlgo.push_back(aRotationnAlgo);
397 std::shared_ptr<GeomAPI_Shape> aCompound =
398 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
399 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
400 aResultBody->storeModified(aBaseShape, aCompound);
402 loadNamingDS2(aListOfTranslationAlgo, aResultBody, aBaseShape);
403 loadNamingDS3(aListOfRotationAlgo, aResultBody, aBaseShape, nbRadial);
404 setResult(aResultBody, aResultIndex);
409 // Remove the rest results if there were produced in the previous pass.
410 removeResults(aResultIndex);
413 //=================================================================================================
414 void FeaturesPlugin_MultiRotation::loadNamingDS2(
415 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > theListOfTranslationAlgo,
416 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
417 std::shared_ptr<GeomAPI_Shape> theBaseShape)
419 for (std::list<std::shared_ptr<GeomAlgoAPI_Translation> >::const_iterator anIt =
420 theListOfTranslationAlgo.begin(); anIt != theListOfTranslationAlgo.cend(); ++anIt) {
422 theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::FACE, "Rotated_Face");
425 theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::EDGE, "Rotated_Edge");
428 theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::VERTEX, "Rotated_Vertex");
432 //=================================================================================================
433 void FeaturesPlugin_MultiRotation::loadNamingDS3(
434 std::list<std::shared_ptr<GeomAlgoAPI_Rotation> > theListOfRotationAlgo,
435 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
436 std::shared_ptr<GeomAPI_Shape> theBaseShape, int nb)
439 std::string aRotatedName;
441 for (std::list<std::shared_ptr<GeomAlgoAPI_Rotation> >::const_iterator anIt =
442 theListOfRotationAlgo.begin(); anIt != theListOfRotationAlgo.cend(); ++anIt) {
446 GeomAPI_ShapeExplorer anExp((*anIt)->shape(), GeomAPI_Shape::FACE);
447 for(; anExp.more(); anExp.next()) {
448 aRotatedName = "Rotated_Face_" + std::to_string((long long) anIndex);
449 aRotatedName = aRotatedName + "_" + std::to_string((long long) numFace);
450 theResultBody->generated(anExp.current(), aRotatedName);
458 //=================================================================================================
459 void FeaturesPlugin_MultiRotation::loadNamingDS(
460 std::list<std::shared_ptr<GeomAlgoAPI_Rotation> > theListOfRotationAlgo,
461 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
462 std::shared_ptr<GeomAPI_Shape> theBaseShape)
464 for (std::list<std::shared_ptr<GeomAlgoAPI_Rotation> >::const_iterator anIt =
465 theListOfRotationAlgo.begin(); anIt != theListOfRotationAlgo.cend(); ++anIt) {
467 theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::FACE, "Rotated_Face");
470 theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::EDGE, "Rotated_Edge");
473 theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::VERTEX, "Rotated_Vertex");