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