Salome HOME
Copyright update 2020
[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   if (!FeaturesPlugin_Tools::shapesFromSelectionList(
95        anObjectsSelList, isKeepSubShapes, anObjects, aParts))
96     return;
97
98   std::shared_ptr<GeomAPI_Dir> aFirstDir, aSecondDir;
99   double aFirstStep, aSecondStep;
100   int aFirstNbCopies, aSecondNbCopies;
101   if (!paramsAlongDirection(0, aFirstDir, aFirstStep, aFirstNbCopies))
102     return;
103
104   bool useSecondDir = !string(USE_SECOND_DIR_ID())->value().empty();
105   if (useSecondDir) {
106     if (!paramsAlongDirection(1, aSecondDir, aSecondStep, aSecondNbCopies))
107       return;
108   }
109   else {
110     aSecondDir = aFirstDir; // direction does not matter
111     aSecondStep = 0.0;
112     aSecondNbCopies = 1;
113   }
114
115   std::string anError;
116   int aResultIndex = 0;
117   // Moving each part.
118   for (std::list<ResultPtr>::iterator aPRes = aParts.begin(); aPRes != aParts.end(); ++aPRes) {
119     ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aPRes);
120     std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
121     for (int j = 0; j < aSecondNbCopies; j++) {
122       for (int i = 0; i < aFirstNbCopies; i++) {
123         double dx = i * aFirstStep * aFirstDir->x() + j * aSecondStep * aSecondDir->x();
124         double dy = i * aFirstStep * aFirstDir->y() + j * aSecondStep * aSecondDir->y();
125         double dz = i * aFirstStep * aFirstDir->z() + j * aSecondStep * aSecondDir->z();
126         aTrsf->setTranslation(dx, dy, dz);
127
128         ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
129         aResultPart->setTrsf(anOrigin, aTrsf);
130         setResult(aResultPart, aResultIndex);
131         aResultIndex++;
132       }
133     }
134   }
135
136   // Collect transformations for each object in a part.
137   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList);
138   for (GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
139        anObjectsIt != anObjects.end(); anObjectsIt++) {
140     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
141     ListOfShape aListOfShape;
142
143     for (int j = 0; j < aSecondNbCopies; j++) {
144       for (int i = 0; i < aFirstNbCopies; i++) {
145         double dx = i * aFirstStep * aFirstDir->x() + j * aSecondStep * aSecondDir->x();
146         double dy = i * aFirstStep * aFirstDir->y() + j * aSecondStep * aSecondDir->y();
147         double dz = i * aFirstStep * aFirstDir->z() + j * aSecondStep * aSecondDir->z();
148         std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
149             new GeomAlgoAPI_Translation(aBaseShape, dx, dy, dz));
150
151         // Checking that the algorithm worked properly.
152         if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(
153             aTranslationAlgo, getKind(), anError)) {
154           setError(anError);
155           break;
156         }
157         aListOfShape.push_back(aTranslationAlgo->shape());
158         aMakeShapeList->appendAlgo(aTranslationAlgo);
159       }
160     }
161     GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
162     anObjects.markModified(aBaseShape, aCompound);
163   }
164
165   // Build results of the operation.
166   const ListOfShape& anOriginalShapes = anObjects.objects();
167   ListOfShape aTopLevel;
168   anObjects.topLevelObjects(aTopLevel);
169   for (ListOfShape::iterator anIt = aTopLevel.begin(); anIt != aTopLevel.end(); ++anIt) {
170     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
171     FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, anOriginalShapes, ListOfShape(),
172                                              aMakeShapeList, *anIt, "Translated");
173     setResult(aResultBody, aResultIndex++);
174   }
175
176   // Remove the rest results if there were produced in the previous pass.
177   removeResults(aResultIndex);
178 }
179
180 //=================================================================================================
181 bool FeaturesPlugin_MultiTranslation::paramsAlongDirection(const int theIndex,
182                                                            std::shared_ptr<GeomAPI_Dir>& theDir,
183                                                            double& theDistance,
184                                                            int& theQuantity)
185 {
186   static std::string THE_AXIS_DIR[2] = { AXIS_FIRST_DIR_ID(), AXIS_SECOND_DIR_ID() };
187   static std::string THE_STEP[2] = { STEP_FIRST_DIR_ID(), STEP_SECOND_DIR_ID() };
188   static std::string THE_COPIES[2] = { NB_COPIES_FIRST_DIR_ID(), NB_COPIES_SECOND_DIR_ID() };
189   static std::string THE_INDEX_ID[2] = { "first", "second" };
190
191   //Getting axis.
192   static const std::string aSelectionError = "Error: The axis shape selection is bad.";
193   AttributeSelectionPtr anObjRef = selection(THE_AXIS_DIR[theIndex]);
194   GeomShapePtr aShape = anObjRef->value();
195   if (!aShape.get() && anObjRef->context().get())
196     aShape = anObjRef->context()->shape();
197   if (!aShape.get()) {
198     setError(aSelectionError);
199     return false;
200   }
201
202   GeomEdgePtr anEdge;
203   if (aShape->isEdge())
204     anEdge = aShape->edge();
205   else if (aShape->isCompound()) {
206     GeomAPI_ShapeIterator anIt(aShape);
207     anEdge = anIt.current()->edge();
208   }
209
210   if (!anEdge.get()) {
211     setError(aSelectionError);
212     return false;
213   }
214
215   theDir = anEdge->line()->direction();
216   theDistance = real(THE_STEP[theIndex])->value();
217   theQuantity = integer(THE_COPIES[theIndex])->value();
218   if (theQuantity <= 0) {
219     std::string aFeatureError = "Multitranslation builder :: the number of copies for the ";
220     aFeatureError += THE_INDEX_ID[theIndex] + " direction is null or negative.";
221     setError(aFeatureError);
222     return false;
223   }
224   return true;
225 }