1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <FeaturesPlugin_MultiTranslation.h>
21 #include <FeaturesPlugin_Tools.h>
23 #include <GeomAlgoAPI_CompoundBuilder.h>
24 #include <GeomAlgoAPI_MakeShapeList.h>
25 #include <GeomAlgoAPI_Tools.h>
27 #include <GeomAPI_Ax1.h>
28 #include <GeomAPI_Edge.h>
29 #include <GeomAPI_Lin.h>
30 #include <GeomAPI_ShapeIterator.h>
31 #include <GeomAPI_Trsf.h>
33 #include <ModelAPI_AttributeDouble.h>
34 #include <ModelAPI_AttributeInteger.h>
35 #include <ModelAPI_AttributeSelectionList.h>
36 #include <ModelAPI_AttributeString.h>
37 #include <ModelAPI_ResultBody.h>
38 #include <ModelAPI_ResultPart.h>
42 //=================================================================================================
43 FeaturesPlugin_MultiTranslation::FeaturesPlugin_MultiTranslation()
47 //=================================================================================================
48 void FeaturesPlugin_MultiTranslation::initAttributes()
50 AttributeSelectionListPtr aSelection =
51 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
52 FeaturesPlugin_MultiTranslation::OBJECTS_LIST_ID(),
53 ModelAPI_AttributeSelectionList::typeId()));
55 data()->addAttribute(FeaturesPlugin_MultiTranslation::AXIS_FIRST_DIR_ID(),
56 ModelAPI_AttributeSelection::typeId());
57 data()->addAttribute(FeaturesPlugin_MultiTranslation::STEP_FIRST_DIR_ID(),
58 ModelAPI_AttributeDouble::typeId());
59 data()->addAttribute(FeaturesPlugin_MultiTranslation::NB_COPIES_FIRST_DIR_ID(),
60 ModelAPI_AttributeInteger::typeId());
62 data()->addAttribute(FeaturesPlugin_MultiTranslation::USE_SECOND_DIR_ID(),
63 ModelAPI_AttributeString::typeId());
64 data()->addAttribute(FeaturesPlugin_MultiTranslation::AXIS_SECOND_DIR_ID(),
65 ModelAPI_AttributeSelection::typeId());
66 data()->addAttribute(FeaturesPlugin_MultiTranslation::STEP_SECOND_DIR_ID(),
67 ModelAPI_AttributeDouble::typeId());
68 data()->addAttribute(FeaturesPlugin_MultiTranslation::NB_COPIES_SECOND_DIR_ID(),
69 ModelAPI_AttributeInteger::typeId());
72 //=================================================================================================
73 void FeaturesPlugin_MultiTranslation::execute()
75 std::string useSecondDir = string(FeaturesPlugin_MultiTranslation::USE_SECOND_DIR_ID())->value();
76 if(!useSecondDir.empty()) {
77 performTwoDirection();
79 performOneDirection();
83 //=================================================================================================
84 void FeaturesPlugin_MultiTranslation::performOneDirection()
87 ListOfShape anObjects;
88 std::list<ResultPtr> aContextes;
89 AttributeSelectionListPtr anObjectsSelList =
90 selectionList(FeaturesPlugin_MultiTranslation::OBJECTS_LIST_ID());
91 if (anObjectsSelList->size() == 0) {
92 setError("Error: empty selection list");
95 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
96 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
97 anObjectsSelList->value(anObjectsIndex);
98 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
99 if(!anObject.get()) { // may be for not-activated parts
102 anObjects.push_back(anObject);
103 aContextes.push_back(anObjectAttr->context());
107 static const std::string aSelectionError = "Error: The axis shape selection is bad.";
108 AttributeSelectionPtr anObjRef = selection(AXIS_FIRST_DIR_ID());
109 GeomShapePtr aShape = anObjRef->value();
111 if (anObjRef->context().get()) {
112 aShape = anObjRef->context()->shape();
116 setError(aSelectionError);
121 if (aShape->isEdge())
123 anEdge = aShape->edge();
125 else if (aShape->isCompound())
127 GeomAPI_ShapeIterator anIt(aShape);
128 anEdge = anIt.current()->edge();
133 setError(aSelectionError);
137 std::shared_ptr<GeomAPI_Ax1> anAxis (new GeomAPI_Ax1(anEdge->line()->location(),
138 anEdge->line()->direction()));
141 double aStep = real(FeaturesPlugin_MultiTranslation::STEP_FIRST_DIR_ID())->value();
143 // Getting number of copies.
145 integer(FeaturesPlugin_MultiTranslation::NB_COPIES_FIRST_DIR_ID())->value();
148 std::string aFeatureError = "Multitranslation builder ";
149 aFeatureError+=":: the number of copies for the first direction is null or negative.";
150 setError(aFeatureError);
154 // Moving each object.
155 int aResultIndex = 0;
156 std::list<ResultPtr>::iterator aContext = aContextes.begin();
157 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
158 anObjectsIt++, aContext++) {
159 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
160 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
164 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
165 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
166 for (int i=0; i<nbCopies; i++) {
167 aTrsf->setTranslation(anAxis, i*aStep);
168 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
169 aResultPart->setTrsf(*aContext, aTrsf);
170 setResult(aResultPart, aResultIndex);
175 ListOfShape aListOfShape;
176 ListOfMakeShape aMakeShapeList;
178 for (int i=0; i<nbCopies; i++) {
179 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
180 new GeomAlgoAPI_Translation(aBaseShape, anAxis, i*aStep));
182 // Checking that the algorithm worked properly.
183 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(
184 aTranslationAlgo, getKind(), anError)) {
188 aListOfShape.push_back(aTranslationAlgo->shape());
189 aMakeShapeList.push_back(aTranslationAlgo);
192 std::shared_ptr<GeomAlgoAPI_MakeShapeList>
193 aListOfTranslationAlgo(new GeomAlgoAPI_MakeShapeList(aMakeShapeList));
195 std::shared_ptr<GeomAPI_Shape> aCompound =
196 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
197 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
199 ListOfShape aBaseShapes;
200 aBaseShapes.push_back(aBaseShape);
201 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShapes, ListOfShape(),
202 aListOfTranslationAlgo, aCompound, "Translated");
204 setResult(aResultBody, aResultIndex);
209 // Remove the rest results if there were produced in the previous pass.
210 removeResults(aResultIndex);
213 //=================================================================================================
214 void FeaturesPlugin_MultiTranslation::performTwoDirection()
217 ListOfShape anObjects;
218 std::list<ResultPtr> aContextes;
219 AttributeSelectionListPtr anObjectsSelList =
220 selectionList(FeaturesPlugin_MultiTranslation::OBJECTS_LIST_ID());
221 if (anObjectsSelList->size() == 0) {
222 setError("Error: empty selection list");
225 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
226 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
227 anObjectsSelList->value(anObjectsIndex);
228 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
229 if(!anObject.get()) { // may be for not-activated parts
232 anObjects.push_back(anObject);
233 aContextes.push_back(anObjectAttr->context());
237 static const std::string aSelectionError = "Error: The axis shape selection is bad.";
238 AttributeSelectionPtr anObjRef = selection(AXIS_FIRST_DIR_ID());
239 GeomShapePtr aShape = anObjRef->value();
241 if (anObjRef->context().get()) {
242 aShape = anObjRef->context()->shape();
246 setError(aSelectionError);
251 if (aShape->isEdge())
253 anEdge = aShape->edge();
255 else if (aShape->isCompound())
257 GeomAPI_ShapeIterator anIt(aShape);
258 anEdge = anIt.current()->edge();
263 setError(aSelectionError);
267 std::shared_ptr<GeomAPI_Ax1> aFirstAxis(new GeomAPI_Ax1(anEdge->line()->location(),
268 anEdge->line()->direction()));
271 anObjRef = selection(AXIS_SECOND_DIR_ID());
272 aShape = anObjRef->value();
274 if (anObjRef->context().get()) {
275 aShape = anObjRef->context()->shape();
279 setError(aSelectionError);
283 if (aShape->isEdge())
285 anEdge = aShape->edge();
287 else if (aShape->isCompound())
289 GeomAPI_ShapeIterator anIt(aShape);
290 anEdge = anIt.current()->edge();
295 setError(aSelectionError);
299 std::shared_ptr<GeomAPI_Ax1> aSecondAxis(new GeomAPI_Ax1(anEdge->line()->location(),
300 anEdge->line()->direction()));
303 double aFirstStep = real(FeaturesPlugin_MultiTranslation::STEP_FIRST_DIR_ID())->value();
304 double aSecondStep = real(FeaturesPlugin_MultiTranslation::STEP_SECOND_DIR_ID())->value();
306 // Getting number of copies.
308 integer(FeaturesPlugin_MultiTranslation::NB_COPIES_FIRST_DIR_ID())->value();
309 int aSecondNbCopies =
310 integer(FeaturesPlugin_MultiTranslation::NB_COPIES_SECOND_DIR_ID())->value();
312 if (aFirstNbCopies <=0) {
313 std::string aFeatureError = "Multitranslation builder ";
314 aFeatureError+=":: the number of copies for the first direction is null or negative.";
315 setError(aFeatureError);
319 if (aSecondNbCopies <=0) {
320 std::string aFeatureError = "Multitranslation builder ";
321 aFeatureError+=":: the number of copies for the second direction is null or negative.";
322 setError(aFeatureError);
327 double x1 = aFirstAxis->dir()->x();
328 double y1 = aFirstAxis->dir()->y();
329 double z1 = aFirstAxis->dir()->z();
330 double norm1 = sqrt(x1*x1 + y1*y1 + z1*z1);
333 double x2 = aSecondAxis->dir()->x();
334 double y2 = aSecondAxis->dir()->y();
335 double z2 = aSecondAxis->dir()->z();
336 double norm2 = sqrt(x2*x2 + y2*y2 + z2*z2);
338 // Moving each object.
339 int aResultIndex = 0;
340 std::list<ResultPtr>::iterator aContext = aContextes.begin();
341 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
342 anObjectsIt++, aContext++) {
343 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
344 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
348 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
349 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
350 for (int j=0; j<aSecondNbCopies; j++) {
351 for (int i=0; i<aFirstNbCopies; i++) {
352 double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
353 double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
354 double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
355 aTrsf->setTranslation(dx, dy, dz);
356 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
357 aResultPart->setTrsf(*aContext, aTrsf);
358 setResult(aResultPart, aResultIndex);
364 ListOfShape aListOfShape;
365 ListOfMakeShape aMakeShapeList;
367 for (int j=0; j<aSecondNbCopies; j++) {
368 for (int i=0; i<aFirstNbCopies; i++) {
369 double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
370 double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
371 double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
372 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
373 new GeomAlgoAPI_Translation(aBaseShape, dx, dy, dz));
375 // Checking that the algorithm worked properly.
376 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(
377 aTranslationAlgo, getKind(), anError)) {
381 aListOfShape.push_back(aTranslationAlgo->shape());
382 aMakeShapeList.push_back(aTranslationAlgo);
386 std::shared_ptr<GeomAlgoAPI_MakeShapeList>
387 aListOfTranslationAlgo(new GeomAlgoAPI_MakeShapeList(aMakeShapeList));
389 std::shared_ptr<GeomAPI_Shape> aCompound =
390 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
391 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
393 ListOfShape aBaseShapes;
394 aBaseShapes.push_back(aBaseShape);
395 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShapes, ListOfShape(),
396 aListOfTranslationAlgo, aCompound, "Translated");
398 setResult(aResultBody, aResultIndex);
403 // Remove the rest results if there were produced in the previous pass.
404 removeResults(aResultIndex);