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>
14 #include <GeomAPI_Trsf.h>
16 #include <ModelAPI_AttributeDouble.h>
17 #include <ModelAPI_AttributeInteger.h>
18 #include <ModelAPI_AttributeSelectionList.h>
19 #include <ModelAPI_AttributeString.h>
20 #include <ModelAPI_ResultBody.h>
21 #include <ModelAPI_ResultPart.h>
25 //=================================================================================================
26 FeaturesPlugin_MultiTranslation::FeaturesPlugin_MultiTranslation()
30 //=================================================================================================
31 void FeaturesPlugin_MultiTranslation::initAttributes()
33 AttributeSelectionListPtr aSelection =
34 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
35 FeaturesPlugin_MultiTranslation::OBJECTS_LIST_ID(),
36 ModelAPI_AttributeSelectionList::typeId()));
38 data()->addAttribute(FeaturesPlugin_MultiTranslation::AXIS_FIRST_DIR_ID(),
39 ModelAPI_AttributeSelection::typeId());
40 data()->addAttribute(FeaturesPlugin_MultiTranslation::STEP_FIRST_DIR_ID(),
41 ModelAPI_AttributeDouble::typeId());
42 data()->addAttribute(FeaturesPlugin_MultiTranslation::NB_COPIES_FIRST_DIR_ID(),
43 ModelAPI_AttributeInteger::typeId());
45 data()->addAttribute(FeaturesPlugin_MultiTranslation::USE_SECOND_DIR_ID(),
46 ModelAPI_AttributeString::typeId());
47 data()->addAttribute(FeaturesPlugin_MultiTranslation::AXIS_SECOND_DIR_ID(),
48 ModelAPI_AttributeSelection::typeId());
49 data()->addAttribute(FeaturesPlugin_MultiTranslation::STEP_SECOND_DIR_ID(),
50 ModelAPI_AttributeDouble::typeId());
51 data()->addAttribute(FeaturesPlugin_MultiTranslation::NB_COPIES_SECOND_DIR_ID(),
52 ModelAPI_AttributeInteger::typeId());
55 //=================================================================================================
56 void FeaturesPlugin_MultiTranslation::execute()
58 std::string useSecondDir = string(FeaturesPlugin_MultiTranslation::USE_SECOND_DIR_ID())->value();
59 if(!useSecondDir.empty()) {
60 performTwoDirection();
62 performOneDirection();
66 //=================================================================================================
67 void FeaturesPlugin_MultiTranslation::performOneDirection()
70 ListOfShape anObjects;
71 std::list<ResultPtr> aContextes;
72 AttributeSelectionListPtr anObjectsSelList =
73 selectionList(FeaturesPlugin_MultiTranslation::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_MultiTranslation::AXIS_FIRST_DIR_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 aStep = real(FeaturesPlugin_MultiTranslation::STEP_FIRST_DIR_ID())->value();
108 // Getting number of copies.
110 integer(FeaturesPlugin_MultiTranslation::NB_COPIES_FIRST_DIR_ID())->value();
113 std::string aFeatureError = "Multitranslation builder ";
114 aFeatureError+=":: the number of copies for the first direction is null or negative.";
115 setError(aFeatureError);
118 // Moving each object.
119 int aResultIndex = 0;
120 std::list<ResultPtr>::iterator aContext = aContextes.begin();
121 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
122 anObjectsIt++, aContext++) {
123 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
124 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
128 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
129 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
130 for (int i=0; i<nbCopies; i++) {
131 aTrsf->setTranslation(anAxis, i*aStep);
132 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
133 aResultPart->setTrsf(*aContext, aTrsf);
134 setResult(aResultPart, aResultIndex);
138 ListOfShape aListOfShape;
139 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > aListOfTranslationAlgo;
141 for (int i=0; i<nbCopies; i++) {
142 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
143 new GeomAlgoAPI_Translation(aBaseShape, anAxis, i*aStep));
145 if (!aTranslationAlgo->check()) {
146 setError(aTranslationAlgo->getError());
150 aTranslationAlgo->build();
152 // Checking that the algorithm worked properly.
153 if (!aTranslationAlgo->isDone()) {
154 static const std::string aFeatureError = "Error : Multitranslation algorithm failed.";
155 setError(aFeatureError);
158 if (aTranslationAlgo->shape()->isNull()) {
159 static const std::string aShapeError = "Error : Resulting shape is null.";
160 setError(aShapeError);
163 if (!aTranslationAlgo->isValid()) {
164 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
165 setError(aFeatureError);
168 aListOfShape.push_back(aTranslationAlgo->shape());
169 aListOfTranslationAlgo.push_back(aTranslationAlgo);
171 std::shared_ptr<GeomAPI_Shape> aCompound =
172 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
173 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
174 aResultBody->storeModified(aBaseShape, aCompound);
175 loadNamingDS(aListOfTranslationAlgo, aResultBody, aBaseShape);
177 setResult(aResultBody, aResultIndex);
182 // Remove the rest results if there were produced in the previous pass.
183 removeResults(aResultIndex);
186 //=================================================================================================
187 void FeaturesPlugin_MultiTranslation::performTwoDirection()
190 ListOfShape anObjects;
191 std::list<ResultPtr> aContextes;
192 AttributeSelectionListPtr anObjectsSelList =
193 selectionList(FeaturesPlugin_MultiTranslation::OBJECTS_LIST_ID());
194 if (anObjectsSelList->size() == 0) {
197 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
198 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
199 anObjectsSelList->value(anObjectsIndex);
200 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
201 if(!anObject.get()) { // may be for not-activated parts
205 anObjects.push_back(anObject);
206 aContextes.push_back(anObjectAttr->context());
210 std::shared_ptr<GeomAPI_Ax1> aFirstAxis;
211 std::shared_ptr<GeomAPI_Edge> anEdge;
212 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
213 selection(FeaturesPlugin_MultiTranslation::AXIS_FIRST_DIR_ID());
214 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
215 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
216 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
217 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
218 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
221 aFirstAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
222 anEdge->line()->direction()));
224 std::shared_ptr<GeomAPI_Ax1> aSecondAxis;
225 anObjRef = selection(FeaturesPlugin_MultiTranslation::AXIS_SECOND_DIR_ID());
226 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
227 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
228 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
229 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
230 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
233 aSecondAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
234 anEdge->line()->direction()));
238 double aFirstStep = real(FeaturesPlugin_MultiTranslation::STEP_FIRST_DIR_ID())->value();
239 double aSecondStep = real(FeaturesPlugin_MultiTranslation::STEP_SECOND_DIR_ID())->value();
241 // Getting number of copies.
243 integer(FeaturesPlugin_MultiTranslation::NB_COPIES_FIRST_DIR_ID())->value();
244 int aSecondNbCopies =
245 integer(FeaturesPlugin_MultiTranslation::NB_COPIES_SECOND_DIR_ID())->value();
247 if (aFirstNbCopies <=0) {
248 std::string aFeatureError = "Multitranslation builder ";
249 aFeatureError+=":: the number of copies for the first direction is null or negative.";
250 setError(aFeatureError);
253 if (aSecondNbCopies <=0) {
254 std::string aFeatureError = "Multitranslation builder ";
255 aFeatureError+=":: the number of copies for the second direction is null or negative.";
256 setError(aFeatureError);
260 double x1 = aFirstAxis->dir()->x();
261 double y1 = aFirstAxis->dir()->y();
262 double z1 = aFirstAxis->dir()->z();
263 double norm1 = sqrt(x1*x1 + y1*y1 + z1*z1);
266 double x2 = aSecondAxis->dir()->x();
267 double y2 = aSecondAxis->dir()->y();
268 double z2 = aSecondAxis->dir()->z();
269 double norm2 = sqrt(x2*x2 + y2*y2 + z2*z2);
271 // Moving each object.
272 int aResultIndex = 0;
273 std::list<ResultPtr>::iterator aContext = aContextes.begin();
274 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
275 anObjectsIt++, aContext++) {
276 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
277 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
281 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
282 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
283 for (int j=0; j<aSecondNbCopies; j++) {
284 for (int i=0; i<aFirstNbCopies; i++) {
285 double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
286 double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
287 double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
288 aTrsf->setTranslation(dx, dy, dz);
289 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
290 aResultPart->setTrsf(*aContext, aTrsf);
291 setResult(aResultPart, aResultIndex);
296 ListOfShape aListOfShape;
297 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > aListOfTranslationAlgo;
299 for (int j=0; j<aSecondNbCopies; j++) {
300 for (int i=0; i<aFirstNbCopies; i++) {
301 double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
302 double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
303 double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
304 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
305 new GeomAlgoAPI_Translation(aBaseShape, dx, dy, dz));
307 if (!aTranslationAlgo->check()) {
308 setError(aTranslationAlgo->getError());
312 aTranslationAlgo->build();
314 // Checking that the algorithm worked properly.
315 if (!aTranslationAlgo->isDone()) {
316 static const std::string aFeatureError = "Error : Multitranslation algorithm failed.";
317 setError(aFeatureError);
320 if (aTranslationAlgo->shape()->isNull()) {
321 static const std::string aShapeError = "Error : Resulting shape is null.";
322 setError(aShapeError);
325 if (!aTranslationAlgo->isValid()) {
326 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
327 setError(aFeatureError);
330 aListOfShape.push_back(aTranslationAlgo->shape());
331 aListOfTranslationAlgo.push_back(aTranslationAlgo);
334 std::shared_ptr<GeomAPI_Shape> aCompound =
335 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
336 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
337 aResultBody->storeModified(aBaseShape, aCompound);
338 loadNamingDS(aListOfTranslationAlgo, aResultBody, aBaseShape);
339 setResult(aResultBody, aResultIndex);
344 // Remove the rest results if there were produced in the previous pass.
345 removeResults(aResultIndex);
348 //=================================================================================================
349 void FeaturesPlugin_MultiTranslation::loadNamingDS(
350 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > theListOfTranslationAlgo,
351 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
352 std::shared_ptr<GeomAPI_Shape> theBaseShape)
356 std::string aTranslatedName;
358 for (std::list<std::shared_ptr<GeomAlgoAPI_Translation> >::const_iterator anIt =
359 theListOfTranslationAlgo.begin(); anIt != theListOfTranslationAlgo.cend(); ++anIt) {
360 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = (*anIt)->mapOfSubShapes();
363 aTranslatedName = "Translated_Face_" + std::to_string((long long) anIndex);
364 theResultBody->loadAndOrientModifiedShapes((*anIt).get(), theBaseShape, GeomAPI_Shape::FACE,
365 aTag++, aTranslatedName, *aSubShapes.get(),
369 aTranslatedName = "Translated_Edge_" + std::to_string((long long) anIndex);
370 theResultBody->loadAndOrientModifiedShapes((*anIt).get(), theBaseShape, GeomAPI_Shape::EDGE,
371 aTag++, aTranslatedName, *aSubShapes.get(),
375 aTranslatedName = "Translated_Vertex_" + std::to_string((long long) anIndex);
376 theResultBody->loadAndOrientModifiedShapes((*anIt).get(), theBaseShape, GeomAPI_Shape::VERTEX,
377 aTag++, aTranslatedName, *aSubShapes.get(),