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