]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_MultiTranslation.cpp
Salome HOME
import image: add texture to transformations
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_MultiTranslation.cpp
1 // Copyright (C) 2014-2020  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <FeaturesPlugin_MultiTranslation.h>
21 #include <FeaturesPlugin_Tools.h>
22
23 #include <GeomAlgoAPI_CompoundBuilder.h>
24 #include <GeomAlgoAPI_MakeShapeList.h>
25 #include <GeomAlgoAPI_Tools.h>
26 #include <GeomAlgoAPI_Translation.h>
27
28 #include <GeomAPI_Ax1.h>
29 #include <GeomAPI_Edge.h>
30 #include <GeomAPI_Lin.h>
31 #include <GeomAPI_ShapeIterator.h>
32 #include <GeomAPI_Trsf.h>
33
34 #include <ModelAPI_AttributeDouble.h>
35 #include <ModelAPI_AttributeInteger.h>
36 #include <ModelAPI_AttributeSelectionList.h>
37 #include <ModelAPI_AttributeString.h>
38 #include <ModelAPI_ResultBody.h>
39 #include <ModelAPI_ResultPart.h>
40
41 #include <math.h>
42
43 static const std::string MULTITRANSLATION_VERSION_1("v9.5");
44
45 //=================================================================================================
46 FeaturesPlugin_MultiTranslation::FeaturesPlugin_MultiTranslation()
47 {
48 }
49
50 //=================================================================================================
51 void FeaturesPlugin_MultiTranslation::initAttributes()
52 {
53   AttributeSelectionListPtr aSelection =
54     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
55     FeaturesPlugin_MultiTranslation::OBJECTS_LIST_ID(),
56     ModelAPI_AttributeSelectionList::typeId()));
57
58   data()->addAttribute(FeaturesPlugin_MultiTranslation::AXIS_FIRST_DIR_ID(),
59                        ModelAPI_AttributeSelection::typeId());
60   data()->addAttribute(FeaturesPlugin_MultiTranslation::STEP_FIRST_DIR_ID(),
61                        ModelAPI_AttributeDouble::typeId());
62   data()->addAttribute(FeaturesPlugin_MultiTranslation::NB_COPIES_FIRST_DIR_ID(),
63                        ModelAPI_AttributeInteger::typeId());
64
65   data()->addAttribute(FeaturesPlugin_MultiTranslation::USE_SECOND_DIR_ID(),
66                        ModelAPI_AttributeString::typeId());
67   data()->addAttribute(FeaturesPlugin_MultiTranslation::AXIS_SECOND_DIR_ID(),
68                        ModelAPI_AttributeSelection::typeId());
69   data()->addAttribute(FeaturesPlugin_MultiTranslation::STEP_SECOND_DIR_ID(),
70                        ModelAPI_AttributeDouble::typeId());
71   data()->addAttribute(FeaturesPlugin_MultiTranslation::NB_COPIES_SECOND_DIR_ID(),
72                        ModelAPI_AttributeInteger::typeId());
73
74   if (!aSelection->isInitialized()) {
75     // new feature, not read from file
76     data()->setVersion(MULTITRANSLATION_VERSION_1);
77   }
78 }
79
80 //=================================================================================================
81 void FeaturesPlugin_MultiTranslation::execute()
82 {
83   bool isKeepSubShapes = data()->version() == MULTITRANSLATION_VERSION_1;
84
85   // Getting objects.
86   AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECTS_LIST_ID());
87   if (anObjectsSelList->size() == 0) {
88     setError("Error: empty selection list");
89     return;
90   }
91
92   GeomAPI_ShapeHierarchy anObjects;
93   std::list<ResultPtr> aParts;
94   std::string theTextureFile;
95   if (!FeaturesPlugin_Tools::shapesFromSelectionList(
96        anObjectsSelList, isKeepSubShapes, anObjects, aParts, theTextureFile))
97     return;
98
99   std::shared_ptr<GeomAPI_Dir> aFirstDir, aSecondDir;
100   double aFirstStep, aSecondStep;
101   int aFirstNbCopies, aSecondNbCopies;
102   if (!paramsAlongDirection(0, aFirstDir, aFirstStep, aFirstNbCopies))
103     return;
104
105   bool useSecondDir = !string(USE_SECOND_DIR_ID())->value().empty();
106   if (useSecondDir) {
107     if (!paramsAlongDirection(1, aSecondDir, aSecondStep, aSecondNbCopies))
108       return;
109   }
110   else {
111     aSecondDir = aFirstDir; // direction does not matter
112     aSecondStep = 0.0;
113     aSecondNbCopies = 1;
114   }
115
116   std::string anError;
117   int aResultIndex = 0;
118   // Moving each part.
119   for (std::list<ResultPtr>::iterator aPRes = aParts.begin(); aPRes != aParts.end(); ++aPRes) {
120     ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aPRes);
121     std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
122     for (int j = 0; j < aSecondNbCopies; j++) {
123       for (int i = 0; i < aFirstNbCopies; i++) {
124         double dx = i * aFirstStep * aFirstDir->x() + j * aSecondStep * aSecondDir->x();
125         double dy = i * aFirstStep * aFirstDir->y() + j * aSecondStep * aSecondDir->y();
126         double dz = i * aFirstStep * aFirstDir->z() + j * aSecondStep * aSecondDir->z();
127         aTrsf->setTranslation(dx, dy, dz);
128
129         ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
130         aResultPart->setTrsf(anOrigin, aTrsf);
131         setResult(aResultPart, aResultIndex);
132         aResultIndex++;
133       }
134     }
135   }
136
137   // Collect transformations for each object in a part.
138   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList);
139   for (GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
140        anObjectsIt != anObjects.end(); anObjectsIt++) {
141     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
142     ListOfShape aListOfShape;
143
144     for (int j = 0; j < aSecondNbCopies; j++) {
145       for (int i = 0; i < aFirstNbCopies; i++) {
146         double dx = i * aFirstStep * aFirstDir->x() + j * aSecondStep * aSecondDir->x();
147         double dy = i * aFirstStep * aFirstDir->y() + j * aSecondStep * aSecondDir->y();
148         double dz = i * aFirstStep * aFirstDir->z() + j * aSecondStep * aSecondDir->z();
149         std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
150             new GeomAlgoAPI_Translation(aBaseShape, dx, dy, dz));
151
152         // Checking that the algorithm worked properly.
153         if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(
154             aTranslationAlgo, getKind(), anError)) {
155           setError(anError);
156           break;
157         }
158         aListOfShape.push_back(aTranslationAlgo->shape());
159         aMakeShapeList->appendAlgo(aTranslationAlgo);
160       }
161     }
162     GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
163     anObjects.markModified(aBaseShape, aCompound);
164   }
165
166   // Build results of the operation.
167   const ListOfShape& anOriginalShapes = anObjects.objects();
168   ListOfShape aTopLevel;
169   anObjects.topLevelObjects(aTopLevel);
170   for (ListOfShape::iterator anIt = aTopLevel.begin(); anIt != aTopLevel.end(); ++anIt) {
171     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
172     FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, anOriginalShapes, ListOfShape(),
173                                              aMakeShapeList, *anIt, "Translated");
174     aResultBody->setTextureFile(theTextureFile);
175     setResult(aResultBody, aResultIndex++);
176   }
177
178   // Remove the rest results if there were produced in the previous pass.
179   removeResults(aResultIndex);
180 }
181
182 //=================================================================================================
183 bool FeaturesPlugin_MultiTranslation::paramsAlongDirection(const int theIndex,
184                                                            std::shared_ptr<GeomAPI_Dir>& theDir,
185                                                            double& theDistance,
186                                                            int& theQuantity)
187 {
188   static std::string THE_AXIS_DIR[2] = { AXIS_FIRST_DIR_ID(), AXIS_SECOND_DIR_ID() };
189   static std::string THE_STEP[2] = { STEP_FIRST_DIR_ID(), STEP_SECOND_DIR_ID() };
190   static std::string THE_COPIES[2] = { NB_COPIES_FIRST_DIR_ID(), NB_COPIES_SECOND_DIR_ID() };
191   static std::string THE_INDEX_ID[2] = { "first", "second" };
192
193   //Getting axis.
194   static const std::string aSelectionError = "Error: The axis shape selection is bad.";
195   AttributeSelectionPtr anObjRef = selection(THE_AXIS_DIR[theIndex]);
196   GeomShapePtr aShape = anObjRef->value();
197   if (!aShape.get() && anObjRef->context().get())
198     aShape = anObjRef->context()->shape();
199   if (!aShape.get()) {
200     setError(aSelectionError);
201     return false;
202   }
203
204   GeomEdgePtr anEdge;
205   if (aShape->isEdge())
206     anEdge = aShape->edge();
207   else if (aShape->isCompound()) {
208     GeomAPI_ShapeIterator anIt(aShape);
209     anEdge = anIt.current()->edge();
210   }
211
212   if (!anEdge.get()) {
213     setError(aSelectionError);
214     return false;
215   }
216
217   theDir = anEdge->line()->direction();
218   theDistance = real(THE_STEP[theIndex])->value();
219   theQuantity = integer(THE_COPIES[theIndex])->value();
220   if (theQuantity <= 0) {
221     std::string aFeatureError = "Multitranslation builder :: the number of copies for the ";
222     aFeatureError += THE_INDEX_ID[theIndex] + " direction is null or negative.";
223     setError(aFeatureError);
224     return false;
225   }
226   return true;
227 }