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