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_Tools.h>
12 #include <GeomAlgoAPI_Translation.h>
14 #include <GeomAPI_ShapeExplorer.h>
16 #include <GeomAPI_Ax1.h>
17 #include <GeomAPI_Edge.h>
18 #include <GeomAPI_Lin.h>
19 #include <GeomAPI_ShapeIterator.h>
20 #include <GeomAPI_Trsf.h>
22 #include <ModelAPI_AttributeDouble.h>
23 #include <ModelAPI_AttributeInteger.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_AttributeString.h>
26 #include <ModelAPI_ResultBody.h>
27 #include <ModelAPI_ResultPart.h>
32 //=================================================================================================
33 FeaturesPlugin_MultiRotation::FeaturesPlugin_MultiRotation()
37 //=================================================================================================
38 void FeaturesPlugin_MultiRotation::initAttributes()
40 AttributeSelectionListPtr aSelection =
41 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
42 FeaturesPlugin_MultiRotation::OBJECTS_LIST_ID(),
43 ModelAPI_AttributeSelectionList::typeId()));
45 data()->addAttribute(FeaturesPlugin_MultiRotation::AXIS_ANGULAR_ID(),
46 ModelAPI_AttributeSelection::typeId());
47 data()->addAttribute(FeaturesPlugin_MultiRotation::USE_ANGULAR_STEP_ID(),
48 ModelAPI_AttributeString::typeId());
49 data()->addAttribute(FeaturesPlugin_MultiRotation::STEP_ANGULAR_ID(),
50 ModelAPI_AttributeDouble::typeId());
51 data()->addAttribute(FeaturesPlugin_MultiRotation::NB_COPIES_ANGULAR_ID(),
52 ModelAPI_AttributeInteger::typeId());
54 #ifdef FEATURE_MULTIROTATION_TWO_DIRECTIONS
55 data()->addAttribute(FeaturesPlugin_MultiRotation::USE_RADIAL_DIR_ID(),
56 ModelAPI_AttributeString::typeId());
57 data()->addAttribute(FeaturesPlugin_MultiRotation::STEP_RADIAL_ID(),
58 ModelAPI_AttributeDouble::typeId());
59 data()->addAttribute(FeaturesPlugin_MultiRotation::NB_COPIES_RADIAL_ID(),
60 ModelAPI_AttributeInteger::typeId());
64 //=================================================================================================
65 void FeaturesPlugin_MultiRotation::execute()
67 #ifdef FEATURE_MULTIROTATION_TWO_DIRECTIONS
68 std::string useRadialDir = string(FeaturesPlugin_MultiRotation::USE_RADIAL_DIR_ID())->value();
69 if (useRadialDir.empty()) {
79 //=================================================================================================
80 void FeaturesPlugin_MultiRotation::performRotation1D()
83 ListOfShape anObjects;
84 std::list<ResultPtr> aContextes;
85 AttributeSelectionListPtr anObjectsSelList =
86 selectionList(FeaturesPlugin_MultiRotation::OBJECTS_LIST_ID());
87 if (anObjectsSelList->size() == 0) {
90 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
91 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
92 anObjectsSelList->value(anObjectsIndex);
93 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
94 if(!anObject.get()) { // may be for not-activated parts
97 anObjects.push_back(anObject);
98 aContextes.push_back(anObjectAttr->context());
102 static const std::string aSelectionError = "Error: The axis shape selection is bad.";
103 AttributeSelectionPtr anObjRef = selection(AXIS_ANGULAR_ID());
104 GeomShapePtr aShape = anObjRef->value();
106 if (anObjRef->context().get()) {
107 aShape = anObjRef->context()->shape();
111 setError(aSelectionError);
116 if (aShape->isEdge())
118 anEdge = aShape->edge();
120 else if (aShape->isCompound())
122 GeomAPI_ShapeIterator anIt(aShape);
123 anEdge = anIt.current()->edge();
128 setError(aSelectionError);
132 std::shared_ptr<GeomAPI_Ax1> anAxis(new GeomAPI_Ax1(anEdge->line()->location(),
133 anEdge->line()->direction()));
135 // Getting number of copies.
137 integer(FeaturesPlugin_MultiRotation::NB_COPIES_ANGULAR_ID())->value();
140 std::string aFeatureError = "Multirotation builder ";
141 aFeatureError+=":: the number of copies for the angular direction is null or negative.";
142 setError(aFeatureError);
148 std::string useAngularStep =
149 string(FeaturesPlugin_MultiRotation::USE_ANGULAR_STEP_ID())->value();
150 if (!useAngularStep.empty()) {
151 anAngle = real(FeaturesPlugin_MultiRotation::STEP_ANGULAR_ID())->value();
153 anAngle = 360./nbCopies;
156 // Moving each object.
157 int aResultIndex = 0;
158 std::list<ResultPtr>::iterator aContext = aContextes.begin();
159 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
160 anObjectsIt++, aContext++) {
161 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
162 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
166 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
167 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
168 for (int i=0; i<nbCopies; i++) {
169 aTrsf->setRotation(anAxis, i*anAngle);
170 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
171 aResultPart->setTrsf(*aContext, aTrsf);
172 setResult(aResultPart, aResultIndex);
177 ListOfShape aListOfShape;
178 std::list<std::shared_ptr<GeomAlgoAPI_Rotation> > aListOfRotationAlgo;
180 for (int i=0; i<nbCopies; i++) {
181 std::shared_ptr<GeomAlgoAPI_Rotation> aRotationnAlgo(
182 new GeomAlgoAPI_Rotation(aBaseShape, anAxis, i*anAngle));
184 if (!aRotationnAlgo->check()) {
185 setError(aRotationnAlgo->getError());
189 aRotationnAlgo->build();
191 // Checking that the algorithm worked properly.
192 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aRotationnAlgo, getKind(), anError)) {
196 aListOfShape.push_back(aRotationnAlgo->shape());
197 aListOfRotationAlgo.push_back(aRotationnAlgo);
199 std::shared_ptr<GeomAPI_Shape> aCompound =
200 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
201 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
202 aResultBody->storeModified(aBaseShape, aCompound);
203 loadNamingDS(aListOfRotationAlgo, aResultBody, aBaseShape);
205 setResult(aResultBody, aResultIndex);
210 // Remove the rest results if there were produced in the previous pass.
211 removeResults(aResultIndex);
214 //=================================================================================================
215 #ifdef FEATURE_MULTIROTATION_TWO_DIRECTIONS
216 void FeaturesPlugin_MultiRotation::performRotation2D()
219 ListOfShape anObjects;
220 std::list<ResultPtr> aContextes;
221 AttributeSelectionListPtr anObjectsSelList =
222 selectionList(FeaturesPlugin_MultiRotation::OBJECTS_LIST_ID());
223 if (anObjectsSelList->size() == 0) {
226 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
227 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
228 anObjectsSelList->value(anObjectsIndex);
229 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
230 if(!anObject.get()) { // may be for not-activated parts
233 anObjects.push_back(anObject);
234 aContextes.push_back(anObjectAttr->context());
238 std::shared_ptr<GeomAPI_Ax1> anAxis;
239 std::shared_ptr<GeomAPI_Edge> anEdge;
240 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
241 selection(FeaturesPlugin_MultiRotation::AXIS_ANGULAR_ID());
242 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
243 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
244 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
245 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
246 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
249 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
250 anEdge->line()->direction()));
253 // Getting number of copies int he angular direction.
255 integer(FeaturesPlugin_MultiRotation::NB_COPIES_ANGULAR_ID())->value();
258 std::string aFeatureError = "Multirotation builder ";
259 aFeatureError+=":: the number of copies for the angular direction is null or negative.";
260 setError(aFeatureError);
264 // Getting number of copies int he radial direction.
266 integer(FeaturesPlugin_MultiRotation::NB_COPIES_RADIAL_ID())->value();
269 std::string aFeatureError = "Multirotation builder ";
270 aFeatureError+=":: the number of copies for the radial direction is null or negative.";
271 setError(aFeatureError);
277 std::string useAngularStep =
278 string(FeaturesPlugin_MultiRotation::USE_ANGULAR_STEP_ID())->value();
279 if (!useAngularStep.empty()) {
280 anAngle = real(FeaturesPlugin_MultiRotation::STEP_ANGULAR_ID())->value();
282 anAngle = 360./nbAngular;
286 double aStep = real(FeaturesPlugin_MultiRotation::STEP_RADIAL_ID())->value();
288 // Moving each object.
289 int aResultIndex = 0;
290 std::list<ResultPtr>::iterator aContext = aContextes.begin();
291 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
292 anObjectsIt++, aContext++) {
293 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
294 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
296 std::shared_ptr<GeomAPI_Dir> aDir =
297 GeomAlgoAPI_ShapeTools::buildDirFromAxisAndShape(aBaseShape, anAxis);
298 double x = aDir->x();
299 double y = aDir->y();
300 double z = aDir->z();
301 double norm = sqrt(x*x+y*y+z*z);
305 /*ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
306 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
307 for (int j=0; j<aSecondNbCopies; j++) {
308 for (int i=0; i<aFirstNbCopies; i++) {
309 double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
310 double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
311 double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
312 aTrsf->setTranslation(dx, dy, dz);
313 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
314 aResultPart->setTrsf(*aContext, aTrsf);
315 setResult(aResultPart, aResultIndex);
320 ListOfShape aListOfShape;
321 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > aListOfTranslationAlgo;
322 std::list<std::shared_ptr<GeomAlgoAPI_Rotation> > aListOfRotationAlgo;
323 for (int j=0; j<nbRadial; j++) {
325 double dx = j*aStep*x/norm;
326 double dy = j*aStep*y/norm;
327 double dz = j*aStep*z/norm;
328 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
329 new GeomAlgoAPI_Translation(aBaseShape, dx, dy, dz));
331 if (!aTranslationAlgo->check()) {
332 setError(aTranslationAlgo->getError());
336 aTranslationAlgo->build();
338 // Checking that the algorithm worked properly.
339 if (!aTranslationAlgo->isDone()) {
340 static const std::string aFeatureError = "Error : Multirotation algorithm failed.";
341 setError(aFeatureError);
344 if (aTranslationAlgo->shape()->isNull()) {
345 static const std::string aShapeError = "Error : Resulting shape is null.";
346 setError(aShapeError);
349 if (!aTranslationAlgo->isValid()) {
350 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
351 setError(aFeatureError);
354 aListOfShape.push_back(aTranslationAlgo->shape());
355 aListOfTranslationAlgo.push_back(aTranslationAlgo);
356 for (int i=1; i<nbAngular; i++) {
357 std::shared_ptr<GeomAlgoAPI_Rotation> aRotationnAlgo(
358 new GeomAlgoAPI_Rotation(aTranslationAlgo->shape(), anAxis, i*anAngle));
359 if (!aRotationnAlgo->check()) {
360 setError(aTranslationAlgo->getError());
363 aRotationnAlgo->build();// Checking that the algorithm worked properly.
364 if (!aRotationnAlgo->isDone()) {
365 static const std::string aFeatureError = "Error : Multirotation algorithm failed.";
366 setError(aFeatureError);
369 if (aRotationnAlgo->shape()->isNull()) {
370 static const std::string aShapeError = "Error : Resulting shape is null.";
371 setError(aShapeError);
374 if (!aRotationnAlgo->isValid()) {
375 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
376 setError(aFeatureError);
379 aListOfShape.push_back(aRotationnAlgo->shape());
380 aListOfRotationAlgo.push_back(aRotationnAlgo);
383 std::shared_ptr<GeomAPI_Shape> aCompound =
384 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
385 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
386 aResultBody->storeModified(aBaseShape, aCompound);
388 loadNamingDS2(aListOfTranslationAlgo, aResultBody, aBaseShape);
389 loadNamingDS3(aListOfRotationAlgo, aResultBody, aBaseShape, nbRadial);
390 setResult(aResultBody, aResultIndex);
395 // Remove the rest results if there were produced in the previous pass.
396 removeResults(aResultIndex);
399 //=================================================================================================
400 void FeaturesPlugin_MultiRotation::loadNamingDS2(
401 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > theListOfTranslationAlgo,
402 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
403 std::shared_ptr<GeomAPI_Shape> theBaseShape)
405 for (std::list<std::shared_ptr<GeomAlgoAPI_Translation> >::const_iterator anIt =
406 theListOfTranslationAlgo.begin(); anIt != theListOfTranslationAlgo.cend(); ++anIt) {
408 theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::FACE, "Rotated_Face");
411 theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::EDGE, "Rotated_Edge");
414 theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::VERTEX, "Rotated_Vertex");
418 //=================================================================================================
419 void FeaturesPlugin_MultiRotation::loadNamingDS3(
420 std::list<std::shared_ptr<GeomAlgoAPI_Rotation> > theListOfRotationAlgo,
421 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
422 std::shared_ptr<GeomAPI_Shape> theBaseShape, int nb)
425 std::string aRotatedName;
427 for (std::list<std::shared_ptr<GeomAlgoAPI_Rotation> >::const_iterator anIt =
428 theListOfRotationAlgo.begin(); anIt != theListOfRotationAlgo.cend(); ++anIt) {
432 GeomAPI_ShapeExplorer anExp((*anIt)->shape(), GeomAPI_Shape::FACE);
433 for(; anExp.more(); anExp.next()) {
434 aRotatedName = "Rotated_Face_" + std::to_string((long long) anIndex);
435 aRotatedName = aRotatedName + "_" + std::to_string((long long) numFace);
436 theResultBody->generated(anExp.current(), aRotatedName);
444 //=================================================================================================
445 void FeaturesPlugin_MultiRotation::loadNamingDS(
446 std::list<std::shared_ptr<GeomAlgoAPI_Rotation> > theListOfRotationAlgo,
447 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
448 std::shared_ptr<GeomAPI_Shape> theBaseShape)
450 for (std::list<std::shared_ptr<GeomAlgoAPI_Rotation> >::const_iterator anIt =
451 theListOfRotationAlgo.begin(); anIt != theListOfRotationAlgo.cend(); ++anIt) {
453 theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::FACE, "Rotated_Face");
456 theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::EDGE, "Rotated_Edge");
459 theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::VERTEX, "Rotated_Vertex");