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
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();
140 setError(aSelectionError);
146 setError(aSelectionError);
150 std::shared_ptr<GeomAPI_Ax1> anAxis(new GeomAPI_Ax1(anEdge->line()->location(),
151 anEdge->line()->direction()));
155 double aDistance = real(FeaturesPlugin_Translation::DISTANCE_ID())->value();
157 // Moving each object.
158 int aResultIndex = 0;
159 std::list<ResultPtr>::iterator aContext = aContextes.begin();
160 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
161 anObjectsIt++, aContext++) {
162 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
163 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
167 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
168 aTrsf->setTranslation(anAxis, aDistance);
169 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
170 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
171 aResultPart->setTrsf(*aContext, aTrsf);
172 setResult(aResultPart, aResultIndex);
174 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
175 new GeomAlgoAPI_Translation(aBaseShape, anAxis, aDistance));
177 if (!aTranslationAlgo->check()) {
178 setError(aTranslationAlgo->getError());
182 aTranslationAlgo->build();
184 // Checking that the algorithm worked properly.
185 if(!aTranslationAlgo->isDone()) {
186 static const std::string aFeatureError = "Error: Translation algorithm failed.";
187 setError(aFeatureError);
190 if(aTranslationAlgo->shape()->isNull()) {
191 static const std::string aShapeError = "Error: Resulting shape is Null.";
192 setError(aShapeError);
195 if(!aTranslationAlgo->isValid()) {
196 std::string aFeatureError = "Error: Resulting shape is not valid.";
197 setError(aFeatureError);
201 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
202 aResultBody->storeModified(aBaseShape, aTranslationAlgo->shape());
203 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
207 setResult(aResultBody, aResultIndex);
212 // Remove the rest results if there were produced in the previous pass.
213 removeResults(aResultIndex);
216 //=================================================================================================
217 void FeaturesPlugin_Translation::performTranslationByDimensions()
220 ListOfShape anObjects;
221 std::list<ResultPtr> aContextes;
222 AttributeSelectionListPtr anObjectsSelList =
223 selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
224 if (anObjectsSelList->size() == 0) {
227 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
228 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
229 anObjectsSelList->value(anObjectsIndex);
230 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
231 if(!anObject.get()) { // may be for not-activated parts
234 anObjects.push_back(anObject);
235 aContextes.push_back(anObjectAttr->context());
238 // Getting dimensions in X, in Y and in Z
239 double aDX = real(FeaturesPlugin_Translation::DX_ID())->value();
240 double aDY = real(FeaturesPlugin_Translation::DY_ID())->value();
241 double aDZ = real(FeaturesPlugin_Translation::DZ_ID())->value();
243 // Moving each object.
244 int aResultIndex = 0;
245 std::list<ResultPtr>::iterator aContext = aContextes.begin();
246 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
247 anObjectsIt++, aContext++) {
248 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
249 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
253 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
254 aTrsf->setTranslation(aDX, aDY, aDZ);
255 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
256 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
257 aResultPart->setTrsf(*aContext, aTrsf);
258 setResult(aResultPart, aResultIndex);
260 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
261 new GeomAlgoAPI_Translation(aBaseShape, aDX, aDY, aDZ));
263 if (!aTranslationAlgo->check()) {
264 setError(aTranslationAlgo->getError());
268 aTranslationAlgo->build();
270 // Checking that the algorithm worked properly.
271 if(!aTranslationAlgo->isDone()) {
272 static const std::string aFeatureError = "Error: Translation algorithm failed.";
273 setError(aFeatureError);
276 if(aTranslationAlgo->shape()->isNull()) {
277 static const std::string aShapeError = "Error: Resulting shape is Null.";
278 setError(aShapeError);
281 if(!aTranslationAlgo->isValid()) {
282 std::string aFeatureError = "Error: Resulting shape is not valid.";
283 setError(aFeatureError);
287 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
288 aResultBody->storeModified(aBaseShape, aTranslationAlgo->shape());
289 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
293 setResult(aResultBody, aResultIndex);
298 // Remove the rest results if there were produced in the previous pass.
299 removeResults(aResultIndex);
302 //=================================================================================================
303 void FeaturesPlugin_Translation::performTranslationByTwoPoints()
306 ListOfShape anObjects;
307 std::list<ResultPtr> aContextes;
308 AttributeSelectionListPtr anObjectsSelList =
309 selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
310 if (anObjectsSelList->size() == 0) {
313 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
314 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
315 anObjectsSelList->value(anObjectsIndex);
316 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
317 if(!anObject.get()) { // may be for not-activated parts
320 anObjects.push_back(anObject);
321 aContextes.push_back(anObjectAttr->context());
324 // Getting the start point and the end point
325 AttributeSelectionPtr aRef1 = data()->selection(FeaturesPlugin_Translation::START_POINT_ID());
326 AttributeSelectionPtr aRef2 = data()->selection(FeaturesPlugin_Translation::END_POINT_ID());
327 std::shared_ptr<GeomAPI_Pnt> aFirstPoint;
328 std::shared_ptr<GeomAPI_Pnt> aSecondPoint;
329 if ((aRef1.get() != NULL) && (aRef2.get() != NULL)) {
330 GeomShapePtr aShape1 = aRef1->value();
331 if (!aShape1.get()) //If we can't get the points directly, try getting them from the context
332 aShape1 = aRef1->context()->shape();
333 GeomShapePtr aShape2 = aRef2->value();
335 aShape2 = aRef2->context()->shape();
336 if (aShape1 && aShape2) {
337 aFirstPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
338 aSecondPoint = GeomAlgoAPI_PointBuilder::point(aShape2);
342 // Moving each object.
343 int aResultIndex = 0;
344 std::list<ResultPtr>::iterator aContext = aContextes.begin();
345 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
346 anObjectsIt++, aContext++) {
347 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
348 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
352 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
353 aTrsf->setTranslation(aFirstPoint, aSecondPoint);
354 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
355 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
356 aResultPart->setTrsf(*aContext, aTrsf);
357 setResult(aResultPart, aResultIndex);
359 std::shared_ptr<GeomAlgoAPI_Translation> aTranslationAlgo(
360 new GeomAlgoAPI_Translation(aBaseShape, aFirstPoint, aSecondPoint));
362 if (!aTranslationAlgo->check()) {
363 setError(aTranslationAlgo->getError());
367 aTranslationAlgo->build();
369 // Checking that the algorithm worked properly.
370 if(!aTranslationAlgo->isDone()) {
371 static const std::string aFeatureError = "Error: Translation algorithm failed.";
372 setError(aFeatureError);
375 if(aTranslationAlgo->shape()->isNull()) {
376 static const std::string aShapeError = "Error: Resulting shape is Null.";
377 setError(aShapeError);
380 if(!aTranslationAlgo->isValid()) {
381 std::string aFeatureError = "Error: Resulting shape is not valid.";
382 setError(aFeatureError);
386 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
387 aResultBody->storeModified(aBaseShape, aTranslationAlgo->shape());
388 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
392 setResult(aResultBody, aResultIndex);
397 // Remove the rest results if there were produced in the previous pass.
398 removeResults(aResultIndex);