1 // Copyright (C) 2014-2022 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_MakeShapeSet.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>
40 #include <ModelAPI_Tools.h>
44 static const std::string MULTITRANSLATION_VERSION_1("v9.5");
46 //=================================================================================================
47 FeaturesPlugin_MultiTranslation::FeaturesPlugin_MultiTranslation()
51 //=================================================================================================
52 void FeaturesPlugin_MultiTranslation::initAttributes()
54 AttributeSelectionListPtr aSelection =
55 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
56 FeaturesPlugin_MultiTranslation::OBJECTS_LIST_ID(),
57 ModelAPI_AttributeSelectionList::typeId()));
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());
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());
75 if (!aSelection->isInitialized()) {
76 // new feature, not read from file
77 data()->setVersion(MULTITRANSLATION_VERSION_1);
81 //=================================================================================================
82 void FeaturesPlugin_MultiTranslation::execute()
84 bool isKeepSubShapes = data()->version() == MULTITRANSLATION_VERSION_1;
87 AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECTS_LIST_ID());
88 if (anObjectsSelList->size() == 0) {
89 setError("Error: empty selection list");
93 GeomAPI_ShapeHierarchy anObjects;
94 std::list<ResultPtr> aParts;
95 ResultPtr aTextureSource;
96 if (!FeaturesPlugin_Tools::shapesFromSelectionList
97 (anObjectsSelList, isKeepSubShapes, anObjects, aParts, aTextureSource))
100 std::shared_ptr<GeomAPI_Dir> aFirstDir, aSecondDir;
101 double aFirstStep, aSecondStep;
102 int aFirstNbCopies, aSecondNbCopies;
103 if (!paramsAlongDirection(0, aFirstDir, aFirstStep, aFirstNbCopies))
106 bool useSecondDir = !string(USE_SECOND_DIR_ID())->value().empty();
108 if (!paramsAlongDirection(1, aSecondDir, aSecondStep, aSecondNbCopies))
112 aSecondDir = aFirstDir; // direction does not matter
118 int aResultIndex = 0;
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);
130 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
131 aResultPart->setTrsf(anOrigin, aTrsf);
132 setResult(aResultPart, aResultIndex);
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;
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));
153 // Checking that the algorithm worked properly.
154 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(
155 aTranslationAlgo, getKind(), anError)) {
159 aListOfShape.push_back(aTranslationAlgo->shape());
160 aMakeShapeList->appendAlgo(aTranslationAlgo);
163 GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
164 anObjects.markModified(aBaseShape, aCompound);
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++);
180 // Remove the rest results if there were produced in the previous pass.
181 removeResults(aResultIndex);
184 //=================================================================================================
185 bool FeaturesPlugin_MultiTranslation::paramsAlongDirection(const int theIndex,
186 std::shared_ptr<GeomAPI_Dir>& theDir,
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" };
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();
202 setError(aSelectionError);
207 if (aShape->isEdge())
208 anEdge = aShape->edge();
209 else if (aShape->isCompound()) {
210 GeomAPI_ShapeIterator anIt(aShape);
211 anEdge = anIt.current()->edge();
215 setError(aSelectionError);
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);