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 std::shared_ptr<GeomAlgoAPI_MakeShapeList>
177 aListOfTranslationAlgo(new GeomAlgoAPI_MakeShapeList);
179 for (int i=0; i<nbCopies; i++) {
180 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
181 new GeomAlgoAPI_Translation(aBaseShape, anAxis, i*aStep));
183 if (!aTranslationAlgo->check()) {
184 setError(aTranslationAlgo->getError());
188 aTranslationAlgo->build();
190 // Checking that the algorithm worked properly.
191 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(
192 aTranslationAlgo, getKind(), anError)) {
196 aListOfShape.push_back(aTranslationAlgo->shape());
197 aListOfTranslationAlgo->appendAlgo(aTranslationAlgo);
199 std::shared_ptr<GeomAPI_Shape> aCompound =
200 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
201 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
203 ListOfShape aBaseShapes;
204 aBaseShapes.push_back(aBaseShape);
205 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShapes, ListOfShape(),
206 aListOfTranslationAlgo, aCompound, "Translated");
208 setResult(aResultBody, aResultIndex);
213 // Remove the rest results if there were produced in the previous pass.
214 removeResults(aResultIndex);
217 //=================================================================================================
218 void FeaturesPlugin_MultiTranslation::performTwoDirection()
221 ListOfShape anObjects;
222 std::list<ResultPtr> aContextes;
223 AttributeSelectionListPtr anObjectsSelList =
224 selectionList(FeaturesPlugin_MultiTranslation::OBJECTS_LIST_ID());
225 if (anObjectsSelList->size() == 0) {
226 setError("Error: empty selection list");
229 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
230 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
231 anObjectsSelList->value(anObjectsIndex);
232 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
233 if(!anObject.get()) { // may be for not-activated parts
236 anObjects.push_back(anObject);
237 aContextes.push_back(anObjectAttr->context());
241 static const std::string aSelectionError = "Error: The axis shape selection is bad.";
242 AttributeSelectionPtr anObjRef = selection(AXIS_FIRST_DIR_ID());
243 GeomShapePtr aShape = anObjRef->value();
245 if (anObjRef->context().get()) {
246 aShape = anObjRef->context()->shape();
250 setError(aSelectionError);
255 if (aShape->isEdge())
257 anEdge = aShape->edge();
259 else if (aShape->isCompound())
261 GeomAPI_ShapeIterator anIt(aShape);
262 anEdge = anIt.current()->edge();
267 setError(aSelectionError);
271 std::shared_ptr<GeomAPI_Ax1> aFirstAxis(new GeomAPI_Ax1(anEdge->line()->location(),
272 anEdge->line()->direction()));
275 anObjRef = selection(AXIS_SECOND_DIR_ID());
276 aShape = anObjRef->value();
278 if (anObjRef->context().get()) {
279 aShape = anObjRef->context()->shape();
283 setError(aSelectionError);
287 if (aShape->isEdge())
289 anEdge = aShape->edge();
291 else if (aShape->isCompound())
293 GeomAPI_ShapeIterator anIt(aShape);
294 anEdge = anIt.current()->edge();
299 setError(aSelectionError);
303 std::shared_ptr<GeomAPI_Ax1> aSecondAxis(new GeomAPI_Ax1(anEdge->line()->location(),
304 anEdge->line()->direction()));
307 double aFirstStep = real(FeaturesPlugin_MultiTranslation::STEP_FIRST_DIR_ID())->value();
308 double aSecondStep = real(FeaturesPlugin_MultiTranslation::STEP_SECOND_DIR_ID())->value();
310 // Getting number of copies.
312 integer(FeaturesPlugin_MultiTranslation::NB_COPIES_FIRST_DIR_ID())->value();
313 int aSecondNbCopies =
314 integer(FeaturesPlugin_MultiTranslation::NB_COPIES_SECOND_DIR_ID())->value();
316 if (aFirstNbCopies <=0) {
317 std::string aFeatureError = "Multitranslation builder ";
318 aFeatureError+=":: the number of copies for the first direction is null or negative.";
319 setError(aFeatureError);
323 if (aSecondNbCopies <=0) {
324 std::string aFeatureError = "Multitranslation builder ";
325 aFeatureError+=":: the number of copies for the second direction is null or negative.";
326 setError(aFeatureError);
331 double x1 = aFirstAxis->dir()->x();
332 double y1 = aFirstAxis->dir()->y();
333 double z1 = aFirstAxis->dir()->z();
334 double norm1 = sqrt(x1*x1 + y1*y1 + z1*z1);
337 double x2 = aSecondAxis->dir()->x();
338 double y2 = aSecondAxis->dir()->y();
339 double z2 = aSecondAxis->dir()->z();
340 double norm2 = sqrt(x2*x2 + y2*y2 + z2*z2);
342 // Moving each object.
343 int aResultIndex = 0;
344 std::list<ResultPtr>::iterator aContext = aContextes.begin();
345 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
346 anObjectsIt++, aContext++) {
347 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
348 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
352 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
353 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
354 for (int j=0; j<aSecondNbCopies; j++) {
355 for (int i=0; i<aFirstNbCopies; i++) {
356 double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
357 double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
358 double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
359 aTrsf->setTranslation(dx, dy, dz);
360 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
361 aResultPart->setTrsf(*aContext, aTrsf);
362 setResult(aResultPart, aResultIndex);
368 ListOfShape aListOfShape;
369 std::shared_ptr<GeomAlgoAPI_MakeShapeList>
370 aListOfTranslationAlgo(new GeomAlgoAPI_MakeShapeList);
372 for (int j=0; j<aSecondNbCopies; j++) {
373 for (int i=0; i<aFirstNbCopies; i++) {
374 double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
375 double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
376 double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
377 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
378 new GeomAlgoAPI_Translation(aBaseShape, dx, dy, dz));
380 if (!aTranslationAlgo->check()) {
381 setError(aTranslationAlgo->getError());
385 aTranslationAlgo->build();
387 // Checking that the algorithm worked properly.
388 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(
389 aTranslationAlgo, getKind(), anError)) {
393 aListOfShape.push_back(aTranslationAlgo->shape());
394 aListOfTranslationAlgo->appendAlgo(aTranslationAlgo);
397 std::shared_ptr<GeomAPI_Shape> aCompound =
398 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
399 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
401 ListOfShape aBaseShapes;
402 aBaseShapes.push_back(aBaseShape);
403 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShapes, ListOfShape(),
404 aListOfTranslationAlgo, aCompound, "Translated");
406 setResult(aResultBody, aResultIndex);
411 // Remove the rest results if there were produced in the previous pass.
412 removeResults(aResultIndex);