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();
112 std::string aFeatureError = "Multitranslation builder ";
113 aFeatureError+=":: the number of copies for the first direction is null or negative.";
114 setError(aFeatureError);
117 // Moving each object.
118 int aResultIndex = 0;
119 std::list<ResultPtr>::iterator aContext = aContextes.begin();
120 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
121 anObjectsIt++, aContext++) {
122 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
123 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
127 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
128 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
129 for (int i=0; i<nbCopies; i++) {
130 aTrsf->setTranslation(anAxis, i*aStep);
131 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
132 aResultPart->setTrsf(*aContext, aTrsf);
133 setResult(aResultPart, aResultIndex);
137 ListOfShape aListOfShape;
138 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > aListOfTranslationAlgo;
140 for (int i=0; i<nbCopies; i++) {
141 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
142 new GeomAlgoAPI_Translation(aBaseShape, anAxis, i*aStep));
144 if (!aTranslationAlgo->check()) {
145 setError(aTranslationAlgo->getError());
149 aTranslationAlgo->build();
151 // Checking that the algorithm worked properly.
152 if (!aTranslationAlgo->isDone()) {
153 static const std::string aFeatureError = "Error : Multitranslation algorithm failed.";
154 setError(aFeatureError);
157 if (aTranslationAlgo->shape()->isNull()) {
158 static const std::string aShapeError = "Error : Resulting shape is null.";
159 setError(aShapeError);
162 if (!aTranslationAlgo->isValid()) {
163 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
164 setError(aFeatureError);
167 aListOfShape.push_back(aTranslationAlgo->shape());
168 aListOfTranslationAlgo.push_back(aTranslationAlgo);
170 std::shared_ptr<GeomAPI_Shape> aCompound =
171 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
172 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
173 aResultBody->storeModified(aBaseShape, aCompound);
174 loadNamingDS(aListOfTranslationAlgo, aResultBody, aBaseShape);
176 setResult(aResultBody, aResultIndex);
181 // Remove the rest results if there were produced in the previous pass.
182 removeResults(aResultIndex);
185 //=================================================================================================
186 void FeaturesPlugin_MultiTranslation::performTwoDirection()
189 ListOfShape anObjects;
190 std::list<ResultPtr> aContextes;
191 AttributeSelectionListPtr anObjectsSelList =
192 selectionList(FeaturesPlugin_MultiTranslation::OBJECTS_LIST_ID());
193 if (anObjectsSelList->size() == 0) {
196 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
197 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
198 anObjectsSelList->value(anObjectsIndex);
199 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
200 if(!anObject.get()) { // may be for not-activated parts
204 anObjects.push_back(anObject);
205 aContextes.push_back(anObjectAttr->context());
209 std::shared_ptr<GeomAPI_Ax1> aFirstAxis;
210 std::shared_ptr<GeomAPI_Edge> anEdge;
211 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
212 selection(FeaturesPlugin_MultiTranslation::AXIS_FIRST_DIR_ID());
213 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
214 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
215 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
216 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
217 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
220 aFirstAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
221 anEdge->line()->direction()));
223 std::shared_ptr<GeomAPI_Ax1> aSecondAxis;
224 anObjRef = selection(FeaturesPlugin_MultiTranslation::AXIS_SECOND_DIR_ID());
225 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
226 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
227 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
228 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
229 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
232 aSecondAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
233 anEdge->line()->direction()));
237 double aFirstStep = real(FeaturesPlugin_MultiTranslation::STEP_FIRST_DIR_ID())->value();
238 double aSecondStep = real(FeaturesPlugin_MultiTranslation::STEP_SECOND_DIR_ID())->value();
240 // Getting number of copies.
242 integer(FeaturesPlugin_MultiTranslation::NB_COPIES_FIRST_DIR_ID())->value();
243 int aSecondNbCopies =
244 integer(FeaturesPlugin_MultiTranslation::NB_COPIES_SECOND_DIR_ID())->value();
246 if (aFirstNbCopies <=0) {
247 std::string aFeatureError = "Multitranslation builder ";
248 aFeatureError+=":: the number of copies for the first direction is null or negative.";
249 setError(aFeatureError);
252 if (aSecondNbCopies <=0) {
253 std::string aFeatureError = "Multitranslation builder ";
254 aFeatureError+=":: the number of copies for the second direction is null or negative.";
255 setError(aFeatureError);
259 double x1 = aFirstAxis->dir()->x();
260 double y1 = aFirstAxis->dir()->y();
261 double z1 = aFirstAxis->dir()->z();
262 double norm1 = sqrt(x1*x1 + y1*y1 + z1*z1);
265 double x2 = aSecondAxis->dir()->x();
266 double y2 = aSecondAxis->dir()->y();
267 double z2 = aSecondAxis->dir()->z();
268 double norm2 = sqrt(x2*x2 + y2*y2 + z2*z2);
270 // Moving each object.
271 int aResultIndex = 0;
272 std::list<ResultPtr>::iterator aContext = aContextes.begin();
273 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
274 anObjectsIt++, aContext++) {
275 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
276 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
280 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
281 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
282 for (int j=0; j<aSecondNbCopies; j++) {
283 for (int i=0; i<aFirstNbCopies; i++) {
284 double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
285 double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
286 double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
287 aTrsf->setTranslation(dx, dy, dz);
288 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
289 aResultPart->setTrsf(*aContext, aTrsf);
290 setResult(aResultPart, aResultIndex);
295 ListOfShape aListOfShape;
296 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > aListOfTranslationAlgo;
298 for (int j=0; j<aSecondNbCopies; j++) {
299 for (int i=0; i<aFirstNbCopies; i++) {
300 double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
301 double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
302 double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
303 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
304 new GeomAlgoAPI_Translation(aBaseShape, dx, dy, dz));
306 if (!aTranslationAlgo->check()) {
307 setError(aTranslationAlgo->getError());
311 aTranslationAlgo->build();
313 // Checking that the algorithm worked properly.
314 if (!aTranslationAlgo->isDone()) {
315 static const std::string aFeatureError = "Error : Multitranslation algorithm failed.";
316 setError(aFeatureError);
319 if (aTranslationAlgo->shape()->isNull()) {
320 static const std::string aShapeError = "Error : Resulting shape is null.";
321 setError(aShapeError);
324 if (!aTranslationAlgo->isValid()) {
325 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
326 setError(aFeatureError);
329 aListOfShape.push_back(aTranslationAlgo->shape());
330 aListOfTranslationAlgo.push_back(aTranslationAlgo);
333 std::shared_ptr<GeomAPI_Shape> aCompound =
334 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
335 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
336 aResultBody->storeModified(aBaseShape, aCompound);
337 loadNamingDS(aListOfTranslationAlgo, aResultBody, aBaseShape);
338 setResult(aResultBody, aResultIndex);
343 // Remove the rest results if there were produced in the previous pass.
344 removeResults(aResultIndex);
347 //=================================================================================================
348 void FeaturesPlugin_MultiTranslation::loadNamingDS(
349 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > theListOfTranslationAlgo,
350 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
351 std::shared_ptr<GeomAPI_Shape> theBaseShape)
355 std::string aTranslatedName;
357 for (std::list<std::shared_ptr<GeomAlgoAPI_Translation> >::const_iterator anIt =
358 theListOfTranslationAlgo.begin(); anIt != theListOfTranslationAlgo.cend(); ++anIt) {
359 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = (*anIt)->mapOfSubShapes();
362 aTranslatedName = "Translated_Face_" + std::to_string((long long) anIndex);
363 theResultBody->loadAndOrientModifiedShapes((*anIt).get(), theBaseShape, GeomAPI_Shape::FACE,
364 aTag++, aTranslatedName, *aSubShapes.get(),
368 aTranslatedName = "Translated_Edge_" + std::to_string((long long) anIndex);
369 theResultBody->loadAndOrientModifiedShapes((*anIt).get(), theBaseShape, GeomAPI_Shape::EDGE,
370 aTag++, aTranslatedName, *aSubShapes.get(),
374 aTranslatedName = "Translated_Vertex_" + std::to_string((long long) anIndex);
375 theResultBody->loadAndOrientModifiedShapes((*anIt).get(), theBaseShape, GeomAPI_Shape::VERTEX,
376 aTag++, aTranslatedName, *aSubShapes.get(),