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>
8 #include <FeaturesPlugin_Tools.h>
10 #include <GeomAlgoAPI_CompoundBuilder.h>
11 #include <GeomAlgoAPI_MakeShapeList.h>
12 #include <GeomAlgoAPI_ShapeTools.h>
13 #include <GeomAlgoAPI_Tools.h>
14 #include <GeomAlgoAPI_Translation.h>
16 #include <GeomAPI_ShapeExplorer.h>
18 #include <GeomAPI_Ax1.h>
19 #include <GeomAPI_Edge.h>
20 #include <GeomAPI_Lin.h>
21 #include <GeomAPI_ShapeIterator.h>
22 #include <GeomAPI_Trsf.h>
24 #include <ModelAPI_AttributeDouble.h>
25 #include <ModelAPI_AttributeInteger.h>
26 #include <ModelAPI_AttributeSelectionList.h>
27 #include <ModelAPI_AttributeString.h>
28 #include <ModelAPI_ResultBody.h>
29 #include <ModelAPI_ResultPart.h>
34 //=================================================================================================
35 FeaturesPlugin_MultiRotation::FeaturesPlugin_MultiRotation()
39 //=================================================================================================
40 void FeaturesPlugin_MultiRotation::initAttributes()
42 AttributeSelectionListPtr aSelection =
43 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
44 FeaturesPlugin_MultiRotation::OBJECTS_LIST_ID(),
45 ModelAPI_AttributeSelectionList::typeId()));
47 data()->addAttribute(FeaturesPlugin_MultiRotation::AXIS_ANGULAR_ID(),
48 ModelAPI_AttributeSelection::typeId());
49 data()->addAttribute(FeaturesPlugin_MultiRotation::USE_ANGULAR_STEP_ID(),
50 ModelAPI_AttributeString::typeId());
51 data()->addAttribute(FeaturesPlugin_MultiRotation::STEP_ANGULAR_ID(),
52 ModelAPI_AttributeDouble::typeId());
53 data()->addAttribute(FeaturesPlugin_MultiRotation::NB_COPIES_ANGULAR_ID(),
54 ModelAPI_AttributeInteger::typeId());
56 #ifdef FEATURE_MULTIROTATION_TWO_DIRECTIONS
57 data()->addAttribute(FeaturesPlugin_MultiRotation::USE_RADIAL_DIR_ID(),
58 ModelAPI_AttributeString::typeId());
59 data()->addAttribute(FeaturesPlugin_MultiRotation::STEP_RADIAL_ID(),
60 ModelAPI_AttributeDouble::typeId());
61 data()->addAttribute(FeaturesPlugin_MultiRotation::NB_COPIES_RADIAL_ID(),
62 ModelAPI_AttributeInteger::typeId());
66 //=================================================================================================
67 void FeaturesPlugin_MultiRotation::execute()
69 #ifdef FEATURE_MULTIROTATION_TWO_DIRECTIONS
70 std::string useRadialDir = string(FeaturesPlugin_MultiRotation::USE_RADIAL_DIR_ID())->value();
71 if (useRadialDir.empty()) {
81 //=================================================================================================
82 void FeaturesPlugin_MultiRotation::performRotation1D()
85 ListOfShape anObjects;
86 std::list<ResultPtr> aContextes;
87 AttributeSelectionListPtr anObjectsSelList =
88 selectionList(FeaturesPlugin_MultiRotation::OBJECTS_LIST_ID());
89 if (anObjectsSelList->size() == 0) {
90 setError("Error: empty selection list");
93 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
94 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
95 anObjectsSelList->value(anObjectsIndex);
96 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
97 if(!anObject.get()) { // may be for not-activated parts
100 anObjects.push_back(anObject);
101 aContextes.push_back(anObjectAttr->context());
105 static const std::string aSelectionError = "Error: The axis shape selection is bad.";
106 AttributeSelectionPtr anObjRef = selection(AXIS_ANGULAR_ID());
107 GeomShapePtr aShape = anObjRef->value();
109 if (anObjRef->context().get()) {
110 aShape = anObjRef->context()->shape();
114 setError(aSelectionError);
119 if (aShape->isEdge())
121 anEdge = aShape->edge();
123 else if (aShape->isCompound())
125 GeomAPI_ShapeIterator anIt(aShape);
126 anEdge = anIt.current()->edge();
131 setError(aSelectionError);
135 std::shared_ptr<GeomAPI_Ax1> anAxis(new GeomAPI_Ax1(anEdge->line()->location(),
136 anEdge->line()->direction()));
138 // Getting number of copies.
140 integer(FeaturesPlugin_MultiRotation::NB_COPIES_ANGULAR_ID())->value();
143 std::string aFeatureError = "Multirotation builder ";
144 aFeatureError+=":: the number of copies for the angular direction is null or negative.";
145 setError(aFeatureError);
151 std::string useAngularStep =
152 string(FeaturesPlugin_MultiRotation::USE_ANGULAR_STEP_ID())->value();
153 if (!useAngularStep.empty()) {
154 anAngle = real(FeaturesPlugin_MultiRotation::STEP_ANGULAR_ID())->value();
156 anAngle = 360./nbCopies;
159 // Moving each object.
160 int aResultIndex = 0;
161 std::list<ResultPtr>::iterator aContext = aContextes.begin();
162 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
163 anObjectsIt++, aContext++) {
164 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
165 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
169 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
170 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
171 for (int i=0; i<nbCopies; i++) {
172 aTrsf->setRotation(anAxis, i*anAngle);
173 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
174 aResultPart->setTrsf(*aContext, aTrsf);
175 setResult(aResultPart, aResultIndex);
180 ListOfShape aListOfShape;
181 std::shared_ptr<GeomAlgoAPI_MakeShapeList>
182 aListOfRotationAlgo(new GeomAlgoAPI_MakeShapeList);
184 for (int i=0; i<nbCopies; i++) {
185 std::shared_ptr<GeomAlgoAPI_Rotation> aRotationnAlgo(
186 new GeomAlgoAPI_Rotation(aBaseShape, anAxis, i*anAngle));
188 if (!aRotationnAlgo->check()) {
189 setError(aRotationnAlgo->getError());
193 aRotationnAlgo->build();
195 // Checking that the algorithm worked properly.
196 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aRotationnAlgo, getKind(), anError)) {
200 aListOfShape.push_back(aRotationnAlgo->shape());
201 aListOfRotationAlgo->appendAlgo(aRotationnAlgo);
203 std::shared_ptr<GeomAPI_Shape> aCompound =
204 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
205 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
207 ListOfShape aBaseShapes;
208 aBaseShapes.push_back(aBaseShape);
209 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShapes, ListOfShape(),
210 aListOfRotationAlgo, aCompound, "Rotated");
212 setResult(aResultBody, aResultIndex);
217 // Remove the rest results if there were produced in the previous pass.
218 removeResults(aResultIndex);
221 //=================================================================================================
222 #ifdef FEATURE_MULTIROTATION_TWO_DIRECTIONS
223 void FeaturesPlugin_MultiRotation::performRotation2D()
226 ListOfShape anObjects;
227 std::list<ResultPtr> aContextes;
228 AttributeSelectionListPtr anObjectsSelList =
229 selectionList(FeaturesPlugin_MultiRotation::OBJECTS_LIST_ID());
230 if (anObjectsSelList->size() == 0) {
233 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
234 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
235 anObjectsSelList->value(anObjectsIndex);
236 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
237 if(!anObject.get()) { // may be for not-activated parts
240 anObjects.push_back(anObject);
241 aContextes.push_back(anObjectAttr->context());
245 std::shared_ptr<GeomAPI_Ax1> anAxis;
246 std::shared_ptr<GeomAPI_Edge> anEdge;
247 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
248 selection(FeaturesPlugin_MultiRotation::AXIS_ANGULAR_ID());
249 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
250 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
251 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
252 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
253 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
256 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
257 anEdge->line()->direction()));
260 // Getting number of copies int he angular direction.
262 integer(FeaturesPlugin_MultiRotation::NB_COPIES_ANGULAR_ID())->value();
265 std::string aFeatureError = "Multirotation builder ";
266 aFeatureError+=":: the number of copies for the angular direction is null or negative.";
267 setError(aFeatureError);
271 // Getting number of copies int he radial direction.
273 integer(FeaturesPlugin_MultiRotation::NB_COPIES_RADIAL_ID())->value();
276 std::string aFeatureError = "Multirotation builder ";
277 aFeatureError+=":: the number of copies for the radial direction is null or negative.";
278 setError(aFeatureError);
284 std::string useAngularStep =
285 string(FeaturesPlugin_MultiRotation::USE_ANGULAR_STEP_ID())->value();
286 if (!useAngularStep.empty()) {
287 anAngle = real(FeaturesPlugin_MultiRotation::STEP_ANGULAR_ID())->value();
289 anAngle = 360./nbAngular;
293 double aStep = real(FeaturesPlugin_MultiRotation::STEP_RADIAL_ID())->value();
295 // Moving each object.
296 int aResultIndex = 0;
297 std::list<ResultPtr>::iterator aContext = aContextes.begin();
298 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
299 anObjectsIt++, aContext++) {
300 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
301 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
303 std::shared_ptr<GeomAPI_Dir> aDir =
304 GeomAlgoAPI_ShapeTools::buildDirFromAxisAndShape(aBaseShape, anAxis);
305 double x = aDir->x();
306 double y = aDir->y();
307 double z = aDir->z();
308 double norm = sqrt(x*x+y*y+z*z);
312 /*ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
313 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
314 for (int j=0; j<aSecondNbCopies; j++) {
315 for (int i=0; i<aFirstNbCopies; i++) {
316 double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
317 double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
318 double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
319 aTrsf->setTranslation(dx, dy, dz);
320 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
321 aResultPart->setTrsf(*aContext, aTrsf);
322 setResult(aResultPart, aResultIndex);
327 ListOfShape aListOfShape;
328 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > aListOfTranslationAlgo;
329 std::list<std::shared_ptr<GeomAlgoAPI_Rotation> > aListOfRotationAlgo;
330 for (int j=0; j<nbRadial; j++) {
332 double dx = j*aStep*x/norm;
333 double dy = j*aStep*y/norm;
334 double dz = j*aStep*z/norm;
335 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
336 new GeomAlgoAPI_Translation(aBaseShape, dx, dy, dz));
338 if (!aTranslationAlgo->check()) {
339 setError(aTranslationAlgo->getError());
343 aTranslationAlgo->build();
345 // Checking that the algorithm worked properly.
346 if (!aTranslationAlgo->isDone()) {
347 static const std::string aFeatureError = "Error : Multirotation algorithm failed.";
348 setError(aFeatureError);
351 if (aTranslationAlgo->shape()->isNull()) {
352 static const std::string aShapeError = "Error : Resulting shape is null.";
353 setError(aShapeError);
356 if (!aTranslationAlgo->isValid()) {
357 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
358 setError(aFeatureError);
361 aListOfShape.push_back(aTranslationAlgo->shape());
362 aListOfTranslationAlgo.push_back(aTranslationAlgo);
363 for (int i=1; i<nbAngular; i++) {
364 std::shared_ptr<GeomAlgoAPI_Rotation> aRotationnAlgo(
365 new GeomAlgoAPI_Rotation(aTranslationAlgo->shape(), anAxis, i*anAngle));
366 if (!aRotationnAlgo->check()) {
367 setError(aTranslationAlgo->getError());
370 aRotationnAlgo->build();// Checking that the algorithm worked properly.
371 if (!aRotationnAlgo->isDone()) {
372 static const std::string aFeatureError = "Error : Multirotation algorithm failed.";
373 setError(aFeatureError);
376 if (aRotationnAlgo->shape()->isNull()) {
377 static const std::string aShapeError = "Error : Resulting shape is null.";
378 setError(aShapeError);
381 if (!aRotationnAlgo->isValid()) {
382 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
383 setError(aFeatureError);
386 aListOfShape.push_back(aRotationnAlgo->shape());
387 aListOfRotationAlgo.push_back(aRotationnAlgo);
390 std::shared_ptr<GeomAPI_Shape> aCompound =
391 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
392 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
393 aResultBody->storeModified(aBaseShape, aCompound);
395 loadNamingDS2(aListOfTranslationAlgo, aResultBody, aBaseShape);
396 loadNamingDS3(aListOfRotationAlgo, aResultBody, aBaseShape, nbRadial);
397 setResult(aResultBody, aResultIndex);
402 // Remove the rest results if there were produced in the previous pass.
403 removeResults(aResultIndex);
406 //=================================================================================================
407 void FeaturesPlugin_MultiRotation::loadNamingDS2(
408 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > theListOfTranslationAlgo,
409 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
410 std::shared_ptr<GeomAPI_Shape> theBaseShape)
412 for (std::list<std::shared_ptr<GeomAlgoAPI_Translation> >::const_iterator anIt =
413 theListOfTranslationAlgo.begin(); anIt != theListOfTranslationAlgo.cend(); ++anIt) {
415 theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::FACE, "Rotated_Face");
418 theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::EDGE, "Rotated_Edge");
421 theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::VERTEX, "Rotated_Vertex");
425 //=================================================================================================
426 void FeaturesPlugin_MultiRotation::loadNamingDS3(
427 std::list<std::shared_ptr<GeomAlgoAPI_Rotation> > theListOfRotationAlgo,
428 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
429 std::shared_ptr<GeomAPI_Shape> theBaseShape, int nb)
432 std::string aRotatedName;
434 for (std::list<std::shared_ptr<GeomAlgoAPI_Rotation> >::const_iterator anIt =
435 theListOfRotationAlgo.begin(); anIt != theListOfRotationAlgo.cend(); ++anIt) {
439 GeomAPI_ShapeExplorer anExp((*anIt)->shape(), GeomAPI_Shape::FACE);
440 for(; anExp.more(); anExp.next()) {
441 aRotatedName = "Rotated_Face_" + std::to_string((long long) anIndex);
442 aRotatedName = aRotatedName + "_" + std::to_string((long long) numFace);
443 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");