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 if (!aTranslationAlgo->check()) {
183 setError(aTranslationAlgo->getError());
187 aTranslationAlgo->build();
189 // Checking that the algorithm worked properly.
190 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(
191 aTranslationAlgo, getKind(), anError)) {
195 aListOfShape.push_back(aTranslationAlgo->shape());
196 aMakeShapeList.push_back(aTranslationAlgo);
199 std::shared_ptr<GeomAlgoAPI_MakeShapeList>
200 aListOfTranslationAlgo(new GeomAlgoAPI_MakeShapeList(aMakeShapeList));
202 std::shared_ptr<GeomAPI_Shape> aCompound =
203 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
204 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
206 ListOfShape aBaseShapes;
207 aBaseShapes.push_back(aBaseShape);
208 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShapes, ListOfShape(),
209 aListOfTranslationAlgo, aCompound, "Translated");
211 setResult(aResultBody, aResultIndex);
216 // Remove the rest results if there were produced in the previous pass.
217 removeResults(aResultIndex);
220 //=================================================================================================
221 void FeaturesPlugin_MultiTranslation::performTwoDirection()
224 ListOfShape anObjects;
225 std::list<ResultPtr> aContextes;
226 AttributeSelectionListPtr anObjectsSelList =
227 selectionList(FeaturesPlugin_MultiTranslation::OBJECTS_LIST_ID());
228 if (anObjectsSelList->size() == 0) {
229 setError("Error: empty selection list");
232 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
233 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
234 anObjectsSelList->value(anObjectsIndex);
235 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
236 if(!anObject.get()) { // may be for not-activated parts
239 anObjects.push_back(anObject);
240 aContextes.push_back(anObjectAttr->context());
244 static const std::string aSelectionError = "Error: The axis shape selection is bad.";
245 AttributeSelectionPtr anObjRef = selection(AXIS_FIRST_DIR_ID());
246 GeomShapePtr aShape = anObjRef->value();
248 if (anObjRef->context().get()) {
249 aShape = anObjRef->context()->shape();
253 setError(aSelectionError);
258 if (aShape->isEdge())
260 anEdge = aShape->edge();
262 else if (aShape->isCompound())
264 GeomAPI_ShapeIterator anIt(aShape);
265 anEdge = anIt.current()->edge();
270 setError(aSelectionError);
274 std::shared_ptr<GeomAPI_Ax1> aFirstAxis(new GeomAPI_Ax1(anEdge->line()->location(),
275 anEdge->line()->direction()));
278 anObjRef = selection(AXIS_SECOND_DIR_ID());
279 aShape = anObjRef->value();
281 if (anObjRef->context().get()) {
282 aShape = anObjRef->context()->shape();
286 setError(aSelectionError);
290 if (aShape->isEdge())
292 anEdge = aShape->edge();
294 else if (aShape->isCompound())
296 GeomAPI_ShapeIterator anIt(aShape);
297 anEdge = anIt.current()->edge();
302 setError(aSelectionError);
306 std::shared_ptr<GeomAPI_Ax1> aSecondAxis(new GeomAPI_Ax1(anEdge->line()->location(),
307 anEdge->line()->direction()));
310 double aFirstStep = real(FeaturesPlugin_MultiTranslation::STEP_FIRST_DIR_ID())->value();
311 double aSecondStep = real(FeaturesPlugin_MultiTranslation::STEP_SECOND_DIR_ID())->value();
313 // Getting number of copies.
315 integer(FeaturesPlugin_MultiTranslation::NB_COPIES_FIRST_DIR_ID())->value();
316 int aSecondNbCopies =
317 integer(FeaturesPlugin_MultiTranslation::NB_COPIES_SECOND_DIR_ID())->value();
319 if (aFirstNbCopies <=0) {
320 std::string aFeatureError = "Multitranslation builder ";
321 aFeatureError+=":: the number of copies for the first direction is null or negative.";
322 setError(aFeatureError);
326 if (aSecondNbCopies <=0) {
327 std::string aFeatureError = "Multitranslation builder ";
328 aFeatureError+=":: the number of copies for the second direction is null or negative.";
329 setError(aFeatureError);
334 double x1 = aFirstAxis->dir()->x();
335 double y1 = aFirstAxis->dir()->y();
336 double z1 = aFirstAxis->dir()->z();
337 double norm1 = sqrt(x1*x1 + y1*y1 + z1*z1);
340 double x2 = aSecondAxis->dir()->x();
341 double y2 = aSecondAxis->dir()->y();
342 double z2 = aSecondAxis->dir()->z();
343 double norm2 = sqrt(x2*x2 + y2*y2 + z2*z2);
345 // Moving each object.
346 int aResultIndex = 0;
347 std::list<ResultPtr>::iterator aContext = aContextes.begin();
348 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
349 anObjectsIt++, aContext++) {
350 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
351 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
355 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
356 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
357 for (int j=0; j<aSecondNbCopies; j++) {
358 for (int i=0; i<aFirstNbCopies; i++) {
359 double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
360 double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
361 double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
362 aTrsf->setTranslation(dx, dy, dz);
363 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
364 aResultPart->setTrsf(*aContext, aTrsf);
365 setResult(aResultPart, aResultIndex);
371 ListOfShape aListOfShape;
372 ListOfMakeShape aMakeShapeList;
374 for (int j=0; j<aSecondNbCopies; j++) {
375 for (int i=0; i<aFirstNbCopies; i++) {
376 double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
377 double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
378 double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
379 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
380 new GeomAlgoAPI_Translation(aBaseShape, dx, dy, dz));
382 if (!aTranslationAlgo->check()) {
383 setError(aTranslationAlgo->getError());
387 aTranslationAlgo->build();
389 // Checking that the algorithm worked properly.
390 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(
391 aTranslationAlgo, getKind(), anError)) {
395 aListOfShape.push_back(aTranslationAlgo->shape());
396 aMakeShapeList.push_back(aTranslationAlgo);
400 std::shared_ptr<GeomAlgoAPI_MakeShapeList>
401 aListOfTranslationAlgo(new GeomAlgoAPI_MakeShapeList(aMakeShapeList));
403 std::shared_ptr<GeomAPI_Shape> aCompound =
404 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
405 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
407 ListOfShape aBaseShapes;
408 aBaseShapes.push_back(aBaseShape);
409 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShapes, ListOfShape(),
410 aListOfTranslationAlgo, aCompound, "Translated");
412 setResult(aResultBody, aResultIndex);
417 // Remove the rest results if there were produced in the previous pass.
418 removeResults(aResultIndex);