Salome HOME
Issues #2027, #2024, #2063, #2067: reentrant message to fill new operation by result...
[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_WFShape.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_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
60
61 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
62 : ViewerData_AISShape(TopoDS_Shape()), myWorkshop(theWorkshop), myUseAISWidth(false)
63 {
64 #ifdef DEBUG_OPERATION_PRS
65   qDebug("PartSet_OperationPrs::PartSet_OperationPrs");
66 #endif
67   myShapeColor = Quantity_Color(1, 1, 1, Quantity_TOC_RGB);
68
69   // first presentation for having correct Compute until presentation with shapes are set
70   gp_Pnt aPnt(0.0, 0.0, 0.0);
71   BRepBuilderAPI_MakeVertex aMaker(aPnt);
72   TopoDS_Vertex aVertex = aMaker.Vertex();
73   myShapeToPrsMap.Bind(aVertex, NULL);
74 }
75
76 bool PartSet_OperationPrs::hasShapes()
77 {
78   return !myShapeToPrsMap.IsEmpty();
79 }
80
81 void PartSet_OperationPrs::setShapeColor(const Quantity_Color& theColor)
82 {
83   myShapeColor = theColor;
84 }
85
86 void PartSet_OperationPrs::useAISWidth()
87 {
88   myUseAISWidth = true;
89 }
90
91 void PartSet_OperationPrs::Compute(
92             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
111       anIter(myShapeToPrsMap); 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_WFShape::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 =
174         std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
175       if (aCompsolidResult.get()) {
176         if (aCompsolidResult->numberOfSubs() > 0) {
177           for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
178             ResultPtr aSubResult = aCompsolidResult->subResult(i);
179             if (aSubResult.get()) {
180               GeomShapePtr aShape;
181               addValue(aSubResult, aShape, theFeature, theWorkshop, theObjectShapes);
182             }
183           }
184           return;
185         }
186       }
187 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
188       else {
189         FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
190         if (aFeature.get()) {
191           AttributeBooleanPtr aCopyAttr =
192             aFeature->data()->boolean(SketchPlugin_SketchEntity::COPY_ID());
193           if (aCopyAttr.get()) {
194             bool isCopy = aCopyAttr->value();
195             if (isCopy)
196               return;
197           }
198         }
199       }
200 #endif
201     }
202
203     GeomShapePtr aShape = theShape;
204     if (!aShape.get()) {
205       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
206       if (aResult.get())
207         aShape = aResult->shape();
208     }
209     if (!isSubObject(theObject, theFeature))
210       appendShapeIfVisible(theWorkshop, theObject, aShape, theObjectShapes);
211   }
212 }
213
214 void PartSet_OperationPrs::appendShapeIfVisible(ModuleBase_IWorkshop* theWorkshop,
215                               const ObjectPtr& theObject,
216                               GeomShapePtr theGeomShape,
217                               QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
218 {
219   XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
220   if (XGUI_Displayer::isVisible(aDisplayer, theObject)) {
221     if (theGeomShape.get()) {
222       if (theObjectShapes.contains(theObject))
223         theObjectShapes[theObject].append(theGeomShape);
224       else {
225         QList<GeomShapePtr> aShapes;
226         aShapes.append(theGeomShape);
227         theObjectShapes[theObject] = aShapes;
228       }
229     } else {
230   #ifdef DEBUG_EMPTY_SHAPE
231       qDebug(QString("Empty shape in result, result: %1")
232               .arg(ModuleBase_Tools::objectInfo(theObject)).toStdString().c_str());
233   #endif
234     }
235   }
236 }
237
238 void PartSet_OperationPrs::getFeatureShapes(const FeaturePtr& theFeature,
239                                             ModuleBase_IWorkshop* theWorkshop,
240                                             QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
241 {
242   theObjectShapes.clear();
243   if (!theFeature.get())
244     return;
245
246   ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
247
248   QList<GeomShapePtr> aShapes;
249   std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
250   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
251   for (; anIt != aLast; anIt++) {
252     AttributePtr anAttribute = *anIt;
253     if (!isSelectionAttribute(anAttribute))
254       continue;
255
256     if (!aValidators->isCase(theFeature, anAttribute->id()))
257       continue; // this attribute is not participated in the current case
258
259     std::string anAttrType = anAttribute->attributeType();
260
261     if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
262       std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
263               std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
264       for(int i = 0; i < aCurSelList->size(); i++) {
265         std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
266         ResultPtr aResult = aSelAttribute->context();
267         GeomShapePtr aShape = aSelAttribute->value();
268         addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
269       }
270     }
271     if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
272       std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
273         std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
274       for (int i = 0; i < aCurSelList->size(); i++) {
275         ObjectPtr anObject = aCurSelList->object(i);
276         FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
277         // if a feature is stored in the attribute, we should obtain the feature results
278         // e.g. feature rectangle uses parameters feature lines in the attribute
279         if (aFeature.get()) {
280           getResultShapes(aFeature, theWorkshop, theObjectShapes, false);
281         }
282         else {
283           ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
284           if (aResult.get()) {
285             GeomShapePtr aShape = aResult->shape();
286             if (aShape.get())
287               addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
288           }
289         }
290       }
291     }
292     else {
293       ObjectPtr anObject;
294       GeomShapePtr aShape;
295       if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
296         AttributeRefAttrPtr anAttr =
297           std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
298         if (anAttr->isObject()) {
299           anObject = anAttr->object();
300         }
301         else {
302           AttributePtr anAttribute = anAttr->attr();
303           aShape = PartSet_Tools::findShapeBy2DPoint(anAttribute, theWorkshop);
304           // the distance point is not found if the point is selected in the 2nd time
305           // TODO: after debug, this check can be removed
306           if (!aShape.get())
307             continue;
308           anObject = anAttr->attr()->owner();
309         }
310       }
311       if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
312         AttributeSelectionPtr anAttr =
313           std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
314         anObject = anAttr->context();
315         aShape = anAttr->value();
316       }
317       if (anAttrType == ModelAPI_AttributeReference::typeId()) {
318         AttributeReferencePtr anAttr =
319           std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
320         anObject = anAttr->value();
321       }
322       addValue(anObject, aShape, theFeature, theWorkshop, theObjectShapes);
323     }
324   }
325 }
326
327 void PartSet_OperationPrs::getResultShapes(const FeaturePtr& theFeature,
328                                            ModuleBase_IWorkshop* theWorkshop,
329                                            QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes,
330                                            const bool theListShouldBeCleared)
331 {
332   if (theListShouldBeCleared)
333     theObjectShapes.clear();
334
335   if (!theFeature.get())
336     return;
337
338   XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
339
340   std::list<ResultPtr> aResults;
341   ModelAPI_Tools::allResults(theFeature, aResults);
342   std::list<ResultPtr>::const_iterator aRIt = aResults.begin(),
343                                        aRLast = aResults.end();
344   for (; aRIt != aRLast; aRIt++) {
345     ResultPtr aResult = *aRIt;
346     GeomShapePtr aGeomShape = aResult->shape();
347     appendShapeIfVisible(theWorkshop, aResult, aGeomShape, theObjectShapes);
348   }
349 }
350
351 void PartSet_OperationPrs::getPresentationShapes(const FeaturePtr& theFeature,
352                                            ModuleBase_IWorkshop* theWorkshop,
353                                            QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes,
354                                            const bool theListShouldBeCleared)
355 {
356   if (theListShouldBeCleared)
357     theObjectShapes.clear();
358
359   if (!theFeature.get() || !theFeature->data()->isValid()) // if feature is already removed
360     return;
361
362   XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
363
364   GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theFeature);
365   if (!aPrs.get())
366     return;
367
368   AISObjectPtr anAIS = aPrs->getAISObject(aDisplayer->getAISObject(theFeature));
369   if (!anAIS.get())
370     return;
371
372   Handle(AIS_InteractiveObject) anAISPrs = anAIS->impl<Handle(AIS_InteractiveObject)>();
373   if (!anAISPrs.IsNull()) {
374     Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(anAISPrs);
375     if (!aShapePrs.IsNull()) {
376       TopoDS_Shape aShape = aShapePrs->Shape();
377       if (!aShape.IsNull()) {
378         std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape());
379         aGeomShape->setImpl(new TopoDS_Shape(aShape));
380         appendShapeIfVisible(theWorkshop, theFeature, aGeomShape, theObjectShapes);
381       }
382     }
383   }
384 }
385
386 void PartSet_OperationPrs::getHighlightedShapes(ModuleBase_IWorkshop* theWorkshop,
387                                                 QMap<ObjectPtr,
388                                                 QList<GeomShapePtr> >& theObjectShapes)
389 {
390   theObjectShapes.clear();
391
392   QList<ModuleBase_ViewerPrsPtr> aValues;
393   ModuleBase_ModelWidget* anActiveWidget = theWorkshop->module()->activeWidget();
394   if (anActiveWidget)
395     anActiveWidget->getHighlighted(aValues);
396
397   QList<GeomShapePtr> aShapes;
398   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIIt = aValues.begin(),
399                                               aILast = aValues.end();
400   for (; anIIt != aILast; anIIt++) {
401     ModuleBase_ViewerPrsPtr aPrs = *anIIt;
402     ObjectPtr anObject = aPrs->object();
403
404     GeomShapePtr aGeomShape = aPrs->shape();
405     if (!aGeomShape.get() || aGeomShape->isNull()) {
406       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
407       if (aResult.get()) {
408         aGeomShape = aResult->shape();
409       }
410     }
411     appendShapeIfVisible(theWorkshop, anObject, aGeomShape, theObjectShapes);
412   }
413 }
414
415
416 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
417 {
418   std::string anAttrType = theAttribute->attributeType();
419
420   return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
421          anAttrType == ModelAPI_AttributeRefList::typeId() ||
422          anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
423          anAttrType == ModelAPI_AttributeSelection::typeId() ||
424          anAttrType == ModelAPI_AttributeReference::typeId();
425 }
426
427 void PartSet_OperationPrs::fillShapeList(
428                             const QMap<ObjectPtr, QList<GeomShapePtr> >& theFeatureShapes,
429                             ModuleBase_IWorkshop* theWorkshop,
430                             NCollection_DataMap<TopoDS_Shape,
431                             Handle(AIS_InteractiveObject)>& theShapeToPrsMap)
432 {
433   theShapeToPrsMap.Clear();
434
435   XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
436
437   // create presentations on the base of the shapes
438   QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = theFeatureShapes.begin(),
439                                                         aLast = theFeatureShapes.end();
440   for (; anIt != aLast; anIt++) {
441     ObjectPtr anObject = anIt.key();
442     QList<GeomShapePtr> aShapes = anIt.value();
443     QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
444     for (; aShIt != aShLast; aShIt++) {
445       GeomShapePtr aGeomShape = *aShIt;
446       // the shape should not be checked here on empty value because it should be checked in
447       // appendShapeIfVisible() on the step of filling theFeatureShapes list
448       // the reason is to avoid empty AIS object visualized in the viewer
449       //if (!aGeomShape.get()) continue;
450       TopoDS_Shape aShape = aGeomShape.get() ? aGeomShape->impl<TopoDS_Shape>() : TopoDS_Shape();
451       if (aShape.IsNull())
452         continue;
453
454       // change deviation coefficient to provide more precise circle
455       Handle(AIS_InteractiveObject) anIO;
456       AISObjectPtr anAISPtr = aDisplayer->getAISObject(anObject);
457       if (anAISPtr.get())
458         anIO = anAISPtr->impl<Handle(AIS_InteractiveObject)>();
459       theShapeToPrsMap.Bind(aShape, anIO);
460     }
461   }
462 }