1 // Copyright (C) 2014-2016 CEA/DEN, EDF R&D
3 // File: FeaturesPlugin_Translation.cpp
4 // Created: 8 June 2015
5 // Author: Dmitry Bobylev
7 // Modified by Clarisse Genrault (CEA) : 17 Nov 2016
9 #include <FeaturesPlugin_Translation.h>
11 #include <ModelAPI_AttributeDouble.h>
12 #include <ModelAPI_AttributeSelectionList.h>
13 #include <ModelAPI_AttributeString.h>
14 #include <ModelAPI_BodyBuilder.h>
15 #include <ModelAPI_ResultBody.h>
16 #include <ModelAPI_ResultPart.h>
17 #include <ModelAPI_Session.h>
19 #include <GeomAPI_Edge.h>
20 #include <GeomAPI_Lin.h>
22 #include <FeaturesPlugin_Tools.h>
24 //=================================================================================================
25 FeaturesPlugin_Translation::FeaturesPlugin_Translation()
29 //=================================================================================================
30 void FeaturesPlugin_Translation::initAttributes()
32 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
34 AttributeSelectionListPtr aSelection =
35 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
36 FeaturesPlugin_Translation::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
38 data()->addAttribute(FeaturesPlugin_Translation::AXIS_OBJECT_ID(),
39 ModelAPI_AttributeSelection::typeId());
40 data()->addAttribute(FeaturesPlugin_Translation::DISTANCE_ID(),
41 ModelAPI_AttributeDouble::typeId());
43 data()->addAttribute(FeaturesPlugin_Translation::DX_ID(),
44 ModelAPI_AttributeDouble::typeId());
45 data()->addAttribute(FeaturesPlugin_Translation::DY_ID(),
46 ModelAPI_AttributeDouble::typeId());
47 data()->addAttribute(FeaturesPlugin_Translation::DZ_ID(),
48 ModelAPI_AttributeDouble::typeId());
51 //=================================================================================================
52 void FeaturesPlugin_Translation::execute()
54 AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Translation::CREATION_METHOD());
55 std::string aMethodType = aMethodTypeAttr->value();
57 if (aMethodType == CREATION_METHOD_BY_DISTANCE()) {
58 performTranslationByAxisAndDistance();
61 if (aMethodType == CREATION_METHOD_BY_DIMENSIONS()) {
62 performTranslationByDimensions();
66 //=================================================================================================
67 void FeaturesPlugin_Translation::performTranslationByAxisAndDistance()
70 ListOfShape anObjects;
71 std::list<ResultPtr> aContextes;
72 AttributeSelectionListPtr anObjectsSelList =
73 selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
74 if (anObjectsSelList->size() == 0) {
77 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
78 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
79 anObjectsSelList->value(anObjectsIndex);
80 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
81 if(!anObject.get()) { // may be for not-activated parts
85 anObjects.push_back(anObject);
86 aContextes.push_back(anObjectAttr->context());
90 std::shared_ptr<GeomAPI_Ax1> anAxis;
91 std::shared_ptr<GeomAPI_Edge> anEdge;
92 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
93 selection(FeaturesPlugin_Translation::AXIS_OBJECT_ID());
94 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
95 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
96 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
97 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
98 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
101 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
102 anEdge->line()->direction()));
106 double aDistance = real(FeaturesPlugin_Translation::DISTANCE_ID())->value();
108 // Moving each object.
109 int aResultIndex = 0;
110 std::list<ResultPtr>::iterator aContext = aContextes.begin();
111 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
112 anObjectsIt++, aContext++) {
113 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
114 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
118 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
119 aTrsf->setTranslation(anAxis, aDistance);
120 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
121 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
122 aResultPart->setTrsf(*aContext, aTrsf);
123 setResult(aResultPart, aResultIndex);
125 GeomAlgoAPI_Translation aTranslationAlgo(aBaseShape, anAxis, aDistance);
127 if (!aTranslationAlgo.check()) {
128 setError(aTranslationAlgo.getError());
132 aTranslationAlgo.build();
134 // Checking that the algorithm worked properly.
135 if(!aTranslationAlgo.isDone()) {
136 static const std::string aFeatureError = "Error: Translation algorithm failed.";
137 setError(aFeatureError);
140 if(aTranslationAlgo.shape()->isNull()) {
141 static const std::string aShapeError = "Error: Resulting shape is Null.";
142 setError(aShapeError);
145 if(!aTranslationAlgo.isValid()) {
146 std::string aFeatureError = "Error: Resulting shape is not valid.";
147 setError(aFeatureError);
151 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
152 loadNamingDS(aTranslationAlgo, aResultBody, aBaseShape);
153 setResult(aResultBody, aResultIndex);
158 // Remove the rest results if there were produced in the previous pass.
159 removeResults(aResultIndex);
162 //=================================================================================================
163 void FeaturesPlugin_Translation::performTranslationByDimensions()
166 ListOfShape anObjects;
167 std::list<ResultPtr> aContextes;
168 AttributeSelectionListPtr anObjectsSelList =
169 selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
170 if (anObjectsSelList->size() == 0) {
173 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
174 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
175 anObjectsSelList->value(anObjectsIndex);
176 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
177 if(!anObject.get()) { // may be for not-activated parts
181 anObjects.push_back(anObject);
182 aContextes.push_back(anObjectAttr->context());
185 // Getting dimensions in X, in Y and in Z
186 double aDX = real(FeaturesPlugin_Translation::DX_ID())->value();
187 double aDY = real(FeaturesPlugin_Translation::DY_ID())->value();
188 double aDZ = real(FeaturesPlugin_Translation::DZ_ID())->value();
190 // Moving each object.
191 int aResultIndex = 0;
192 std::list<ResultPtr>::iterator aContext = aContextes.begin();
193 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
194 anObjectsIt++, aContext++) {
195 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
196 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
200 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
201 aTrsf->setTranslation(aDX, aDY, aDZ);
202 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
203 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
204 aResultPart->setTrsf(*aContext, aTrsf);
205 setResult(aResultPart, aResultIndex);
207 GeomAlgoAPI_Translation aTranslationAlgo(aBaseShape, aDX, aDY, aDZ);
209 if (!aTranslationAlgo.check()) {
210 setError(aTranslationAlgo.getError());
214 aTranslationAlgo.build();
216 // Checking that the algorithm worked properly.
217 if(!aTranslationAlgo.isDone()) {
218 static const std::string aFeatureError = "Error: Translation algorithm failed.";
219 setError(aFeatureError);
222 if(aTranslationAlgo.shape()->isNull()) {
223 static const std::string aShapeError = "Error: Resulting shape is Null.";
224 setError(aShapeError);
227 if(!aTranslationAlgo.isValid()) {
228 std::string aFeatureError = "Error: Resulting shape is not valid.";
229 setError(aFeatureError);
233 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
234 loadNamingDS(aTranslationAlgo, aResultBody, aBaseShape);
235 setResult(aResultBody, aResultIndex);
240 // Remove the rest results if there were produced in the previous pass.
241 removeResults(aResultIndex);
244 //=================================================================================================
245 void FeaturesPlugin_Translation::loadNamingDS(GeomAlgoAPI_Translation& theTranslationAlgo,
246 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
247 std::shared_ptr<GeomAPI_Shape> theBaseShape)
250 theResultBody->storeModified(theBaseShape, theTranslationAlgo.shape());
252 int aTranslatedTag = 1;
253 std::string aTranslatedName = "Translated";
254 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theTranslationAlgo.mapOfSubShapes();
256 FeaturesPlugin_Tools::storeModifiedShapes(theTranslationAlgo, theResultBody,
257 theBaseShape, aTranslatedTag, aTranslatedName,