1 // Copyright (C) 2014-2019 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
20 #include <FeaturesPlugin_MultiTranslation.h>
21 #include <FeaturesPlugin_Tools.h>
23 #include <GeomAlgoAPI_CompoundBuilder.h>
24 #include <GeomAlgoAPI_MakeShapeList.h>
25 #include <GeomAlgoAPI_Tools.h>
26 #include <GeomAlgoAPI_Translation.h>
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>
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>
43 static const std::string MULTITRANSLATION_VERSION_1("v9.5");
45 //=================================================================================================
46 FeaturesPlugin_MultiTranslation::FeaturesPlugin_MultiTranslation()
50 //=================================================================================================
51 void FeaturesPlugin_MultiTranslation::initAttributes()
53 AttributeSelectionListPtr aSelection =
54 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
55 FeaturesPlugin_MultiTranslation::OBJECTS_LIST_ID(),
56 ModelAPI_AttributeSelectionList::typeId()));
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());
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());
74 if (!aSelection->isInitialized()) {
75 // new feature, not read from file
76 data()->setVersion(MULTITRANSLATION_VERSION_1);
80 //=================================================================================================
81 void FeaturesPlugin_MultiTranslation::execute()
83 bool isKeepSubShapes = data()->version() == MULTITRANSLATION_VERSION_1;
86 AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECTS_LIST_ID());
87 if (anObjectsSelList->size() == 0) {
88 setError("Error: empty selection list");
92 GeomAPI_ShapeHierarchy anObjects;
93 std::list<ResultPtr> aParts;
94 if (!FeaturesPlugin_Tools::shapesFromSelectionList(
95 anObjectsSelList, isKeepSubShapes, anObjects, aParts))
98 std::shared_ptr<GeomAPI_Dir> aFirstDir, aSecondDir;
99 double aFirstStep, aSecondStep;
100 int aFirstNbCopies, aSecondNbCopies;
101 if (!paramsAlongDirection(0, aFirstDir, aFirstStep, aFirstNbCopies))
104 bool useSecondDir = !string(USE_SECOND_DIR_ID())->value().empty();
106 if (!paramsAlongDirection(1, aSecondDir, aSecondStep, aSecondNbCopies))
110 aSecondDir = aFirstDir; // direction does not matter
116 int aResultIndex = 0;
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);
128 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
129 aResultPart->setTrsf(anOrigin, aTrsf);
130 setResult(aResultPart, aResultIndex);
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;
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));
151 // Checking that the algorithm worked properly.
152 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(
153 aTranslationAlgo, getKind(), anError)) {
157 aListOfShape.push_back(aTranslationAlgo->shape());
158 aMakeShapeList->appendAlgo(aTranslationAlgo);
161 GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
162 anObjects.markModified(aBaseShape, aCompound);
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++);
176 // Remove the rest results if there were produced in the previous pass.
177 removeResults(aResultIndex);
180 //=================================================================================================
181 bool FeaturesPlugin_MultiTranslation::paramsAlongDirection(const int theIndex,
182 std::shared_ptr<GeomAPI_Dir>& theDir,
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" };
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();
198 setError(aSelectionError);
203 if (aShape->isEdge())
204 anEdge = aShape->edge();
205 else if (aShape->isCompound()) {
206 GeomAPI_ShapeIterator anIt(aShape);
207 anEdge = anIt.current()->edge();
211 setError(aSelectionError);
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);