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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
20 #include <FeaturesPlugin_MultiTranslation.h>
22 #include <GeomAlgoAPI_CompoundBuilder.h>
24 #include <GeomAPI_Ax1.h>
25 #include <GeomAPI_Edge.h>
26 #include <GeomAPI_Lin.h>
27 #include <GeomAPI_Trsf.h>
29 #include <ModelAPI_AttributeDouble.h>
30 #include <ModelAPI_AttributeInteger.h>
31 #include <ModelAPI_AttributeSelectionList.h>
32 #include <ModelAPI_AttributeString.h>
33 #include <ModelAPI_ResultBody.h>
34 #include <ModelAPI_ResultPart.h>
38 //=================================================================================================
39 FeaturesPlugin_MultiTranslation::FeaturesPlugin_MultiTranslation()
43 //=================================================================================================
44 void FeaturesPlugin_MultiTranslation::initAttributes()
46 AttributeSelectionListPtr aSelection =
47 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
48 FeaturesPlugin_MultiTranslation::OBJECTS_LIST_ID(),
49 ModelAPI_AttributeSelectionList::typeId()));
51 data()->addAttribute(FeaturesPlugin_MultiTranslation::AXIS_FIRST_DIR_ID(),
52 ModelAPI_AttributeSelection::typeId());
53 data()->addAttribute(FeaturesPlugin_MultiTranslation::STEP_FIRST_DIR_ID(),
54 ModelAPI_AttributeDouble::typeId());
55 data()->addAttribute(FeaturesPlugin_MultiTranslation::NB_COPIES_FIRST_DIR_ID(),
56 ModelAPI_AttributeInteger::typeId());
58 data()->addAttribute(FeaturesPlugin_MultiTranslation::USE_SECOND_DIR_ID(),
59 ModelAPI_AttributeString::typeId());
60 data()->addAttribute(FeaturesPlugin_MultiTranslation::AXIS_SECOND_DIR_ID(),
61 ModelAPI_AttributeSelection::typeId());
62 data()->addAttribute(FeaturesPlugin_MultiTranslation::STEP_SECOND_DIR_ID(),
63 ModelAPI_AttributeDouble::typeId());
64 data()->addAttribute(FeaturesPlugin_MultiTranslation::NB_COPIES_SECOND_DIR_ID(),
65 ModelAPI_AttributeInteger::typeId());
68 //=================================================================================================
69 void FeaturesPlugin_MultiTranslation::execute()
71 std::string useSecondDir = string(FeaturesPlugin_MultiTranslation::USE_SECOND_DIR_ID())->value();
72 if(!useSecondDir.empty()) {
73 performTwoDirection();
75 performOneDirection();
79 //=================================================================================================
80 void FeaturesPlugin_MultiTranslation::performOneDirection()
83 ListOfShape anObjects;
84 std::list<ResultPtr> aContextes;
85 AttributeSelectionListPtr anObjectsSelList =
86 selectionList(FeaturesPlugin_MultiTranslation::OBJECTS_LIST_ID());
87 if (anObjectsSelList->size() == 0) {
90 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
91 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
92 anObjectsSelList->value(anObjectsIndex);
93 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
94 if(!anObject.get()) { // may be for not-activated parts
98 anObjects.push_back(anObject);
99 aContextes.push_back(anObjectAttr->context());
103 std::shared_ptr<GeomAPI_Ax1> anAxis;
104 std::shared_ptr<GeomAPI_Edge> anEdge;
105 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
106 selection(FeaturesPlugin_MultiTranslation::AXIS_FIRST_DIR_ID());
107 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
108 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
109 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
110 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
111 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
114 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
115 anEdge->line()->direction()));
119 double aStep = real(FeaturesPlugin_MultiTranslation::STEP_FIRST_DIR_ID())->value();
121 // Getting number of copies.
123 integer(FeaturesPlugin_MultiTranslation::NB_COPIES_FIRST_DIR_ID())->value();
126 std::string aFeatureError = "Multitranslation builder ";
127 aFeatureError+=":: the number of copies for the first direction is null or negative.";
128 setError(aFeatureError);
132 // Moving each object.
133 int aResultIndex = 0;
134 std::list<ResultPtr>::iterator aContext = aContextes.begin();
135 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
136 anObjectsIt++, aContext++) {
137 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
138 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
142 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
143 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
144 for (int i=0; i<nbCopies; i++) {
145 aTrsf->setTranslation(anAxis, i*aStep);
146 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
147 aResultPart->setTrsf(*aContext, aTrsf);
148 setResult(aResultPart, aResultIndex);
152 ListOfShape aListOfShape;
153 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > aListOfTranslationAlgo;
155 for (int i=0; i<nbCopies; i++) {
156 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
157 new GeomAlgoAPI_Translation(aBaseShape, anAxis, i*aStep));
159 if (!aTranslationAlgo->check()) {
160 setError(aTranslationAlgo->getError());
164 aTranslationAlgo->build();
166 // Checking that the algorithm worked properly.
167 if (!aTranslationAlgo->isDone()) {
168 static const std::string aFeatureError = "Error : Multitranslation algorithm failed.";
169 setError(aFeatureError);
172 if (aTranslationAlgo->shape()->isNull()) {
173 static const std::string aShapeError = "Error : Resulting shape is null.";
174 setError(aShapeError);
177 if (!aTranslationAlgo->isValid()) {
178 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
179 setError(aFeatureError);
182 aListOfShape.push_back(aTranslationAlgo->shape());
183 aListOfTranslationAlgo.push_back(aTranslationAlgo);
185 std::shared_ptr<GeomAPI_Shape> aCompound =
186 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
187 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
188 aResultBody->storeModified(aBaseShape, aCompound);
189 loadNamingDS(aListOfTranslationAlgo, aResultBody, aBaseShape);
191 setResult(aResultBody, aResultIndex);
196 // Remove the rest results if there were produced in the previous pass.
197 removeResults(aResultIndex);
200 //=================================================================================================
201 void FeaturesPlugin_MultiTranslation::performTwoDirection()
204 ListOfShape anObjects;
205 std::list<ResultPtr> aContextes;
206 AttributeSelectionListPtr anObjectsSelList =
207 selectionList(FeaturesPlugin_MultiTranslation::OBJECTS_LIST_ID());
208 if (anObjectsSelList->size() == 0) {
211 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
212 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
213 anObjectsSelList->value(anObjectsIndex);
214 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
215 if(!anObject.get()) { // may be for not-activated parts
219 anObjects.push_back(anObject);
220 aContextes.push_back(anObjectAttr->context());
224 std::shared_ptr<GeomAPI_Ax1> aFirstAxis;
225 std::shared_ptr<GeomAPI_Edge> anEdge;
226 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
227 selection(FeaturesPlugin_MultiTranslation::AXIS_FIRST_DIR_ID());
228 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
229 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
230 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
231 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
232 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
235 aFirstAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
236 anEdge->line()->direction()));
238 std::shared_ptr<GeomAPI_Ax1> aSecondAxis;
239 anObjRef = selection(FeaturesPlugin_MultiTranslation::AXIS_SECOND_DIR_ID());
240 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
241 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
242 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
243 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
244 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
247 aSecondAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
248 anEdge->line()->direction()));
252 double aFirstStep = real(FeaturesPlugin_MultiTranslation::STEP_FIRST_DIR_ID())->value();
253 double aSecondStep = real(FeaturesPlugin_MultiTranslation::STEP_SECOND_DIR_ID())->value();
255 // Getting number of copies.
257 integer(FeaturesPlugin_MultiTranslation::NB_COPIES_FIRST_DIR_ID())->value();
258 int aSecondNbCopies =
259 integer(FeaturesPlugin_MultiTranslation::NB_COPIES_SECOND_DIR_ID())->value();
261 if (aFirstNbCopies <=0) {
262 std::string aFeatureError = "Multitranslation builder ";
263 aFeatureError+=":: the number of copies for the first direction is null or negative.";
264 setError(aFeatureError);
268 if (aSecondNbCopies <=0) {
269 std::string aFeatureError = "Multitranslation builder ";
270 aFeatureError+=":: the number of copies for the second direction is null or negative.";
271 setError(aFeatureError);
276 double x1 = aFirstAxis->dir()->x();
277 double y1 = aFirstAxis->dir()->y();
278 double z1 = aFirstAxis->dir()->z();
279 double norm1 = sqrt(x1*x1 + y1*y1 + z1*z1);
282 double x2 = aSecondAxis->dir()->x();
283 double y2 = aSecondAxis->dir()->y();
284 double z2 = aSecondAxis->dir()->z();
285 double norm2 = sqrt(x2*x2 + y2*y2 + z2*z2);
287 // Moving each object.
288 int aResultIndex = 0;
289 std::list<ResultPtr>::iterator aContext = aContextes.begin();
290 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
291 anObjectsIt++, aContext++) {
292 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
293 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
297 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
298 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
299 for (int j=0; j<aSecondNbCopies; j++) {
300 for (int i=0; i<aFirstNbCopies; i++) {
301 double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
302 double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
303 double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
304 aTrsf->setTranslation(dx, dy, dz);
305 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
306 aResultPart->setTrsf(*aContext, aTrsf);
307 setResult(aResultPart, aResultIndex);
312 ListOfShape aListOfShape;
313 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > aListOfTranslationAlgo;
315 for (int j=0; j<aSecondNbCopies; j++) {
316 for (int i=0; i<aFirstNbCopies; i++) {
317 double dx = i*aFirstStep*x1/norm1+j*aSecondStep*x2/norm2;
318 double dy = i*aFirstStep*y1/norm1+j*aSecondStep*y2/norm2;
319 double dz = i*aFirstStep*z1/norm1+j*aSecondStep*z2/norm2;
320 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
321 new GeomAlgoAPI_Translation(aBaseShape, dx, dy, dz));
323 if (!aTranslationAlgo->check()) {
324 setError(aTranslationAlgo->getError());
328 aTranslationAlgo->build();
330 // Checking that the algorithm worked properly.
331 if (!aTranslationAlgo->isDone()) {
332 static const std::string aFeatureError = "Error : Multitranslation algorithm failed.";
333 setError(aFeatureError);
336 if (aTranslationAlgo->shape()->isNull()) {
337 static const std::string aShapeError = "Error : Resulting shape is null.";
338 setError(aShapeError);
341 if (!aTranslationAlgo->isValid()) {
342 static const std::string aFeatureError = "Error : Resulting shape in not valid.";
343 setError(aFeatureError);
346 aListOfShape.push_back(aTranslationAlgo->shape());
347 aListOfTranslationAlgo.push_back(aTranslationAlgo);
350 std::shared_ptr<GeomAPI_Shape> aCompound =
351 GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
352 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
353 aResultBody->storeModified(aBaseShape, aCompound);
354 loadNamingDS(aListOfTranslationAlgo, aResultBody, aBaseShape);
355 setResult(aResultBody, aResultIndex);
360 // Remove the rest results if there were produced in the previous pass.
361 removeResults(aResultIndex);
364 //=================================================================================================
365 void FeaturesPlugin_MultiTranslation::loadNamingDS(
366 std::list<std::shared_ptr<GeomAlgoAPI_Translation> > theListOfTranslationAlgo,
367 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
368 std::shared_ptr<GeomAPI_Shape> theBaseShape)
372 std::string aTranslatedName;
374 for (std::list<std::shared_ptr<GeomAlgoAPI_Translation> >::const_iterator anIt =
375 theListOfTranslationAlgo.begin(); anIt != theListOfTranslationAlgo.cend(); ++anIt) {
376 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = (*anIt)->mapOfSubShapes();
379 aTranslatedName = "Translated_Face_" + std::to_string((long long) anIndex);
380 theResultBody->loadAndOrientModifiedShapes((*anIt).get(), theBaseShape, GeomAPI_Shape::FACE,
381 aTag++, aTranslatedName, *aSubShapes.get(),
385 aTranslatedName = "Translated_Edge_" + std::to_string((long long) anIndex);
386 theResultBody->loadAndOrientModifiedShapes((*anIt).get(), theBaseShape, GeomAPI_Shape::EDGE,
387 aTag++, aTranslatedName, *aSubShapes.get(),
391 aTranslatedName = "Translated_Vertex_" + std::to_string((long long) anIndex);
392 theResultBody->loadAndOrientModifiedShapes((*anIt).get(), theBaseShape, GeomAPI_Shape::VERTEX,
393 aTag++, aTranslatedName, *aSubShapes.get(),