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