1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: FeaturesPlugin_Translation.cpp
4 // Created: 8 June 2015
5 // Author: Dmitry Bobylev
7 #include <FeaturesPlugin_Translation.h>
9 #include <ModelAPI_AttributeDouble.h>
10 #include <ModelAPI_AttributeSelectionList.h>
11 #include <ModelAPI_BodyBuilder.h>
12 #include <ModelAPI_ResultBody.h>
13 #include <ModelAPI_ResultPart.h>
14 #include <ModelAPI_Session.h>
16 #include <GeomAPI_Edge.h>
17 #include <GeomAPI_Lin.h>
19 //=================================================================================================
20 FeaturesPlugin_Translation::FeaturesPlugin_Translation()
24 //=================================================================================================
25 void FeaturesPlugin_Translation::initAttributes()
27 AttributeSelectionListPtr aSelection =
28 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
29 FeaturesPlugin_Translation::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
30 // revolution works with faces always
31 aSelection->setSelectionType("SOLID");
33 data()->addAttribute(FeaturesPlugin_Translation::AXIS_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
34 data()->addAttribute(FeaturesPlugin_Translation::DISTANCE_ID(), ModelAPI_AttributeDouble::typeId());
37 //=================================================================================================
38 void FeaturesPlugin_Translation::execute()
41 ListOfShape anObjects;
42 std::list<ResultPtr> aContextes;
43 AttributeSelectionListPtr anObjectsSelList = selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
44 if (anObjectsSelList->size() == 0) {
47 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
48 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
49 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
50 if(!anObject.get()) { // may be for not-activated parts
54 anObjects.push_back(anObject);
55 aContextes.push_back(anObjectAttr->context());
59 std::shared_ptr<GeomAPI_Ax1> anAxis;
60 std::shared_ptr<GeomAPI_Edge> anEdge;
61 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(FeaturesPlugin_Translation::AXIS_OBJECT_ID());
62 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
63 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
64 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
65 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
66 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
69 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(), anEdge->line()->direction()));
73 double aDistance = real(FeaturesPlugin_Translation::DISTANCE_ID())->value();
75 // Moving 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->setTranslation(anAxis, aDistance);
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);
92 GeomAlgoAPI_Translation aTranslationAlgo(aBaseShape, anAxis, aDistance);
94 // Checking that the algorithm worked properly.
95 if(!aTranslationAlgo.isDone()) {
96 static const std::string aFeatureError = "Translation algorithm failed";
97 setError(aFeatureError);
100 if(aTranslationAlgo.shape()->isNull()) {
101 static const std::string aShapeError = "Resulting shape is Null";
102 setError(aShapeError);
105 if(!aTranslationAlgo.isValid()) {
106 std::string aFeatureError = "Warning: resulting shape is not valid";
107 setError(aFeatureError);
111 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
112 loadNamingDS(aTranslationAlgo, 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_Translation::loadNamingDS(GeomAlgoAPI_Translation& theTranslationAlgo,
123 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
124 std::shared_ptr<GeomAPI_Shape> theBaseShape)
127 theResultBody->storeModified(theBaseShape, theTranslationAlgo.shape());
129 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theTranslationAlgo.mapOfSubShapes();
131 int aTranslatedTag = 1;
132 std::string aTranslatedName = "Translated";
133 theResultBody->loadAndOrientModifiedShapes(&theTranslationAlgo,
134 theBaseShape, GeomAPI_Shape::FACE,
135 aTranslatedTag, aTranslatedName, *aSubShapes.get());