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 #include <FeaturesPlugin_Tools.h>
21 //=================================================================================================
22 FeaturesPlugin_Translation::FeaturesPlugin_Translation()
26 //=================================================================================================
27 void FeaturesPlugin_Translation::initAttributes()
29 AttributeSelectionListPtr aSelection =
30 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
31 FeaturesPlugin_Translation::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
33 data()->addAttribute(FeaturesPlugin_Translation::AXIS_OBJECT_ID(),
34 ModelAPI_AttributeSelection::typeId());
35 data()->addAttribute(FeaturesPlugin_Translation::DISTANCE_ID(),
36 ModelAPI_AttributeDouble::typeId());
39 //=================================================================================================
40 void FeaturesPlugin_Translation::execute()
43 ListOfShape anObjects;
44 std::list<ResultPtr> aContextes;
45 AttributeSelectionListPtr anObjectsSelList =
46 selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
47 if (anObjectsSelList->size() == 0) {
50 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
51 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
52 anObjectsSelList->value(anObjectsIndex);
53 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
54 if(!anObject.get()) { // may be for not-activated parts
58 anObjects.push_back(anObject);
59 aContextes.push_back(anObjectAttr->context());
63 std::shared_ptr<GeomAPI_Ax1> anAxis;
64 std::shared_ptr<GeomAPI_Edge> anEdge;
65 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
66 selection(FeaturesPlugin_Translation::AXIS_OBJECT_ID());
67 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
68 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
69 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
70 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
71 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
74 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
75 anEdge->line()->direction()));
79 double aDistance = real(FeaturesPlugin_Translation::DISTANCE_ID())->value();
81 // Moving each object.
83 std::list<ResultPtr>::iterator aContext = aContextes.begin();
84 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
85 anObjectsIt++, aContext++) {
86 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
87 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
91 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
92 aTrsf->setTranslation(anAxis, aDistance);
93 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
94 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
95 aResultPart->setTrsf(*aContext, aTrsf);
96 setResult(aResultPart, aResultIndex);
98 GeomAlgoAPI_Translation aTranslationAlgo(aBaseShape, anAxis, aDistance);
100 // Checking that the algorithm worked properly.
101 if(!aTranslationAlgo.isDone()) {
102 static const std::string aFeatureError = "Error: Translation algorithm failed.";
103 setError(aFeatureError);
106 if(aTranslationAlgo.shape()->isNull()) {
107 static const std::string aShapeError = "Error: Resulting shape is Null.";
108 setError(aShapeError);
111 if(!aTranslationAlgo.isValid()) {
112 std::string aFeatureError = "Error: Resulting shape is not valid.";
113 setError(aFeatureError);
117 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
118 loadNamingDS(aTranslationAlgo, aResultBody, aBaseShape);
119 setResult(aResultBody, aResultIndex);
124 // Remove the rest results if there were produced in the previous pass.
125 removeResults(aResultIndex);
128 void FeaturesPlugin_Translation::loadNamingDS(GeomAlgoAPI_Translation& theTranslationAlgo,
129 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
130 std::shared_ptr<GeomAPI_Shape> theBaseShape)
133 theResultBody->storeModified(theBaseShape, theTranslationAlgo.shape());
135 int aTranslatedTag = 1;
136 std::string aTranslatedName = "Translated";
137 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theTranslationAlgo.mapOfSubShapes();
139 FeaturesPlugin_Tools::storeModifiedShapes(theTranslationAlgo, theResultBody, theBaseShape, aTranslatedTag, aTranslatedName, *aSubShapes.get());