1 // Copyright (C) 2017-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File: FeaturesPlugin_MultiRotation.cpp
21 // Created: 30 Jan 2017
22 // Author: Clarisse Genrault (CEA)
24 #include <FeaturesPlugin_MultiRotation.h>
25 #include <FeaturesPlugin_Tools.h>
27 #include <GeomAlgoAPI_CompoundBuilder.h>
28 #include <GeomAlgoAPI_MakeShapeList.h>
29 #include <GeomAlgoAPI_ShapeTools.h>
30 #include <GeomAlgoAPI_Tools.h>
31 #include <GeomAlgoAPI_Translation.h>
33 #include <GeomAPI_ShapeExplorer.h>
35 #include <GeomAPI_Ax1.h>
36 #include <GeomAPI_Edge.h>
37 #include <GeomAPI_Lin.h>
38 #include <GeomAPI_ShapeIterator.h>
39 #include <GeomAPI_Trsf.h>
41 #include <ModelAPI_AttributeDouble.h>
42 #include <ModelAPI_AttributeInteger.h>
43 #include <ModelAPI_AttributeSelectionList.h>
44 #include <ModelAPI_AttributeString.h>
45 #include <ModelAPI_ResultBody.h>
46 #include <ModelAPI_ResultPart.h>
51 //=================================================================================================
52 FeaturesPlugin_MultiRotation::FeaturesPlugin_MultiRotation()
56 //=================================================================================================
57 void FeaturesPlugin_MultiRotation::initAttributes()
59 AttributeSelectionListPtr aSelection =
60 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
61 FeaturesPlugin_MultiRotation::OBJECTS_LIST_ID(),
62 ModelAPI_AttributeSelectionList::typeId()));
64 data()->addAttribute(FeaturesPlugin_MultiRotation::AXIS_ANGULAR_ID(),
65 ModelAPI_AttributeSelection::typeId());
66 data()->addAttribute(FeaturesPlugin_MultiRotation::USE_ANGULAR_STEP_ID(),
67 ModelAPI_AttributeString::typeId());
68 data()->addAttribute(FeaturesPlugin_MultiRotation::STEP_ANGULAR_ID(),
69 ModelAPI_AttributeDouble::typeId());
70 data()->addAttribute(FeaturesPlugin_MultiRotation::NB_COPIES_ANGULAR_ID(),
71 ModelAPI_AttributeInteger::typeId());
73 #ifdef FEATURE_MULTIROTATION_TWO_DIRECTIONS
74 data()->addAttribute(FeaturesPlugin_MultiRotation::USE_RADIAL_DIR_ID(),
75 ModelAPI_AttributeString::typeId());
76 data()->addAttribute(FeaturesPlugin_MultiRotation::STEP_RADIAL_ID(),
77 ModelAPI_AttributeDouble::typeId());
78 data()->addAttribute(FeaturesPlugin_MultiRotation::NB_COPIES_RADIAL_ID(),
79 ModelAPI_AttributeInteger::typeId());
83 //=================================================================================================
84 void FeaturesPlugin_MultiRotation::execute()
86 #ifdef FEATURE_MULTIROTATION_TWO_DIRECTIONS
87 std::string useRadialDir = string(FeaturesPlugin_MultiRotation::USE_RADIAL_DIR_ID())->value();
88 if (useRadialDir.empty()) {
98 //=================================================================================================
99 void FeaturesPlugin_MultiRotation::performRotation1D()
102 ListOfShape anObjects;
103 std::list<ResultPtr> aContextes;
104 AttributeSelectionListPtr anObjectsSelList =
105 selectionList(FeaturesPlugin_MultiRotation::OBJECTS_LIST_ID());
106 if (anObjectsSelList->size() == 0) {
107 setError("Error: empty selection list");
110 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
111 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
112 anObjectsSelList->value(anObjectsIndex);
113 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
114 if(!anObject.get()) { // may be for not-activated parts
117 anObjects.push_back(anObject);
118 aContextes.push_back(anObjectAttr->context());
122 static const std::string aSelectionError = "Error: The axis shape selection is bad.";
123 AttributeSelectionPtr anObjRef = selection(AXIS_ANGULAR_ID());
124 GeomShapePtr aShape = anObjRef->value();
126 if (anObjRef->context().get()) {
127 aShape = anObjRef->context()->shape();
131 setError(aSelectionError);
136 if (aShape->isEdge())
138 anEdge = aShape->edge();
140 else if (aShape->isCompound())
142 GeomAPI_ShapeIterator anIt(aShape);
143 anEdge = anIt.current()->edge();
148 setError(aSelectionError);
152 std::shared_ptr<GeomAPI_Ax1> anAxis(new GeomAPI_Ax1(anEdge->line()->location(),
153 anEdge->line()->direction()));
155 // Getting number of copies.
157 integer(FeaturesPlugin_MultiRotation::NB_COPIES_ANGULAR_ID())->value();
160 std::string aFeatureError = "Multirotation builder ";
161 aFeatureError+=":: the number of copies for the angular direction is null or negative.";
162 setError(aFeatureError);
168 std::string useAngularStep =
169 string(FeaturesPlugin_MultiRotation::USE_ANGULAR_STEP_ID())->value();
170 if (!useAngularStep.empty()) {
171 anAngle = real(FeaturesPlugin_MultiRotation::STEP_ANGULAR_ID())->value();
173 anAngle = 360./nbCopies;
176 // Moving each object.
177 int aResultIndex = 0;
178 std::list<ResultPtr>::iterator aContext = aContextes.begin();
179 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
180 anObjectsIt++, aContext++) {
181 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
182 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
186 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
187 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
188 for (int i=0; i<nbCopies; i++) {
189 aTrsf->setRotation(anAxis, i*anAngle);
190 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
191 aResultPart->setTrsf(*aContext, aTrsf);
192 setResult(aResultPart, aResultIndex);
197 ListOfShape aListOfShape;
198 std::shared_ptr<GeomAlgoAPI_MakeShapeList>
199 aListOfRotationAlgo(new GeomAlgoAPI_MakeShapeList);
201 for (int i=0; i<nbCopies; i++) {
202 std::shared_ptr<GeomAlgoAPI_Rotation> aRotationnAlgo(
203 new GeomAlgoAPI_Rotation(aBaseShape, anAxis, i*anAngle));
205 // Checking that the algorithm worked properly.
206 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aRotationnAlgo, getKind(), anError)) {
210 aListOfShape.push_back(aRotationnAlgo->shape());
211 aListOfRotationAlgo->appendAlgo(aRotationnAlgo);
213 std::shared_ptr<GeomAPI_Shape> aCompound =
214 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
215 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
217 ListOfShape aBaseShapes;
218 aBaseShapes.push_back(aBaseShape);
219 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShapes, ListOfShape(),
220 aListOfRotationAlgo, aCompound, "Rotated");
222 setResult(aResultBody, aResultIndex);
227 // Remove the rest results if there were produced in the previous pass.
228 removeResults(aResultIndex);
231 //=================================================================================================
232 #ifdef FEATURE_MULTIROTATION_TWO_DIRECTIONS
233 void FeaturesPlugin_MultiRotation::performRotation2D()
236 ListOfShape anObjects;
237 std::list<ResultPtr> aContextes;
238 AttributeSelectionListPtr anObjectsSelList =
239 selectionList(FeaturesPlugin_MultiRotation::OBJECTS_LIST_ID());
240 if (anObjectsSelList->size() == 0) {
243 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
244 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
245 anObjectsSelList->value(anObjectsIndex);
246 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
247 if(!anObject.get()) { // may be for not-activated parts
250 anObjects.push_back(anObject);
251 aContextes.push_back(anObjectAttr->context());
255 std::shared_ptr<GeomAPI_Ax1> anAxis;
256 std::shared_ptr<GeomAPI_Edge> anEdge;
257 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
258 selection(FeaturesPlugin_MultiRotation::AXIS_ANGULAR_ID());
259 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
260 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
261 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
262 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
263 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
266 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
267 anEdge->line()->direction()));
270 // Getting number of copies int he angular direction.
272 integer(FeaturesPlugin_MultiRotation::NB_COPIES_ANGULAR_ID())->value();
275 std::string aFeatureError = "Multirotation builder ";
276 aFeatureError+=":: the number of copies for the angular direction is null or negative.";
277 setError(aFeatureError);
281 // Getting number of copies int he radial direction.
283 integer(FeaturesPlugin_MultiRotation::NB_COPIES_RADIAL_ID())->value();
286 std::string aFeatureError = "Multirotation builder ";
287 aFeatureError+=":: the number of copies for the radial direction is null or negative.";
288 setError(aFeatureError);
294 std::string useAngularStep =
295 string(FeaturesPlugin_MultiRotation::USE_ANGULAR_STEP_ID())->value();
296 if (!useAngularStep.empty()) {
297 anAngle = real(FeaturesPlugin_MultiRotation::STEP_ANGULAR_ID())->value();
299 anAngle = 360./nbAngular;
303 double aStep = real(FeaturesPlugin_MultiRotation::STEP_RADIAL_ID())->value();
305 // Moving each object.
306 int aResultIndex = 0;
307 std::list<ResultPtr>::iterator aContext = aContextes.begin();
308 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
309 anObjectsIt++, aContext++) {
310 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
311 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
313 std::shared_ptr<GeomAPI_Dir> aDir =
314 GeomAlgoAPI_ShapeTools::buildDirFromAxisAndShape(aBaseShape, anAxis);
315 double x = aDir->x();
316 double y = aDir->y();
317 double z = aDir->z();
318 double norm = sqrt(x*x+y*y+z*z);
322 /*ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
323 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
324 for (int j=0; j<aSecondNbCopies; j++) {
325 for (int i=0; i<aFirstNbCopies; i++) {
326 double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
327 double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
328 double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
329 aTrsf->setTranslation(dx, dy, dz);
330 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
331 aResultPart->setTrsf(*aContext, aTrsf);
332 setResult(aResultPart, aResultIndex);
337 ListOfShape aListOfShape;
338 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > aListOfTranslationAlgo;
339 std::list<std::shared_ptr<GeomAlgoAPI_Rotation> > aListOfRotationAlgo;
340 for (int j=0; j<nbRadial; j++) {
342 double dx = j*aStep*x/norm;
343 double dy = j*aStep*y/norm;
344 double dz = j*aStep*z/norm;
345 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
346 new GeomAlgoAPI_Translation(aBaseShape, dx, dy, dz));
348 if (!aTranslationAlgo->check()) {
349 setError(aTranslationAlgo->getError());
353 aTranslationAlgo->build();
355 // Checking that the algorithm worked properly.
356 if (!aTranslationAlgo->isDone()) {
357 static const std::string aFeatureError = "Error : Multirotation algorithm failed.";
358 setError(aFeatureError);
361 if (aTranslationAlgo->shape()->isNull()) {
362 static const std::string aShapeError = "Error : Resulting shape is null.";
363 setError(aShapeError);
366 if (!aTranslationAlgo->isValid()) {
367 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
368 setError(aFeatureError);
371 aListOfShape.push_back(aTranslationAlgo->shape());
372 aListOfTranslationAlgo.push_back(aTranslationAlgo);
373 for (int i=1; i<nbAngular; i++) {
374 std::shared_ptr<GeomAlgoAPI_Rotation> aRotationnAlgo(
375 new GeomAlgoAPI_Rotation(aTranslationAlgo->shape(), anAxis, i*anAngle));
376 if (!aRotationnAlgo->check()) {
377 setError(aTranslationAlgo->getError());
380 aRotationnAlgo->build();// Checking that the algorithm worked properly.
381 if (!aRotationnAlgo->isDone()) {
382 static const std::string aFeatureError = "Error : Multirotation algorithm failed.";
383 setError(aFeatureError);
386 if (aRotationnAlgo->shape()->isNull()) {
387 static const std::string aShapeError = "Error : Resulting shape is null.";
388 setError(aShapeError);
391 if (!aRotationnAlgo->isValid()) {
392 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
393 setError(aFeatureError);
396 aListOfShape.push_back(aRotationnAlgo->shape());
397 aListOfRotationAlgo.push_back(aRotationnAlgo);
400 std::shared_ptr<GeomAPI_Shape> aCompound =
401 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
402 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
403 aResultBody->storeModified(aBaseShape, aCompound);
405 loadNamingDS2(aListOfTranslationAlgo, aResultBody, aBaseShape);
406 loadNamingDS3(aListOfRotationAlgo, aResultBody, aBaseShape, nbRadial);
407 setResult(aResultBody, aResultIndex);
412 // Remove the rest results if there were produced in the previous pass.
413 removeResults(aResultIndex);
416 //=================================================================================================
417 void FeaturesPlugin_MultiRotation::loadNamingDS2(
418 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > theListOfTranslationAlgo,
419 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
420 std::shared_ptr<GeomAPI_Shape> theBaseShape)
422 for (std::list<std::shared_ptr<GeomAlgoAPI_Translation> >::const_iterator anIt =
423 theListOfTranslationAlgo.begin(); anIt != theListOfTranslationAlgo.cend(); ++anIt) {
425 theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::FACE, "Rotated_Face");
428 theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::EDGE, "Rotated_Edge");
431 theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::VERTEX, "Rotated_Vertex");
435 //=================================================================================================
436 void FeaturesPlugin_MultiRotation::loadNamingDS3(
437 std::list<std::shared_ptr<GeomAlgoAPI_Rotation> > theListOfRotationAlgo,
438 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
439 std::shared_ptr<GeomAPI_Shape> theBaseShape, int nb)
442 std::string aRotatedName;
444 for (std::list<std::shared_ptr<GeomAlgoAPI_Rotation> >::const_iterator anIt =
445 theListOfRotationAlgo.begin(); anIt != theListOfRotationAlgo.cend(); ++anIt) {
449 GeomAPI_ShapeExplorer anExp((*anIt)->shape(), GeomAPI_Shape::FACE);
450 for(; anExp.more(); anExp.next()) {
451 aRotatedName = "Rotated_Face_" + std::to_string((long long) anIndex);
452 aRotatedName = aRotatedName + "_" + std::to_string((long long) numFace);
453 theResultBody->generated(anExp.current(), aRotatedName);