1 // Copyright (C) 2014-201x CEA/DEN, EDF R&D
3 // File: FeaturesPlugin_MultiTranslation.cpp
4 // Created: 30 Jan 2017
5 // Author: Clarisse Genrault (CEA)
7 #include <FeaturesPlugin_MultiTranslation.h>
9 #include <GeomAlgoAPI_CompoundBuilder.h>
11 #include <GeomAPI_Ax1.h>
12 #include <GeomAPI_Edge.h>
13 #include <GeomAPI_Lin.h>
15 #include <ModelAPI_AttributeDouble.h>
16 #include <ModelAPI_AttributeInteger.h>
17 #include <ModelAPI_AttributeSelectionList.h>
18 #include <ModelAPI_AttributeString.h>
19 #include <ModelAPI_ResultBody.h>
20 #include <ModelAPI_ResultPart.h>
24 //=================================================================================================
25 FeaturesPlugin_MultiTranslation::FeaturesPlugin_MultiTranslation()
29 //=================================================================================================
30 void FeaturesPlugin_MultiTranslation::initAttributes()
32 AttributeSelectionListPtr aSelection =
33 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
34 FeaturesPlugin_MultiTranslation::OBJECTS_LIST_ID(),
35 ModelAPI_AttributeSelectionList::typeId()));
37 data()->addAttribute(FeaturesPlugin_MultiTranslation::AXIS_FIRST_DIR_ID(),
38 ModelAPI_AttributeSelection::typeId());
39 data()->addAttribute(FeaturesPlugin_MultiTranslation::STEP_FIRST_DIR_ID(),
40 ModelAPI_AttributeDouble::typeId());
41 data()->addAttribute(FeaturesPlugin_MultiTranslation::NB_COPIES_FIRST_DIR_ID(),
42 ModelAPI_AttributeInteger::typeId());
44 data()->addAttribute(FeaturesPlugin_MultiTranslation::USE_SECOND_DIR_ID(),
45 ModelAPI_AttributeString::typeId());
46 data()->addAttribute(FeaturesPlugin_MultiTranslation::AXIS_SECOND_DIR_ID(),
47 ModelAPI_AttributeSelection::typeId());
48 data()->addAttribute(FeaturesPlugin_MultiTranslation::STEP_SECOND_DIR_ID(),
49 ModelAPI_AttributeDouble::typeId());
50 data()->addAttribute(FeaturesPlugin_MultiTranslation::NB_COPIES_SECOND_DIR_ID(),
51 ModelAPI_AttributeInteger::typeId());
54 //=================================================================================================
55 void FeaturesPlugin_MultiTranslation::execute()
57 std::string useSecondDir = string(FeaturesPlugin_MultiTranslation::USE_SECOND_DIR_ID())->value();
58 if(!useSecondDir.empty()) {
59 performTwoDirection();
61 performOneDirection();
65 //=================================================================================================
66 void FeaturesPlugin_MultiTranslation::performOneDirection()
69 ListOfShape anObjects;
70 std::list<ResultPtr> aContextes;
71 AttributeSelectionListPtr anObjectsSelList =
72 selectionList(FeaturesPlugin_MultiTranslation::OBJECTS_LIST_ID());
73 if (anObjectsSelList->size() == 0) {
76 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
77 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
78 anObjectsSelList->value(anObjectsIndex);
79 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
80 if(!anObject.get()) { // may be for not-activated parts
84 anObjects.push_back(anObject);
85 aContextes.push_back(anObjectAttr->context());
89 std::shared_ptr<GeomAPI_Ax1> anAxis;
90 std::shared_ptr<GeomAPI_Edge> anEdge;
91 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
92 selection(FeaturesPlugin_MultiTranslation::AXIS_FIRST_DIR_ID());
93 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
94 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
95 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
96 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
97 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
100 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
101 anEdge->line()->direction()));
105 double aStep = real(FeaturesPlugin_MultiTranslation::STEP_FIRST_DIR_ID())->value();
107 // Getting number of copies.
109 integer(FeaturesPlugin_MultiTranslation::NB_COPIES_FIRST_DIR_ID())->value();
111 // Moving each object.
112 int aResultIndex = 0;
113 std::list<ResultPtr>::iterator aContext = aContextes.begin();
114 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
115 anObjectsIt++, aContext++) {
116 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
117 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
121 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
122 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
123 for (int i=0; i<nbCopies; i++) {
124 aTrsf->setTranslation(anAxis, i*aStep);
125 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
126 aResultPart->setTrsf(*aContext, aTrsf);
127 setResult(aResultPart, aResultIndex);
131 ListOfShape aListOfShape;
132 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > aListOfTranslationAlgo;
134 for (int i=0; i<nbCopies; i++) {
135 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
136 new GeomAlgoAPI_Translation(aBaseShape, anAxis, i*aStep));
138 if (!aTranslationAlgo->check()) {
139 setError(aTranslationAlgo->getError());
143 aTranslationAlgo->build();
145 // Checking that the algorithm worked properly.
146 if (!aTranslationAlgo->isDone()) {
147 static const std::string aFeatureError = "Error : Multitranslation algorithm failed.";
148 setError(aFeatureError);
151 if (aTranslationAlgo->shape()->isNull()) {
152 static const std::string aShapeError = "Error : Resulting shape is null.";
153 setError(aShapeError);
156 if (!aTranslationAlgo->isValid()) {
157 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
158 setError(aFeatureError);
161 aListOfShape.push_back(aTranslationAlgo->shape());
162 aListOfTranslationAlgo.push_back(aTranslationAlgo);
164 std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
165 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
166 aResultBody->storeModified(aBaseShape, aCompound);
167 loadNamingDS(aListOfTranslationAlgo, aResultBody, aBaseShape);
169 setResult(aResultBody, aResultIndex);
174 // Remove the rest results if there were produced in the previous pass.
175 removeResults(aResultIndex);
178 //=================================================================================================
179 void FeaturesPlugin_MultiTranslation::performTwoDirection()
182 ListOfShape anObjects;
183 std::list<ResultPtr> aContextes;
184 AttributeSelectionListPtr anObjectsSelList =
185 selectionList(FeaturesPlugin_MultiTranslation::OBJECTS_LIST_ID());
186 if (anObjectsSelList->size() == 0) {
189 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
190 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
191 anObjectsSelList->value(anObjectsIndex);
192 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
193 if(!anObject.get()) { // may be for not-activated parts
197 anObjects.push_back(anObject);
198 aContextes.push_back(anObjectAttr->context());
202 std::shared_ptr<GeomAPI_Ax1> aFirstAxis;
203 std::shared_ptr<GeomAPI_Edge> anEdge;
204 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
205 selection(FeaturesPlugin_MultiTranslation::AXIS_FIRST_DIR_ID());
206 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
207 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
208 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
209 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
210 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
213 aFirstAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
214 anEdge->line()->direction()));
216 std::shared_ptr<GeomAPI_Ax1> aSecondAxis;
217 anObjRef = selection(FeaturesPlugin_MultiTranslation::AXIS_SECOND_DIR_ID());
218 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
219 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
220 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
221 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
222 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
225 aSecondAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
226 anEdge->line()->direction()));
230 double aFirstStep = real(FeaturesPlugin_MultiTranslation::STEP_FIRST_DIR_ID())->value();
231 double aSecondStep = real(FeaturesPlugin_MultiTranslation::STEP_SECOND_DIR_ID())->value();
233 // Getting number of copies.
235 integer(FeaturesPlugin_MultiTranslation::NB_COPIES_FIRST_DIR_ID())->value();
236 int aSecondNbCopies =
237 integer(FeaturesPlugin_MultiTranslation::NB_COPIES_SECOND_DIR_ID())->value();
240 double x1 = aFirstAxis->dir()->x();
241 double y1 = aFirstAxis->dir()->y();
242 double z1 = aFirstAxis->dir()->z();
243 double norm1 = sqrt(x1*x1 + y1*y1 + z1*z1);
246 double x2 = aSecondAxis->dir()->x();
247 double y2 = aSecondAxis->dir()->y();
248 double z2 = aSecondAxis->dir()->z();
249 double norm2 = sqrt(x2*x2 + y2*y2 + z2*z2);
251 // Moving each object.
252 int aResultIndex = 0;
253 std::list<ResultPtr>::iterator aContext = aContextes.begin();
254 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
255 anObjectsIt++, aContext++) {
256 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
257 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
261 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
262 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
263 for (int j=0; j<aSecondNbCopies; j++) {
264 for (int i=0; i<aFirstNbCopies; i++) {
265 double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
266 double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
267 double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
268 aTrsf->setTranslation(dx, dy, dz);
269 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
270 aResultPart->setTrsf(*aContext, aTrsf);
271 setResult(aResultPart, aResultIndex);
276 ListOfShape aListOfShape;
277 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > aListOfTranslationAlgo;
279 for (int j=0; j<aSecondNbCopies; j++) {
280 for (int i=0; i<aFirstNbCopies; i++) {
281 double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
282 double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
283 double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
284 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
285 new GeomAlgoAPI_Translation(aBaseShape, dx, dy, dz));
287 if (!aTranslationAlgo->check()) {
288 setError(aTranslationAlgo->getError());
292 aTranslationAlgo->build();
294 // Checking that the algorithm worked properly.
295 if (!aTranslationAlgo->isDone()) {
296 static const std::string aFeatureError = "Error : Multitranslation algorithm failed.";
297 setError(aFeatureError);
300 if (aTranslationAlgo->shape()->isNull()) {
301 static const std::string aShapeError = "Error : Resulting shape is null.";
302 setError(aShapeError);
305 if (!aTranslationAlgo->isValid()) {
306 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
307 setError(aFeatureError);
310 aListOfShape.push_back(aTranslationAlgo->shape());
311 aListOfTranslationAlgo.push_back(aTranslationAlgo);
314 std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
315 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
316 aResultBody->storeModified(aBaseShape, aCompound);
317 loadNamingDS(aListOfTranslationAlgo, aResultBody, aBaseShape);
318 setResult(aResultBody, aResultIndex);
323 // Remove the rest results if there were produced in the previous pass.
324 removeResults(aResultIndex);
327 //=================================================================================================
328 void FeaturesPlugin_MultiTranslation::loadNamingDS(
329 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > theListOfTranslationAlgo,
330 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
331 std::shared_ptr<GeomAPI_Shape> theBaseShape)
335 std::string aTranslatedName;
337 for (std::list<std::shared_ptr<GeomAlgoAPI_Translation> >::const_iterator anIt =
338 theListOfTranslationAlgo.begin(); anIt != theListOfTranslationAlgo.cend(); ++anIt) {
339 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = (*anIt)->mapOfSubShapes();
342 aTranslatedName = "Translated_Face_" + std::to_string((long long) anIndex);
343 theResultBody->loadAndOrientModifiedShapes((*anIt).get(), theBaseShape, GeomAPI_Shape::FACE,
344 aTag++, aTranslatedName, *aSubShapes.get(),
348 aTranslatedName = "Translated_Edge_" + std::to_string((long long) anIndex);
349 theResultBody->loadAndOrientModifiedShapes((*anIt).get(), theBaseShape, GeomAPI_Shape::EDGE,
350 aTag++, aTranslatedName, *aSubShapes.get(),
354 aTranslatedName = "Translated_Vertex_" + std::to_string((long long) anIndex);
355 theResultBody->loadAndOrientModifiedShapes((*anIt).get(), theBaseShape, GeomAPI_Shape::VERTEX,
356 aTag++, aTranslatedName, *aSubShapes.get(),