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_Trsf.h>
35 #include <GeomAlgoAPI_PointBuilder.h>
37 #include <FeaturesPlugin_Tools.h>
39 //=================================================================================================
40 FeaturesPlugin_Translation::FeaturesPlugin_Translation()
44 //=================================================================================================
45 void FeaturesPlugin_Translation::initAttributes()
47 data()->addAttribute(FeaturesPlugin_Translation::CREATION_METHOD(),
48 ModelAPI_AttributeString::typeId());
50 AttributeSelectionListPtr aSelection =
51 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
52 FeaturesPlugin_Translation::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
54 data()->addAttribute(FeaturesPlugin_Translation::AXIS_OBJECT_ID(),
55 ModelAPI_AttributeSelection::typeId());
56 data()->addAttribute(FeaturesPlugin_Translation::DISTANCE_ID(),
57 ModelAPI_AttributeDouble::typeId());
59 data()->addAttribute(FeaturesPlugin_Translation::DX_ID(),
60 ModelAPI_AttributeDouble::typeId());
61 data()->addAttribute(FeaturesPlugin_Translation::DY_ID(),
62 ModelAPI_AttributeDouble::typeId());
63 data()->addAttribute(FeaturesPlugin_Translation::DZ_ID(),
64 ModelAPI_AttributeDouble::typeId());
66 data()->addAttribute(FeaturesPlugin_Translation::START_POINT_ID(),
67 ModelAPI_AttributeSelection::typeId());
68 data()->addAttribute(FeaturesPlugin_Translation::END_POINT_ID(),
69 ModelAPI_AttributeSelection::typeId());
72 //=================================================================================================
73 void FeaturesPlugin_Translation::execute()
75 AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Translation::CREATION_METHOD());
76 std::string aMethodType = aMethodTypeAttr->value();
78 if (aMethodType == CREATION_METHOD_BY_DISTANCE()) {
79 performTranslationByAxisAndDistance();
82 if (aMethodType == CREATION_METHOD_BY_DIMENSIONS()) {
83 performTranslationByDimensions();
86 if (aMethodType == CREATION_METHOD_BY_TWO_POINTS()) {
87 performTranslationByTwoPoints();
91 //=================================================================================================
92 void FeaturesPlugin_Translation::performTranslationByAxisAndDistance()
95 ListOfShape anObjects;
96 std::list<ResultPtr> aContextes;
97 AttributeSelectionListPtr anObjectsSelList =
98 selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
99 if (anObjectsSelList->size() == 0) {
102 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
103 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
104 anObjectsSelList->value(anObjectsIndex);
105 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
106 if(!anObject.get()) { // may be for not-activated parts
110 anObjects.push_back(anObject);
111 aContextes.push_back(anObjectAttr->context());
115 std::shared_ptr<GeomAPI_Ax1> anAxis;
116 std::shared_ptr<GeomAPI_Edge> anEdge;
117 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
118 selection(FeaturesPlugin_Translation::AXIS_OBJECT_ID());
119 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
120 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
121 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
122 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
123 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
126 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
127 anEdge->line()->direction()));
131 double aDistance = real(FeaturesPlugin_Translation::DISTANCE_ID())->value();
133 // Moving each object.
134 int aResultIndex = 0;
135 std::list<ResultPtr>::iterator aContext = aContextes.begin();
136 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
137 anObjectsIt++, aContext++) {
138 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
139 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
143 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
144 aTrsf->setTranslation(anAxis, aDistance);
145 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
146 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
147 aResultPart->setTrsf(*aContext, aTrsf);
148 setResult(aResultPart, aResultIndex);
150 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
151 new GeomAlgoAPI_Translation(aBaseShape, anAxis, aDistance));
153 if (!aTranslationAlgo->check()) {
154 setError(aTranslationAlgo->getError());
158 aTranslationAlgo->build();
160 // Checking that the algorithm worked properly.
161 if(!aTranslationAlgo->isDone()) {
162 static const std::string aFeatureError = "Error: Translation algorithm failed.";
163 setError(aFeatureError);
166 if(aTranslationAlgo->shape()->isNull()) {
167 static const std::string aShapeError = "Error: Resulting shape is Null.";
168 setError(aShapeError);
171 if(!aTranslationAlgo->isValid()) {
172 std::string aFeatureError = "Error: Resulting shape is not valid.";
173 setError(aFeatureError);
177 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
178 aResultBody->storeModified(aBaseShape, aTranslationAlgo->shape());
179 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
183 setResult(aResultBody, aResultIndex);
188 // Remove the rest results if there were produced in the previous pass.
189 removeResults(aResultIndex);
192 //=================================================================================================
193 void FeaturesPlugin_Translation::performTranslationByDimensions()
196 ListOfShape anObjects;
197 std::list<ResultPtr> aContextes;
198 AttributeSelectionListPtr anObjectsSelList =
199 selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
200 if (anObjectsSelList->size() == 0) {
203 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
204 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
205 anObjectsSelList->value(anObjectsIndex);
206 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
207 if(!anObject.get()) { // may be for not-activated parts
211 anObjects.push_back(anObject);
212 aContextes.push_back(anObjectAttr->context());
215 // Getting dimensions in X, in Y and in Z
216 double aDX = real(FeaturesPlugin_Translation::DX_ID())->value();
217 double aDY = real(FeaturesPlugin_Translation::DY_ID())->value();
218 double aDZ = real(FeaturesPlugin_Translation::DZ_ID())->value();
220 // Moving each object.
221 int aResultIndex = 0;
222 std::list<ResultPtr>::iterator aContext = aContextes.begin();
223 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
224 anObjectsIt++, aContext++) {
225 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
226 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
230 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
231 aTrsf->setTranslation(aDX, aDY, aDZ);
232 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
233 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
234 aResultPart->setTrsf(*aContext, aTrsf);
235 setResult(aResultPart, aResultIndex);
237 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
238 new GeomAlgoAPI_Translation(aBaseShape, aDX, aDY, aDZ));
240 if (!aTranslationAlgo->check()) {
241 setError(aTranslationAlgo->getError());
245 aTranslationAlgo->build();
247 // Checking that the algorithm worked properly.
248 if(!aTranslationAlgo->isDone()) {
249 static const std::string aFeatureError = "Error: Translation algorithm failed.";
250 setError(aFeatureError);
253 if(aTranslationAlgo->shape()->isNull()) {
254 static const std::string aShapeError = "Error: Resulting shape is Null.";
255 setError(aShapeError);
258 if(!aTranslationAlgo->isValid()) {
259 std::string aFeatureError = "Error: Resulting shape is not valid.";
260 setError(aFeatureError);
264 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
265 aResultBody->storeModified(aBaseShape, aTranslationAlgo->shape());
266 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
270 setResult(aResultBody, aResultIndex);
275 // Remove the rest results if there were produced in the previous pass.
276 removeResults(aResultIndex);
279 //=================================================================================================
280 void FeaturesPlugin_Translation::performTranslationByTwoPoints()
283 ListOfShape anObjects;
284 std::list<ResultPtr> aContextes;
285 AttributeSelectionListPtr anObjectsSelList =
286 selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
287 if (anObjectsSelList->size() == 0) {
290 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
291 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
292 anObjectsSelList->value(anObjectsIndex);
293 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
294 if(!anObject.get()) { // may be for not-activated parts
298 anObjects.push_back(anObject);
299 aContextes.push_back(anObjectAttr->context());
302 // Getting the start point and the end point
303 AttributeSelectionPtr aRef1 = data()->selection(FeaturesPlugin_Translation::START_POINT_ID());
304 AttributeSelectionPtr aRef2 = data()->selection(FeaturesPlugin_Translation::END_POINT_ID());
305 std::shared_ptr<GeomAPI_Pnt> aFirstPoint;
306 std::shared_ptr<GeomAPI_Pnt> aSecondPoint;
307 if ((aRef1.get() != NULL) && (aRef2.get() != NULL)) {
308 GeomShapePtr aShape1 = aRef1->value();
309 if (!aShape1.get()) //If we can't get the points directly, try getting them from the context
310 aShape1 = aRef1->context()->shape();
311 GeomShapePtr aShape2 = aRef2->value();
313 aShape2 = aRef2->context()->shape();
314 if (aShape1 && aShape2) {
315 aFirstPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
316 aSecondPoint = GeomAlgoAPI_PointBuilder::point(aShape2);
320 // Moving each object.
321 int aResultIndex = 0;
322 std::list<ResultPtr>::iterator aContext = aContextes.begin();
323 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
324 anObjectsIt++, aContext++) {
325 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
326 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
330 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
331 aTrsf->setTranslation(aFirstPoint, aSecondPoint);
332 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
333 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
334 aResultPart->setTrsf(*aContext, aTrsf);
335 setResult(aResultPart, aResultIndex);
337 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
338 new GeomAlgoAPI_Translation(aBaseShape, aFirstPoint, aSecondPoint));
340 if (!aTranslationAlgo->check()) {
341 setError(aTranslationAlgo->getError());
345 aTranslationAlgo->build();
347 // Checking that the algorithm worked properly.
348 if(!aTranslationAlgo->isDone()) {
349 static const std::string aFeatureError = "Error: Translation algorithm failed.";
350 setError(aFeatureError);
353 if(aTranslationAlgo->shape()->isNull()) {
354 static const std::string aShapeError = "Error: Resulting shape is Null.";
355 setError(aShapeError);
358 if(!aTranslationAlgo->isValid()) {
359 std::string aFeatureError = "Error: Resulting shape is not valid.";
360 setError(aFeatureError);
364 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
365 aResultBody->storeModified(aBaseShape, aTranslationAlgo->shape());
366 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
370 setResult(aResultBody, aResultIndex);
375 // Remove the rest results if there were produced in the previous pass.
376 removeResults(aResultIndex);