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