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_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_Edge.h>
31 #include <GeomAPI_Lin.h>
32 #include <GeomAPI_ShapeIterator.h>
33 #include <GeomAPI_Trsf.h>
35 #include <GeomAlgoAPI_PointBuilder.h>
36 #include <GeomAlgoAPI_Tools.h>
38 #include <FeaturesPlugin_Tools.h>
40 //=================================================================================================
41 FeaturesPlugin_Translation::FeaturesPlugin_Translation()
45 //=================================================================================================
46 void FeaturesPlugin_Translation::initAttributes()
48 data()->addAttribute(FeaturesPlugin_Translation::CREATION_METHOD(),
49 ModelAPI_AttributeString::typeId());
51 AttributeSelectionListPtr aSelection =
52 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
53 FeaturesPlugin_Translation::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
55 data()->addAttribute(FeaturesPlugin_Translation::AXIS_OBJECT_ID(),
56 ModelAPI_AttributeSelection::typeId());
57 data()->addAttribute(FeaturesPlugin_Translation::DISTANCE_ID(),
58 ModelAPI_AttributeDouble::typeId());
60 data()->addAttribute(FeaturesPlugin_Translation::DX_ID(),
61 ModelAPI_AttributeDouble::typeId());
62 data()->addAttribute(FeaturesPlugin_Translation::DY_ID(),
63 ModelAPI_AttributeDouble::typeId());
64 data()->addAttribute(FeaturesPlugin_Translation::DZ_ID(),
65 ModelAPI_AttributeDouble::typeId());
67 data()->addAttribute(FeaturesPlugin_Translation::START_POINT_ID(),
68 ModelAPI_AttributeSelection::typeId());
69 data()->addAttribute(FeaturesPlugin_Translation::END_POINT_ID(),
70 ModelAPI_AttributeSelection::typeId());
73 //=================================================================================================
74 void FeaturesPlugin_Translation::execute()
76 AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Translation::CREATION_METHOD());
77 std::string aMethodType = aMethodTypeAttr->value();
79 if (aMethodType == CREATION_METHOD_BY_DISTANCE()) {
80 performTranslationByAxisAndDistance();
83 if (aMethodType == CREATION_METHOD_BY_DIMENSIONS()) {
84 performTranslationByDimensions();
87 if (aMethodType == CREATION_METHOD_BY_TWO_POINTS()) {
88 performTranslationByTwoPoints();
92 //=================================================================================================
93 void FeaturesPlugin_Translation::performTranslationByAxisAndDistance()
96 ListOfShape anObjects;
97 std::list<ResultPtr> aContextes;
98 AttributeSelectionListPtr anObjectsSelList =
99 selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
100 if (anObjectsSelList->size() == 0) {
103 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
104 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
105 anObjectsSelList->value(anObjectsIndex);
106 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
107 if(!anObject.get()) { // may be for not-activated parts
110 anObjects.push_back(anObject);
111 aContextes.push_back(anObjectAttr->context());
115 static const std::string aSelectionError = "Error: The axis shape selection is bad.";
116 AttributeSelectionPtr anObjRef = selection(AXIS_OBJECT_ID());
117 GeomShapePtr aShape = anObjRef->value();
119 if (anObjRef->context().get()) {
120 aShape = anObjRef->context()->shape();
124 setError(aSelectionError);
129 if (aShape->isEdge())
131 anEdge = aShape->edge();
133 else if (aShape->isCompound())
135 GeomAPI_ShapeIterator anIt(aShape);
136 anEdge = anIt.current()->edge();
141 setError(aSelectionError);
145 std::shared_ptr<GeomAPI_Ax1> anAxis(new GeomAPI_Ax1(anEdge->line()->location(),
146 anEdge->line()->direction()));
150 double aDistance = real(FeaturesPlugin_Translation::DISTANCE_ID())->value();
152 // Moving each object.
154 int aResultIndex = 0;
155 std::list<ResultPtr>::iterator aContext = aContextes.begin();
156 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
157 anObjectsIt++, aContext++) {
158 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
159 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
163 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
164 aTrsf->setTranslation(anAxis, aDistance);
165 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
166 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
167 aResultPart->setTrsf(*aContext, aTrsf);
168 setResult(aResultPart, aResultIndex);
170 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
171 new GeomAlgoAPI_Translation(aBaseShape, anAxis, aDistance));
173 if (!aTranslationAlgo->check()) {
174 setError(aTranslationAlgo->getError());
178 aTranslationAlgo->build();
180 // Checking that the algorithm worked properly.
181 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aTranslationAlgo, getKind(), anError)) {
186 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
187 aResultBody->storeModified(aBaseShape, aTranslationAlgo->shape());
188 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
192 setResult(aResultBody, aResultIndex);
197 // Remove the rest results if there were produced in the previous pass.
198 removeResults(aResultIndex);
201 //=================================================================================================
202 void FeaturesPlugin_Translation::performTranslationByDimensions()
205 ListOfShape anObjects;
206 std::list<ResultPtr> aContextes;
207 AttributeSelectionListPtr anObjectsSelList =
208 selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
209 if (anObjectsSelList->size() == 0) {
212 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
213 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
214 anObjectsSelList->value(anObjectsIndex);
215 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
216 if(!anObject.get()) { // may be for not-activated parts
219 anObjects.push_back(anObject);
220 aContextes.push_back(anObjectAttr->context());
223 // Getting dimensions in X, in Y and in Z
224 double aDX = real(FeaturesPlugin_Translation::DX_ID())->value();
225 double aDY = real(FeaturesPlugin_Translation::DY_ID())->value();
226 double aDZ = real(FeaturesPlugin_Translation::DZ_ID())->value();
228 // Moving each object.
230 int aResultIndex = 0;
231 std::list<ResultPtr>::iterator aContext = aContextes.begin();
232 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
233 anObjectsIt++, aContext++) {
234 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
235 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
239 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
240 aTrsf->setTranslation(aDX, aDY, aDZ);
241 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
242 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
243 aResultPart->setTrsf(*aContext, aTrsf);
244 setResult(aResultPart, aResultIndex);
246 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
247 new GeomAlgoAPI_Translation(aBaseShape, aDX, aDY, aDZ));
249 if (!aTranslationAlgo->check()) {
250 setError(aTranslationAlgo->getError());
254 aTranslationAlgo->build();
256 // Checking that the algorithm worked properly.
257 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aTranslationAlgo, getKind(), anError)) {
262 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
263 aResultBody->storeModified(aBaseShape, aTranslationAlgo->shape());
264 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
268 setResult(aResultBody, aResultIndex);
273 // Remove the rest results if there were produced in the previous pass.
274 removeResults(aResultIndex);
277 //=================================================================================================
278 void FeaturesPlugin_Translation::performTranslationByTwoPoints()
281 ListOfShape anObjects;
282 std::list<ResultPtr> aContextes;
283 AttributeSelectionListPtr anObjectsSelList =
284 selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
285 if (anObjectsSelList->size() == 0) {
288 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
289 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
290 anObjectsSelList->value(anObjectsIndex);
291 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
292 if(!anObject.get()) { // may be for not-activated parts
295 anObjects.push_back(anObject);
296 aContextes.push_back(anObjectAttr->context());
299 // Getting the start point and the end point
300 AttributeSelectionPtr aRef1 = data()->selection(FeaturesPlugin_Translation::START_POINT_ID());
301 AttributeSelectionPtr aRef2 = data()->selection(FeaturesPlugin_Translation::END_POINT_ID());
302 std::shared_ptr<GeomAPI_Pnt> aFirstPoint;
303 std::shared_ptr<GeomAPI_Pnt> aSecondPoint;
304 if ((aRef1.get() != NULL) && (aRef2.get() != NULL)) {
305 GeomShapePtr aShape1 = aRef1->value();
306 if (!aShape1.get()) //If we can't get the points directly, try getting them from the context
307 aShape1 = aRef1->context()->shape();
308 GeomShapePtr aShape2 = aRef2->value();
310 aShape2 = aRef2->context()->shape();
311 if (aShape1 && aShape2) {
312 aFirstPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
313 aSecondPoint = GeomAlgoAPI_PointBuilder::point(aShape2);
317 // Moving each object.
319 int aResultIndex = 0;
320 std::list<ResultPtr>::iterator aContext = aContextes.begin();
321 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
322 anObjectsIt++, aContext++) {
323 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
324 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
328 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
329 aTrsf->setTranslation(aFirstPoint, aSecondPoint);
330 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
331 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
332 aResultPart->setTrsf(*aContext, aTrsf);
333 setResult(aResultPart, aResultIndex);
335 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
336 new GeomAlgoAPI_Translation(aBaseShape, aFirstPoint, aSecondPoint));
338 if (!aTranslationAlgo->check()) {
339 setError(aTranslationAlgo->getError());
343 aTranslationAlgo->build();
345 // Checking that the algorithm worked properly.
346 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aTranslationAlgo, getKind(), anError)) {
351 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
352 aResultBody->storeModified(aBaseShape, aTranslationAlgo->shape());
353 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
357 setResult(aResultBody, aResultIndex);
362 // Remove the rest results if there were produced in the previous pass.
363 removeResults(aResultIndex);