Salome HOME
Fix for solids color changing after sketch modification
[modules/shaper.git] / src / PartSet / PartSet_OperationPrs.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_OperationPrs.cpp
4 // Created:     01 Jul 2015
5 // Author:      Natalia ERMOLAEVA
6
7 #include "PartSet_OperationPrs.h"
8 #include "PartSet_Tools.h"
9
10 #include "XGUI_Workshop.h"
11 #include "XGUI_ModuleConnector.h"
12 #include "XGUI_Displayer.h"
13
14 #include "ModuleBase_Tools.h"
15 #include "ModuleBase_IModule.h"
16
17 #include <ModelAPI_Result.h>
18 #include <ModelAPI_Attribute.h>
19 #include <ModelAPI_AttributeRefAttr.h>
20 #include <ModelAPI_AttributeReference.h>
21 #include <ModelAPI_AttributeSelection.h>
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_AttributeRefList.h>
24 #include <ModelAPI_Validator.h>
25 #include <ModelAPI_Session.h>
26 #include <ModelAPI_ResultCompSolid.h>
27
28 #include <GeomValidators_Tools.h>
29
30 #include <GeomAPI_IPresentable.h>
31
32 #include <StdPrs_WFDeflectionShape.hxx>
33
34 #include <QList>
35
36 IMPLEMENT_STANDARD_HANDLE(PartSet_OperationPrs, ViewerData_AISShape);
37 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
38
39 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
40   : ViewerData_AISShape(TopoDS_Shape()), myFeature(FeaturePtr()), myWorkshop(theWorkshop)
41 {
42   myShapeColor = ModuleBase_Tools::color("Visualization", "construction_plane_color", "1,1,0");
43   myResultColor = ModuleBase_Tools::color("Visualization", "construction_plane_color", "0,1,0");
44 }
45
46 bool PartSet_OperationPrs::canActivate(const FeaturePtr& theFeature)
47 {
48   bool aHasSelectionAttribute = false;
49
50   std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
51   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
52   for (; anIt != aLast && !aHasSelectionAttribute; anIt++)
53     aHasSelectionAttribute = isSelectionAttribute(*anIt);
54
55   return aHasSelectionAttribute;
56 }
57
58 void PartSet_OperationPrs::setFeature(const FeaturePtr& theFeature)
59 {
60   myFeature = theFeature;
61   updateShapes();
62 }
63
64 /*bool PartSet_OperationPrs::dependOn(const ObjectPtr& theResult)
65 {
66   return myFeatureShapes.contains(theResult);
67 }*/
68
69 void PartSet_OperationPrs::updateShapes()
70 {
71   myFeatureShapes.clear();
72   getFeatureShapes(myFeatureShapes);
73
74   myFeatureResults.clear();
75   if (myFeature)
76     myFeatureResults = myFeature->results();
77 }
78
79 bool PartSet_OperationPrs::hasShapes()
80 {
81   return !myFeatureShapes.empty() || !myFeatureResults.empty();
82 }
83
84 void PartSet_OperationPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
85                                    const Handle(Prs3d_Presentation)& thePresentation, 
86                                    const Standard_Integer theMode)
87 {
88   if (!hasShapes())
89     return;
90   // when the feature can not be visualized in the module, the operation preview should not
91   // be visualized also
92   if (!myWorkshop->module()->canDisplayObject(myFeature))
93     return;
94
95   Quantity_Color aColor(1., 1., 0., Quantity_TOC_RGB); // yellow
96   SetColor(aColor);
97
98   thePresentation->Clear();
99   XGUI_Displayer* aDisplayer = workshop()->displayer();
100
101   // create presentations on the base of the shapes
102   Handle(Prs3d_Drawer) aDrawer = Attributes();
103   QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = myFeatureShapes.begin(),
104                                                         aLast = myFeatureShapes.end();
105   for (; anIt != aLast; anIt++) {
106     ObjectPtr anObject = anIt.key();
107     if (!isVisible(aDisplayer, anObject))
108       continue;
109     QList<GeomShapePtr> aShapes = anIt.value();
110     QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
111     for (; aShIt != aShLast; aShIt++) {
112       GeomShapePtr aGeomShape = *aShIt;
113       if (!aGeomShape.get())
114         continue;
115       TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
116       // change deviation coefficient to provide more precise circle
117       ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
118       StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
119     }
120   }
121
122   aColor = Quantity_Color(0., 1., 0., Quantity_TOC_RGB); // green
123   SetColor(aColor);
124
125   std::list<ResultPtr>::const_iterator aRIt = myFeatureResults.begin(),
126                                        aRLast = myFeatureResults.end();
127   for (; aRIt != aRLast; aRIt++) {
128     ResultPtr aResult = *aRIt;
129     if (!isVisible(aDisplayer, aResult))
130       continue;
131     GeomShapePtr aGeomShape = aResult->shape();
132     if (!aGeomShape.get())
133       continue;
134     TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
135     // change deviation coefficient to provide more precise circle
136     ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
137     StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
138   }
139 }
140
141 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
142                                             const Standard_Integer aMode)
143 {
144   // the presentation is not used in the selection
145 }
146
147 bool PartSet_OperationPrs::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
148 {
149   bool aVisible = false;
150   GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
151   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
152   if (aPrs.get() || aResult.get())
153     aVisible = theDisplayer->isVisible(theObject);
154   else {
155     // check if all results of the feature are visible
156     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
157     std::list<ResultPtr> aResults = aFeature->results();
158     std::list<ResultPtr>::const_iterator aIt;
159     aVisible = !aResults.empty();
160     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
161       aVisible = aVisible && theDisplayer->isVisible(*aIt);
162     }
163   }
164   return aVisible;
165 }
166
167 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
168 {
169   bool isSub = false;
170   CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
171   if (aComposite.get())
172     isSub = aComposite->isSub(theObject);
173
174   return isSub;
175 }
176
177 void addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
178               const FeaturePtr& theFeature,
179               QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
180 {
181   if (theObject.get()) {
182     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
183     if (aResult.get()) {
184       ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
185       if (aCompsolidResult.get()) {
186         for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
187           ResultPtr aSubResult = aCompsolidResult->subResult(i);
188           if (aSubResult.get()) {
189             GeomShapePtr aShape;
190             addValue(aSubResult, aShape, theFeature, theObjectShapes);
191           }
192         }
193         return;
194       }
195     }
196
197
198     GeomShapePtr aShape = theShape;
199     if (!aShape.get()) {
200       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
201       if (aResult.get())
202         aShape = aResult->shape();
203     }
204     if (!isSubObject(theObject, theFeature)) {
205       if (theObjectShapes.contains(theObject))
206         theObjectShapes[theObject].append(aShape);
207       else {
208         QList<GeomShapePtr> aShapes;
209         aShapes.append(aShape);
210         theObjectShapes[theObject] = aShapes;
211       }
212     }
213   }
214 }
215
216 void PartSet_OperationPrs::getFeatureShapes(QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
217 {
218   if (!myFeature.get())
219     return;
220
221   ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
222
223   QList<GeomShapePtr> aShapes;
224   std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
225   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
226   for (; anIt != aLast; anIt++) {
227     AttributePtr anAttribute = *anIt;
228     if (!isSelectionAttribute(anAttribute))
229       continue;
230
231     if (!aValidators->isCase(myFeature, anAttribute->id()))
232       continue; // this attribute is not participated in the current case
233
234     std::string anAttrType = anAttribute->attributeType();
235
236     if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
237       std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList = 
238               std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
239       for(int i = 0; i < aCurSelList->size(); i++) {
240         std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
241         ResultPtr aResult = aSelAttribute->context();
242         GeomShapePtr aShape = aSelAttribute->value();
243         addValue(aResult, aShape, myFeature, theObjectShapes);
244       }
245     }
246     if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
247       std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
248         std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
249       for (int i = 0; i < aCurSelList->size(); i++) {
250         GeomShapePtr aShape;
251         addValue(aCurSelList->object(i), aShape, myFeature, theObjectShapes);
252       }
253     }
254     else {
255       ObjectPtr anObject;
256       GeomShapePtr aShape;
257       if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
258         AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
259         if (anAttr->isObject()) {
260           anObject = anAttr->object();
261         }
262         else {
263           aShape = PartSet_Tools::findShapeBy2DPoint(anAttr, myWorkshop);
264           // the distance point is not found if the point is selected in the 2nd time
265           // TODO: after debug, this check can be removed
266           if (!aShape.get())
267             continue;
268           anObject = anAttr->attr()->owner();
269         }
270       }
271       if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
272         AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
273         anObject = anAttr->context();
274         aShape = anAttr->value();
275       }
276       if (anAttrType == ModelAPI_AttributeReference::typeId()) {
277         AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
278         anObject = anAttr->value();
279       }
280       addValue(anObject, aShape, myFeature, theObjectShapes);
281     }
282   }
283 }
284
285 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
286 {
287   std::string anAttrType = theAttribute->attributeType();
288
289   return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
290          anAttrType == ModelAPI_AttributeRefList::typeId() ||
291          anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
292          anAttrType == ModelAPI_AttributeSelection::typeId() ||
293          anAttrType == ModelAPI_AttributeReference::typeId();
294 }
295
296 XGUI_Workshop* PartSet_OperationPrs::workshop() const
297 {
298   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
299   return aConnector->workshop();
300 }