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>
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
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();
141 setError(aSelectionError);
147 setError(aSelectionError);
151 std::shared_ptr<GeomAPI_Ax1> anAxis(new GeomAPI_Ax1(anEdge->line()->location(),
152 anEdge->line()->direction()));
156 double aDistance = real(FeaturesPlugin_Translation::DISTANCE_ID())->value();
158 // Moving each object.
159 int aResultIndex = 0;
160 std::list<ResultPtr>::iterator aContext = aContextes.begin();
161 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
162 anObjectsIt++, aContext++) {
163 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
164 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
168 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
169 aTrsf->setTranslation(anAxis, aDistance);
170 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
171 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
172 aResultPart->setTrsf(*aContext, aTrsf);
173 setResult(aResultPart, aResultIndex);
175 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
176 new GeomAlgoAPI_Translation(aBaseShape, anAxis, aDistance));
178 if (!aTranslationAlgo->check()) {
179 setError(aTranslationAlgo->getError());
183 aTranslationAlgo->build();
185 // Checking that the algorithm worked properly.
186 if(!aTranslationAlgo->isDone()) {
187 static const std::string aFeatureError = "Error: Translation algorithm failed.";
188 setError(aFeatureError);
191 if(aTranslationAlgo->shape()->isNull()) {
192 static const std::string aShapeError = "Error: Resulting shape is Null.";
193 setError(aShapeError);
196 if(!aTranslationAlgo->isValid()) {
197 std::string aFeatureError = "Error: Resulting shape is not valid.";
198 setError(aFeatureError);
202 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
203 aResultBody->storeModified(aBaseShape, aTranslationAlgo->shape());
204 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
208 setResult(aResultBody, aResultIndex);
213 // Remove the rest results if there were produced in the previous pass.
214 removeResults(aResultIndex);
217 //=================================================================================================
218 void FeaturesPlugin_Translation::performTranslationByDimensions()
221 ListOfShape anObjects;
222 std::list<ResultPtr> aContextes;
223 AttributeSelectionListPtr anObjectsSelList =
224 selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
225 if (anObjectsSelList->size() == 0) {
228 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
229 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
230 anObjectsSelList->value(anObjectsIndex);
231 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
232 if(!anObject.get()) { // may be for not-activated parts
236 anObjects.push_back(anObject);
237 aContextes.push_back(anObjectAttr->context());
240 // Getting dimensions in X, in Y and in Z
241 double aDX = real(FeaturesPlugin_Translation::DX_ID())->value();
242 double aDY = real(FeaturesPlugin_Translation::DY_ID())->value();
243 double aDZ = real(FeaturesPlugin_Translation::DZ_ID())->value();
245 // Moving each object.
246 int aResultIndex = 0;
247 std::list<ResultPtr>::iterator aContext = aContextes.begin();
248 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
249 anObjectsIt++, aContext++) {
250 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
251 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
255 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
256 aTrsf->setTranslation(aDX, aDY, aDZ);
257 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
258 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
259 aResultPart->setTrsf(*aContext, aTrsf);
260 setResult(aResultPart, aResultIndex);
262 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
263 new GeomAlgoAPI_Translation(aBaseShape, aDX, aDY, aDZ));
265 if (!aTranslationAlgo->check()) {
266 setError(aTranslationAlgo->getError());
270 aTranslationAlgo->build();
272 // Checking that the algorithm worked properly.
273 if(!aTranslationAlgo->isDone()) {
274 static const std::string aFeatureError = "Error: Translation algorithm failed.";
275 setError(aFeatureError);
278 if(aTranslationAlgo->shape()->isNull()) {
279 static const std::string aShapeError = "Error: Resulting shape is Null.";
280 setError(aShapeError);
283 if(!aTranslationAlgo->isValid()) {
284 std::string aFeatureError = "Error: Resulting shape is not valid.";
285 setError(aFeatureError);
289 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
290 aResultBody->storeModified(aBaseShape, aTranslationAlgo->shape());
291 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
295 setResult(aResultBody, aResultIndex);
300 // Remove the rest results if there were produced in the previous pass.
301 removeResults(aResultIndex);
304 //=================================================================================================
305 void FeaturesPlugin_Translation::performTranslationByTwoPoints()
308 ListOfShape anObjects;
309 std::list<ResultPtr> aContextes;
310 AttributeSelectionListPtr anObjectsSelList =
311 selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
312 if (anObjectsSelList->size() == 0) {
315 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
316 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
317 anObjectsSelList->value(anObjectsIndex);
318 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
319 if(!anObject.get()) { // may be for not-activated parts
323 anObjects.push_back(anObject);
324 aContextes.push_back(anObjectAttr->context());
327 // Getting the start point and the end point
328 AttributeSelectionPtr aRef1 = data()->selection(FeaturesPlugin_Translation::START_POINT_ID());
329 AttributeSelectionPtr aRef2 = data()->selection(FeaturesPlugin_Translation::END_POINT_ID());
330 std::shared_ptr<GeomAPI_Pnt> aFirstPoint;
331 std::shared_ptr<GeomAPI_Pnt> aSecondPoint;
332 if ((aRef1.get() != NULL) && (aRef2.get() != NULL)) {
333 GeomShapePtr aShape1 = aRef1->value();
334 if (!aShape1.get()) //If we can't get the points directly, try getting them from the context
335 aShape1 = aRef1->context()->shape();
336 GeomShapePtr aShape2 = aRef2->value();
338 aShape2 = aRef2->context()->shape();
339 if (aShape1 && aShape2) {
340 aFirstPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
341 aSecondPoint = GeomAlgoAPI_PointBuilder::point(aShape2);
345 // Moving each object.
346 int aResultIndex = 0;
347 std::list<ResultPtr>::iterator aContext = aContextes.begin();
348 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
349 anObjectsIt++, aContext++) {
350 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
351 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
355 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
356 aTrsf->setTranslation(aFirstPoint, aSecondPoint);
357 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
358 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
359 aResultPart->setTrsf(*aContext, aTrsf);
360 setResult(aResultPart, aResultIndex);
362 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
363 new GeomAlgoAPI_Translation(aBaseShape, aFirstPoint, aSecondPoint));
365 if (!aTranslationAlgo->check()) {
366 setError(aTranslationAlgo->getError());
370 aTranslationAlgo->build();
372 // Checking that the algorithm worked properly.
373 if(!aTranslationAlgo->isDone()) {
374 static const std::string aFeatureError = "Error: Translation algorithm failed.";
375 setError(aFeatureError);
378 if(aTranslationAlgo->shape()->isNull()) {
379 static const std::string aShapeError = "Error: Resulting shape is Null.";
380 setError(aShapeError);
383 if(!aTranslationAlgo->isValid()) {
384 std::string aFeatureError = "Error: Resulting shape is not valid.";
385 setError(aFeatureError);
389 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
390 aResultBody->storeModified(aBaseShape, aTranslationAlgo->shape());
391 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
395 setResult(aResultBody, aResultIndex);
400 // Remove the rest results if there were produced in the previous pass.
401 removeResults(aResultIndex);