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