Salome HOME
Issue #1440 Crash when edit box with parameter: erase custom presentation if it is...
[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 #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_Error::throwException("An empty AIS presentation: PartSet_OperationPrs");
119     std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
120                 new Events_Message(Events_Loop::eventByName(EVENT_EMPTY_OPERATION_PRESENTATION)));
121     Events_Loop::loop()->send(aMsg);
122   }
123 }
124
125 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
126                                             const Standard_Integer aMode)
127 {
128   // the presentation is not used in the selection
129 }
130
131 NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>& PartSet_OperationPrs::shapesMap()
132 {
133   return myShapeToPrsMap;
134 }
135
136 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
137 {
138   bool isSub = false;
139   CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
140   if (aComposite.get())
141     isSub = aComposite->isSub(theObject);
142
143   return isSub;
144 }
145
146 void PartSet_OperationPrs::addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
147                                     const FeaturePtr& theFeature, ModuleBase_IWorkshop* theWorkshop,
148                                     QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
149 {
150   if (theObject.get()) {
151     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
152     if (aResult.get()) {
153       ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
154       if (aCompsolidResult.get()) {
155         if (aCompsolidResult->numberOfSubs() > 0) {
156           for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
157             ResultPtr aSubResult = aCompsolidResult->subResult(i);
158             if (aSubResult.get()) {
159               GeomShapePtr aShape;
160               addValue(aSubResult, aShape, theFeature, theWorkshop, theObjectShapes);
161             }
162           }
163           return;
164         }
165       }
166 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
167       else {
168         FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
169         if (aFeature.get()) {
170           AttributeBooleanPtr aCopyAttr = aFeature->data()->boolean(SketchPlugin_SketchEntity::COPY_ID());
171           if (aCopyAttr.get()) {
172             bool isCopy = aCopyAttr->value();
173             if (isCopy)
174               return;
175           }
176         }
177       }
178 #endif
179     }
180
181     GeomShapePtr aShape = theShape;
182     if (!aShape.get()) {
183       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
184       if (aResult.get())
185         aShape = aResult->shape();
186     }
187     if (!isSubObject(theObject, theFeature))
188       appendShapeIfVisible(theWorkshop, theObject, aShape, theObjectShapes);
189   }
190 }
191
192 void PartSet_OperationPrs::appendShapeIfVisible(ModuleBase_IWorkshop* theWorkshop,
193                               const ObjectPtr& theObject,
194                               GeomShapePtr theGeomShape,
195                               QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
196 {
197   XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
198   if (XGUI_Displayer::isVisible(aDisplayer, theObject)) {
199     if (theGeomShape.get()) {
200       if (theObjectShapes.contains(theObject))
201         theObjectShapes[theObject].append(theGeomShape);
202       else {
203         QList<GeomShapePtr> aShapes;
204         aShapes.append(theGeomShape);
205         theObjectShapes[theObject] = aShapes;
206       }
207     } else {
208   #ifdef DEBUG_EMPTY_SHAPE
209       qDebug(QString("Empty shape in result, result: %1")
210               .arg(ModuleBase_Tools::objectInfo(theObject)).toStdString().c_str());
211   #endif
212     }
213   }
214 }
215
216 void PartSet_OperationPrs::getFeatureShapes(const FeaturePtr& theFeature,
217                                             ModuleBase_IWorkshop* theWorkshop,
218                                             QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
219 {
220   theObjectShapes.clear();
221   if (!theFeature.get())
222     return;
223
224   ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
225
226   QList<GeomShapePtr> aShapes;
227   std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
228   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
229   for (; anIt != aLast; anIt++) {
230     AttributePtr anAttribute = *anIt;
231     if (!isSelectionAttribute(anAttribute))
232       continue;
233
234     if (!aValidators->isCase(theFeature, anAttribute->id()))
235       continue; // this attribute is not participated in the current case
236
237     std::string anAttrType = anAttribute->attributeType();
238
239     if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
240       std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList = 
241               std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
242       for(int i = 0; i < aCurSelList->size(); i++) {
243         std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
244         ResultPtr aResult = aSelAttribute->context();
245         GeomShapePtr aShape = aSelAttribute->value();
246         addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
247       }
248     }
249     if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
250       std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
251         std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
252       for (int i = 0; i < aCurSelList->size(); i++) {
253         ObjectPtr anObject = aCurSelList->object(i);
254         FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
255         // if a feature is stored in the attribute, we should obtain the feature results
256         // e.g. feature rectangle uses parameters feature lines in the attribute
257         if (aFeature.get()) {
258           getResultShapes(aFeature, theWorkshop, theObjectShapes, false);
259         }
260         else {
261           ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
262           if (aResult.get()) {
263             GeomShapePtr aShape = aResult->shape();
264             if (aShape.get())
265               addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
266           }
267         }
268       }
269     }
270     else {
271       ObjectPtr anObject;
272       GeomShapePtr aShape;
273       if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
274         AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
275         if (anAttr->isObject()) {
276           anObject = anAttr->object();
277         }
278         else {
279           AttributePtr anAttribute = anAttr->attr();
280           aShape = PartSet_Tools::findShapeBy2DPoint(anAttribute, theWorkshop);
281           // the distance point is not found if the point is selected in the 2nd time
282           // TODO: after debug, this check can be removed
283           if (!aShape.get())
284             continue;
285           anObject = anAttr->attr()->owner();
286         }
287       }
288       if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
289         AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
290         anObject = anAttr->context();
291         aShape = anAttr->value();
292       }
293       if (anAttrType == ModelAPI_AttributeReference::typeId()) {
294         AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
295         anObject = anAttr->value();
296       }
297       addValue(anObject, aShape, theFeature, theWorkshop, theObjectShapes);
298     }
299   }
300 }
301
302 void PartSet_OperationPrs::getResultShapes(const FeaturePtr& theFeature,
303                                            ModuleBase_IWorkshop* theWorkshop,
304                                            QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes,
305                                            const bool theListShouldBeCleared)
306 {
307   if (theListShouldBeCleared)
308     theObjectShapes.clear();
309
310   if (!theFeature.get())
311     return;
312
313   XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
314
315   std::list<ResultPtr> aFeatureResults = theFeature->results();
316   std::list<ResultPtr>::const_iterator aRIt = aFeatureResults.begin(),
317                                        aRLast = aFeatureResults.end();
318   for (; aRIt != aRLast; aRIt++) {
319     ResultPtr aResult = *aRIt;
320     GeomShapePtr aGeomShape = aResult->shape();
321     appendShapeIfVisible(theWorkshop, aResult, aGeomShape, theObjectShapes);
322   }
323 }
324
325 void PartSet_OperationPrs::getHighlightedShapes(ModuleBase_IWorkshop* theWorkshop,
326                                                 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
327 {
328   theObjectShapes.clear();
329
330   QList<ModuleBase_ViewerPrsPtr> aValues;
331   ModuleBase_IPropertyPanel* aPanel = theWorkshop->propertyPanel();
332   if (aPanel) {
333     ModuleBase_ModelWidget* aWidget = aPanel->activeWidget();
334     if (aWidget) {
335       aWidget->getHighlighted(aValues);
336     }
337   }
338
339   QList<GeomShapePtr> aShapes;
340   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIIt = aValues.begin(),
341                                               aILast = aValues.end();
342   for (; anIIt != aILast; anIIt++) {
343     ModuleBase_ViewerPrsPtr aPrs = *anIIt;
344     ObjectPtr anObject = aPrs->object();
345
346     GeomShapePtr aGeomShape = aPrs->shape();
347     if (!aGeomShape.get() || aGeomShape->isNull()) {
348       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
349       if (aResult.get()) {
350         aGeomShape = aResult->shape();
351       }
352     }
353     appendShapeIfVisible(theWorkshop, anObject, aGeomShape, theObjectShapes);
354   }
355 }
356
357
358 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
359 {
360   std::string anAttrType = theAttribute->attributeType();
361
362   return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
363          anAttrType == ModelAPI_AttributeRefList::typeId() ||
364          anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
365          anAttrType == ModelAPI_AttributeSelection::typeId() ||
366          anAttrType == ModelAPI_AttributeReference::typeId();
367 }
368
369 void PartSet_OperationPrs::fillShapeList(const QMap<ObjectPtr, QList<GeomShapePtr> >& theFeatureShapes,
370                             ModuleBase_IWorkshop* theWorkshop,
371                             NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>& theShapeToPrsMap)
372 {
373   theShapeToPrsMap.Clear();
374
375   XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
376
377   // create presentations on the base of the shapes
378   QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = theFeatureShapes.begin(),
379                                                         aLast = theFeatureShapes.end();
380   for (; anIt != aLast; anIt++) {
381     ObjectPtr anObject = anIt.key();
382     QList<GeomShapePtr> aShapes = anIt.value();
383     QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
384     for (; aShIt != aShLast; aShIt++) {
385       GeomShapePtr aGeomShape = *aShIt;
386       // the shape should not be checked here on empty value because it should be checked in
387       // appendShapeIfVisible() on the step of filling theFeatureShapes list
388       // the reason is to avoid empty AIS object visualized in the viewer
389       //if (!aGeomShape.get()) continue;
390       TopoDS_Shape aShape = aGeomShape.get() ? aGeomShape->impl<TopoDS_Shape>() : TopoDS_Shape();
391       if (aShape.IsNull())
392         continue;
393
394       // change deviation coefficient to provide more precise circle
395       Handle(AIS_InteractiveObject) anIO;
396       AISObjectPtr anAISPtr = aDisplayer->getAISObject(anObject);
397       if (anAISPtr.get())
398         anIO = anAISPtr->impl<Handle(AIS_InteractiveObject)>();
399       theShapeToPrsMap.Bind(aShape, anIO);
400     }
401   }
402 }