Salome HOME
Issue #662 Warning on remove or rename of (may be) used object in PartSet
[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
14 #include <ModelAPI_Result.h>
15 #include <ModelAPI_Attribute.h>
16 #include <ModelAPI_AttributeRefAttr.h>
17 #include <ModelAPI_AttributeReference.h>
18 #include <ModelAPI_AttributeSelection.h>
19 #include <ModelAPI_AttributeSelectionList.h>
20
21 #include <GeomValidators_Tools.h>
22
23 #include <GeomAPI_IPresentable.h>
24
25 #include <StdPrs_WFDeflectionShape.hxx>
26
27 #include <QList>
28
29 IMPLEMENT_STANDARD_HANDLE(PartSet_OperationPrs, ViewerData_AISShape);
30 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
31
32 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
33   : ViewerData_AISShape(TopoDS_Shape()), myFeature(FeaturePtr()), myWorkshop(theWorkshop)
34 {
35 }
36
37 bool PartSet_OperationPrs::canActivate(const FeaturePtr& theFeature)
38 {
39   bool aHasSelectionAttribute = false;
40
41   std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
42   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
43   for (; anIt != aLast && !aHasSelectionAttribute; anIt++)
44     aHasSelectionAttribute = isSelectionAttribute(*anIt);
45
46   return aHasSelectionAttribute;
47 }
48
49 void PartSet_OperationPrs::setFeature(const FeaturePtr& theFeature)
50 {
51   myFeature = theFeature;
52   updateShapes();
53 }
54
55 bool PartSet_OperationPrs::dependOn(const ObjectPtr& theResult)
56 {
57   return myFeatureShapes.contains(theResult);
58 }
59
60 void PartSet_OperationPrs::updateShapes()
61 {
62   myFeatureShapes.clear();
63   getFeatureShapes(myFeatureShapes);
64 }
65
66 void PartSet_OperationPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
67                                    const Handle(Prs3d_Presentation)& thePresentation, 
68                                    const Standard_Integer theMode)
69 {
70   thePresentation->Clear();
71   XGUI_Displayer* aDisplayer = workshop()->displayer();
72
73   // create presentations on the base of the shapes
74   Handle(Prs3d_Drawer) aDrawer = Attributes();
75   QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = myFeatureShapes.begin(),
76                                                         aLast = myFeatureShapes.end();
77   for (; anIt != aLast; anIt++) {
78     ObjectPtr anObject = anIt.key();
79     if (!isVisible(aDisplayer, anObject))
80       continue;
81     QList<GeomShapePtr> aShapes = anIt.value();
82     QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
83     for (; aShIt != aShLast; aShIt++) {
84       GeomShapePtr aGeomShape = *aShIt;
85       TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
86       StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
87     }
88   }
89 }
90
91 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
92                                             const Standard_Integer aMode)
93 {
94   // the presentation is not used in the selection
95 }
96
97 bool PartSet_OperationPrs::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
98 {
99   bool aVisible = false;
100   GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
101   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
102   if (aPrs.get() || aResult.get())
103     aVisible = theDisplayer->isVisible(theObject);
104   else {
105     // check if all results of the feature are visible
106     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
107     std::list<ResultPtr> aResults = aFeature->results();
108     std::list<ResultPtr>::const_iterator aIt;
109     aVisible = !aResults.empty();
110     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
111       aVisible = aVisible && theDisplayer->isVisible(*aIt);
112     }
113   }
114   return aVisible;
115 }
116
117 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
118 {
119   bool isSub = false;
120   CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
121   if (aComposite.get())
122     isSub = aComposite->isSub(theObject);
123
124   return isSub;
125 }
126
127 void addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
128               const FeaturePtr& theFeature,
129               QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
130 {
131   if (theObject.get()) {
132     GeomShapePtr aShape = theShape;
133     if (!aShape.get()) {
134       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
135       if (aResult.get())
136         aShape = aResult->shape();
137     }
138     if (!isSubObject(theObject, theFeature)) {
139       if (theObjectShapes.contains(theObject))
140         theObjectShapes[theObject].append(aShape);
141       else {
142         QList<GeomShapePtr> aShapes;
143         aShapes.append(aShape);
144         theObjectShapes[theObject] = aShapes;
145       }
146     }
147   }
148 }
149
150 void PartSet_OperationPrs::getFeatureShapes(QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
151 {
152   if (!myFeature.get())
153     return;
154
155   QList<GeomShapePtr> aShapes;
156   std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
157   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
158   for (; anIt != aLast; anIt++) {
159     AttributePtr anAttribute = *anIt;
160     if (!isSelectionAttribute(anAttribute))
161       continue;
162
163     std::string anAttrType = anAttribute->attributeType();
164
165     if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
166       std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList = 
167               std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
168       for(int i = 0; i < aCurSelList->size(); i++) {
169         std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
170         ResultPtr aResult = aSelAttribute->context();
171         GeomShapePtr aShape = aSelAttribute->value();
172         addValue(aResult, aShape, myFeature, theObjectShapes);
173       }
174     }
175     else {
176       ObjectPtr anObject;
177       GeomShapePtr aShape;
178       if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
179         AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
180         if (anAttr->isObject()) {
181           anObject = anAttr->object();
182         }
183         else {
184           aShape = PartSet_Tools::findShapeBy2DPoint(anAttr, myWorkshop);
185           // the distance point is not found if the point is selected in the 2nd time
186           // TODO: after debug, this check can be removed
187           if (!aShape.get())
188             continue;
189           anObject = anAttr->attr()->owner();
190         }
191       }
192       if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
193         AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
194         anObject = anAttr->context();
195         aShape = anAttr->value();
196       }
197       if (anAttrType == ModelAPI_AttributeReference::typeId()) {
198         AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
199         anObject = anAttr->value();
200       }
201       addValue(anObject, aShape, myFeature, theObjectShapes);
202     }
203   }
204 }
205
206 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
207 {
208   std::string anAttrType = theAttribute->attributeType();
209
210   return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
211          anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
212          anAttrType == ModelAPI_AttributeSelection::typeId() ||
213          anAttrType == ModelAPI_AttributeReference::typeId();
214 }
215
216 XGUI_Workshop* PartSet_OperationPrs::workshop() const
217 {
218   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
219   return aConnector->workshop();
220 }