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 if (!aRotationnAlgo->check()) {
206 setError(aRotationnAlgo->getError());
210 aRotationnAlgo->build();
212 // Checking that the algorithm worked properly.
213 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aRotationnAlgo, getKind(), anError)) {
217 aListOfShape.push_back(aRotationnAlgo->shape());
218 aListOfRotationAlgo->appendAlgo(aRotationnAlgo);
220 std::shared_ptr<GeomAPI_Shape> aCompound =
221 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
222 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
224 ListOfShape aBaseShapes;
225 aBaseShapes.push_back(aBaseShape);
226 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShapes, ListOfShape(),
227 aListOfRotationAlgo, aCompound, "Rotated");
229 setResult(aResultBody, aResultIndex);
234 // Remove the rest results if there were produced in the previous pass.
235 removeResults(aResultIndex);
238 //=================================================================================================
239 #ifdef FEATURE_MULTIROTATION_TWO_DIRECTIONS
240 void FeaturesPlugin_MultiRotation::performRotation2D()
243 ListOfShape anObjects;
244 std::list<ResultPtr> aContextes;
245 AttributeSelectionListPtr anObjectsSelList =
246 selectionList(FeaturesPlugin_MultiRotation::OBJECTS_LIST_ID());
247 if (anObjectsSelList->size() == 0) {
250 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
251 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
252 anObjectsSelList->value(anObjectsIndex);
253 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
254 if(!anObject.get()) { // may be for not-activated parts
257 anObjects.push_back(anObject);
258 aContextes.push_back(anObjectAttr->context());
262 std::shared_ptr<GeomAPI_Ax1> anAxis;
263 std::shared_ptr<GeomAPI_Edge> anEdge;
264 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
265 selection(FeaturesPlugin_MultiRotation::AXIS_ANGULAR_ID());
266 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
267 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
268 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
269 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
270 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
273 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
274 anEdge->line()->direction()));
277 // Getting number of copies int he angular direction.
279 integer(FeaturesPlugin_MultiRotation::NB_COPIES_ANGULAR_ID())->value();
282 std::string aFeatureError = "Multirotation builder ";
283 aFeatureError+=":: the number of copies for the angular direction is null or negative.";
284 setError(aFeatureError);
288 // Getting number of copies int he radial direction.
290 integer(FeaturesPlugin_MultiRotation::NB_COPIES_RADIAL_ID())->value();
293 std::string aFeatureError = "Multirotation builder ";
294 aFeatureError+=":: the number of copies for the radial direction is null or negative.";
295 setError(aFeatureError);
301 std::string useAngularStep =
302 string(FeaturesPlugin_MultiRotation::USE_ANGULAR_STEP_ID())->value();
303 if (!useAngularStep.empty()) {
304 anAngle = real(FeaturesPlugin_MultiRotation::STEP_ANGULAR_ID())->value();
306 anAngle = 360./nbAngular;
310 double aStep = real(FeaturesPlugin_MultiRotation::STEP_RADIAL_ID())->value();
312 // Moving each object.
313 int aResultIndex = 0;
314 std::list<ResultPtr>::iterator aContext = aContextes.begin();
315 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
316 anObjectsIt++, aContext++) {
317 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
318 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
320 std::shared_ptr<GeomAPI_Dir> aDir =
321 GeomAlgoAPI_ShapeTools::buildDirFromAxisAndShape(aBaseShape, anAxis);
322 double x = aDir->x();
323 double y = aDir->y();
324 double z = aDir->z();
325 double norm = sqrt(x*x+y*y+z*z);
329 /*ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
330 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
331 for (int j=0; j<aSecondNbCopies; j++) {
332 for (int i=0; i<aFirstNbCopies; i++) {
333 double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
334 double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
335 double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
336 aTrsf->setTranslation(dx, dy, dz);
337 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
338 aResultPart->setTrsf(*aContext, aTrsf);
339 setResult(aResultPart, aResultIndex);
344 ListOfShape aListOfShape;
345 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > aListOfTranslationAlgo;
346 std::list<std::shared_ptr<GeomAlgoAPI_Rotation> > aListOfRotationAlgo;
347 for (int j=0; j<nbRadial; j++) {
349 double dx = j*aStep*x/norm;
350 double dy = j*aStep*y/norm;
351 double dz = j*aStep*z/norm;
352 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
353 new GeomAlgoAPI_Translation(aBaseShape, dx, dy, dz));
355 if (!aTranslationAlgo->check()) {
356 setError(aTranslationAlgo->getError());
360 aTranslationAlgo->build();
362 // Checking that the algorithm worked properly.
363 if (!aTranslationAlgo->isDone()) {
364 static const std::string aFeatureError = "Error : Multirotation algorithm failed.";
365 setError(aFeatureError);
368 if (aTranslationAlgo->shape()->isNull()) {
369 static const std::string aShapeError = "Error : Resulting shape is null.";
370 setError(aShapeError);
373 if (!aTranslationAlgo->isValid()) {
374 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
375 setError(aFeatureError);
378 aListOfShape.push_back(aTranslationAlgo->shape());
379 aListOfTranslationAlgo.push_back(aTranslationAlgo);
380 for (int i=1; i<nbAngular; i++) {
381 std::shared_ptr<GeomAlgoAPI_Rotation> aRotationnAlgo(
382 new GeomAlgoAPI_Rotation(aTranslationAlgo->shape(), anAxis, i*anAngle));
383 if (!aRotationnAlgo->check()) {
384 setError(aTranslationAlgo->getError());
387 aRotationnAlgo->build();// Checking that the algorithm worked properly.
388 if (!aRotationnAlgo->isDone()) {
389 static const std::string aFeatureError = "Error : Multirotation algorithm failed.";
390 setError(aFeatureError);
393 if (aRotationnAlgo->shape()->isNull()) {
394 static const std::string aShapeError = "Error : Resulting shape is null.";
395 setError(aShapeError);
398 if (!aRotationnAlgo->isValid()) {
399 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
400 setError(aFeatureError);
403 aListOfShape.push_back(aRotationnAlgo->shape());
404 aListOfRotationAlgo.push_back(aRotationnAlgo);
407 std::shared_ptr<GeomAPI_Shape> aCompound =
408 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
409 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
410 aResultBody->storeModified(aBaseShape, aCompound);
412 loadNamingDS2(aListOfTranslationAlgo, aResultBody, aBaseShape);
413 loadNamingDS3(aListOfRotationAlgo, aResultBody, aBaseShape, nbRadial);
414 setResult(aResultBody, aResultIndex);
419 // Remove the rest results if there were produced in the previous pass.
420 removeResults(aResultIndex);
423 //=================================================================================================
424 void FeaturesPlugin_MultiRotation::loadNamingDS2(
425 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > theListOfTranslationAlgo,
426 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
427 std::shared_ptr<GeomAPI_Shape> theBaseShape)
429 for (std::list<std::shared_ptr<GeomAlgoAPI_Translation> >::const_iterator anIt =
430 theListOfTranslationAlgo.begin(); anIt != theListOfTranslationAlgo.cend(); ++anIt) {
432 theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::FACE, "Rotated_Face");
435 theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::EDGE, "Rotated_Edge");
438 theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::VERTEX, "Rotated_Vertex");
442 //=================================================================================================
443 void FeaturesPlugin_MultiRotation::loadNamingDS3(
444 std::list<std::shared_ptr<GeomAlgoAPI_Rotation> > theListOfRotationAlgo,
445 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
446 std::shared_ptr<GeomAPI_Shape> theBaseShape, int nb)
449 std::string aRotatedName;
451 for (std::list<std::shared_ptr<GeomAlgoAPI_Rotation> >::const_iterator anIt =
452 theListOfRotationAlgo.begin(); anIt != theListOfRotationAlgo.cend(); ++anIt) {
456 GeomAPI_ShapeExplorer anExp((*anIt)->shape(), GeomAPI_Shape::FACE);
457 for(; anExp.more(); anExp.next()) {
458 aRotatedName = "Rotated_Face_" + std::to_string((long long) anIndex);
459 aRotatedName = aRotatedName + "_" + std::to_string((long long) numFace);
460 theResultBody->generated(anExp.current(), aRotatedName);