1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <FeaturesPlugin_Translation.h>
23 #include <ModelAPI_AttributeDouble.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_AttributeString.h>
26 #include <ModelAPI_BodyBuilder.h>
27 #include <ModelAPI_ResultBody.h>
28 #include <ModelAPI_ResultPart.h>
29 #include <ModelAPI_Session.h>
31 #include <GeomAPI_Edge.h>
32 #include <GeomAPI_Lin.h>
33 #include <GeomAPI_ShapeIterator.h>
34 #include <GeomAPI_Trsf.h>
36 #include <GeomAlgoAPI_PointBuilder.h>
37 #include <GeomAlgoAPI_Tools.h>
39 #include <FeaturesPlugin_Tools.h>
41 //=================================================================================================
42 FeaturesPlugin_Translation::FeaturesPlugin_Translation()
46 //=================================================================================================
47 void FeaturesPlugin_Translation::initAttributes()
49 data()->addAttribute(FeaturesPlugin_Translation::CREATION_METHOD(),
50 ModelAPI_AttributeString::typeId());
52 AttributeSelectionListPtr aSelection =
53 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
54 FeaturesPlugin_Translation::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
56 data()->addAttribute(FeaturesPlugin_Translation::AXIS_OBJECT_ID(),
57 ModelAPI_AttributeSelection::typeId());
58 data()->addAttribute(FeaturesPlugin_Translation::DISTANCE_ID(),
59 ModelAPI_AttributeDouble::typeId());
61 data()->addAttribute(FeaturesPlugin_Translation::DX_ID(),
62 ModelAPI_AttributeDouble::typeId());
63 data()->addAttribute(FeaturesPlugin_Translation::DY_ID(),
64 ModelAPI_AttributeDouble::typeId());
65 data()->addAttribute(FeaturesPlugin_Translation::DZ_ID(),
66 ModelAPI_AttributeDouble::typeId());
68 data()->addAttribute(FeaturesPlugin_Translation::START_POINT_ID(),
69 ModelAPI_AttributeSelection::typeId());
70 data()->addAttribute(FeaturesPlugin_Translation::END_POINT_ID(),
71 ModelAPI_AttributeSelection::typeId());
74 //=================================================================================================
75 void FeaturesPlugin_Translation::execute()
77 AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Translation::CREATION_METHOD());
78 std::string aMethodType = aMethodTypeAttr->value();
80 if (aMethodType == CREATION_METHOD_BY_DISTANCE()) {
81 performTranslationByAxisAndDistance();
84 if (aMethodType == CREATION_METHOD_BY_DIMENSIONS()) {
85 performTranslationByDimensions();
88 if (aMethodType == CREATION_METHOD_BY_TWO_POINTS()) {
89 performTranslationByTwoPoints();
93 //=================================================================================================
94 void FeaturesPlugin_Translation::performTranslationByAxisAndDistance()
97 ListOfShape anObjects;
98 std::list<ResultPtr> aContextes;
99 AttributeSelectionListPtr anObjectsSelList =
100 selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
101 if (anObjectsSelList->size() == 0) {
104 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
105 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
106 anObjectsSelList->value(anObjectsIndex);
107 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
108 if(!anObject.get()) { // may be for not-activated parts
111 anObjects.push_back(anObject);
112 aContextes.push_back(anObjectAttr->context());
116 static const std::string aSelectionError = "Error: The axis shape selection is bad.";
117 AttributeSelectionPtr anObjRef = selection(AXIS_OBJECT_ID());
118 GeomShapePtr aShape = anObjRef->value();
120 if (anObjRef->context().get()) {
121 aShape = anObjRef->context()->shape();
125 setError(aSelectionError);
130 if (aShape->isEdge())
132 anEdge = aShape->edge();
134 else if (aShape->isCompound())
136 GeomAPI_ShapeIterator anIt(aShape);
137 anEdge = anIt.current()->edge();
142 setError(aSelectionError);
146 std::shared_ptr<GeomAPI_Ax1> anAxis(new GeomAPI_Ax1(anEdge->line()->location(),
147 anEdge->line()->direction()));
151 double aDistance = real(FeaturesPlugin_Translation::DISTANCE_ID())->value();
153 // Moving each object.
155 int aResultIndex = 0;
156 std::list<ResultPtr>::iterator aContext = aContextes.begin();
157 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
158 anObjectsIt++, aContext++) {
159 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
160 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
164 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
165 aTrsf->setTranslation(anAxis, aDistance);
166 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
167 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
168 aResultPart->setTrsf(*aContext, aTrsf);
169 setResult(aResultPart, aResultIndex);
171 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
172 new GeomAlgoAPI_Translation(aBaseShape, anAxis, aDistance));
174 if (!aTranslationAlgo->check()) {
175 setError(aTranslationAlgo->getError());
179 aTranslationAlgo->build();
181 // Checking that the algorithm worked properly.
182 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aTranslationAlgo, getKind(), anError)) {
187 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
190 aShapes.push_back(aBaseShape);
191 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
195 aTranslationAlgo->shape(),
197 setResult(aResultBody, aResultIndex);
202 // Remove the rest results if there were produced in the previous pass.
203 removeResults(aResultIndex);
206 //=================================================================================================
207 void FeaturesPlugin_Translation::performTranslationByDimensions()
210 ListOfShape anObjects;
211 std::list<ResultPtr> aContextes;
212 AttributeSelectionListPtr anObjectsSelList =
213 selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
214 if (anObjectsSelList->size() == 0) {
217 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
218 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
219 anObjectsSelList->value(anObjectsIndex);
220 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
221 if(!anObject.get()) { // may be for not-activated parts
224 anObjects.push_back(anObject);
225 aContextes.push_back(anObjectAttr->context());
228 // Getting dimensions in X, in Y and in Z
229 double aDX = real(FeaturesPlugin_Translation::DX_ID())->value();
230 double aDY = real(FeaturesPlugin_Translation::DY_ID())->value();
231 double aDZ = real(FeaturesPlugin_Translation::DZ_ID())->value();
233 // Moving each object.
235 int aResultIndex = 0;
236 std::list<ResultPtr>::iterator aContext = aContextes.begin();
237 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
238 anObjectsIt++, aContext++) {
239 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
240 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
244 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
245 aTrsf->setTranslation(aDX, aDY, aDZ);
246 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
247 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
248 aResultPart->setTrsf(*aContext, aTrsf);
249 setResult(aResultPart, aResultIndex);
251 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
252 new GeomAlgoAPI_Translation(aBaseShape, aDX, aDY, aDZ));
254 if (!aTranslationAlgo->check()) {
255 setError(aTranslationAlgo->getError());
259 aTranslationAlgo->build();
261 // Checking that the algorithm worked properly.
262 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aTranslationAlgo, getKind(), anError)) {
267 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
270 aShapes.push_back(aBaseShape);
271 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
275 aTranslationAlgo->shape(),
277 setResult(aResultBody, aResultIndex);
282 // Remove the rest results if there were produced in the previous pass.
283 removeResults(aResultIndex);
286 //=================================================================================================
287 void FeaturesPlugin_Translation::performTranslationByTwoPoints()
290 ListOfShape anObjects;
291 std::list<ResultPtr> aContextes;
292 AttributeSelectionListPtr anObjectsSelList =
293 selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
294 if (anObjectsSelList->size() == 0) {
297 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
298 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
299 anObjectsSelList->value(anObjectsIndex);
300 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
301 if(!anObject.get()) { // may be for not-activated parts
304 anObjects.push_back(anObject);
305 aContextes.push_back(anObjectAttr->context());
308 // Getting the start point and the end point
309 AttributeSelectionPtr aRef1 = data()->selection(FeaturesPlugin_Translation::START_POINT_ID());
310 AttributeSelectionPtr aRef2 = data()->selection(FeaturesPlugin_Translation::END_POINT_ID());
311 std::shared_ptr<GeomAPI_Pnt> aFirstPoint;
312 std::shared_ptr<GeomAPI_Pnt> aSecondPoint;
313 if ((aRef1.get() != NULL) && (aRef2.get() != NULL)) {
314 GeomShapePtr aShape1 = aRef1->value();
315 if (!aShape1.get()) //If we can't get the points directly, try getting them from the context
316 aShape1 = aRef1->context()->shape();
317 GeomShapePtr aShape2 = aRef2->value();
319 aShape2 = aRef2->context()->shape();
320 if (aShape1 && aShape2) {
321 aFirstPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
322 aSecondPoint = GeomAlgoAPI_PointBuilder::point(aShape2);
326 // Moving each object.
328 int aResultIndex = 0;
329 std::list<ResultPtr>::iterator aContext = aContextes.begin();
330 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
331 anObjectsIt++, aContext++) {
332 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
333 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
337 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
338 aTrsf->setTranslation(aFirstPoint, aSecondPoint);
339 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
340 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
341 aResultPart->setTrsf(*aContext, aTrsf);
342 setResult(aResultPart, aResultIndex);
344 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
345 new GeomAlgoAPI_Translation(aBaseShape, aFirstPoint, aSecondPoint));
347 if (!aTranslationAlgo->check()) {
348 setError(aTranslationAlgo->getError());
352 aTranslationAlgo->build();
354 // Checking that the algorithm worked properly.
355 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aTranslationAlgo, getKind(), anError)) {
360 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
363 aShapes.push_back(aBaseShape);
364 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
368 aTranslationAlgo->shape(),
370 setResult(aResultBody, aResultIndex);
375 // Remove the rest results if there were produced in the previous pass.
376 removeResults(aResultIndex);