1 // Copyright (C) 2017-2020 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 static const std::string MULTIROTATION_VERSION_1("v9.5");
53 //=================================================================================================
54 FeaturesPlugin_MultiRotation::FeaturesPlugin_MultiRotation()
58 //=================================================================================================
59 void FeaturesPlugin_MultiRotation::initAttributes()
61 AttributeSelectionListPtr aSelection =
62 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
63 FeaturesPlugin_MultiRotation::OBJECTS_LIST_ID(),
64 ModelAPI_AttributeSelectionList::typeId()));
66 data()->addAttribute(FeaturesPlugin_MultiRotation::AXIS_ANGULAR_ID(),
67 ModelAPI_AttributeSelection::typeId());
68 data()->addAttribute(FeaturesPlugin_MultiRotation::USE_ANGULAR_STEP_ID(),
69 ModelAPI_AttributeString::typeId());
70 data()->addAttribute(FeaturesPlugin_MultiRotation::STEP_ANGULAR_ID(),
71 ModelAPI_AttributeDouble::typeId());
72 data()->addAttribute(FeaturesPlugin_MultiRotation::NB_COPIES_ANGULAR_ID(),
73 ModelAPI_AttributeInteger::typeId());
75 #ifdef FEATURE_MULTIROTATION_TWO_DIRECTIONS
76 data()->addAttribute(FeaturesPlugin_MultiRotation::USE_RADIAL_DIR_ID(),
77 ModelAPI_AttributeString::typeId());
78 data()->addAttribute(FeaturesPlugin_MultiRotation::STEP_RADIAL_ID(),
79 ModelAPI_AttributeDouble::typeId());
80 data()->addAttribute(FeaturesPlugin_MultiRotation::NB_COPIES_RADIAL_ID(),
81 ModelAPI_AttributeInteger::typeId());
84 if (!aSelection->isInitialized()) {
85 // new feature, not read from file
86 data()->setVersion(MULTIROTATION_VERSION_1);
90 //=================================================================================================
91 void FeaturesPlugin_MultiRotation::execute()
93 #ifdef FEATURE_MULTIROTATION_TWO_DIRECTIONS
94 std::string useRadialDir = string(FeaturesPlugin_MultiRotation::USE_RADIAL_DIR_ID())->value();
95 if (useRadialDir.empty()) {
105 //=================================================================================================
106 bool FeaturesPlugin_MultiRotation::paramsOfRotation(std::shared_ptr<GeomAPI_Ax1>& theAxis,
111 static const std::string aSelectionError = "Error: The axis shape selection is bad.";
112 AttributeSelectionPtr anObjRef = selection(AXIS_ANGULAR_ID());
113 GeomShapePtr aShape = anObjRef->value();
114 if (!aShape.get() && anObjRef->context().get())
115 aShape = anObjRef->context()->shape();
117 setError(aSelectionError);
122 if (aShape->isEdge())
123 anEdge = aShape->edge();
124 else if (aShape->isCompound()) {
125 GeomAPI_ShapeIterator anIt(aShape);
126 anEdge = anIt.current()->edge();
130 setError(aSelectionError);
134 theAxis.reset(new GeomAPI_Ax1(anEdge->line()->location(), anEdge->line()->direction()));
136 // Getting number of copies.
137 theQuantity = integer(FeaturesPlugin_MultiRotation::NB_COPIES_ANGULAR_ID())->value();
138 if (theQuantity <= 0) {
139 std::string aFeatureError = "Multirotation builder ";
140 aFeatureError += ":: the number of copies for the angular direction is null or negative.";
141 setError(aFeatureError);
146 std::string useAngularStep =
147 string(FeaturesPlugin_MultiRotation::USE_ANGULAR_STEP_ID())->value();
148 if (!useAngularStep.empty())
149 theAngle = real(FeaturesPlugin_MultiRotation::STEP_ANGULAR_ID())->value();
151 theAngle = 360. / theQuantity;
155 //=================================================================================================
156 void FeaturesPlugin_MultiRotation::performRotation1D()
158 bool isKeepSubShapes = data()->version() == MULTIROTATION_VERSION_1;
161 AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECTS_LIST_ID());
162 if (anObjectsSelList->size() == 0) {
163 setError("Error: empty selection list");
167 GeomAPI_ShapeHierarchy anObjects;
168 std::list<ResultPtr> aParts;
169 if (!FeaturesPlugin_Tools::shapesFromSelectionList(
170 anObjectsSelList, isKeepSubShapes, anObjects, aParts))
173 // Parameters of rotation.
174 std::shared_ptr<GeomAPI_Ax1> anAxis;
175 double anAngle = 0.0;
177 if (!paramsOfRotation(anAxis, anAngle, nbCopies))
182 int aResultIndex = 0;
184 for (std::list<ResultPtr>::iterator aPRes = aParts.begin(); aPRes != aParts.end(); ++aPRes) {
185 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aPRes);
186 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
187 for (int i = 0; i < nbCopies; ++i) {
188 aTrsf->setRotation(anAxis, i * anAngle);
189 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
190 aResultPart->setTrsf(anOrigin, aTrsf);
191 setResult(aResultPart, aResultIndex++);
195 // Collect transformations for each object in a part.
196 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList);
197 for (GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
198 anObjectsIt != anObjects.end(); anObjectsIt++) {
199 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
200 ListOfShape aListOfShape;
202 for (int i = 0; i < nbCopies; i++) {
203 std::shared_ptr<GeomAlgoAPI_Rotation> aRotationnAlgo(
204 new GeomAlgoAPI_Rotation(aBaseShape, anAxis, i * anAngle));
206 // Checking that the algorithm worked properly.
207 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aRotationnAlgo, getKind(), anError)) {
211 aListOfShape.push_back(aRotationnAlgo->shape());
212 aMakeShapeList->appendAlgo(aRotationnAlgo);
215 GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
216 anObjects.markModified(aBaseShape, aCompound);
219 // Build results of the operation.
220 const ListOfShape& anOriginalShapes = anObjects.objects();
221 ListOfShape aTopLevel;
222 anObjects.topLevelObjects(aTopLevel);
223 for (ListOfShape::iterator anIt = aTopLevel.begin(); anIt != aTopLevel.end(); ++anIt) {
224 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
225 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, anOriginalShapes, ListOfShape(),
226 aMakeShapeList, *anIt, "Rotated");
227 setResult(aResultBody, aResultIndex++);
230 // Remove the rest results if there were produced in the previous pass.
231 removeResults(aResultIndex);
234 //=================================================================================================
235 #ifdef FEATURE_MULTIROTATION_TWO_DIRECTIONS
236 void FeaturesPlugin_MultiRotation::performRotation2D()
239 ListOfShape anObjects;
240 std::list<ResultPtr> aContextes;
241 AttributeSelectionListPtr anObjectsSelList =
242 selectionList(FeaturesPlugin_MultiRotation::OBJECTS_LIST_ID());
243 if (anObjectsSelList->size() == 0) {
246 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
247 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
248 anObjectsSelList->value(anObjectsIndex);
249 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
250 if(!anObject.get()) { // may be for not-activated parts
253 anObjects.push_back(anObject);
254 aContextes.push_back(anObjectAttr->context());
257 // Parameters of rotation.
258 std::shared_ptr<GeomAPI_Ax1> anAxis;
259 double anAngle = 0.0;
261 if (!paramsOfRotation(anAxis, anAngle, nbCopies))
265 // Getting number of copies int he radial direction.
267 integer(FeaturesPlugin_MultiRotation::NB_COPIES_RADIAL_ID())->value();
270 std::string aFeatureError = "Multirotation builder ";
271 aFeatureError+=":: the number of copies for the radial direction is null or negative.";
272 setError(aFeatureError);
277 double aStep = real(FeaturesPlugin_MultiRotation::STEP_RADIAL_ID())->value();
279 // Moving each object.
280 int aResultIndex = 0;
281 std::list<ResultPtr>::iterator aContext = aContextes.begin();
282 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
283 anObjectsIt++, aContext++) {
284 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
285 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
287 std::shared_ptr<GeomAPI_Dir> aDir =
288 GeomAlgoAPI_ShapeTools::buildDirFromAxisAndShape(aBaseShape, anAxis);
289 double x = aDir->x();
290 double y = aDir->y();
291 double z = aDir->z();
292 double norm = sqrt(x*x+y*y+z*z);
296 /*ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
297 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
298 for (int j=0; j<aSecondNbCopies; j++) {
299 for (int i=0; i<aFirstNbCopies; i++) {
300 double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
301 double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
302 double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
303 aTrsf->setTranslation(dx, dy, dz);
304 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
305 aResultPart->setTrsf(*aContext, aTrsf);
306 setResult(aResultPart, aResultIndex);
311 ListOfShape aListOfShape;
312 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > aListOfTranslationAlgo;
313 std::list<std::shared_ptr<GeomAlgoAPI_Rotation> > aListOfRotationAlgo;
314 for (int j=0; j<nbRadial; j++) {
316 double dx = j*aStep*x/norm;
317 double dy = j*aStep*y/norm;
318 double dz = j*aStep*z/norm;
319 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
320 new GeomAlgoAPI_Translation(aBaseShape, dx, dy, dz));
322 if (!aTranslationAlgo->check()) {
323 setError(aTranslationAlgo->getError());
327 aTranslationAlgo->build();
329 // Checking that the algorithm worked properly.
330 if (!aTranslationAlgo->isDone()) {
331 static const std::string aFeatureError = "Error : Multirotation algorithm failed.";
332 setError(aFeatureError);
335 if (aTranslationAlgo->shape()->isNull()) {
336 static const std::string aShapeError = "Error : Resulting shape is null.";
337 setError(aShapeError);
340 if (!aTranslationAlgo->isValid()) {
341 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
342 setError(aFeatureError);
345 aListOfShape.push_back(aTranslationAlgo->shape());
346 aListOfTranslationAlgo.push_back(aTranslationAlgo);
347 for (int i=1; i<nbAngular; i++) {
348 std::shared_ptr<GeomAlgoAPI_Rotation> aRotationnAlgo(
349 new GeomAlgoAPI_Rotation(aTranslationAlgo->shape(), anAxis, i*anAngle));
350 if (!aRotationnAlgo->check()) {
351 setError(aTranslationAlgo->getError());
354 aRotationnAlgo->build();// Checking that the algorithm worked properly.
355 if (!aRotationnAlgo->isDone()) {
356 static const std::string aFeatureError = "Error : Multirotation algorithm failed.";
357 setError(aFeatureError);
360 if (aRotationnAlgo->shape()->isNull()) {
361 static const std::string aShapeError = "Error : Resulting shape is null.";
362 setError(aShapeError);
365 if (!aRotationnAlgo->isValid()) {
366 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
367 setError(aFeatureError);
370 aListOfShape.push_back(aRotationnAlgo->shape());
371 aListOfRotationAlgo.push_back(aRotationnAlgo);
374 std::shared_ptr<GeomAPI_Shape> aCompound =
375 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
376 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
377 aResultBody->storeModified(aBaseShape, aCompound);
379 loadNamingDS2(aListOfTranslationAlgo, aResultBody, aBaseShape);
380 loadNamingDS3(aListOfRotationAlgo, aResultBody, aBaseShape, nbRadial);
381 setResult(aResultBody, aResultIndex);
386 // Remove the rest results if there were produced in the previous pass.
387 removeResults(aResultIndex);
390 //=================================================================================================
391 void FeaturesPlugin_MultiRotation::loadNamingDS2(
392 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > theListOfTranslationAlgo,
393 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
394 std::shared_ptr<GeomAPI_Shape> theBaseShape)
396 for (std::list<std::shared_ptr<GeomAlgoAPI_Translation> >::const_iterator anIt =
397 theListOfTranslationAlgo.begin(); anIt != theListOfTranslationAlgo.cend(); ++anIt) {
399 theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::FACE, "Rotated_Face");
402 theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::EDGE, "Rotated_Edge");
405 theResultBody->loadModifiedShapes(*anIt, theBaseShape, GeomAPI_Shape::VERTEX, "Rotated_Vertex");
409 //=================================================================================================
410 void FeaturesPlugin_MultiRotation::loadNamingDS3(
411 std::list<std::shared_ptr<GeomAlgoAPI_Rotation> > theListOfRotationAlgo,
412 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
413 std::shared_ptr<GeomAPI_Shape> theBaseShape, int nb)
416 std::string aRotatedName;
418 for (std::list<std::shared_ptr<GeomAlgoAPI_Rotation> >::const_iterator anIt =
419 theListOfRotationAlgo.begin(); anIt != theListOfRotationAlgo.cend(); ++anIt) {
423 GeomAPI_ShapeExplorer anExp((*anIt)->shape(), GeomAPI_Shape::FACE);
424 for(; anExp.more(); anExp.next()) {
425 aRotatedName = "Rotated_Face_" + std::to_string((long long) anIndex);
426 aRotatedName = aRotatedName + "_" + std::to_string((long long) numFace);
427 theResultBody->generated(anExp.current(), aRotatedName);