Salome HOME
OperationPrs: do not visualize parameters of feature, if they are not inCase. Example...
[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 "ModuleBase_Tools.h"
15
16 #include <ModelAPI_Result.h>
17 #include <ModelAPI_Attribute.h>
18 #include <ModelAPI_AttributeRefAttr.h>
19 #include <ModelAPI_AttributeReference.h>
20 #include <ModelAPI_AttributeSelection.h>
21 #include <ModelAPI_AttributeSelectionList.h>
22 #include <ModelAPI_AttributeRefList.h>
23 #include <ModelAPI_Validator.h>
24 #include <ModelAPI_Session.h>
25
26 #include <GeomValidators_Tools.h>
27
28 #include <GeomAPI_IPresentable.h>
29
30 #include <StdPrs_WFDeflectionShape.hxx>
31
32 #include <QList>
33
34 IMPLEMENT_STANDARD_HANDLE(PartSet_OperationPrs, ViewerData_AISShape);
35 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
36
37 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
38   : ViewerData_AISShape(TopoDS_Shape()), myFeature(FeaturePtr()), myWorkshop(theWorkshop)
39 {
40 }
41
42 bool PartSet_OperationPrs::canActivate(const FeaturePtr& theFeature)
43 {
44   bool aHasSelectionAttribute = false;
45
46   std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
47   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
48   for (; anIt != aLast && !aHasSelectionAttribute; anIt++)
49     aHasSelectionAttribute = isSelectionAttribute(*anIt);
50
51   return aHasSelectionAttribute;
52 }
53
54 void PartSet_OperationPrs::setFeature(const FeaturePtr& theFeature)
55 {
56   myFeature = theFeature;
57   updateShapes();
58 }
59
60 /*bool PartSet_OperationPrs::dependOn(const ObjectPtr& theResult)
61 {
62   return myFeatureShapes.contains(theResult);
63 }*/
64
65 void PartSet_OperationPrs::updateShapes()
66 {
67   myFeatureShapes.clear();
68   getFeatureShapes(myFeatureShapes);
69
70   myFeatureResults.clear();
71   if (myFeature)
72     myFeatureResults = myFeature->results();
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   Quantity_Color aColor(1., 1., 0., Quantity_TOC_RGB); // yellow
80   SetColor(aColor);
81
82   thePresentation->Clear();
83   XGUI_Displayer* aDisplayer = workshop()->displayer();
84
85   // create presentations on the base of the shapes
86   Handle(Prs3d_Drawer) aDrawer = Attributes();
87   QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = myFeatureShapes.begin(),
88                                                         aLast = myFeatureShapes.end();
89   for (; anIt != aLast; anIt++) {
90     ObjectPtr anObject = anIt.key();
91     if (!isVisible(aDisplayer, anObject))
92       continue;
93     QList<GeomShapePtr> aShapes = anIt.value();
94     QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
95     for (; aShIt != aShLast; aShIt++) {
96       GeomShapePtr aGeomShape = *aShIt;
97       if (!aGeomShape.get())
98         continue;
99       TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
100       // change deviation coefficient to provide more precise circle
101       ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
102       StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
103     }
104   }
105
106   aColor = Quantity_Color(0., 1., 0., Quantity_TOC_RGB); // green
107   SetColor(aColor);
108
109   std::list<ResultPtr>::const_iterator aRIt = myFeatureResults.begin(),
110                                        aRLast = myFeatureResults.end();
111   for (; aRIt != aRLast; aRIt++) {
112     ResultPtr aResult = *aRIt;
113     if (!isVisible(aDisplayer, aResult))
114       continue;
115     GeomShapePtr aGeomShape = aResult->shape();
116     if (!aGeomShape.get())
117       continue;
118     TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
119     // change deviation coefficient to provide more precise circle
120     ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
121     StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
122   }
123 }
124
125 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
126                                             const Standard_Integer aMode)
127 {
128   // the presentation is not used in the selection
129 }
130
131 bool PartSet_OperationPrs::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
132 {
133   bool aVisible = false;
134   GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
135   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
136   if (aPrs.get() || aResult.get())
137     aVisible = theDisplayer->isVisible(theObject);
138   else {
139     // check if all results of the feature are visible
140     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
141     std::list<ResultPtr> aResults = aFeature->results();
142     std::list<ResultPtr>::const_iterator aIt;
143     aVisible = !aResults.empty();
144     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
145       aVisible = aVisible && theDisplayer->isVisible(*aIt);
146     }
147   }
148   return aVisible;
149 }
150
151 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
152 {
153   bool isSub = false;
154   CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
155   if (aComposite.get())
156     isSub = aComposite->isSub(theObject);
157
158   return isSub;
159 }
160
161 void addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
162               const FeaturePtr& theFeature,
163               QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
164 {
165   if (theObject.get()) {
166     GeomShapePtr aShape = theShape;
167     if (!aShape.get()) {
168       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
169       if (aResult.get())
170         aShape = aResult->shape();
171     }
172     if (!isSubObject(theObject, theFeature)) {
173       if (theObjectShapes.contains(theObject))
174         theObjectShapes[theObject].append(aShape);
175       else {
176         QList<GeomShapePtr> aShapes;
177         aShapes.append(aShape);
178         theObjectShapes[theObject] = aShapes;
179       }
180     }
181   }
182 }
183
184 void PartSet_OperationPrs::getFeatureShapes(QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
185 {
186   if (!myFeature.get())
187     return;
188
189   ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
190
191   QList<GeomShapePtr> aShapes;
192   std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
193   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
194   for (; anIt != aLast; anIt++) {
195     AttributePtr anAttribute = *anIt;
196     if (!isSelectionAttribute(anAttribute))
197       continue;
198
199     if (!aValidators->isCase(myFeature, anAttribute->id()))
200       continue; // this attribute is not participated in the current case
201
202     std::string anAttrType = anAttribute->attributeType();
203
204     if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
205       std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList = 
206               std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
207       for(int i = 0; i < aCurSelList->size(); i++) {
208         std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
209         ResultPtr aResult = aSelAttribute->context();
210         GeomShapePtr aShape = aSelAttribute->value();
211         addValue(aResult, aShape, myFeature, theObjectShapes);
212       }
213     }
214     if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
215       std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
216         std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
217       for (int i = 0; i < aCurSelList->size(); i++) {
218         GeomShapePtr aShape;
219         addValue(aCurSelList->object(i), aShape, myFeature, theObjectShapes);
220       }
221     }
222     else {
223       ObjectPtr anObject;
224       GeomShapePtr aShape;
225       if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
226         AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
227         if (anAttr->isObject()) {
228           anObject = anAttr->object();
229         }
230         else {
231           aShape = PartSet_Tools::findShapeBy2DPoint(anAttr, myWorkshop);
232           // the distance point is not found if the point is selected in the 2nd time
233           // TODO: after debug, this check can be removed
234           if (!aShape.get())
235             continue;
236           anObject = anAttr->attr()->owner();
237         }
238       }
239       if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
240         AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
241         anObject = anAttr->context();
242         aShape = anAttr->value();
243       }
244       if (anAttrType == ModelAPI_AttributeReference::typeId()) {
245         AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
246         anObject = anAttr->value();
247       }
248       addValue(anObject, aShape, myFeature, theObjectShapes);
249     }
250   }
251 }
252
253 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
254 {
255   std::string anAttrType = theAttribute->attributeType();
256
257   return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
258          anAttrType == ModelAPI_AttributeRefList::typeId() ||
259          anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
260          anAttrType == ModelAPI_AttributeSelection::typeId() ||
261          anAttrType == ModelAPI_AttributeReference::typeId();
262 }
263
264 XGUI_Workshop* PartSet_OperationPrs::workshop() const
265 {
266   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
267   return aConnector->workshop();
268 }