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