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 GeomAlgoAPI_Translation aTranslationAlgo(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 loadNamingDS(aTranslationAlgo, aResultBody, aBaseShape);
203 setResult(aResultBody, aResultIndex);
208 // Remove the rest results if there were produced in the previous pass.
209 removeResults(aResultIndex);
212 //=================================================================================================
213 void FeaturesPlugin_Translation::performTranslationByDimensions()
216 ListOfShape anObjects;
217 std::list<ResultPtr> aContextes;
218 AttributeSelectionListPtr anObjectsSelList =
219 selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
220 if (anObjectsSelList->size() == 0) {
223 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
224 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
225 anObjectsSelList->value(anObjectsIndex);
226 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
227 if(!anObject.get()) { // may be for not-activated parts
231 anObjects.push_back(anObject);
232 aContextes.push_back(anObjectAttr->context());
235 // Getting dimensions in X, in Y and in Z
236 double aDX = real(FeaturesPlugin_Translation::DX_ID())->value();
237 double aDY = real(FeaturesPlugin_Translation::DY_ID())->value();
238 double aDZ = real(FeaturesPlugin_Translation::DZ_ID())->value();
240 // Moving each object.
241 int aResultIndex = 0;
242 std::list<ResultPtr>::iterator aContext = aContextes.begin();
243 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
244 anObjectsIt++, aContext++) {
245 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
246 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
250 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
251 aTrsf->setTranslation(aDX, aDY, aDZ);
252 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
253 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
254 aResultPart->setTrsf(*aContext, aTrsf);
255 setResult(aResultPart, aResultIndex);
257 GeomAlgoAPI_Translation aTranslationAlgo(aBaseShape, aDX, aDY, aDZ);
259 if (!aTranslationAlgo.check()) {
260 setError(aTranslationAlgo.getError());
264 aTranslationAlgo.build();
266 // Checking that the algorithm worked properly.
267 if(!aTranslationAlgo.isDone()) {
268 static const std::string aFeatureError = "Error: Translation algorithm failed.";
269 setError(aFeatureError);
272 if(aTranslationAlgo.shape()->isNull()) {
273 static const std::string aShapeError = "Error: Resulting shape is Null.";
274 setError(aShapeError);
277 if(!aTranslationAlgo.isValid()) {
278 std::string aFeatureError = "Error: Resulting shape is not valid.";
279 setError(aFeatureError);
283 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
284 loadNamingDS(aTranslationAlgo, aResultBody, aBaseShape);
285 setResult(aResultBody, aResultIndex);
290 // Remove the rest results if there were produced in the previous pass.
291 removeResults(aResultIndex);
294 //=================================================================================================
295 void FeaturesPlugin_Translation::performTranslationByTwoPoints()
298 ListOfShape anObjects;
299 std::list<ResultPtr> aContextes;
300 AttributeSelectionListPtr anObjectsSelList =
301 selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
302 if (anObjectsSelList->size() == 0) {
305 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
306 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
307 anObjectsSelList->value(anObjectsIndex);
308 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
309 if(!anObject.get()) { // may be for not-activated parts
313 anObjects.push_back(anObject);
314 aContextes.push_back(anObjectAttr->context());
317 // Getting the start point and the end point
318 AttributeSelectionPtr aRef1 = data()->selection(FeaturesPlugin_Translation::START_POINT_ID());
319 AttributeSelectionPtr aRef2 = data()->selection(FeaturesPlugin_Translation::END_POINT_ID());
320 std::shared_ptr<GeomAPI_Pnt> aFirstPoint;
321 std::shared_ptr<GeomAPI_Pnt> aSecondPoint;
322 if ((aRef1.get() != NULL) && (aRef2.get() != NULL)) {
323 GeomShapePtr aShape1 = aRef1->value();
324 if (!aShape1.get()) //If we can't get the points directly, try getting them from the context
325 aShape1 = aRef1->context()->shape();
326 GeomShapePtr aShape2 = aRef2->value();
328 aShape2 = aRef2->context()->shape();
329 if (aShape1 && aShape2) {
330 aFirstPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
331 aSecondPoint = GeomAlgoAPI_PointBuilder::point(aShape2);
335 // Moving each object.
336 int aResultIndex = 0;
337 std::list<ResultPtr>::iterator aContext = aContextes.begin();
338 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
339 anObjectsIt++, aContext++) {
340 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
341 bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group();
345 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
346 aTrsf->setTranslation(aFirstPoint, aSecondPoint);
347 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
348 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
349 aResultPart->setTrsf(*aContext, aTrsf);
350 setResult(aResultPart, aResultIndex);
352 GeomAlgoAPI_Translation aTranslationAlgo(aBaseShape, aFirstPoint, aSecondPoint);
354 if (!aTranslationAlgo.check()) {
355 setError(aTranslationAlgo.getError());
359 aTranslationAlgo.build();
361 // Checking that the algorithm worked properly.
362 if(!aTranslationAlgo.isDone()) {
363 static const std::string aFeatureError = "Error: Translation algorithm failed.";
364 setError(aFeatureError);
367 if(aTranslationAlgo.shape()->isNull()) {
368 static const std::string aShapeError = "Error: Resulting shape is Null.";
369 setError(aShapeError);
372 if(!aTranslationAlgo.isValid()) {
373 std::string aFeatureError = "Error: Resulting shape is not valid.";
374 setError(aFeatureError);
378 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
379 loadNamingDS(aTranslationAlgo, aResultBody, aBaseShape);
380 setResult(aResultBody, aResultIndex);
385 // Remove the rest results if there were produced in the previous pass.
386 removeResults(aResultIndex);
389 //=================================================================================================
390 void FeaturesPlugin_Translation::loadNamingDS(GeomAlgoAPI_Translation& theTranslationAlgo,
391 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
392 std::shared_ptr<GeomAPI_Shape> theBaseShape)
395 theResultBody->storeModified(theBaseShape, theTranslationAlgo.shape());
397 std::string aTranslatedName = "Translated";
398 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theTranslationAlgo.mapOfSubShapes();
400 FeaturesPlugin_Tools::storeModifiedShapes(theTranslationAlgo, theResultBody,
401 theBaseShape, 1, 2, 3, aTranslatedName,