Salome HOME
Imrove multi-selector control to provide items multi-selection. Preferences color...
[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 static const int AIS_DEFAULT_WIDTH = 2;
37
38 IMPLEMENT_STANDARD_HANDLE(PartSet_OperationPrs, ViewerData_AISShape);
39 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
40
41 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
42 : ViewerData_AISShape(TopoDS_Shape()), myWorkshop(theWorkshop), myUseAISWidth(false)
43 {
44   myShapeColor = Quantity_Color(1, 1, 1, Quantity_TOC_RGB);
45 }
46
47 bool PartSet_OperationPrs::hasShapes()
48 {
49   return !myFeatureShapes.empty();
50 }
51
52 void PartSet_OperationPrs::setShapeColor(const Quantity_Color& theColor)
53 {
54   myShapeColor = theColor;
55 }
56
57 void PartSet_OperationPrs::useAISWidth()
58 {
59   myUseAISWidth = true;
60 }
61
62 void PartSet_OperationPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
63                                    const Handle(Prs3d_Presentation)& thePresentation, 
64                                    const Standard_Integer theMode)
65 {
66   SetColor(myShapeColor);
67   thePresentation->Clear();
68
69   XGUI_Displayer* aDisplayer = workshop(myWorkshop)->displayer();
70   Handle(Prs3d_Drawer) aDrawer = Attributes();
71
72   // create presentations on the base of the shapes
73   QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = myFeatureShapes.begin(),
74                                                         aLast = myFeatureShapes.end();
75   for (; anIt != aLast; anIt++) {
76     ObjectPtr anObject = anIt.key();
77     if (!isVisible(aDisplayer, anObject))
78       continue;
79     QList<GeomShapePtr> aShapes = anIt.value();
80     QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
81     for (; aShIt != aShLast; aShIt++) {
82       GeomShapePtr aGeomShape = *aShIt;
83       if (!aGeomShape.get())
84         continue;
85       TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
86       // change deviation coefficient to provide more precise circle
87       ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
88
89       if (myUseAISWidth) {
90         AISObjectPtr anAISPtr = aDisplayer->getAISObject(anObject);
91         if (anAISPtr.get()) {
92           Handle(AIS_InteractiveObject) anIO = anAISPtr->impl<Handle(AIS_InteractiveObject)>();
93           if (!anIO.IsNull()) {
94             int aWidth = anIO->Width();
95             /// workaround for zero width. Else, there will be a crash
96             if (aWidth == 0) { // width returns of TSolid shape is zero
97               bool isDisplayed = !anIO->GetContext().IsNull();
98               aWidth = AIS_DEFAULT_WIDTH;// default width value
99             }
100             setWidth(aDrawer, aWidth);
101           }
102         }
103       }
104       StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
105     }
106   }
107 }
108
109 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
110                                             const Standard_Integer aMode)
111 {
112   // the presentation is not used in the selection
113 }
114
115 bool PartSet_OperationPrs::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
116 {
117   bool aVisible = false;
118   GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
119   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
120   if (aPrs.get() || aResult.get())
121     aVisible = theDisplayer->isVisible(theObject);
122   else {
123     // check if all results of the feature are visible
124     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
125     std::list<ResultPtr> aResults = aFeature->results();
126     std::list<ResultPtr>::const_iterator aIt;
127     aVisible = !aResults.empty();
128     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
129       aVisible = aVisible && theDisplayer->isVisible(*aIt);
130     }
131   }
132   return aVisible;
133 }
134
135 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
136 {
137   bool isSub = false;
138   CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
139   if (aComposite.get())
140     isSub = aComposite->isSub(theObject);
141
142   return isSub;
143 }
144
145 void addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
146               const FeaturePtr& theFeature,
147               QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
148 {
149   if (theObject.get()) {
150     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
151     if (aResult.get()) {
152       ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
153       if (aCompsolidResult.get()) {
154         for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
155           ResultPtr aSubResult = aCompsolidResult->subResult(i);
156           if (aSubResult.get()) {
157             GeomShapePtr aShape;
158             addValue(aSubResult, aShape, theFeature, theObjectShapes);
159           }
160         }
161         return;
162       }
163     }
164
165
166     GeomShapePtr aShape = theShape;
167     if (!aShape.get()) {
168       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
169       if (aResult.get())
170         aShape = aResult->shape();
171     }
172     if (!isSubObject(theObject, theFeature)) {
173       if (theObjectShapes.contains(theObject))
174         theObjectShapes[theObject].append(aShape);
175       else {
176         QList<GeomShapePtr> aShapes;
177         aShapes.append(aShape);
178         theObjectShapes[theObject] = aShapes;
179       }
180     }
181   }
182 }
183
184 void PartSet_OperationPrs::getFeatureShapes(const FeaturePtr& theFeature,
185                                             ModuleBase_IWorkshop* theWorkshop,
186                                             QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
187 {
188   theObjectShapes.clear();
189   if (!theFeature.get())
190     return;
191
192   ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
193
194   QList<GeomShapePtr> aShapes;
195   std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
196   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
197   for (; anIt != aLast; anIt++) {
198     AttributePtr anAttribute = *anIt;
199     if (!isSelectionAttribute(anAttribute))
200       continue;
201
202     if (!aValidators->isCase(theFeature, anAttribute->id()))
203       continue; // this attribute is not participated in the current case
204
205     std::string anAttrType = anAttribute->attributeType();
206
207     if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
208       std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList = 
209               std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
210       for(int i = 0; i < aCurSelList->size(); i++) {
211         std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
212         ResultPtr aResult = aSelAttribute->context();
213         GeomShapePtr aShape = aSelAttribute->value();
214         addValue(aResult, aShape, theFeature, theObjectShapes);
215       }
216     }
217     if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
218       std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
219         std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
220       for (int i = 0; i < aCurSelList->size(); i++) {
221         GeomShapePtr aShape;
222         addValue(aCurSelList->object(i), aShape, theFeature, theObjectShapes);
223       }
224     }
225     else {
226       ObjectPtr anObject;
227       GeomShapePtr aShape;
228       if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
229         AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
230         if (anAttr->isObject()) {
231           anObject = anAttr->object();
232         }
233         else {
234           aShape = PartSet_Tools::findShapeBy2DPoint(anAttr, theWorkshop);
235           // the distance point is not found if the point is selected in the 2nd time
236           // TODO: after debug, this check can be removed
237           if (!aShape.get())
238             continue;
239           anObject = anAttr->attr()->owner();
240         }
241       }
242       if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
243         AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
244         anObject = anAttr->context();
245         aShape = anAttr->value();
246       }
247       if (anAttrType == ModelAPI_AttributeReference::typeId()) {
248         AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
249         anObject = anAttr->value();
250       }
251       addValue(anObject, aShape, theFeature, theObjectShapes);
252     }
253   }
254 }
255
256 void PartSet_OperationPrs::getResultShapes(const FeaturePtr& theFeature,
257                                            ModuleBase_IWorkshop* theWorkshop,
258                                            QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
259 {
260   theObjectShapes.clear();
261
262   if (!theFeature.get())
263     return;
264
265   XGUI_Displayer* aDisplayer = workshop(theWorkshop)->displayer();
266
267   std::list<ResultPtr> aFeatureResults = theFeature->results();
268   std::list<ResultPtr>::const_iterator aRIt = aFeatureResults.begin(),
269                                        aRLast = aFeatureResults.end();
270   for (; aRIt != aRLast; aRIt++) {
271     ResultPtr aResult = *aRIt;
272     if (!isVisible(aDisplayer, aResult))
273       continue;
274     GeomShapePtr aGeomShape = aResult->shape();
275     if (!aGeomShape.get())
276       continue;
277
278     if (theObjectShapes.contains(aResult))
279       theObjectShapes[aResult].append(aGeomShape);
280     else {
281       QList<GeomShapePtr> aShapes;
282       aShapes.append(aGeomShape);
283       theObjectShapes[aResult] = aShapes;
284     }
285   }
286 }
287
288 void PartSet_OperationPrs::getHighlightedShapes(ModuleBase_IWorkshop* theWorkshop,
289                                                 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
290 {
291   theObjectShapes.clear();
292
293   QList<ModuleBase_ViewerPrs> aValues;
294   ModuleBase_IPropertyPanel* aPanel = theWorkshop->propertyPanel();
295   if (aPanel) {
296     ModuleBase_ModelWidget* aWidget = aPanel->activeWidget();
297     if (aWidget) {
298       aWidget->getHighlighted(aValues);
299     }
300   }
301
302   QList<GeomShapePtr> aShapes;
303   QList<ModuleBase_ViewerPrs>::const_iterator anIIt = aValues.begin(),
304                                               aILast = aValues.end();
305   for (; anIIt != aILast; anIIt++) {
306     ModuleBase_ViewerPrs aPrs = *anIIt;
307     ObjectPtr anObject = aPrs.object();
308
309     GeomShapePtr aGeomShape;
310
311     TopoDS_Shape aShape = aPrs.shape();
312     if (!aShape.IsNull()) {
313       aGeomShape = GeomShapePtr(new GeomAPI_Shape());
314       aGeomShape->setImpl(new TopoDS_Shape(aShape));
315     }
316     else {
317       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
318       if (aResult.get()) {
319         aGeomShape = aResult->shape();
320       }
321     }
322
323     if (theObjectShapes.contains(anObject))
324       theObjectShapes[anObject].append(aGeomShape);
325     else {
326       QList<GeomShapePtr> aShapes;
327       aShapes.append(aGeomShape);
328       theObjectShapes[anObject] = aShapes;
329     }
330   }
331 }
332
333
334 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
335 {
336   std::string anAttrType = theAttribute->attributeType();
337
338   return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
339          anAttrType == ModelAPI_AttributeRefList::typeId() ||
340          anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
341          anAttrType == ModelAPI_AttributeSelection::typeId() ||
342          anAttrType == ModelAPI_AttributeReference::typeId();
343 }
344
345 XGUI_Workshop* PartSet_OperationPrs::workshop(ModuleBase_IWorkshop* theWorkshop)
346 {
347   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
348   return aConnector->workshop();
349 }