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