]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Scale.cpp
Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Scale.cpp
1 // Copyright (C) 2014-201x CEA/DEN, EDF R&D
2
3 // File:        FeaturesPlugin_Scale.cpp
4 // Created:     13 Jan 2017
5 // Author:      Clarisse Genrault (CEA)
6
7 #include <FeaturesPlugin_Scale.h>
8
9 #include <GeomAlgoAPI_PointBuilder.h>
10
11 #include <ModelAPI_AttributeDouble.h>
12 #include <ModelAPI_AttributeSelectionList.h>
13 #include <ModelAPI_AttributeString.h>
14 #include <ModelAPI_ResultBody.h>
15 #include <ModelAPI_ResultPart.h>
16
17 #include <iostream>
18
19 //=================================================================================================
20 FeaturesPlugin_Scale::FeaturesPlugin_Scale()
21 {
22 }
23
24 //=================================================================================================
25 void FeaturesPlugin_Scale::initAttributes()
26 {
27   AttributeSelectionListPtr aSelection =
28     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
29     FeaturesPlugin_Scale::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
30
31   data()->addAttribute(FeaturesPlugin_Scale::CENTER_POINT_ID(),
32                        ModelAPI_AttributeSelection::typeId());
33
34   data()->addAttribute(FeaturesPlugin_Scale::SCALE_FACTOR_ID(),
35                        ModelAPI_AttributeDouble::typeId());
36 }
37
38 //=================================================================================================
39 void FeaturesPlugin_Scale::execute()
40 {
41   // Getting objects.
42   ListOfShape anObjects;
43   std::list<ResultPtr> aContextes;
44   AttributeSelectionListPtr anObjectsSelList =
45     selectionList(FeaturesPlugin_Scale::OBJECTS_LIST_ID());
46   if (anObjectsSelList->size() == 0) {
47     return;
48   }
49   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
50     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
51       anObjectsSelList->value(anObjectsIndex);
52     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
53     if(!anObject.get()) { // may be for not-activated parts
54       eraseResults();
55       return;
56     }
57     anObjects.push_back(anObject);
58     aContextes.push_back(anObjectAttr->context());
59   }
60
61   // Getting the center point
62   std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
63   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
64     selection(FeaturesPlugin_Scale::CENTER_POINT_ID());
65   if (anObjRef.get() != NULL) {
66     GeomShapePtr aShape = anObjRef->value();
67     if (!aShape.get()) {
68       aShape = anObjRef->context()->shape();
69     }
70     if (aShape) {
71       aCenterPoint = GeomAlgoAPI_PointBuilder::point(aShape);
72     }
73   }
74
75   // Getting scale factor
76   double aScaleFactor = real(FeaturesPlugin_Scale::SCALE_FACTOR_ID())->value();
77
78   // Moving each object.
79   int aResultIndex = 0;
80   std::list<ResultPtr>::iterator aContext = aContextes.begin();
81   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
82         anObjectsIt++, aContext++) {
83     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
84     bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
85
86     // Setting result.
87     if (isPart) {
88       std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
89       aTrsf->setScale(aCenterPoint, aScaleFactor);
90       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
91       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
92       aResultPart->setTrsf(*aContext, aTrsf);
93       setResult(aResultPart, aResultIndex);
94     } else {
95       GeomAlgoAPI_Scale aScaleAlgo(aBaseShape, aCenterPoint, aScaleFactor);
96
97       if (!aScaleAlgo.check()) {
98         setError(aScaleAlgo.getError());
99         return;
100       }
101
102       aScaleAlgo.build();
103
104       // Checking that the algorithm worked properly.
105       if(!aScaleAlgo.isDone()) {
106         static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
107         setError(aFeatureError);
108         break;
109       }
110       if(aScaleAlgo.shape()->isNull()) {
111         static const std::string aShapeError = "Error: Resulting shape is Null.";
112         setError(aShapeError);
113         break;
114       }
115       if(!aScaleAlgo.isValid()) {
116         std::string aFeatureError = "Error: Resulting shape is not valid.";
117         setError(aFeatureError);
118         break;
119       }
120
121       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
122       loadNamingDS(aScaleAlgo, aResultBody, aBaseShape);
123       setResult(aResultBody, aResultIndex);
124     }
125     aResultIndex++;
126   }
127
128   // Remove the rest results if there were produced in the previous pass.
129   removeResults(aResultIndex);
130 }
131
132 //=================================================================================================
133 void FeaturesPlugin_Scale::loadNamingDS(GeomAlgoAPI_Scale& theScaleAlgo,
134                                         std::shared_ptr<ModelAPI_ResultBody> theResultBody,
135                                         std::shared_ptr<GeomAPI_Shape> theBaseShape)
136 {
137   // Store and name the result.
138   theResultBody->storeModified(theBaseShape, theScaleAlgo.shape());
139
140   // Name the faces
141   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theScaleAlgo.mapOfSubShapes();
142   int aReflectedTag = 1;
143   std::string aReflectedName = "Scaled";
144   theResultBody->loadAndOrientModifiedShapes(&theScaleAlgo,
145                                               theBaseShape, GeomAPI_Shape::FACE,
146                                               aReflectedTag, aReflectedName, *aSubShapes.get());
147 }