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
99 anObjects.push_back(anObject);
100 aContextes.push_back(anObjectAttr->context());
104 static const std::string aSelectionError = "Error: The axis shape selection is bad.";
105 AttributeSelectionPtr anObjRef = selection(AXIS_FIRST_DIR_ID());
106 GeomShapePtr aShape = anObjRef->value();
108 if (anObjRef->context().get()) {
109 aShape = anObjRef->context()->shape();
113 setError(aSelectionError);
118 if (aShape->isEdge())
120 anEdge = aShape->edge();
122 else if (aShape->isCompound())
124 GeomAPI_ShapeIterator anIt(aShape);
125 anEdge = anIt.current()->edge();
129 setError(aSelectionError);
135 setError(aSelectionError);
139 std::shared_ptr<GeomAPI_Ax1> anAxis (new GeomAPI_Ax1(anEdge->line()->location(),
140 anEdge->line()->direction()));
143 double aStep = real(FeaturesPlugin_MultiTranslation::STEP_FIRST_DIR_ID())->value();
145 // Getting number of copies.
147 integer(FeaturesPlugin_MultiTranslation::NB_COPIES_FIRST_DIR_ID())->value();
150 std::string aFeatureError = "Multitranslation builder ";
151 aFeatureError+=":: the number of copies for the first direction is null or negative.";
152 setError(aFeatureError);
156 // Moving each object.
157 int aResultIndex = 0;
158 std::list<ResultPtr>::iterator aContext = aContextes.begin();
159 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
160 anObjectsIt++, aContext++) {
161 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
162 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
166 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
167 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
168 for (int i=0; i<nbCopies; i++) {
169 aTrsf->setTranslation(anAxis, i*aStep);
170 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
171 aResultPart->setTrsf(*aContext, aTrsf);
172 setResult(aResultPart, aResultIndex);
176 ListOfShape aListOfShape;
177 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > aListOfTranslationAlgo;
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 (!aTranslationAlgo->isDone()) {
192 static const std::string aFeatureError = "Error : Multitranslation algorithm failed.";
193 setError(aFeatureError);
196 if (aTranslationAlgo->shape()->isNull()) {
197 static const std::string aShapeError = "Error : Resulting shape is null.";
198 setError(aShapeError);
201 if (!aTranslationAlgo->isValid()) {
202 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
203 setError(aFeatureError);
206 aListOfShape.push_back(aTranslationAlgo->shape());
207 aListOfTranslationAlgo.push_back(aTranslationAlgo);
209 std::shared_ptr<GeomAPI_Shape> aCompound =
210 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
211 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
212 aResultBody->storeModified(aBaseShape, aCompound);
213 loadNamingDS(aListOfTranslationAlgo, aResultBody, aBaseShape);
215 setResult(aResultBody, aResultIndex);
220 // Remove the rest results if there were produced in the previous pass.
221 removeResults(aResultIndex);
224 //=================================================================================================
225 void FeaturesPlugin_MultiTranslation::performTwoDirection()
228 ListOfShape anObjects;
229 std::list<ResultPtr> aContextes;
230 AttributeSelectionListPtr anObjectsSelList =
231 selectionList(FeaturesPlugin_MultiTranslation::OBJECTS_LIST_ID());
232 if (anObjectsSelList->size() == 0) {
235 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
236 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
237 anObjectsSelList->value(anObjectsIndex);
238 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
239 if(!anObject.get()) { // may be for not-activated parts
242 anObjects.push_back(anObject);
243 aContextes.push_back(anObjectAttr->context());
247 static const std::string aSelectionError = "Error: The axis shape selection is bad.";
248 AttributeSelectionPtr anObjRef = selection(AXIS_FIRST_DIR_ID());
249 GeomShapePtr aShape = anObjRef->value();
251 if (anObjRef->context().get()) {
252 aShape = anObjRef->context()->shape();
256 setError(aSelectionError);
261 if (aShape->isEdge())
263 anEdge = aShape->edge();
265 else if (aShape->isCompound())
267 GeomAPI_ShapeIterator anIt(aShape);
268 anEdge = anIt.current()->edge();
272 setError(aSelectionError);
278 setError(aSelectionError);
282 std::shared_ptr<GeomAPI_Ax1> aFirstAxis(new GeomAPI_Ax1(anEdge->line()->location(),
283 anEdge->line()->direction()));
286 anObjRef = selection(AXIS_SECOND_DIR_ID());
287 aShape = anObjRef->value();
289 if (anObjRef->context().get()) {
290 aShape = anObjRef->context()->shape();
294 setError(aSelectionError);
298 if (aShape->isEdge())
300 anEdge = aShape->edge();
302 else if (aShape->isCompound())
304 GeomAPI_ShapeIterator anIt(aShape);
305 anEdge = anIt.current()->edge();
309 setError(aSelectionError);
315 setError(aSelectionError);
319 std::shared_ptr<GeomAPI_Ax1> aSecondAxis(new GeomAPI_Ax1(anEdge->line()->location(),
320 anEdge->line()->direction()));
323 double aFirstStep = real(FeaturesPlugin_MultiTranslation::STEP_FIRST_DIR_ID())->value();
324 double aSecondStep = real(FeaturesPlugin_MultiTranslation::STEP_SECOND_DIR_ID())->value();
326 // Getting number of copies.
328 integer(FeaturesPlugin_MultiTranslation::NB_COPIES_FIRST_DIR_ID())->value();
329 int aSecondNbCopies =
330 integer(FeaturesPlugin_MultiTranslation::NB_COPIES_SECOND_DIR_ID())->value();
332 if (aFirstNbCopies <=0) {
333 std::string aFeatureError = "Multitranslation builder ";
334 aFeatureError+=":: the number of copies for the first direction is null or negative.";
335 setError(aFeatureError);
339 if (aSecondNbCopies <=0) {
340 std::string aFeatureError = "Multitranslation builder ";
341 aFeatureError+=":: the number of copies for the second direction is null or negative.";
342 setError(aFeatureError);
347 double x1 = aFirstAxis->dir()->x();
348 double y1 = aFirstAxis->dir()->y();
349 double z1 = aFirstAxis->dir()->z();
350 double norm1 = sqrt(x1*x1 + y1*y1 + z1*z1);
353 double x2 = aSecondAxis->dir()->x();
354 double y2 = aSecondAxis->dir()->y();
355 double z2 = aSecondAxis->dir()->z();
356 double norm2 = sqrt(x2*x2 + y2*y2 + z2*z2);
358 // Moving each object.
359 int aResultIndex = 0;
360 std::list<ResultPtr>::iterator aContext = aContextes.begin();
361 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
362 anObjectsIt++, aContext++) {
363 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
364 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
368 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
369 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
370 for (int j=0; j<aSecondNbCopies; j++) {
371 for (int i=0; i<aFirstNbCopies; i++) {
372 double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
373 double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
374 double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
375 aTrsf->setTranslation(dx, dy, dz);
376 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
377 aResultPart->setTrsf(*aContext, aTrsf);
378 setResult(aResultPart, aResultIndex);
383 ListOfShape aListOfShape;
384 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > aListOfTranslationAlgo;
386 for (int j=0; j<aSecondNbCopies; j++) {
387 for (int i=0; i<aFirstNbCopies; i++) {
388 double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
389 double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
390 double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
391 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
392 new GeomAlgoAPI_Translation(aBaseShape, dx, dy, dz));
394 if (!aTranslationAlgo->check()) {
395 setError(aTranslationAlgo->getError());
399 aTranslationAlgo->build();
401 // Checking that the algorithm worked properly.
402 if (!aTranslationAlgo->isDone()) {
403 static const std::string aFeatureError = "Error : Multitranslation algorithm failed.";
404 setError(aFeatureError);
407 if (aTranslationAlgo->shape()->isNull()) {
408 static const std::string aShapeError = "Error : Resulting shape is null.";
409 setError(aShapeError);
412 if (!aTranslationAlgo->isValid()) {
413 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
414 setError(aFeatureError);
417 aListOfShape.push_back(aTranslationAlgo->shape());
418 aListOfTranslationAlgo.push_back(aTranslationAlgo);
421 std::shared_ptr<GeomAPI_Shape> aCompound =
422 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
423 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
424 aResultBody->storeModified(aBaseShape, aCompound);
425 loadNamingDS(aListOfTranslationAlgo, aResultBody, aBaseShape);
426 setResult(aResultBody, aResultIndex);
431 // Remove the rest results if there were produced in the previous pass.
432 removeResults(aResultIndex);
435 //=================================================================================================
436 void FeaturesPlugin_MultiTranslation::loadNamingDS(
437 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > theListOfTranslationAlgo,
438 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
439 std::shared_ptr<GeomAPI_Shape> theBaseShape)
441 for (std::list<std::shared_ptr<GeomAlgoAPI_Translation> >::const_iterator anIt =
442 theListOfTranslationAlgo.begin(); anIt != theListOfTranslationAlgo.cend(); ++anIt) {
444 theResultBody->loadModifiedShapes(*anIt,
450 theResultBody->loadModifiedShapes(*anIt,
456 theResultBody->loadModifiedShapes(*anIt,
458 GeomAPI_Shape::VERTEX,
459 "Translated_Vertex");