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