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