]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationPrs.cpp
Salome HOME
Selection has been already filtered, so in the setSelection() the filtering flag...
[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 <ModelAPI_Result.h>
11 #include <ModelAPI_Attribute.h>
12 #include <ModelAPI_AttributeRefAttr.h>
13 #include <ModelAPI_AttributeReference.h>
14 #include <ModelAPI_AttributeSelection.h>
15 #include <ModelAPI_AttributeSelectionList.h>
16 #include <GeomValidators_Tools.h>
17
18 #include <StdPrs_WFDeflectionShape.hxx>
19
20 #include <QList>
21
22 IMPLEMENT_STANDARD_HANDLE(PartSet_OperationPrs, ViewerData_AISShape);
23 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
24
25 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
26   : ViewerData_AISShape(TopoDS_Shape()), myFeature(FeaturePtr()), myWorkshop(theWorkshop)
27 {
28 }
29
30 bool PartSet_OperationPrs::canActivate(const FeaturePtr& theFeature)
31 {
32   bool aHasSelectionAttribute = false;
33
34   std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
35   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
36   for (; anIt != aLast && !aHasSelectionAttribute; anIt++)
37     aHasSelectionAttribute = isSelectionAttribute(*anIt);
38
39   return aHasSelectionAttribute;
40 }
41
42 void PartSet_OperationPrs::setFeature(const FeaturePtr& theFeature)
43 {
44   myFeature = theFeature;
45   updateShapes();
46 }
47
48 bool PartSet_OperationPrs::dependOn(const ObjectPtr& theResult)
49 {
50   return myFeatureShapes.contains(theResult);
51 }
52
53 void PartSet_OperationPrs::updateShapes()
54 {
55   myFeatureShapes.clear();
56   getFeatureShapes(myFeatureShapes);
57 }
58
59 void PartSet_OperationPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
60                                    const Handle(Prs3d_Presentation)& thePresentation, 
61                                    const Standard_Integer theMode)
62 {
63   thePresentation->Clear();
64
65   // create presentations on the base of the shapes
66   Handle(Prs3d_Drawer) aDrawer = Attributes();
67   QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = myFeatureShapes.begin(),
68                                                         aLast = myFeatureShapes.end();
69   for (; anIt != aLast; anIt++) {
70     QList<GeomShapePtr> aShapes = anIt.value();
71     QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
72     for (; aShIt != aShLast; aShIt++) {
73       GeomShapePtr aGeomShape = *aShIt;
74       TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
75       StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
76     }
77   }
78 }
79
80 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
81                                             const Standard_Integer aMode)
82 {
83   // the presentation is not used in the selection
84 }
85
86 void addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
87               QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
88 {
89   if (theObjectShapes.contains(theObject))
90     theObjectShapes[theObject].append(theShape);
91   else {
92     QList<GeomShapePtr> aShapes;
93     aShapes.append(theShape);
94     theObjectShapes[theObject] = aShapes;
95   }
96 }
97
98 void PartSet_OperationPrs::getFeatureShapes(QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
99 {
100   if (!myFeature.get())
101     return;
102
103   QList<GeomShapePtr> aShapes;
104   std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
105   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
106   for (; anIt != aLast; anIt++) {
107     AttributePtr anAttribute = *anIt;
108     if (!isSelectionAttribute(anAttribute))
109       continue;
110
111     std::string anAttrType = anAttribute->attributeType();
112
113     if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
114       std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList = 
115               std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
116       for(int i = 0; i < aCurSelList->size(); i++) {
117         std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
118         ResultPtr aResult = aSelAttribute->context();
119         GeomShapePtr aShape = aSelAttribute->value();
120         if (!aShape.get())
121           aShape = aResult->shape();
122         addValue(aResult, aShape, theObjectShapes);
123       }
124     }
125     else {
126       ObjectPtr anObject;
127       GeomShapePtr aShape;
128       if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
129         AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
130         if (anAttr->isObject()) {
131           anObject = anAttr->object();
132         }
133         else {
134           aShape = PartSet_Tools::findShapeBy2DPoint(anAttr, myWorkshop);
135           // the distance point is not found if the point is selected in the 2nd time
136           // TODO: after debug, this check can be removed
137           if (!aShape.get())
138             continue;
139           anObject = anAttr->attr()->owner();
140         }
141       }
142       if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
143         AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
144         anObject = anAttr->context();
145         aShape = anAttr->value();
146       }
147       if (anAttrType == ModelAPI_AttributeReference::typeId()) {
148         AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
149         anObject = anAttr->value();
150       }
151
152       if (anObject.get()) {
153         if (!aShape.get()) {
154           ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
155           if (aResult.get())
156             aShape = aResult->shape();
157         }
158         addValue(anObject, aShape, theObjectShapes);
159       }
160     }
161   }
162 }
163
164 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
165 {
166   std::string anAttrType = theAttribute->attributeType();
167
168   return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
169          anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
170          anAttrType == ModelAPI_AttributeSelection::typeId() ||
171          anAttrType == ModelAPI_AttributeReference::typeId();
172 }