]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationPrs.cpp
Salome HOME
Improve multi-selector control to provide "Delete" key processing.
[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 #include <ModuleBase_IPropertyPanel.h>
17 #include <ModuleBase_ModelWidget.h>
18
19 #include <ModelAPI_Result.h>
20 #include <ModelAPI_Attribute.h>
21 #include <ModelAPI_AttributeRefAttr.h>
22 #include <ModelAPI_AttributeReference.h>
23 #include <ModelAPI_AttributeSelection.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_AttributeRefList.h>
26 #include <ModelAPI_Validator.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_ResultCompSolid.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
104   QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = myFeatureShapes.begin(),
105                                                         aLast = myFeatureShapes.end();
106   for (; anIt != aLast; anIt++) {
107     ObjectPtr anObject = anIt.key();
108     if (!isVisible(aDisplayer, anObject))
109       continue;
110     QList<GeomShapePtr> aShapes = anIt.value();
111     QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
112     for (; aShIt != aShLast; aShIt++) {
113       GeomShapePtr aGeomShape = *aShIt;
114       if (!aGeomShape.get())
115         continue;
116       TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
117       // change deviation coefficient to provide more precise circle
118       ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
119       StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
120     }
121   }
122
123   aColor = Quantity_Color(0., 1., 0., Quantity_TOC_RGB); // green
124   SetColor(aColor);
125
126   std::list<ResultPtr>::const_iterator aRIt = myFeatureResults.begin(),
127                                        aRLast = myFeatureResults.end();
128   for (; aRIt != aRLast; aRIt++) {
129     ResultPtr aResult = *aRIt;
130     if (!isVisible(aDisplayer, aResult))
131       continue;
132     GeomShapePtr aGeomShape = aResult->shape();
133     if (!aGeomShape.get())
134       continue;
135     TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
136     // change deviation coefficient to provide more precise circle
137     ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
138     StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
139   }
140   QList<ModuleBase_ViewerPrs> aValues;
141   ModuleBase_IPropertyPanel* aPanel = myWorkshop->propertyPanel();
142   if (aPanel) {
143     ModuleBase_ModelWidget* aWidget = aPanel->activeWidget();
144     if (aWidget) {
145       aWidget->getHighlighted(aValues);
146     }
147   }
148
149   Standard_Real aPreviousWidth = Width();
150   setWidth(aDrawer, aPreviousWidth+3);
151   Handle(AIS_InteractiveContext) aContext = GetContext();
152   Quantity_NameOfColor anHColor = aContext->HilightColor();
153
154   aColor = Quantity_Color(anHColor);
155   aColor = Quantity_Color((1. + aColor.Red())/2., (1. + aColor.Green())/2.,
156                           (1. + aColor.Blue())/2., Quantity_TOC_RGB);
157   SetColor(aColor);
158
159   QList<ModuleBase_ViewerPrs>::const_iterator anIIt = aValues.begin(),
160                                               aILast = aValues.end();
161   for (; anIIt != aILast; anIIt++) {
162     ModuleBase_ViewerPrs aPrs = *anIIt;
163     ObjectPtr anObject = aPrs.object();
164     TopoDS_Shape aShape = aPrs.shape();
165     if (aShape.IsNull()) {
166       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
167       if (aResult.get()) {
168         GeomShapePtr aGeomShape = aResult->shape();
169         if (aGeomShape.get())
170           aShape = aGeomShape->impl<TopoDS_Shape>();
171       }
172     }
173     if (!aShape.IsNull()) {
174       ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
175       StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
176     }
177   }
178   setWidth(aDrawer, aPreviousWidth);
179 }
180
181 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
182                                             const Standard_Integer aMode)
183 {
184   // the presentation is not used in the selection
185 }
186
187 bool PartSet_OperationPrs::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
188 {
189   bool aVisible = false;
190   GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
191   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
192   if (aPrs.get() || aResult.get())
193     aVisible = theDisplayer->isVisible(theObject);
194   else {
195     // check if all results of the feature are visible
196     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
197     std::list<ResultPtr> aResults = aFeature->results();
198     std::list<ResultPtr>::const_iterator aIt;
199     aVisible = !aResults.empty();
200     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
201       aVisible = aVisible && theDisplayer->isVisible(*aIt);
202     }
203   }
204   return aVisible;
205 }
206
207 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
208 {
209   bool isSub = false;
210   CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
211   if (aComposite.get())
212     isSub = aComposite->isSub(theObject);
213
214   return isSub;
215 }
216
217 void addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
218               const FeaturePtr& theFeature,
219               QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
220 {
221   if (theObject.get()) {
222     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
223     if (aResult.get()) {
224       ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
225       if (aCompsolidResult.get()) {
226         for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
227           ResultPtr aSubResult = aCompsolidResult->subResult(i);
228           if (aSubResult.get()) {
229             GeomShapePtr aShape;
230             addValue(aSubResult, aShape, theFeature, theObjectShapes);
231           }
232         }
233         return;
234       }
235     }
236
237
238     GeomShapePtr aShape = theShape;
239     if (!aShape.get()) {
240       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
241       if (aResult.get())
242         aShape = aResult->shape();
243     }
244     if (!isSubObject(theObject, theFeature)) {
245       if (theObjectShapes.contains(theObject))
246         theObjectShapes[theObject].append(aShape);
247       else {
248         QList<GeomShapePtr> aShapes;
249         aShapes.append(aShape);
250         theObjectShapes[theObject] = aShapes;
251       }
252     }
253   }
254 }
255
256 void PartSet_OperationPrs::getFeatureShapes(QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
257 {
258   if (!myFeature.get())
259     return;
260
261   ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
262
263   QList<GeomShapePtr> aShapes;
264   std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
265   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
266   for (; anIt != aLast; anIt++) {
267     AttributePtr anAttribute = *anIt;
268     if (!isSelectionAttribute(anAttribute))
269       continue;
270
271     if (!aValidators->isCase(myFeature, anAttribute->id()))
272       continue; // this attribute is not participated in the current case
273
274     std::string anAttrType = anAttribute->attributeType();
275
276     if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
277       std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList = 
278               std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
279       for(int i = 0; i < aCurSelList->size(); i++) {
280         std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
281         ResultPtr aResult = aSelAttribute->context();
282         GeomShapePtr aShape = aSelAttribute->value();
283         addValue(aResult, aShape, myFeature, theObjectShapes);
284       }
285     }
286     if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
287       std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
288         std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
289       for (int i = 0; i < aCurSelList->size(); i++) {
290         GeomShapePtr aShape;
291         addValue(aCurSelList->object(i), aShape, myFeature, theObjectShapes);
292       }
293     }
294     else {
295       ObjectPtr anObject;
296       GeomShapePtr aShape;
297       if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
298         AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
299         if (anAttr->isObject()) {
300           anObject = anAttr->object();
301         }
302         else {
303           aShape = PartSet_Tools::findShapeBy2DPoint(anAttr, myWorkshop);
304           // the distance point is not found if the point is selected in the 2nd time
305           // TODO: after debug, this check can be removed
306           if (!aShape.get())
307             continue;
308           anObject = anAttr->attr()->owner();
309         }
310       }
311       if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
312         AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
313         anObject = anAttr->context();
314         aShape = anAttr->value();
315       }
316       if (anAttrType == ModelAPI_AttributeReference::typeId()) {
317         AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
318         anObject = anAttr->value();
319       }
320       addValue(anObject, aShape, myFeature, theObjectShapes);
321     }
322   }
323 }
324
325 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
326 {
327   std::string anAttrType = theAttribute->attributeType();
328
329   /// workaround: start
330   /// this is a TEMPORARY correction in order to avoid crash by parameter edit
331   /// which is reproduced only on Linux Debian.
332   /// The possible reason is an empty AIS presentation visualized in the viewer.
333   /// The best solution is to analize whether the feature has shapes/results and only
334   /// in this case visualize custom presentation and hide it as soon as they disappear.
335   bool isAttributeArgument = theAttribute->isArgument();
336   if (!isAttributeArgument)
337     return false;
338   /// workaround: end
339
340   return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
341          anAttrType == ModelAPI_AttributeRefList::typeId() ||
342          anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
343          anAttrType == ModelAPI_AttributeSelection::typeId() ||
344          anAttrType == ModelAPI_AttributeReference::typeId();
345 }
346
347 XGUI_Workshop* PartSet_OperationPrs::workshop() const
348 {
349   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
350   return aConnector->workshop();
351 }