Salome HOME
using the previous type of Circle/Arc during reentrant operation. First correction...
[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     // compsolid is not visualized in the viewer, but should have presentation when all sub solids are
123     // visible. It is useful for highlight presentation where compsolid shape is selectable
124     if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultCompSolid::group()) {
125       ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
126       if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
127         bool anAllSubsVisible = aCompsolidResult->numberOfSubs() > 0;
128         for(int i = 0; i < aCompsolidResult->numberOfSubs() && anAllSubsVisible; i++) {
129           anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
130         }
131         aVisible = anAllSubsVisible;
132       }
133     }
134   }
135   else {
136     // check if all results of the feature are visible
137     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
138     std::list<ResultPtr> aResults = aFeature->results();
139     std::list<ResultPtr>::const_iterator aIt;
140     aVisible = !aResults.empty();
141     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
142       aVisible = aVisible && theDisplayer->isVisible(*aIt);
143     }
144   }
145   return aVisible;
146 }
147
148 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
149 {
150   bool isSub = false;
151   CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
152   if (aComposite.get())
153     isSub = aComposite->isSub(theObject);
154
155   return isSub;
156 }
157
158 void addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
159               const FeaturePtr& theFeature,
160               QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
161 {
162   if (theObject.get()) {
163     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
164     if (aResult.get()) {
165       ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
166       if (aCompsolidResult.get()) {
167         for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
168           ResultPtr aSubResult = aCompsolidResult->subResult(i);
169           if (aSubResult.get()) {
170             GeomShapePtr aShape;
171             addValue(aSubResult, aShape, theFeature, theObjectShapes);
172           }
173         }
174         return;
175       }
176     }
177
178
179     GeomShapePtr aShape = theShape;
180     if (!aShape.get()) {
181       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
182       if (aResult.get())
183         aShape = aResult->shape();
184     }
185     if (!isSubObject(theObject, theFeature)) {
186       if (theObjectShapes.contains(theObject))
187         theObjectShapes[theObject].append(aShape);
188       else {
189         QList<GeomShapePtr> aShapes;
190         aShapes.append(aShape);
191         theObjectShapes[theObject] = aShapes;
192       }
193     }
194   }
195 }
196
197 void PartSet_OperationPrs::getFeatureShapes(const FeaturePtr& theFeature,
198                                             ModuleBase_IWorkshop* theWorkshop,
199                                             QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
200 {
201   theObjectShapes.clear();
202   if (!theFeature.get())
203     return;
204
205   ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
206
207   QList<GeomShapePtr> aShapes;
208   std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
209   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
210   for (; anIt != aLast; anIt++) {
211     AttributePtr anAttribute = *anIt;
212     if (!isSelectionAttribute(anAttribute))
213       continue;
214
215     if (!aValidators->isCase(theFeature, anAttribute->id()))
216       continue; // this attribute is not participated in the current case
217
218     std::string anAttrType = anAttribute->attributeType();
219
220     if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
221       std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList = 
222               std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
223       for(int i = 0; i < aCurSelList->size(); i++) {
224         std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
225         ResultPtr aResult = aSelAttribute->context();
226         GeomShapePtr aShape = aSelAttribute->value();
227         addValue(aResult, aShape, theFeature, theObjectShapes);
228       }
229     }
230     if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
231       std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
232         std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
233       for (int i = 0; i < aCurSelList->size(); i++) {
234         GeomShapePtr aShape;
235         addValue(aCurSelList->object(i), aShape, theFeature, theObjectShapes);
236       }
237     }
238     else {
239       ObjectPtr anObject;
240       GeomShapePtr aShape;
241       if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
242         AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
243         if (anAttr->isObject()) {
244           anObject = anAttr->object();
245         }
246         else {
247           aShape = PartSet_Tools::findShapeBy2DPoint(anAttr, theWorkshop);
248           // the distance point is not found if the point is selected in the 2nd time
249           // TODO: after debug, this check can be removed
250           if (!aShape.get())
251             continue;
252           anObject = anAttr->attr()->owner();
253         }
254       }
255       if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
256         AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
257         anObject = anAttr->context();
258         aShape = anAttr->value();
259       }
260       if (anAttrType == ModelAPI_AttributeReference::typeId()) {
261         AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
262         anObject = anAttr->value();
263       }
264       addValue(anObject, aShape, theFeature, theObjectShapes);
265     }
266   }
267 }
268
269 void PartSet_OperationPrs::getResultShapes(const FeaturePtr& theFeature,
270                                            ModuleBase_IWorkshop* theWorkshop,
271                                            QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
272 {
273   theObjectShapes.clear();
274
275   if (!theFeature.get())
276     return;
277
278   XGUI_Displayer* aDisplayer = workshop(theWorkshop)->displayer();
279
280   std::list<ResultPtr> aFeatureResults = theFeature->results();
281   std::list<ResultPtr>::const_iterator aRIt = aFeatureResults.begin(),
282                                        aRLast = aFeatureResults.end();
283   for (; aRIt != aRLast; aRIt++) {
284     ResultPtr aResult = *aRIt;
285     if (!isVisible(aDisplayer, aResult))
286       continue;
287     GeomShapePtr aGeomShape = aResult->shape();
288     if (!aGeomShape.get())
289       continue;
290
291     if (theObjectShapes.contains(aResult))
292       theObjectShapes[aResult].append(aGeomShape);
293     else {
294       QList<GeomShapePtr> aShapes;
295       aShapes.append(aGeomShape);
296       theObjectShapes[aResult] = aShapes;
297     }
298   }
299 }
300
301 void PartSet_OperationPrs::getHighlightedShapes(ModuleBase_IWorkshop* theWorkshop,
302                                                 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
303 {
304   theObjectShapes.clear();
305
306   QList<ModuleBase_ViewerPrs> aValues;
307   ModuleBase_IPropertyPanel* aPanel = theWorkshop->propertyPanel();
308   if (aPanel) {
309     ModuleBase_ModelWidget* aWidget = aPanel->activeWidget();
310     if (aWidget) {
311       aWidget->getHighlighted(aValues);
312     }
313   }
314
315   QList<GeomShapePtr> aShapes;
316   QList<ModuleBase_ViewerPrs>::const_iterator anIIt = aValues.begin(),
317                                               aILast = aValues.end();
318   for (; anIIt != aILast; anIIt++) {
319     ModuleBase_ViewerPrs aPrs = *anIIt;
320     ObjectPtr anObject = aPrs.object();
321
322     GeomShapePtr aGeomShape;
323
324     TopoDS_Shape aShape = aPrs.shape();
325     if (!aShape.IsNull()) {
326       aGeomShape = GeomShapePtr(new GeomAPI_Shape());
327       aGeomShape->setImpl(new TopoDS_Shape(aShape));
328     }
329     else {
330       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
331       if (aResult.get()) {
332         aGeomShape = aResult->shape();
333       }
334     }
335
336     if (theObjectShapes.contains(anObject))
337       theObjectShapes[anObject].append(aGeomShape);
338     else {
339       QList<GeomShapePtr> aShapes;
340       aShapes.append(aGeomShape);
341       theObjectShapes[anObject] = aShapes;
342     }
343   }
344 }
345
346
347 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
348 {
349   std::string anAttrType = theAttribute->attributeType();
350
351   return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
352          anAttrType == ModelAPI_AttributeRefList::typeId() ||
353          anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
354          anAttrType == ModelAPI_AttributeSelection::typeId() ||
355          anAttrType == ModelAPI_AttributeReference::typeId();
356 }
357
358 XGUI_Workshop* PartSet_OperationPrs::workshop(ModuleBase_IWorkshop* theWorkshop)
359 {
360   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
361   return aConnector->workshop();
362 }