]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationPrs.cpp
Salome HOME
#1404 Random crash with Shaper: AIS presentations: avoid IsReadyToDisplay calling...
[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 #include "XGUI_Tools.h"
14
15 #include "ModuleBase_Tools.h"
16 #include "ModuleBase_IModule.h"
17 #include <ModuleBase_IPropertyPanel.h>
18 #include <ModuleBase_ModelWidget.h>
19 #include <ModuleBase_ViewerPrs.h>
20
21 #include <ModelAPI_Events.h>
22 #include <ModelAPI_Result.h>
23 #include <ModelAPI_Attribute.h>
24 #include <ModelAPI_AttributeRefAttr.h>
25 #include <ModelAPI_AttributeReference.h>
26 #include <ModelAPI_AttributeSelection.h>
27 #include <ModelAPI_AttributeSelectionList.h>
28 #include <ModelAPI_AttributeRefList.h>
29 #include <ModelAPI_Validator.h>
30 #include <ModelAPI_Session.h>
31 #include <ModelAPI_ResultCompSolid.h>
32
33 #include <Events_Error.h>
34 #include <Events_Loop.h>
35
36 #include <GeomAPI_IPresentable.h>
37
38 #include <StdPrs_WFDeflectionShape.hxx>
39
40 #include <QList>
41
42 //#define DEBUG_EMPTY_SHAPE
43
44 // multi-rotation/translation operation
45 //#define DEBUG_HIDE_COPY_ATTRIBUTE
46 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
47 #include <ModelAPI_AttributeBoolean.h>
48 #include <SketchPlugin_SketchEntity.h>
49 #endif
50
51 IMPLEMENT_STANDARD_HANDLE(PartSet_OperationPrs, ViewerData_AISShape);
52 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
53
54 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
55 : ViewerData_AISShape(TopoDS_Shape()), myWorkshop(theWorkshop), myUseAISWidth(false)
56 {
57   myShapeColor = Quantity_Color(1, 1, 1, Quantity_TOC_RGB);
58 }
59
60 bool PartSet_OperationPrs::hasShapes()
61 {
62   return !myFeatureShapes.empty();
63 }
64
65 void PartSet_OperationPrs::setShapeColor(const Quantity_Color& theColor)
66 {
67   myShapeColor = theColor;
68 }
69
70 void PartSet_OperationPrs::useAISWidth()
71 {
72   myUseAISWidth = true;
73 }
74
75 void PartSet_OperationPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
76                                    const Handle(Prs3d_Presentation)& thePresentation, 
77                                    const Standard_Integer theMode)
78 {
79   SetColor(myShapeColor);
80   thePresentation->Clear();
81
82   NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)> aShapeToPrsMap;
83   fillShapeList(myFeatureShapes, aShapeToPrsMap);
84
85   bool aReadyToDisplay = !myShapeToPrsMap.IsEmpty();
86   if (aReadyToDisplay) {
87     myShapeToPrsMap.Clear();
88     myShapeToPrsMap.Assign(aShapeToPrsMap);
89   }
90
91   XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(myWorkshop)->displayer();
92   Handle(Prs3d_Drawer) aDrawer = Attributes();
93   // create presentations on the base of the shapes
94   for(NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>::Iterator anIter(myShapeToPrsMap);
95       anIter.More(); anIter.Next()) {
96     const TopoDS_Shape& aShape = anIter.Key();
97     // change deviation coefficient to provide more precise circle
98     ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
99
100     if (myUseAISWidth) {
101       Handle(AIS_InteractiveObject) anIO = anIter.Value();
102       if (!anIO.IsNull()) {
103         int aWidth = anIO->Width();
104         /// workaround for zero width. Else, there will be a crash
105         if (aWidth == 0) { // width returns of TSolid shape is zero
106           bool isDisplayed = !anIO->GetContext().IsNull();
107           aWidth = PartSet_Tools::getAISDefaultWidth();// default width value
108         }
109         setWidth(aDrawer, aWidth);
110       }
111     }
112     StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
113   }
114
115   if (!aReadyToDisplay) {
116     Events_Error::throwException("An empty AIS presentation: PartSet_OperationPrs");
117     //std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
118     //            new Events_Message(Events_Loop::eventByName(EVENT_EMPTY_OPERATION_PRESENTATION)));
119     //Events_Loop::loop()->send(aMsg);
120   }
121 }
122
123 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
124                                             const Standard_Integer aMode)
125 {
126   // the presentation is not used in the selection
127 }
128
129 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
130 {
131   bool isSub = false;
132   CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
133   if (aComposite.get())
134     isSub = aComposite->isSub(theObject);
135
136   return isSub;
137 }
138
139 void PartSet_OperationPrs::addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
140                                     const FeaturePtr& theFeature, ModuleBase_IWorkshop* theWorkshop,
141                                     QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
142 {
143   if (theObject.get()) {
144     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
145     if (aResult.get()) {
146       ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
147       if (aCompsolidResult.get()) {
148         if (aCompsolidResult->numberOfSubs() > 0) {
149           for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
150             ResultPtr aSubResult = aCompsolidResult->subResult(i);
151             if (aSubResult.get()) {
152               GeomShapePtr aShape;
153               addValue(aSubResult, aShape, theFeature, theWorkshop, theObjectShapes);
154             }
155           }
156           return;
157         }
158       }
159 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
160       else {
161         FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
162         if (aFeature.get()) {
163           AttributeBooleanPtr aCopyAttr = aFeature->data()->boolean(SketchPlugin_SketchEntity::COPY_ID());
164           if (aCopyAttr.get()) {
165             bool isCopy = aCopyAttr->value();
166             if (isCopy)
167               return;
168           }
169         }
170       }
171 #endif
172     }
173
174     GeomShapePtr aShape = theShape;
175     if (!aShape.get()) {
176       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
177       if (aResult.get())
178         aShape = aResult->shape();
179     }
180     if (!isSubObject(theObject, theFeature))
181       appendShapeIfVisible(theWorkshop, theObject, aShape, theObjectShapes);
182   }
183 }
184
185 void PartSet_OperationPrs::appendShapeIfVisible(ModuleBase_IWorkshop* theWorkshop,
186                               const ObjectPtr& theObject,
187                               GeomShapePtr theGeomShape,
188                               QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
189 {
190   XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
191   if (XGUI_Displayer::isVisible(aDisplayer, theObject)) {
192     if (theGeomShape.get()) {
193       if (theObjectShapes.contains(theObject))
194         theObjectShapes[theObject].append(theGeomShape);
195       else {
196         QList<GeomShapePtr> aShapes;
197         aShapes.append(theGeomShape);
198         theObjectShapes[theObject] = aShapes;
199       }
200     } else {
201   #ifdef DEBUG_EMPTY_SHAPE
202       qDebug(QString("Empty shape in result, result: %1")
203               .arg(ModuleBase_Tools::objectInfo(theObject)).toStdString().c_str());
204   #endif
205     }
206   }
207 }
208
209 void PartSet_OperationPrs::getFeatureShapes(const FeaturePtr& theFeature,
210                                             ModuleBase_IWorkshop* theWorkshop,
211                                             QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
212 {
213   theObjectShapes.clear();
214   if (!theFeature.get())
215     return;
216
217   ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
218
219   QList<GeomShapePtr> aShapes;
220   std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
221   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
222   for (; anIt != aLast; anIt++) {
223     AttributePtr anAttribute = *anIt;
224     if (!isSelectionAttribute(anAttribute))
225       continue;
226
227     if (!aValidators->isCase(theFeature, anAttribute->id()))
228       continue; // this attribute is not participated in the current case
229
230     std::string anAttrType = anAttribute->attributeType();
231
232     if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
233       std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList = 
234               std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
235       for(int i = 0; i < aCurSelList->size(); i++) {
236         std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
237         ResultPtr aResult = aSelAttribute->context();
238         GeomShapePtr aShape = aSelAttribute->value();
239         addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
240       }
241     }
242     if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
243       std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
244         std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
245       for (int i = 0; i < aCurSelList->size(); i++) {
246         ObjectPtr anObject = aCurSelList->object(i);
247         FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
248         // if a feature is stored in the attribute, we should obtain the feature results
249         // e.g. feature rectangle uses parameters feature lines in the attribute
250         if (aFeature.get()) {
251           getResultShapes(aFeature, theWorkshop, theObjectShapes, false);
252         }
253         else {
254           ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
255           if (aResult.get()) {
256             GeomShapePtr aShape = aResult->shape();
257             if (aShape.get())
258               addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
259           }
260         }
261       }
262     }
263     else {
264       ObjectPtr anObject;
265       GeomShapePtr aShape;
266       if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
267         AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
268         if (anAttr->isObject()) {
269           anObject = anAttr->object();
270         }
271         else {
272           AttributePtr anAttribute = anAttr->attr();
273           aShape = PartSet_Tools::findShapeBy2DPoint(anAttribute, theWorkshop);
274           // the distance point is not found if the point is selected in the 2nd time
275           // TODO: after debug, this check can be removed
276           if (!aShape.get())
277             continue;
278           anObject = anAttr->attr()->owner();
279         }
280       }
281       if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
282         AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
283         anObject = anAttr->context();
284         aShape = anAttr->value();
285       }
286       if (anAttrType == ModelAPI_AttributeReference::typeId()) {
287         AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
288         anObject = anAttr->value();
289       }
290       addValue(anObject, aShape, theFeature, theWorkshop, theObjectShapes);
291     }
292   }
293 }
294
295 void PartSet_OperationPrs::getResultShapes(const FeaturePtr& theFeature,
296                                            ModuleBase_IWorkshop* theWorkshop,
297                                            QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes,
298                                            const bool theListShouldBeCleared)
299 {
300   if (theListShouldBeCleared)
301     theObjectShapes.clear();
302
303   if (!theFeature.get())
304     return;
305
306   XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
307
308   std::list<ResultPtr> aFeatureResults = theFeature->results();
309   std::list<ResultPtr>::const_iterator aRIt = aFeatureResults.begin(),
310                                        aRLast = aFeatureResults.end();
311   for (; aRIt != aRLast; aRIt++) {
312     ResultPtr aResult = *aRIt;
313     GeomShapePtr aGeomShape = aResult->shape();
314     appendShapeIfVisible(theWorkshop, aResult, aGeomShape, theObjectShapes);
315   }
316 }
317
318 void PartSet_OperationPrs::getHighlightedShapes(ModuleBase_IWorkshop* theWorkshop,
319                                                 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
320 {
321   theObjectShapes.clear();
322
323   QList<ModuleBase_ViewerPrsPtr> aValues;
324   ModuleBase_IPropertyPanel* aPanel = theWorkshop->propertyPanel();
325   if (aPanel) {
326     ModuleBase_ModelWidget* aWidget = aPanel->activeWidget();
327     if (aWidget) {
328       aWidget->getHighlighted(aValues);
329     }
330   }
331
332   QList<GeomShapePtr> aShapes;
333   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIIt = aValues.begin(),
334                                               aILast = aValues.end();
335   for (; anIIt != aILast; anIIt++) {
336     ModuleBase_ViewerPrsPtr aPrs = *anIIt;
337     ObjectPtr anObject = aPrs->object();
338
339     GeomShapePtr aGeomShape = aPrs->shape();
340     if (!aGeomShape.get() || aGeomShape->isNull()) {
341       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
342       if (aResult.get()) {
343         aGeomShape = aResult->shape();
344       }
345     }
346     appendShapeIfVisible(theWorkshop, anObject, aGeomShape, theObjectShapes);
347   }
348 }
349
350
351 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
352 {
353   std::string anAttrType = theAttribute->attributeType();
354
355   return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
356          anAttrType == ModelAPI_AttributeRefList::typeId() ||
357          anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
358          anAttrType == ModelAPI_AttributeSelection::typeId() ||
359          anAttrType == ModelAPI_AttributeReference::typeId();
360 }
361
362 void PartSet_OperationPrs::fillShapeList(const QMap<ObjectPtr, QList<GeomShapePtr> >& theFeatureShapes,
363                             NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>& theShapeToPrsMap)
364 {
365   theShapeToPrsMap.Clear();
366
367   XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(myWorkshop)->displayer();
368   Handle(Prs3d_Drawer) aDrawer = Attributes();
369
370   // create presentations on the base of the shapes
371   QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = myFeatureShapes.begin(),
372                                                         aLast = myFeatureShapes.end();
373   for (; anIt != aLast; anIt++) {
374     ObjectPtr anObject = anIt.key();
375     QList<GeomShapePtr> aShapes = anIt.value();
376     QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
377     for (; aShIt != aShLast; aShIt++) {
378       GeomShapePtr aGeomShape = *aShIt;
379       // the shape should not be checked here on empty value because it should be checked in
380       // appendShapeIfVisible() on the step of filling myFeatureShapes list
381       // the reason is to avoid empty AIS object visualized in the viewer
382       //if (!aGeomShape.get()) continue;
383       TopoDS_Shape aShape = aGeomShape.get() ? aGeomShape->impl<TopoDS_Shape>() : TopoDS_Shape();
384       // change deviation coefficient to provide more precise circle
385       Handle(AIS_InteractiveObject) anIO;
386       if (myUseAISWidth) {
387         AISObjectPtr anAISPtr = aDisplayer->getAISObject(anObject);
388         if (anAISPtr.get())
389           anIO = anAISPtr->impl<Handle(AIS_InteractiveObject)>();
390       }
391       theShapeToPrsMap.Bind(aShape, anIO);
392     }
393   }
394 }