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);
188 aResultBody->storeModified(aBaseShape, aTranslationAlgo->shape());
189 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
193 setResult(aResultBody, aResultIndex);
198 // Remove the rest results if there were produced in the previous pass.
199 removeResults(aResultIndex);
202 //=================================================================================================
203 void FeaturesPlugin_Translation::performTranslationByDimensions()
206 ListOfShape anObjects;
207 std::list<ResultPtr> aContextes;
208 AttributeSelectionListPtr anObjectsSelList =
209 selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
210 if (anObjectsSelList->size() == 0) {
213 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
214 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
215 anObjectsSelList->value(anObjectsIndex);
216 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
217 if(!anObject.get()) { // may be for not-activated parts
220 anObjects.push_back(anObject);
221 aContextes.push_back(anObjectAttr->context());
224 // Getting dimensions in X, in Y and in Z
225 double aDX = real(FeaturesPlugin_Translation::DX_ID())->value();
226 double aDY = real(FeaturesPlugin_Translation::DY_ID())->value();
227 double aDZ = real(FeaturesPlugin_Translation::DZ_ID())->value();
229 // Moving each object.
231 int aResultIndex = 0;
232 std::list<ResultPtr>::iterator aContext = aContextes.begin();
233 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
234 anObjectsIt++, aContext++) {
235 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
236 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
240 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
241 aTrsf->setTranslation(aDX, aDY, aDZ);
242 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
243 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
244 aResultPart->setTrsf(*aContext, aTrsf);
245 setResult(aResultPart, aResultIndex);
247 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
248 new GeomAlgoAPI_Translation(aBaseShape, aDX, aDY, aDZ));
250 if (!aTranslationAlgo->check()) {
251 setError(aTranslationAlgo->getError());
255 aTranslationAlgo->build();
257 // Checking that the algorithm worked properly.
258 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aTranslationAlgo, getKind(), anError)) {
263 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
264 aResultBody->storeModified(aBaseShape, aTranslationAlgo->shape());
265 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
269 setResult(aResultBody, aResultIndex);
274 // Remove the rest results if there were produced in the previous pass.
275 removeResults(aResultIndex);
278 //=================================================================================================
279 void FeaturesPlugin_Translation::performTranslationByTwoPoints()
282 ListOfShape anObjects;
283 std::list<ResultPtr> aContextes;
284 AttributeSelectionListPtr anObjectsSelList =
285 selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
286 if (anObjectsSelList->size() == 0) {
289 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
290 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
291 anObjectsSelList->value(anObjectsIndex);
292 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
293 if(!anObject.get()) { // may be for not-activated parts
296 anObjects.push_back(anObject);
297 aContextes.push_back(anObjectAttr->context());
300 // Getting the start point and the end point
301 AttributeSelectionPtr aRef1 = data()->selection(FeaturesPlugin_Translation::START_POINT_ID());
302 AttributeSelectionPtr aRef2 = data()->selection(FeaturesPlugin_Translation::END_POINT_ID());
303 std::shared_ptr<GeomAPI_Pnt> aFirstPoint;
304 std::shared_ptr<GeomAPI_Pnt> aSecondPoint;
305 if ((aRef1.get() != NULL) && (aRef2.get() != NULL)) {
306 GeomShapePtr aShape1 = aRef1->value();
307 if (!aShape1.get()) //If we can't get the points directly, try getting them from the context
308 aShape1 = aRef1->context()->shape();
309 GeomShapePtr aShape2 = aRef2->value();
311 aShape2 = aRef2->context()->shape();
312 if (aShape1 && aShape2) {
313 aFirstPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
314 aSecondPoint = GeomAlgoAPI_PointBuilder::point(aShape2);
318 // Moving each object.
320 int aResultIndex = 0;
321 std::list<ResultPtr>::iterator aContext = aContextes.begin();
322 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
323 anObjectsIt++, aContext++) {
324 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
325 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
329 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
330 aTrsf->setTranslation(aFirstPoint, aSecondPoint);
331 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
332 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
333 aResultPart->setTrsf(*aContext, aTrsf);
334 setResult(aResultPart, aResultIndex);
336 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
337 new GeomAlgoAPI_Translation(aBaseShape, aFirstPoint, aSecondPoint));
339 if (!aTranslationAlgo->check()) {
340 setError(aTranslationAlgo->getError());
344 aTranslationAlgo->build();
346 // Checking that the algorithm worked properly.
347 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aTranslationAlgo, getKind(), anError)) {
352 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
353 aResultBody->storeModified(aBaseShape, aTranslationAlgo->shape());
354 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
358 setResult(aResultBody, aResultIndex);
363 // Remove the rest results if there were produced in the previous pass.
364 removeResults(aResultIndex);