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