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 =
165 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
166 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
167 aResultBody->storeModified(aBaseShape, aCompound);
168 loadNamingDS(aListOfTranslationAlgo, aResultBody, aBaseShape);
170 setResult(aResultBody, aResultIndex);
175 // Remove the rest results if there were produced in the previous pass.
176 removeResults(aResultIndex);
179 //=================================================================================================
180 void FeaturesPlugin_MultiTranslation::performTwoDirection()
183 ListOfShape anObjects;
184 std::list<ResultPtr> aContextes;
185 AttributeSelectionListPtr anObjectsSelList =
186 selectionList(FeaturesPlugin_MultiTranslation::OBJECTS_LIST_ID());
187 if (anObjectsSelList->size() == 0) {
190 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
191 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
192 anObjectsSelList->value(anObjectsIndex);
193 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
194 if(!anObject.get()) { // may be for not-activated parts
198 anObjects.push_back(anObject);
199 aContextes.push_back(anObjectAttr->context());
203 std::shared_ptr<GeomAPI_Ax1> aFirstAxis;
204 std::shared_ptr<GeomAPI_Edge> anEdge;
205 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
206 selection(FeaturesPlugin_MultiTranslation::AXIS_FIRST_DIR_ID());
207 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
208 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
209 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
210 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
211 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
214 aFirstAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
215 anEdge->line()->direction()));
217 std::shared_ptr<GeomAPI_Ax1> aSecondAxis;
218 anObjRef = selection(FeaturesPlugin_MultiTranslation::AXIS_SECOND_DIR_ID());
219 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
220 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
221 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
222 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
223 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
226 aSecondAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
227 anEdge->line()->direction()));
231 double aFirstStep = real(FeaturesPlugin_MultiTranslation::STEP_FIRST_DIR_ID())->value();
232 double aSecondStep = real(FeaturesPlugin_MultiTranslation::STEP_SECOND_DIR_ID())->value();
234 // Getting number of copies.
236 integer(FeaturesPlugin_MultiTranslation::NB_COPIES_FIRST_DIR_ID())->value();
237 int aSecondNbCopies =
238 integer(FeaturesPlugin_MultiTranslation::NB_COPIES_SECOND_DIR_ID())->value();
241 double x1 = aFirstAxis->dir()->x();
242 double y1 = aFirstAxis->dir()->y();
243 double z1 = aFirstAxis->dir()->z();
244 double norm1 = sqrt(x1*x1 + y1*y1 + z1*z1);
247 double x2 = aSecondAxis->dir()->x();
248 double y2 = aSecondAxis->dir()->y();
249 double z2 = aSecondAxis->dir()->z();
250 double norm2 = sqrt(x2*x2 + y2*y2 + z2*z2);
252 // Moving each object.
253 int aResultIndex = 0;
254 std::list<ResultPtr>::iterator aContext = aContextes.begin();
255 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
256 anObjectsIt++, aContext++) {
257 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
258 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
262 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
263 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
264 for (int j=0; j<aSecondNbCopies; j++) {
265 for (int i=0; i<aFirstNbCopies; i++) {
266 double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
267 double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
268 double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
269 aTrsf->setTranslation(dx, dy, dz);
270 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
271 aResultPart->setTrsf(*aContext, aTrsf);
272 setResult(aResultPart, aResultIndex);
277 ListOfShape aListOfShape;
278 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > aListOfTranslationAlgo;
280 for (int j=0; j<aSecondNbCopies; j++) {
281 for (int i=0; i<aFirstNbCopies; i++) {
282 double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
283 double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
284 double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
285 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
286 new GeomAlgoAPI_Translation(aBaseShape, dx, dy, dz));
288 if (!aTranslationAlgo->check()) {
289 setError(aTranslationAlgo->getError());
293 aTranslationAlgo->build();
295 // Checking that the algorithm worked properly.
296 if (!aTranslationAlgo->isDone()) {
297 static const std::string aFeatureError = "Error : Multitranslation algorithm failed.";
298 setError(aFeatureError);
301 if (aTranslationAlgo->shape()->isNull()) {
302 static const std::string aShapeError = "Error : Resulting shape is null.";
303 setError(aShapeError);
306 if (!aTranslationAlgo->isValid()) {
307 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
308 setError(aFeatureError);
311 aListOfShape.push_back(aTranslationAlgo->shape());
312 aListOfTranslationAlgo.push_back(aTranslationAlgo);
315 std::shared_ptr<GeomAPI_Shape> aCompound =
316 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
317 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
318 aResultBody->storeModified(aBaseShape, aCompound);
319 loadNamingDS(aListOfTranslationAlgo, aResultBody, aBaseShape);
320 setResult(aResultBody, aResultIndex);
325 // Remove the rest results if there were produced in the previous pass.
326 removeResults(aResultIndex);
329 //=================================================================================================
330 void FeaturesPlugin_MultiTranslation::loadNamingDS(
331 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > theListOfTranslationAlgo,
332 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
333 std::shared_ptr<GeomAPI_Shape> theBaseShape)
337 std::string aTranslatedName;
339 for (std::list<std::shared_ptr<GeomAlgoAPI_Translation> >::const_iterator anIt =
340 theListOfTranslationAlgo.begin(); anIt != theListOfTranslationAlgo.cend(); ++anIt) {
341 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = (*anIt)->mapOfSubShapes();
344 aTranslatedName = "Translated_Face_" + std::to_string((long long) anIndex);
345 theResultBody->loadAndOrientModifiedShapes((*anIt).get(), theBaseShape, GeomAPI_Shape::FACE,
346 aTag++, aTranslatedName, *aSubShapes.get(),
350 aTranslatedName = "Translated_Edge_" + std::to_string((long long) anIndex);
351 theResultBody->loadAndOrientModifiedShapes((*anIt).get(), theBaseShape, GeomAPI_Shape::EDGE,
352 aTag++, aTranslatedName, *aSubShapes.get(),
356 aTranslatedName = "Translated_Vertex_" + std::to_string((long long) anIndex);
357 theResultBody->loadAndOrientModifiedShapes((*anIt).get(), theBaseShape, GeomAPI_Shape::VERTEX,
358 aTag++, aTranslatedName, *aSubShapes.get(),