1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: FeaturesPlugin_Rotation.cpp
4 // Created: 12 May 2015
5 // Author: Dmitry Bobylev
7 #include <FeaturesPlugin_Rotation.h>
9 #include <ModelAPI_AttributeDouble.h>
10 #include <ModelAPI_AttributeSelectionList.h>
11 #include <ModelAPI_ResultBody.h>
12 #include <ModelAPI_ResultPart.h>
14 #include <GeomAPI_Edge.h>
15 #include <GeomAPI_Lin.h>
17 //=================================================================================================
18 FeaturesPlugin_Rotation::FeaturesPlugin_Rotation()
22 //=================================================================================================
23 void FeaturesPlugin_Rotation::initAttributes()
25 AttributeSelectionListPtr aSelection =
26 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
27 FeaturesPlugin_Rotation::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
29 data()->addAttribute(FeaturesPlugin_Rotation::AXIS_OBJECT_ID(),
30 ModelAPI_AttributeSelection::typeId());
31 data()->addAttribute(FeaturesPlugin_Rotation::ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
34 //=================================================================================================
35 void FeaturesPlugin_Rotation::execute()
38 ListOfShape anObjects;
39 std::list<ResultPtr> aContextes;
40 AttributeSelectionListPtr anObjectsSelList =
41 selectionList(FeaturesPlugin_Rotation::OBJECTS_LIST_ID());
42 if (anObjectsSelList->size() == 0) {
45 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
46 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
47 anObjectsSelList->value(anObjectsIndex);
48 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
52 anObjects.push_back(anObject);
53 aContextes.push_back(anObjectAttr->context());
57 std::shared_ptr<GeomAPI_Ax1> anAxis;
58 std::shared_ptr<GeomAPI_Edge> anEdge;
59 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
60 selection(FeaturesPlugin_Rotation::AXIS_OBJECT_ID());
61 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
62 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
63 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
64 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
65 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
68 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
69 anEdge->line()->direction()));
73 double anAngle = real(FeaturesPlugin_Rotation::ANGLE_ID())->value();
75 // Rotating each object.
77 std::list<ResultPtr>::iterator aContext = aContextes.begin();
78 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
79 anObjectsIt++, aContext++) {
80 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
81 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
85 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
86 aTrsf->setRotation(anAxis, anAngle);
87 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
88 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
89 aResultPart->setTrsf(*aContext, aTrsf);
90 setResult(aResultPart, aResultIndex);
92 GeomAlgoAPI_Rotation aRotationAlgo(aBaseShape, anAxis, anAngle);
94 // Checking that the algorithm worked properly.
95 if(!aRotationAlgo.isDone()) {
96 static const std::string aFeatureError = "Error: Rotation algorithm failed.";
97 setError(aFeatureError);
100 if(aRotationAlgo.shape()->isNull()) {
101 static const std::string aShapeError = "Error: Resulting shape is Null.";
102 setError(aShapeError);
105 if(!aRotationAlgo.isValid()) {
106 std::string aFeatureError = "Error: Resulting shape is not valid.";
107 setError(aFeatureError);
111 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
112 loadNamingDS(aRotationAlgo, aResultBody, aBaseShape);
113 setResult(aResultBody, aResultIndex);
118 // Remove the rest results if there were produced in the previous pass.
119 removeResults(aResultIndex);
122 void FeaturesPlugin_Rotation::loadNamingDS(GeomAlgoAPI_Rotation& theRotaionAlgo,
123 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
124 std::shared_ptr<GeomAPI_Shape> theBaseShape)
127 theResultBody->storeModified(theBaseShape, theRotaionAlgo.shape());
129 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theRotaionAlgo.mapOfSubShapes();
132 std::string aRotatedName = "Rotated";
134 switch(theBaseShape->shapeType()) {
135 case GeomAPI_Shape::COMPOUND:
136 case GeomAPI_Shape::COMPSOLID:
137 case GeomAPI_Shape::SOLID:
138 case GeomAPI_Shape::SHELL:
139 theResultBody->loadAndOrientModifiedShapes(&theRotaionAlgo,
140 theBaseShape, GeomAPI_Shape::FACE,
141 aRotatedTag, aRotatedName + "_Face", *aSubShapes.get());
142 case GeomAPI_Shape::FACE:
143 case GeomAPI_Shape::WIRE:
144 theResultBody->loadAndOrientModifiedShapes(&theRotaionAlgo,
145 theBaseShape, GeomAPI_Shape::EDGE,
146 ++aRotatedTag, aRotatedName + "_Edge", *aSubShapes.get());
147 case GeomAPI_Shape::EDGE:
148 theResultBody->loadAndOrientModifiedShapes(&theRotaionAlgo,
149 theBaseShape, GeomAPI_Shape::VERTEX,
150 ++aRotatedTag, aRotatedName + "_Vertex", *aSubShapes.get());