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(), ModelAPI_AttributeSelection::typeId());
30 data()->addAttribute(FeaturesPlugin_Rotation::ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
33 //=================================================================================================
34 void FeaturesPlugin_Rotation::execute()
37 ListOfShape anObjects;
38 std::list<ResultPtr> aContextes;
39 AttributeSelectionListPtr anObjectsSelList = selectionList(FeaturesPlugin_Rotation::OBJECTS_LIST_ID());
40 if (anObjectsSelList->size() == 0) {
43 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
44 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
45 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
49 anObjects.push_back(anObject);
50 aContextes.push_back(anObjectAttr->context());
54 std::shared_ptr<GeomAPI_Ax1> anAxis;
55 std::shared_ptr<GeomAPI_Edge> anEdge;
56 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(FeaturesPlugin_Rotation::AXIS_OBJECT_ID());
57 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
58 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
59 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
60 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
61 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
64 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(), anEdge->line()->direction()));
68 double anAngle = real(FeaturesPlugin_Rotation::ANGLE_ID())->value();
70 // Rotating each object.
72 std::list<ResultPtr>::iterator aContext = aContextes.begin();
73 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
74 anObjectsIt++, aContext++) {
75 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
76 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
80 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
81 aTrsf->setRotation(anAxis, anAngle);
82 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
83 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
84 aResultPart->setTrsf(*aContext, aTrsf);
85 setResult(aResultPart);
87 GeomAlgoAPI_Rotation aRotationAlgo(aBaseShape, anAxis, anAngle);
89 // Checking that the algorithm worked properly.
90 if(!aRotationAlgo.isDone()) {
91 static const std::string aFeatureError = "Error: Rotation algorithm failed.";
92 setError(aFeatureError);
95 if(aRotationAlgo.shape()->isNull()) {
96 static const std::string aShapeError = "Error: Resulting shape is Null.";
97 setError(aShapeError);
100 if(!aRotationAlgo.isValid()) {
101 std::string aFeatureError = "Error: Resulting shape is not valid.";
102 setError(aFeatureError);
106 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
107 loadNamingDS(aRotationAlgo, aResultBody, aBaseShape);
108 setResult(aResultBody, aResultIndex);
113 // Remove the rest results if there were produced in the previous pass.
114 removeResults(aResultIndex);
117 void FeaturesPlugin_Rotation::loadNamingDS(GeomAlgoAPI_Rotation& theRotaionAlgo,
118 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
119 std::shared_ptr<GeomAPI_Shape> theBaseShape)
122 theResultBody->storeModified(theBaseShape, theRotaionAlgo.shape());
124 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theRotaionAlgo.mapOfSubShapes();
127 std::string aRotatedName = "Rotated";
128 theResultBody->loadAndOrientModifiedShapes(&theRotaionAlgo,
129 theBaseShape, GeomAPI_Shape::FACE,
130 aRotatedTag, aRotatedName, *aSubShapes.get());