]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Placement.cpp
Salome HOME
Issue #2052: Modification of parameters don't work (sketch, extrusion)
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Placement.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        FeaturesPlugin_Placement.cpp
4 // Created:     2 Dec 2014
5 // Author:      Artem ZHIDKOV
6
7 #include "FeaturesPlugin_Placement.h"
8
9 #include <ModelAPI_ResultConstruction.h>
10 #include <ModelAPI_ResultBody.h>
11 #include <ModelAPI_ResultPart.h>
12 #include <ModelAPI_AttributeSelection.h>
13 #include <ModelAPI_AttributeBoolean.h>
14 #include <ModelAPI_AttributeSelectionList.h>
15 #include <ModelAPI_BodyBuilder.h>
16
17 #include <GeomAPI_Edge.h>
18 #include <GeomAPI_Face.h>
19 #include <GeomAPI_Pln.h>
20 #include <GeomAlgoAPI_Placement.h>
21 #include <GeomAlgoAPI_Transform.h>
22
23 #include <FeaturesPlugin_Tools.h>
24
25 FeaturesPlugin_Placement::FeaturesPlugin_Placement()
26 {
27 }
28
29 void FeaturesPlugin_Placement::initAttributes()
30 {
31
32   AttributeSelectionListPtr aSelection =
33     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
34     OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
35
36   data()->addAttribute(START_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
37   data()->addAttribute(END_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
38   data()->addAttribute(REVERSE_ID(), ModelAPI_AttributeBoolean::typeId());
39   data()->addAttribute(CENTERING_ID(), ModelAPI_AttributeBoolean::typeId());
40 }
41
42 void FeaturesPlugin_Placement::execute()
43 {
44   // Getting objects.
45   ListOfShape anObjects;
46   std::list<ResultPtr> aContextes;
47   AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECTS_LIST_ID());
48   if(anObjectsSelList->size() == 0) {
49     return;
50   }
51   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
52     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
53       anObjectsSelList->value(anObjectsIndex);
54     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
55     if(!anObject.get()) { // may be for not-activated parts
56       eraseResults();
57       return;
58     }
59     anObjects.push_back(anObject);
60     aContextes.push_back(anObjectAttr->context());
61   }
62
63   // Verify the start shape
64   AttributeSelectionPtr anObjRef = selection(START_SHAPE_ID());
65   if(!anObjRef) {
66     return;
67   }
68   std::shared_ptr<GeomAPI_Shape> aStartShape = anObjRef->value();
69   if(!aStartShape) {
70     static const std::string aSelectionError = "Error: The start shape selection is bad.";
71     setError(aSelectionError);
72     return;
73   }
74
75
76   std::shared_ptr<GeomAPI_Shape> aStartShapeContext;
77   ResultPtr aContextRes = anObjRef->context();
78   if (aContextRes.get()) {
79     aStartShapeContext = aContextRes->shape();
80   }
81   if(!aStartShapeContext.get()) {
82     static const std::string aContextError = "Error: The start shape selection context is bad.";
83     setError(aContextError);
84     return;
85   }
86
87   // Verify the end shape
88   anObjRef = selection(END_SHAPE_ID());
89   std::shared_ptr<GeomAPI_Shape> anEndShape = anObjRef->value();
90   if(!anEndShape) {
91     static const std::string aSelectionError = "Error: The end shape selection is bad.";
92     setError(aSelectionError);
93     return;
94   }
95
96   std::shared_ptr<GeomAPI_Shape> anEndShapeContext;
97   aContextRes = anObjRef->context();
98   if(aContextRes.get()) {
99     anEndShapeContext = aContextRes->shape();
100   }
101   if(!anEndShapeContext.get()) {
102     static const std::string aContextError = "Error: The end shape selection context is bad.";
103     setError(aContextError);
104     return;
105   }
106
107   // Verify planarity of faces and linearity of edges
108   std::shared_ptr<GeomAPI_Shape> aShapes[2] = {aStartShape, anEndShape};
109   for (int i = 0; i < 2; i++) {
110     if (aShapes[i]->isFace()) {
111       std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShapes[i]));
112       if (!aFace->isPlanar()) {
113         static const std::string aPlanarityError = "Error: One of selected faces is not planar.";
114         setError(aPlanarityError);
115         return;
116       }
117     }
118     else if (aShapes[i]->isEdge()) {
119       std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShapes[i]));
120       if (!anEdge->isLine()) {
121         static const std::string aLinearityError = "Error: One of selected endges is not linear.";
122         setError(aLinearityError);
123         return;
124       }
125     }
126   }
127
128   // Flags of the Placement
129   bool isReverse = boolean(REVERSE_ID())->value();
130   bool isCentering = boolean(CENTERING_ID())->value();
131
132   // Getting transformation.
133   GeomAlgoAPI_Placement aPlacementAlgo(
134     aStartShapeContext, anEndShapeContext, aStartShape, anEndShape, isReverse, isCentering, true);
135   if(!aPlacementAlgo.isDone()) {
136     static const std::string aFeatureError = "Error: Placement algorithm failed.";
137     setError(aFeatureError);
138     return;
139   }
140   std::shared_ptr<GeomAPI_Trsf> aTrsf = aPlacementAlgo.transformation();
141
142   // Applying transformation to each object.
143   int aResultIndex = 0;
144   std::list<ResultPtr>::iterator aContext = aContextes.begin();
145   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
146       anObjectsIt++, aContext++) {
147
148     // for part results just set transformation
149     if ((*aContext)->groupName() == ModelAPI_ResultPart::group()) {
150       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
151       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
152       aResultPart->setTrsf(aContextRes, aTrsf);
153       setResult(aResultPart, aResultIndex);
154     } else {
155       std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
156       GeomAlgoAPI_Transform aTransformAlgo(aBaseShape, aTrsf);
157
158       // Checking that the algorithm worked properly.
159       if(!aTransformAlgo.isDone()) {
160         static const std::string aFeatureError = "Error: Transform algorithm failed.";
161         setError(aFeatureError);
162         break;
163       }
164       if(aTransformAlgo.shape()->isNull()) {
165         static const std::string aShapeError = "Error: Resulting shape is Null.";
166         setError(aShapeError);
167         break;
168       }
169       if(!aTransformAlgo.isValid()) {
170         std::string aFeatureError = "Error: Resulting shape is not valid.";
171         setError(aFeatureError);
172         break;
173       }
174
175       //LoadNamingDS
176       std::shared_ptr<ModelAPI_ResultBody> aResultBody =
177         document()->createBody(data(), aResultIndex);
178       loadNamingDS(aTransformAlgo, aResultBody, aBaseShape);
179       setResult(aResultBody, aResultIndex);
180     }
181     aResultIndex++;
182   }
183
184   // Remove the rest results if there were produced in the previous pass.
185   removeResults(aResultIndex);
186 }
187
188 //============================================================================
189 void FeaturesPlugin_Placement::loadNamingDS(GeomAlgoAPI_Transform& theTransformAlgo,
190                                             std::shared_ptr<ModelAPI_ResultBody> theResultBody,
191                                             std::shared_ptr<GeomAPI_Shape> theBaseShape)
192 {
193   //load result
194   theResultBody->storeModified(theBaseShape, theTransformAlgo.shape());
195
196   std::string aPlacedName = "Placed";
197   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theTransformAlgo.mapOfSubShapes();
198
199   FeaturesPlugin_Tools::storeModifiedShapes(theTransformAlgo, theResultBody,
200                                             theBaseShape, 1, 2, 3, aPlacedName,
201                                             *aSubShapes.get());
202 }