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>
24 #include <GeomAlgoAPI_Tools.h>
26 #include <GeomAPI_Ax1.h>
27 #include <GeomAPI_Edge.h>
28 #include <GeomAPI_Lin.h>
29 #include <GeomAPI_ShapeIterator.h>
30 #include <GeomAPI_Trsf.h>
32 #include <ModelAPI_AttributeDouble.h>
33 #include <ModelAPI_AttributeInteger.h>
34 #include <ModelAPI_AttributeSelectionList.h>
35 #include <ModelAPI_AttributeString.h>
36 #include <ModelAPI_ResultBody.h>
37 #include <ModelAPI_ResultPart.h>
41 //=================================================================================================
42 FeaturesPlugin_MultiTranslation::FeaturesPlugin_MultiTranslation()
46 //=================================================================================================
47 void FeaturesPlugin_MultiTranslation::initAttributes()
49 AttributeSelectionListPtr aSelection =
50 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
51 FeaturesPlugin_MultiTranslation::OBJECTS_LIST_ID(),
52 ModelAPI_AttributeSelectionList::typeId()));
54 data()->addAttribute(FeaturesPlugin_MultiTranslation::AXIS_FIRST_DIR_ID(),
55 ModelAPI_AttributeSelection::typeId());
56 data()->addAttribute(FeaturesPlugin_MultiTranslation::STEP_FIRST_DIR_ID(),
57 ModelAPI_AttributeDouble::typeId());
58 data()->addAttribute(FeaturesPlugin_MultiTranslation::NB_COPIES_FIRST_DIR_ID(),
59 ModelAPI_AttributeInteger::typeId());
61 data()->addAttribute(FeaturesPlugin_MultiTranslation::USE_SECOND_DIR_ID(),
62 ModelAPI_AttributeString::typeId());
63 data()->addAttribute(FeaturesPlugin_MultiTranslation::AXIS_SECOND_DIR_ID(),
64 ModelAPI_AttributeSelection::typeId());
65 data()->addAttribute(FeaturesPlugin_MultiTranslation::STEP_SECOND_DIR_ID(),
66 ModelAPI_AttributeDouble::typeId());
67 data()->addAttribute(FeaturesPlugin_MultiTranslation::NB_COPIES_SECOND_DIR_ID(),
68 ModelAPI_AttributeInteger::typeId());
71 //=================================================================================================
72 void FeaturesPlugin_MultiTranslation::execute()
74 std::string useSecondDir = string(FeaturesPlugin_MultiTranslation::USE_SECOND_DIR_ID())->value();
75 if(!useSecondDir.empty()) {
76 performTwoDirection();
78 performOneDirection();
82 //=================================================================================================
83 void FeaturesPlugin_MultiTranslation::performOneDirection()
86 ListOfShape anObjects;
87 std::list<ResultPtr> aContextes;
88 AttributeSelectionListPtr anObjectsSelList =
89 selectionList(FeaturesPlugin_MultiTranslation::OBJECTS_LIST_ID());
90 if (anObjectsSelList->size() == 0) {
93 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
94 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
95 anObjectsSelList->value(anObjectsIndex);
96 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
97 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);
178 ListOfShape aListOfShape;
179 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > aListOfTranslationAlgo;
181 for (int i=0; i<nbCopies; i++) {
182 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
183 new GeomAlgoAPI_Translation(aBaseShape, anAxis, i*aStep));
185 if (!aTranslationAlgo->check()) {
186 setError(aTranslationAlgo->getError());
190 aTranslationAlgo->build();
192 // Checking that the algorithm worked properly.
193 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(
194 aTranslationAlgo, getKind(), anError)) {
198 aListOfShape.push_back(aTranslationAlgo->shape());
199 aListOfTranslationAlgo.push_back(aTranslationAlgo);
201 std::shared_ptr<GeomAPI_Shape> aCompound =
202 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
203 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
204 aResultBody->storeModified(aBaseShape, aCompound);
205 loadNamingDS(aListOfTranslationAlgo, aResultBody, aBaseShape);
207 setResult(aResultBody, aResultIndex);
212 // Remove the rest results if there were produced in the previous pass.
213 removeResults(aResultIndex);
216 //=================================================================================================
217 void FeaturesPlugin_MultiTranslation::performTwoDirection()
220 ListOfShape anObjects;
221 std::list<ResultPtr> aContextes;
222 AttributeSelectionListPtr anObjectsSelList =
223 selectionList(FeaturesPlugin_MultiTranslation::OBJECTS_LIST_ID());
224 if (anObjectsSelList->size() == 0) {
227 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
228 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
229 anObjectsSelList->value(anObjectsIndex);
230 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
231 if(!anObject.get()) { // may be for not-activated parts
234 anObjects.push_back(anObject);
235 aContextes.push_back(anObjectAttr->context());
239 static const std::string aSelectionError = "Error: The axis shape selection is bad.";
240 AttributeSelectionPtr anObjRef = selection(AXIS_FIRST_DIR_ID());
241 GeomShapePtr aShape = anObjRef->value();
243 if (anObjRef->context().get()) {
244 aShape = anObjRef->context()->shape();
248 setError(aSelectionError);
253 if (aShape->isEdge())
255 anEdge = aShape->edge();
257 else if (aShape->isCompound())
259 GeomAPI_ShapeIterator anIt(aShape);
260 anEdge = anIt.current()->edge();
264 setError(aSelectionError);
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();
301 setError(aSelectionError);
307 setError(aSelectionError);
311 std::shared_ptr<GeomAPI_Ax1> aSecondAxis(new GeomAPI_Ax1(anEdge->line()->location(),
312 anEdge->line()->direction()));
315 double aFirstStep = real(FeaturesPlugin_MultiTranslation::STEP_FIRST_DIR_ID())->value();
316 double aSecondStep = real(FeaturesPlugin_MultiTranslation::STEP_SECOND_DIR_ID())->value();
318 // Getting number of copies.
320 integer(FeaturesPlugin_MultiTranslation::NB_COPIES_FIRST_DIR_ID())->value();
321 int aSecondNbCopies =
322 integer(FeaturesPlugin_MultiTranslation::NB_COPIES_SECOND_DIR_ID())->value();
324 if (aFirstNbCopies <=0) {
325 std::string aFeatureError = "Multitranslation builder ";
326 aFeatureError+=":: the number of copies for the first direction is null or negative.";
327 setError(aFeatureError);
331 if (aSecondNbCopies <=0) {
332 std::string aFeatureError = "Multitranslation builder ";
333 aFeatureError+=":: the number of copies for the second direction is null or negative.";
334 setError(aFeatureError);
339 double x1 = aFirstAxis->dir()->x();
340 double y1 = aFirstAxis->dir()->y();
341 double z1 = aFirstAxis->dir()->z();
342 double norm1 = sqrt(x1*x1 + y1*y1 + z1*z1);
345 double x2 = aSecondAxis->dir()->x();
346 double y2 = aSecondAxis->dir()->y();
347 double z2 = aSecondAxis->dir()->z();
348 double norm2 = sqrt(x2*x2 + y2*y2 + z2*z2);
350 // Moving each object.
351 int aResultIndex = 0;
352 std::list<ResultPtr>::iterator aContext = aContextes.begin();
353 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
354 anObjectsIt++, aContext++) {
355 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
356 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
360 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
361 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
362 for (int j=0; j<aSecondNbCopies; j++) {
363 for (int i=0; i<aFirstNbCopies; i++) {
364 double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
365 double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
366 double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
367 aTrsf->setTranslation(dx, dy, dz);
368 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
369 aResultPart->setTrsf(*aContext, aTrsf);
370 setResult(aResultPart, aResultIndex);
376 ListOfShape aListOfShape;
377 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > aListOfTranslationAlgo;
379 for (int j=0; j<aSecondNbCopies; j++) {
380 for (int i=0; i<aFirstNbCopies; i++) {
381 double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
382 double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
383 double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
384 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
385 new GeomAlgoAPI_Translation(aBaseShape, dx, dy, dz));
387 if (!aTranslationAlgo->check()) {
388 setError(aTranslationAlgo->getError());
392 aTranslationAlgo->build();
394 // Checking that the algorithm worked properly.
395 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(
396 aTranslationAlgo, getKind(), anError)) {
400 aListOfShape.push_back(aTranslationAlgo->shape());
401 aListOfTranslationAlgo.push_back(aTranslationAlgo);
404 std::shared_ptr<GeomAPI_Shape> aCompound =
405 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
406 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
407 aResultBody->storeModified(aBaseShape, aCompound);
408 loadNamingDS(aListOfTranslationAlgo, aResultBody, aBaseShape);
409 setResult(aResultBody, aResultIndex);
414 // Remove the rest results if there were produced in the previous pass.
415 removeResults(aResultIndex);
418 //=================================================================================================
419 void FeaturesPlugin_MultiTranslation::loadNamingDS(
420 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > theListOfTranslationAlgo,
421 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
422 std::shared_ptr<GeomAPI_Shape> theBaseShape)
424 for (std::list<std::shared_ptr<GeomAlgoAPI_Translation> >::const_iterator anIt =
425 theListOfTranslationAlgo.begin(); anIt != theListOfTranslationAlgo.cend(); ++anIt) {
427 theResultBody->loadModifiedShapes(*anIt,
433 theResultBody->loadModifiedShapes(*anIt,
439 theResultBody->loadModifiedShapes(*anIt,
441 GeomAPI_Shape::VERTEX,
442 "Translated_Vertex");