1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <FeaturesPlugin_MultiTranslation.h>
23 #include <GeomAlgoAPI_CompoundBuilder.h>
25 #include <GeomAPI_Ax1.h>
26 #include <GeomAPI_Edge.h>
27 #include <GeomAPI_Lin.h>
28 #include <GeomAPI_ShapeIterator.h>
29 #include <GeomAPI_Trsf.h>
31 #include <ModelAPI_AttributeDouble.h>
32 #include <ModelAPI_AttributeInteger.h>
33 #include <ModelAPI_AttributeSelectionList.h>
34 #include <ModelAPI_AttributeString.h>
35 #include <ModelAPI_ResultBody.h>
36 #include <ModelAPI_ResultPart.h>
40 //=================================================================================================
41 FeaturesPlugin_MultiTranslation::FeaturesPlugin_MultiTranslation()
45 //=================================================================================================
46 void FeaturesPlugin_MultiTranslation::initAttributes()
48 AttributeSelectionListPtr aSelection =
49 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
50 FeaturesPlugin_MultiTranslation::OBJECTS_LIST_ID(),
51 ModelAPI_AttributeSelectionList::typeId()));
53 data()->addAttribute(FeaturesPlugin_MultiTranslation::AXIS_FIRST_DIR_ID(),
54 ModelAPI_AttributeSelection::typeId());
55 data()->addAttribute(FeaturesPlugin_MultiTranslation::STEP_FIRST_DIR_ID(),
56 ModelAPI_AttributeDouble::typeId());
57 data()->addAttribute(FeaturesPlugin_MultiTranslation::NB_COPIES_FIRST_DIR_ID(),
58 ModelAPI_AttributeInteger::typeId());
60 data()->addAttribute(FeaturesPlugin_MultiTranslation::USE_SECOND_DIR_ID(),
61 ModelAPI_AttributeString::typeId());
62 data()->addAttribute(FeaturesPlugin_MultiTranslation::AXIS_SECOND_DIR_ID(),
63 ModelAPI_AttributeSelection::typeId());
64 data()->addAttribute(FeaturesPlugin_MultiTranslation::STEP_SECOND_DIR_ID(),
65 ModelAPI_AttributeDouble::typeId());
66 data()->addAttribute(FeaturesPlugin_MultiTranslation::NB_COPIES_SECOND_DIR_ID(),
67 ModelAPI_AttributeInteger::typeId());
70 //=================================================================================================
71 void FeaturesPlugin_MultiTranslation::execute()
73 std::string useSecondDir = string(FeaturesPlugin_MultiTranslation::USE_SECOND_DIR_ID())->value();
74 if(!useSecondDir.empty()) {
75 performTwoDirection();
77 performOneDirection();
81 //=================================================================================================
82 void FeaturesPlugin_MultiTranslation::performOneDirection()
85 ListOfShape anObjects;
86 std::list<ResultPtr> aContextes;
87 AttributeSelectionListPtr anObjectsSelList =
88 selectionList(FeaturesPlugin_MultiTranslation::OBJECTS_LIST_ID());
89 if (anObjectsSelList->size() == 0) {
92 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
93 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
94 anObjectsSelList->value(anObjectsIndex);
95 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
96 if(!anObject.get()) { // may be for not-activated parts
100 anObjects.push_back(anObject);
101 aContextes.push_back(anObjectAttr->context());
105 static const std::string aSelectionError = "Error: The axis shape selection is bad.";
106 AttributeSelectionPtr anObjRef = selection(AXIS_FIRST_DIR_ID());
107 GeomShapePtr aShape = anObjRef->value();
109 if (anObjRef->context().get()) {
110 aShape = anObjRef->context()->shape();
114 setError(aSelectionError);
119 if (aShape->isEdge())
121 anEdge = aShape->edge();
123 else if (aShape->isCompound())
125 GeomAPI_ShapeIterator anIt(aShape);
126 anEdge = anIt.current()->edge();
130 setError(aSelectionError);
136 setError(aSelectionError);
140 std::shared_ptr<GeomAPI_Ax1> anAxis (new GeomAPI_Ax1(anEdge->line()->location(),
141 anEdge->line()->direction()));
144 double aStep = real(FeaturesPlugin_MultiTranslation::STEP_FIRST_DIR_ID())->value();
146 // Getting number of copies.
148 integer(FeaturesPlugin_MultiTranslation::NB_COPIES_FIRST_DIR_ID())->value();
151 std::string aFeatureError = "Multitranslation builder ";
152 aFeatureError+=":: the number of copies for the first direction is null or negative.";
153 setError(aFeatureError);
157 // Moving each object.
158 int aResultIndex = 0;
159 std::list<ResultPtr>::iterator aContext = aContextes.begin();
160 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
161 anObjectsIt++, aContext++) {
162 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
163 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
167 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
168 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
169 for (int i=0; i<nbCopies; i++) {
170 aTrsf->setTranslation(anAxis, i*aStep);
171 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
172 aResultPart->setTrsf(*aContext, aTrsf);
173 setResult(aResultPart, aResultIndex);
177 ListOfShape aListOfShape;
178 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > aListOfTranslationAlgo;
180 for (int i=0; i<nbCopies; i++) {
181 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
182 new GeomAlgoAPI_Translation(aBaseShape, anAxis, i*aStep));
184 if (!aTranslationAlgo->check()) {
185 setError(aTranslationAlgo->getError());
189 aTranslationAlgo->build();
191 // Checking that the algorithm worked properly.
192 if (!aTranslationAlgo->isDone()) {
193 static const std::string aFeatureError = "Error : Multitranslation algorithm failed.";
194 setError(aFeatureError);
197 if (aTranslationAlgo->shape()->isNull()) {
198 static const std::string aShapeError = "Error : Resulting shape is null.";
199 setError(aShapeError);
202 if (!aTranslationAlgo->isValid()) {
203 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
204 setError(aFeatureError);
207 aListOfShape.push_back(aTranslationAlgo->shape());
208 aListOfTranslationAlgo.push_back(aTranslationAlgo);
210 std::shared_ptr<GeomAPI_Shape> aCompound =
211 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
212 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
213 aResultBody->storeModified(aBaseShape, aCompound);
214 loadNamingDS(aListOfTranslationAlgo, aResultBody, aBaseShape);
216 setResult(aResultBody, aResultIndex);
221 // Remove the rest results if there were produced in the previous pass.
222 removeResults(aResultIndex);
225 //=================================================================================================
226 void FeaturesPlugin_MultiTranslation::performTwoDirection()
229 ListOfShape anObjects;
230 std::list<ResultPtr> aContextes;
231 AttributeSelectionListPtr anObjectsSelList =
232 selectionList(FeaturesPlugin_MultiTranslation::OBJECTS_LIST_ID());
233 if (anObjectsSelList->size() == 0) {
236 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
237 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
238 anObjectsSelList->value(anObjectsIndex);
239 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
240 if(!anObject.get()) { // may be for not-activated parts
244 anObjects.push_back(anObject);
245 aContextes.push_back(anObjectAttr->context());
249 static const std::string aSelectionError = "Error: The axis shape selection is bad.";
250 AttributeSelectionPtr anObjRef = selection(AXIS_FIRST_DIR_ID());
251 GeomShapePtr aShape = anObjRef->value();
253 if (anObjRef->context().get()) {
254 aShape = anObjRef->context()->shape();
258 setError(aSelectionError);
263 if (aShape->isEdge())
265 anEdge = aShape->edge();
267 else if (aShape->isCompound())
269 GeomAPI_ShapeIterator anIt(aShape);
270 anEdge = anIt.current()->edge();
274 setError(aSelectionError);
280 setError(aSelectionError);
284 std::shared_ptr<GeomAPI_Ax1> aFirstAxis(new GeomAPI_Ax1(anEdge->line()->location(),
285 anEdge->line()->direction()));
288 anObjRef = selection(AXIS_SECOND_DIR_ID());
289 aShape = anObjRef->value();
291 if (anObjRef->context().get()) {
292 aShape = anObjRef->context()->shape();
296 setError(aSelectionError);
300 if (aShape->isEdge())
302 anEdge = aShape->edge();
304 else if (aShape->isCompound())
306 GeomAPI_ShapeIterator anIt(aShape);
307 anEdge = anIt.current()->edge();
311 setError(aSelectionError);
317 setError(aSelectionError);
321 std::shared_ptr<GeomAPI_Ax1> aSecondAxis(new GeomAPI_Ax1(anEdge->line()->location(),
322 anEdge->line()->direction()));
325 double aFirstStep = real(FeaturesPlugin_MultiTranslation::STEP_FIRST_DIR_ID())->value();
326 double aSecondStep = real(FeaturesPlugin_MultiTranslation::STEP_SECOND_DIR_ID())->value();
328 // Getting number of copies.
330 integer(FeaturesPlugin_MultiTranslation::NB_COPIES_FIRST_DIR_ID())->value();
331 int aSecondNbCopies =
332 integer(FeaturesPlugin_MultiTranslation::NB_COPIES_SECOND_DIR_ID())->value();
334 if (aFirstNbCopies <=0) {
335 std::string aFeatureError = "Multitranslation builder ";
336 aFeatureError+=":: the number of copies for the first direction is null or negative.";
337 setError(aFeatureError);
341 if (aSecondNbCopies <=0) {
342 std::string aFeatureError = "Multitranslation builder ";
343 aFeatureError+=":: the number of copies for the second direction is null or negative.";
344 setError(aFeatureError);
349 double x1 = aFirstAxis->dir()->x();
350 double y1 = aFirstAxis->dir()->y();
351 double z1 = aFirstAxis->dir()->z();
352 double norm1 = sqrt(x1*x1 + y1*y1 + z1*z1);
355 double x2 = aSecondAxis->dir()->x();
356 double y2 = aSecondAxis->dir()->y();
357 double z2 = aSecondAxis->dir()->z();
358 double norm2 = sqrt(x2*x2 + y2*y2 + z2*z2);
360 // Moving each object.
361 int aResultIndex = 0;
362 std::list<ResultPtr>::iterator aContext = aContextes.begin();
363 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
364 anObjectsIt++, aContext++) {
365 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
366 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
370 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
371 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
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 aTrsf->setTranslation(dx, dy, dz);
378 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
379 aResultPart->setTrsf(*aContext, aTrsf);
380 setResult(aResultPart, aResultIndex);
385 ListOfShape aListOfShape;
386 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > aListOfTranslationAlgo;
388 for (int j=0; j<aSecondNbCopies; j++) {
389 for (int i=0; i<aFirstNbCopies; i++) {
390 double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
391 double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
392 double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
393 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
394 new GeomAlgoAPI_Translation(aBaseShape, dx, dy, dz));
396 if (!aTranslationAlgo->check()) {
397 setError(aTranslationAlgo->getError());
401 aTranslationAlgo->build();
403 // Checking that the algorithm worked properly.
404 if (!aTranslationAlgo->isDone()) {
405 static const std::string aFeatureError = "Error : Multitranslation algorithm failed.";
406 setError(aFeatureError);
409 if (aTranslationAlgo->shape()->isNull()) {
410 static const std::string aShapeError = "Error : Resulting shape is null.";
411 setError(aShapeError);
414 if (!aTranslationAlgo->isValid()) {
415 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
416 setError(aFeatureError);
419 aListOfShape.push_back(aTranslationAlgo->shape());
420 aListOfTranslationAlgo.push_back(aTranslationAlgo);
423 std::shared_ptr<GeomAPI_Shape> aCompound =
424 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
425 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
426 aResultBody->storeModified(aBaseShape, aCompound);
427 loadNamingDS(aListOfTranslationAlgo, aResultBody, aBaseShape);
428 setResult(aResultBody, aResultIndex);
433 // Remove the rest results if there were produced in the previous pass.
434 removeResults(aResultIndex);
437 //=================================================================================================
438 void FeaturesPlugin_MultiTranslation::loadNamingDS(
439 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > theListOfTranslationAlgo,
440 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
441 std::shared_ptr<GeomAPI_Shape> theBaseShape)
443 for (std::list<std::shared_ptr<GeomAlgoAPI_Translation> >::const_iterator anIt =
444 theListOfTranslationAlgo.begin(); anIt != theListOfTranslationAlgo.cend(); ++anIt) {
446 theResultBody->loadModifiedShapes(*anIt,
452 theResultBody->loadModifiedShapes(*anIt,
458 theResultBody->loadModifiedShapes(*anIt,
460 GeomAPI_Shape::VERTEX,
461 "Translated_Vertex");