Salome HOME
Merge branch 'Dev_1.3.0' of newgeom:newgeom into Dev_1.3.0
[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               QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
129 {
130   if (theObjectShapes.contains(theObject))
131     theObjectShapes[theObject].append(theShape);
132   else {
133     QList<GeomShapePtr> aShapes;
134     aShapes.append(theShape);
135     theObjectShapes[theObject] = aShapes;
136   }
137 }
138
139 void PartSet_OperationPrs::getFeatureShapes(QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
140 {
141   if (!myFeature.get())
142     return;
143
144   QList<GeomShapePtr> aShapes;
145   std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
146   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
147   for (; anIt != aLast; anIt++) {
148     AttributePtr anAttribute = *anIt;
149     if (!isSelectionAttribute(anAttribute))
150       continue;
151
152     std::string anAttrType = anAttribute->attributeType();
153
154     if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
155       std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList = 
156               std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
157       for(int i = 0; i < aCurSelList->size(); i++) {
158         std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
159         ResultPtr aResult = aSelAttribute->context();
160         GeomShapePtr aShape = aSelAttribute->value();
161         if (!aShape.get())
162           aShape = aResult->shape();
163         if (!isSubObject(aResult, myFeature))
164           addValue(aResult, aShape, theObjectShapes);
165       }
166     }
167     else {
168       ObjectPtr anObject;
169       GeomShapePtr aShape;
170       if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
171         AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
172         if (anAttr->isObject()) {
173           anObject = anAttr->object();
174         }
175         else {
176           aShape = PartSet_Tools::findShapeBy2DPoint(anAttr, myWorkshop);
177           // the distance point is not found if the point is selected in the 2nd time
178           // TODO: after debug, this check can be removed
179           if (!aShape.get())
180             continue;
181           anObject = anAttr->attr()->owner();
182         }
183       }
184       if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
185         AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
186         anObject = anAttr->context();
187         aShape = anAttr->value();
188       }
189       if (anAttrType == ModelAPI_AttributeReference::typeId()) {
190         AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
191         anObject = anAttr->value();
192       }
193
194       if (anObject.get()) {
195         if (!aShape.get()) {
196           ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
197           if (aResult.get())
198             aShape = aResult->shape();
199         }
200         if (!isSubObject(anObject, myFeature))
201           addValue(anObject, aShape, theObjectShapes);
202       }
203     }
204   }
205 }
206
207 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
208 {
209   std::string anAttrType = theAttribute->attributeType();
210
211   return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
212          anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
213          anAttrType == ModelAPI_AttributeSelection::typeId() ||
214          anAttrType == ModelAPI_AttributeReference::typeId();
215 }
216
217 XGUI_Workshop* PartSet_OperationPrs::workshop() const
218 {
219   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
220   return aConnector->workshop();
221 }