1 // Copyright (C) 2014-2020 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_Translation.h>
22 #include <ModelAPI_AttributeDouble.h>
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_AttributeString.h>
25 #include <ModelAPI_BodyBuilder.h>
26 #include <ModelAPI_ResultBody.h>
27 #include <ModelAPI_ResultPart.h>
28 #include <ModelAPI_Session.h>
30 #include <GeomAPI_Ax1.h>
31 #include <GeomAPI_Edge.h>
32 #include <GeomAPI_Lin.h>
33 #include <GeomAPI_ShapeIterator.h>
34 #include <GeomAPI_ShapeHierarchy.h>
35 #include <GeomAPI_Trsf.h>
37 #include <GeomAlgoAPI_MakeShapeList.h>
38 #include <GeomAlgoAPI_PointBuilder.h>
39 #include <GeomAlgoAPI_Tools.h>
40 #include <GeomAlgoAPI_Transform.h>
42 #include <FeaturesPlugin_Tools.h>
44 static const std::string TRANSLATION_VERSION_1("v9.5");
46 //=================================================================================================
47 FeaturesPlugin_Translation::FeaturesPlugin_Translation()
51 //=================================================================================================
52 void FeaturesPlugin_Translation::initAttributes()
54 data()->addAttribute(FeaturesPlugin_Translation::CREATION_METHOD(),
55 ModelAPI_AttributeString::typeId());
57 AttributeSelectionListPtr aSelection =
58 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
59 FeaturesPlugin_Translation::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
61 data()->addAttribute(FeaturesPlugin_Translation::AXIS_OBJECT_ID(),
62 ModelAPI_AttributeSelection::typeId());
63 data()->addAttribute(FeaturesPlugin_Translation::DISTANCE_ID(),
64 ModelAPI_AttributeDouble::typeId());
66 data()->addAttribute(FeaturesPlugin_Translation::DX_ID(),
67 ModelAPI_AttributeDouble::typeId());
68 data()->addAttribute(FeaturesPlugin_Translation::DY_ID(),
69 ModelAPI_AttributeDouble::typeId());
70 data()->addAttribute(FeaturesPlugin_Translation::DZ_ID(),
71 ModelAPI_AttributeDouble::typeId());
73 data()->addAttribute(FeaturesPlugin_Translation::START_POINT_ID(),
74 ModelAPI_AttributeSelection::typeId());
75 data()->addAttribute(FeaturesPlugin_Translation::END_POINT_ID(),
76 ModelAPI_AttributeSelection::typeId());
78 if (!aSelection->isInitialized()) {
79 // new feature, not read from file
80 data()->setVersion(TRANSLATION_VERSION_1);
84 //=================================================================================================
85 void FeaturesPlugin_Translation::execute()
87 AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Translation::CREATION_METHOD());
88 std::string aMethodType = aMethodTypeAttr->value();
91 if (aMethodType == CREATION_METHOD_BY_DISTANCE())
92 aTrsf = translationByAxisAndDistance();
93 else if (aMethodType == CREATION_METHOD_BY_DIMENSIONS())
94 aTrsf = translationByDimensions();
95 else if (aMethodType == CREATION_METHOD_BY_TWO_POINTS())
96 aTrsf = translationByTwoPoints();
98 performTranslation(aTrsf);
101 //=================================================================================================
102 GeomTrsfPtr FeaturesPlugin_Translation::translationByAxisAndDistance()
105 static const std::string aSelectionError = "Error: The axis shape selection is bad.";
106 AttributeSelectionPtr anObjRef = selection(AXIS_OBJECT_ID());
107 GeomShapePtr aShape = anObjRef->value();
109 if (anObjRef->context().get()) {
110 aShape = anObjRef->context()->shape();
114 setError(aSelectionError);
115 return GeomTrsfPtr();
119 if (aShape->isEdge())
121 anEdge = aShape->edge();
123 else if (aShape->isCompound())
125 GeomAPI_ShapeIterator anIt(aShape);
126 anEdge = anIt.current()->edge();
131 setError(aSelectionError);
132 return GeomTrsfPtr();
135 std::shared_ptr<GeomAPI_Ax1> anAxis(new GeomAPI_Ax1(anEdge->line()->location(),
136 anEdge->line()->direction()));
139 double aDistance = real(FeaturesPlugin_Translation::DISTANCE_ID())->value();
141 GeomTrsfPtr aTrsf(new GeomAPI_Trsf);
142 aTrsf->setTranslation(anAxis, aDistance);
146 //=================================================================================================
147 GeomTrsfPtr FeaturesPlugin_Translation::translationByDimensions()
149 // Getting dimensions in X, in Y and in Z
150 double aDX = real(FeaturesPlugin_Translation::DX_ID())->value();
151 double aDY = real(FeaturesPlugin_Translation::DY_ID())->value();
152 double aDZ = real(FeaturesPlugin_Translation::DZ_ID())->value();
154 GeomTrsfPtr aTrsf(new GeomAPI_Trsf);
155 aTrsf->setTranslation(aDX, aDY, aDZ);
159 //=================================================================================================
160 GeomTrsfPtr FeaturesPlugin_Translation::translationByTwoPoints()
162 // Getting the start point and the end point
163 AttributeSelectionPtr aRef1 = data()->selection(FeaturesPlugin_Translation::START_POINT_ID());
164 AttributeSelectionPtr aRef2 = data()->selection(FeaturesPlugin_Translation::END_POINT_ID());
165 std::shared_ptr<GeomAPI_Pnt> aFirstPoint;
166 std::shared_ptr<GeomAPI_Pnt> aSecondPoint;
167 if ((aRef1.get() != NULL) && (aRef2.get() != NULL)) {
168 GeomShapePtr aShape1 = aRef1->value();
169 if (!aShape1.get()) //If we can't get the points directly, try getting them from the context
170 aShape1 = aRef1->context()->shape();
171 GeomShapePtr aShape2 = aRef2->value();
173 aShape2 = aRef2->context()->shape();
174 if (aShape1 && aShape2) {
175 aFirstPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
176 aSecondPoint = GeomAlgoAPI_PointBuilder::point(aShape2);
180 GeomTrsfPtr aTrsf(new GeomAPI_Trsf);
181 if (aFirstPoint && aSecondPoint) {
182 aTrsf->setTranslation(aFirstPoint, aSecondPoint);
187 //=================================================================================================
188 void FeaturesPlugin_Translation::performTranslation(const GeomTrsfPtr& theTrsf)
191 setError("Invalid transformation.");
195 bool isKeepSubShapes = data()->version() == TRANSLATION_VERSION_1;
198 GeomAPI_ShapeHierarchy anObjects;
199 std::list<ResultPtr> aParts;
200 AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECTS_LIST_ID());
201 if (!FeaturesPlugin_Tools::shapesFromSelectionList(
202 anObjectsSelList, isKeepSubShapes, anObjects, aParts))
206 int aResultIndex = 0;
208 for (std::list<ResultPtr>::iterator aPRes = aParts.begin(); aPRes != aParts.end(); ++aPRes) {
209 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aPRes);
210 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
211 aResultPart->setTrsf(anOrigin, theTrsf);
212 setResult(aResultPart, aResultIndex++);
215 // Collect transformations for each object in a part.
216 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList);
218 for (GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
219 anObjectsIt != anObjects.end(); anObjectsIt++) {
220 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
221 std::shared_ptr<GeomAlgoAPI_Transform> aTransformAlgo(
222 new GeomAlgoAPI_Transform(aBaseShape, theTrsf));
224 // Checking that the algorithm worked properly.
225 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aTransformAlgo, getKind(), anError)) {
230 anObjects.markModified(aBaseShape, aTransformAlgo->shape());
231 aMakeShapeList->appendAlgo(aTransformAlgo);
234 // Build results of the operation.
235 const ListOfShape& anOriginalShapes = anObjects.objects();
236 ListOfShape aTopLevel;
237 anObjects.topLevelObjects(aTopLevel);
238 for (ListOfShape::iterator anIt = aTopLevel.begin(); anIt != aTopLevel.end(); ++anIt) {
240 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
241 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, anOriginalShapes, ListOfShape(),
242 aMakeShapeList, *anIt, "Translated");
243 setResult(aResultBody, aResultIndex++);
246 // Remove the rest results if there were produced in the previous pass.
247 removeResults(aResultIndex);