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>
21 #include <GeomAPI_Trsf.h>
23 #include <GeomAlgoAPI_PointBuilder.h>
25 #include <FeaturesPlugin_Tools.h>
27 //=================================================================================================
28 FeaturesPlugin_Translation::FeaturesPlugin_Translation()
32 //=================================================================================================
33 void FeaturesPlugin_Translation::initAttributes()
35 data()->addAttribute(FeaturesPlugin_Translation::CREATION_METHOD(),
36 ModelAPI_AttributeString::typeId());
38 AttributeSelectionListPtr aSelection =
39 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
40 FeaturesPlugin_Translation::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
42 data()->addAttribute(FeaturesPlugin_Translation::AXIS_OBJECT_ID(),
43 ModelAPI_AttributeSelection::typeId());
44 data()->addAttribute(FeaturesPlugin_Translation::DISTANCE_ID(),
45 ModelAPI_AttributeDouble::typeId());
47 data()->addAttribute(FeaturesPlugin_Translation::DX_ID(),
48 ModelAPI_AttributeDouble::typeId());
49 data()->addAttribute(FeaturesPlugin_Translation::DY_ID(),
50 ModelAPI_AttributeDouble::typeId());
51 data()->addAttribute(FeaturesPlugin_Translation::DZ_ID(),
52 ModelAPI_AttributeDouble::typeId());
54 data()->addAttribute(FeaturesPlugin_Translation::START_POINT_ID(),
55 ModelAPI_AttributeSelection::typeId());
56 data()->addAttribute(FeaturesPlugin_Translation::END_POINT_ID(),
57 ModelAPI_AttributeSelection::typeId());
60 //=================================================================================================
61 void FeaturesPlugin_Translation::execute()
63 AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Translation::CREATION_METHOD());
64 std::string aMethodType = aMethodTypeAttr->value();
66 if (aMethodType == CREATION_METHOD_BY_DISTANCE()) {
67 performTranslationByAxisAndDistance();
70 if (aMethodType == CREATION_METHOD_BY_DIMENSIONS()) {
71 performTranslationByDimensions();
74 if (aMethodType == CREATION_METHOD_BY_TWO_POINTS()) {
75 performTranslationByTwoPoints();
79 //=================================================================================================
80 void FeaturesPlugin_Translation::performTranslationByAxisAndDistance()
83 ListOfShape anObjects;
84 std::list<ResultPtr> aContextes;
85 AttributeSelectionListPtr anObjectsSelList =
86 selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
87 if (anObjectsSelList->size() == 0) {
90 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
91 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
92 anObjectsSelList->value(anObjectsIndex);
93 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
94 if(!anObject.get()) { // may be for not-activated parts
98 anObjects.push_back(anObject);
99 aContextes.push_back(anObjectAttr->context());
103 std::shared_ptr<GeomAPI_Ax1> anAxis;
104 std::shared_ptr<GeomAPI_Edge> anEdge;
105 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
106 selection(FeaturesPlugin_Translation::AXIS_OBJECT_ID());
107 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
108 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
109 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
110 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
111 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
114 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
115 anEdge->line()->direction()));
119 double aDistance = real(FeaturesPlugin_Translation::DISTANCE_ID())->value();
121 // Moving each object.
122 int aResultIndex = 0;
123 std::list<ResultPtr>::iterator aContext = aContextes.begin();
124 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
125 anObjectsIt++, aContext++) {
126 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
127 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
131 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
132 aTrsf->setTranslation(anAxis, aDistance);
133 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
134 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
135 aResultPart->setTrsf(*aContext, aTrsf);
136 setResult(aResultPart, aResultIndex);
138 GeomAlgoAPI_Translation aTranslationAlgo(aBaseShape, anAxis, aDistance);
140 if (!aTranslationAlgo.check()) {
141 setError(aTranslationAlgo.getError());
145 aTranslationAlgo.build();
147 // Checking that the algorithm worked properly.
148 if(!aTranslationAlgo.isDone()) {
149 static const std::string aFeatureError = "Error: Translation algorithm failed.";
150 setError(aFeatureError);
153 if(aTranslationAlgo.shape()->isNull()) {
154 static const std::string aShapeError = "Error: Resulting shape is Null.";
155 setError(aShapeError);
158 if(!aTranslationAlgo.isValid()) {
159 std::string aFeatureError = "Error: Resulting shape is not valid.";
160 setError(aFeatureError);
164 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
165 loadNamingDS(aTranslationAlgo, aResultBody, aBaseShape);
166 setResult(aResultBody, aResultIndex);
171 // Remove the rest results if there were produced in the previous pass.
172 removeResults(aResultIndex);
175 //=================================================================================================
176 void FeaturesPlugin_Translation::performTranslationByDimensions()
179 ListOfShape anObjects;
180 std::list<ResultPtr> aContextes;
181 AttributeSelectionListPtr anObjectsSelList =
182 selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
183 if (anObjectsSelList->size() == 0) {
186 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
187 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
188 anObjectsSelList->value(anObjectsIndex);
189 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
190 if(!anObject.get()) { // may be for not-activated parts
194 anObjects.push_back(anObject);
195 aContextes.push_back(anObjectAttr->context());
198 // Getting dimensions in X, in Y and in Z
199 double aDX = real(FeaturesPlugin_Translation::DX_ID())->value();
200 double aDY = real(FeaturesPlugin_Translation::DY_ID())->value();
201 double aDZ = real(FeaturesPlugin_Translation::DZ_ID())->value();
203 // Moving each object.
204 int aResultIndex = 0;
205 std::list<ResultPtr>::iterator aContext = aContextes.begin();
206 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
207 anObjectsIt++, aContext++) {
208 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
209 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
213 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
214 aTrsf->setTranslation(aDX, aDY, aDZ);
215 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
216 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
217 aResultPart->setTrsf(*aContext, aTrsf);
218 setResult(aResultPart, aResultIndex);
220 GeomAlgoAPI_Translation aTranslationAlgo(aBaseShape, aDX, aDY, aDZ);
222 if (!aTranslationAlgo.check()) {
223 setError(aTranslationAlgo.getError());
227 aTranslationAlgo.build();
229 // Checking that the algorithm worked properly.
230 if(!aTranslationAlgo.isDone()) {
231 static const std::string aFeatureError = "Error: Translation algorithm failed.";
232 setError(aFeatureError);
235 if(aTranslationAlgo.shape()->isNull()) {
236 static const std::string aShapeError = "Error: Resulting shape is Null.";
237 setError(aShapeError);
240 if(!aTranslationAlgo.isValid()) {
241 std::string aFeatureError = "Error: Resulting shape is not valid.";
242 setError(aFeatureError);
246 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
247 loadNamingDS(aTranslationAlgo, aResultBody, aBaseShape);
248 setResult(aResultBody, aResultIndex);
253 // Remove the rest results if there were produced in the previous pass.
254 removeResults(aResultIndex);
257 //=================================================================================================
258 void FeaturesPlugin_Translation::performTranslationByTwoPoints()
261 ListOfShape anObjects;
262 std::list<ResultPtr> aContextes;
263 AttributeSelectionListPtr anObjectsSelList =
264 selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
265 if (anObjectsSelList->size() == 0) {
268 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
269 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
270 anObjectsSelList->value(anObjectsIndex);
271 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
272 if(!anObject.get()) { // may be for not-activated parts
276 anObjects.push_back(anObject);
277 aContextes.push_back(anObjectAttr->context());
280 // Getting the start point and the end point
281 AttributeSelectionPtr aRef1 = data()->selection(FeaturesPlugin_Translation::START_POINT_ID());
282 AttributeSelectionPtr aRef2 = data()->selection(FeaturesPlugin_Translation::END_POINT_ID());
283 std::shared_ptr<GeomAPI_Pnt> aFirstPoint;
284 std::shared_ptr<GeomAPI_Pnt> aSecondPoint;
285 if ((aRef1.get() != NULL) && (aRef2.get() != NULL)) {
286 GeomShapePtr aShape1 = aRef1->value();
287 if (!aShape1.get()) //If we can't get the points directly, try getting them from the context
288 aShape1 = aRef1->context()->shape();
289 GeomShapePtr aShape2 = aRef2->value();
291 aShape2 = aRef2->context()->shape();
292 if (aShape1 && aShape2) {
293 aFirstPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
294 aSecondPoint = GeomAlgoAPI_PointBuilder::point(aShape2);
298 // Moving each object.
299 int aResultIndex = 0;
300 std::list<ResultPtr>::iterator aContext = aContextes.begin();
301 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
302 anObjectsIt++, aContext++) {
303 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
304 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
308 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
309 aTrsf->setTranslation(aFirstPoint, aSecondPoint);
310 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
311 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
312 aResultPart->setTrsf(*aContext, aTrsf);
313 setResult(aResultPart, aResultIndex);
315 GeomAlgoAPI_Translation aTranslationAlgo(aBaseShape, aFirstPoint, aSecondPoint);
317 if (!aTranslationAlgo.check()) {
318 setError(aTranslationAlgo.getError());
322 aTranslationAlgo.build();
324 // Checking that the algorithm worked properly.
325 if(!aTranslationAlgo.isDone()) {
326 static const std::string aFeatureError = "Error: Translation algorithm failed.";
327 setError(aFeatureError);
330 if(aTranslationAlgo.shape()->isNull()) {
331 static const std::string aShapeError = "Error: Resulting shape is Null.";
332 setError(aShapeError);
335 if(!aTranslationAlgo.isValid()) {
336 std::string aFeatureError = "Error: Resulting shape is not valid.";
337 setError(aFeatureError);
341 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
342 loadNamingDS(aTranslationAlgo, aResultBody, aBaseShape);
343 setResult(aResultBody, aResultIndex);
348 // Remove the rest results if there were produced in the previous pass.
349 removeResults(aResultIndex);
352 //=================================================================================================
353 void FeaturesPlugin_Translation::loadNamingDS(GeomAlgoAPI_Translation& theTranslationAlgo,
354 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
355 std::shared_ptr<GeomAPI_Shape> theBaseShape)
358 theResultBody->storeModified(theBaseShape, theTranslationAlgo.shape());
360 std::string aTranslatedName = "Translated";
361 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theTranslationAlgo.mapOfSubShapes();
363 FeaturesPlugin_Tools::storeModifiedShapes(theTranslationAlgo, theResultBody,
364 theBaseShape, 1, 2, 3, aTranslatedName,