Salome HOME
DOF is visualized in label widget: correction of unit tests on Linux
[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 #include "XGUI_Tools.h"
14
15 #include "ModuleBase_Tools.h"
16 #include "ModuleBase_IModule.h"
17 #include <ModuleBase_IPropertyPanel.h>
18 #include <ModuleBase_ModelWidget.h>
19 #include <ModuleBase_ViewerPrs.h>
20
21 #include <ModelAPI_Result.h>
22 #include <ModelAPI_Attribute.h>
23 #include <ModelAPI_AttributeRefAttr.h>
24 #include <ModelAPI_AttributeReference.h>
25 #include <ModelAPI_AttributeSelection.h>
26 #include <ModelAPI_AttributeSelectionList.h>
27 #include <ModelAPI_AttributeRefList.h>
28 #include <ModelAPI_Validator.h>
29 #include <ModelAPI_Session.h>
30 #include <ModelAPI_ResultCompSolid.h>
31
32 #include <Events_Error.h>
33
34 #include <GeomAPI_IPresentable.h>
35
36 #include <StdPrs_WFDeflectionShape.hxx>
37
38 #include <QList>
39
40 //#define DEBUG_EMPTY_SHAPE
41
42 // multi-rotation/translation operation
43 //#define DEBUG_HIDE_COPY_ATTRIBUTE
44 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
45 #include <ModelAPI_AttributeBoolean.h>
46 #include <SketchPlugin_SketchEntity.h>
47 #endif
48
49 IMPLEMENT_STANDARD_HANDLE(PartSet_OperationPrs, ViewerData_AISShape);
50 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
51
52 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
53 : ViewerData_AISShape(TopoDS_Shape()), myWorkshop(theWorkshop), myUseAISWidth(false)
54 {
55   myShapeColor = Quantity_Color(1, 1, 1, Quantity_TOC_RGB);
56 }
57
58 bool PartSet_OperationPrs::hasShapes()
59 {
60   return !myFeatureShapes.empty();
61 }
62
63 void PartSet_OperationPrs::setShapeColor(const Quantity_Color& theColor)
64 {
65   myShapeColor = theColor;
66 }
67
68 void PartSet_OperationPrs::useAISWidth()
69 {
70   myUseAISWidth = true;
71 }
72
73 void PartSet_OperationPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
74                                    const Handle(Prs3d_Presentation)& thePresentation, 
75                                    const Standard_Integer theMode)
76 {
77   SetColor(myShapeColor);
78   thePresentation->Clear();
79
80   XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(myWorkshop)->displayer();
81   Handle(Prs3d_Drawer) aDrawer = Attributes();
82
83   // create presentations on the base of the shapes
84   bool anEmptyAIS = true;
85   QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = myFeatureShapes.begin(),
86                                                         aLast = myFeatureShapes.end();
87   for (; anIt != aLast; anIt++) {
88     ObjectPtr anObject = anIt.key();
89     QList<GeomShapePtr> aShapes = anIt.value();
90     QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
91     for (; aShIt != aShLast; aShIt++) {
92       GeomShapePtr aGeomShape = *aShIt;
93       // the shape should not be checked here on empty value because it should be checked in
94       // appendShapeIfVisible() on the step of filling myFeatureShapes list
95       // the reason is to avoid empty AIS object visualized in the viewer
96       //if (!aGeomShape.get()) continue;
97       TopoDS_Shape aShape = aGeomShape.get() ? aGeomShape->impl<TopoDS_Shape>() : TopoDS_Shape();
98       // change deviation coefficient to provide more precise circle
99       ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
100
101       if (myUseAISWidth) {
102         AISObjectPtr anAISPtr = aDisplayer->getAISObject(anObject);
103         if (anAISPtr.get()) {
104           Handle(AIS_InteractiveObject) anIO = anAISPtr->impl<Handle(AIS_InteractiveObject)>();
105           if (!anIO.IsNull()) {
106             int aWidth = anIO->Width();
107             /// workaround for zero width. Else, there will be a crash
108             if (aWidth == 0) { // width returns of TSolid shape is zero
109               bool isDisplayed = !anIO->GetContext().IsNull();
110               aWidth = PartSet_Tools::getAISDefaultWidth();// default width value
111             }
112             setWidth(aDrawer, aWidth);
113           }
114         }
115       }
116       StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
117       if (anEmptyAIS)
118         anEmptyAIS = false;
119     }
120   }
121   if (anEmptyAIS)
122     Events_Error::throwException("An empty AIS presentation: PartSet_OperationPrs");
123 }
124
125 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
126                                             const Standard_Integer aMode)
127 {
128   // the presentation is not used in the selection
129 }
130
131 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
132 {
133   bool isSub = false;
134   CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
135   if (aComposite.get())
136     isSub = aComposite->isSub(theObject);
137
138   return isSub;
139 }
140
141 void PartSet_OperationPrs::addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
142                                     const FeaturePtr& theFeature, ModuleBase_IWorkshop* theWorkshop,
143                                     QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
144 {
145   if (theObject.get()) {
146     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
147     if (aResult.get()) {
148       ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
149       if (aCompsolidResult.get()) {
150         if (aCompsolidResult->numberOfSubs() > 0) {
151           for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
152             ResultPtr aSubResult = aCompsolidResult->subResult(i);
153             if (aSubResult.get()) {
154               GeomShapePtr aShape;
155               addValue(aSubResult, aShape, theFeature, theWorkshop, theObjectShapes);
156             }
157           }
158           return;
159         }
160       }
161 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
162       else {
163         FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
164         if (aFeature.get()) {
165           AttributeBooleanPtr aCopyAttr = aFeature->data()->boolean(SketchPlugin_SketchEntity::COPY_ID());
166           if (aCopyAttr.get()) {
167             bool isCopy = aCopyAttr->value();
168             if (isCopy)
169               return;
170           }
171         }
172       }
173 #endif
174     }
175
176     GeomShapePtr aShape = theShape;
177     if (!aShape.get()) {
178       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
179       if (aResult.get())
180         aShape = aResult->shape();
181     }
182     if (!isSubObject(theObject, theFeature))
183       appendShapeIfVisible(theWorkshop, theObject, aShape, theObjectShapes);
184   }
185 }
186
187 void PartSet_OperationPrs::appendShapeIfVisible(ModuleBase_IWorkshop* theWorkshop,
188                               const ObjectPtr& theObject,
189                               GeomShapePtr theGeomShape,
190                               QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
191 {
192   XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
193   if (XGUI_Displayer::isVisible(aDisplayer, theObject)) {
194     if (theGeomShape.get()) {
195       if (theObjectShapes.contains(theObject))
196         theObjectShapes[theObject].append(theGeomShape);
197       else {
198         QList<GeomShapePtr> aShapes;
199         aShapes.append(theGeomShape);
200         theObjectShapes[theObject] = aShapes;
201       }
202     } else {
203   #ifdef DEBUG_EMPTY_SHAPE
204       qDebug(QString("Empty shape in result, result: %1")
205               .arg(ModuleBase_Tools::objectInfo(theObject)).toStdString().c_str());
206   #endif
207     }
208   }
209 }
210
211 void PartSet_OperationPrs::getFeatureShapes(const FeaturePtr& theFeature,
212                                             ModuleBase_IWorkshop* theWorkshop,
213                                             QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
214 {
215   theObjectShapes.clear();
216   if (!theFeature.get())
217     return;
218
219   ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
220
221   QList<GeomShapePtr> aShapes;
222   std::list<AttributePtr> anAttributes = theFeature->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(theFeature, 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, theFeature, theWorkshop, 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         ObjectPtr anObject = aCurSelList->object(i);
249         FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
250         // if a feature is stored in the attribute, we should obtain the feature results
251         // e.g. feature rectangle uses parameters feature lines in the attribute
252         if (aFeature.get()) {
253           getResultShapes(aFeature, theWorkshop, theObjectShapes, false);
254         }
255         else {
256           ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
257           if (aResult.get()) {
258             GeomShapePtr aShape = aResult->shape();
259             if (aShape.get())
260               addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
261           }
262         }
263       }
264     }
265     else {
266       ObjectPtr anObject;
267       GeomShapePtr aShape;
268       if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
269         AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
270         if (anAttr->isObject()) {
271           anObject = anAttr->object();
272         }
273         else {
274           AttributePtr anAttribute = anAttr->attr();
275           aShape = PartSet_Tools::findShapeBy2DPoint(anAttribute, theWorkshop);
276           // the distance point is not found if the point is selected in the 2nd time
277           // TODO: after debug, this check can be removed
278           if (!aShape.get())
279             continue;
280           anObject = anAttr->attr()->owner();
281         }
282       }
283       if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
284         AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
285         anObject = anAttr->context();
286         aShape = anAttr->value();
287       }
288       if (anAttrType == ModelAPI_AttributeReference::typeId()) {
289         AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
290         anObject = anAttr->value();
291       }
292       addValue(anObject, aShape, theFeature, theWorkshop, theObjectShapes);
293     }
294   }
295 }
296
297 void PartSet_OperationPrs::getResultShapes(const FeaturePtr& theFeature,
298                                            ModuleBase_IWorkshop* theWorkshop,
299                                            QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes,
300                                            const bool theListShouldBeCleared)
301 {
302   if (theListShouldBeCleared)
303     theObjectShapes.clear();
304
305   if (!theFeature.get())
306     return;
307
308   XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
309
310   std::list<ResultPtr> aFeatureResults = theFeature->results();
311   std::list<ResultPtr>::const_iterator aRIt = aFeatureResults.begin(),
312                                        aRLast = aFeatureResults.end();
313   for (; aRIt != aRLast; aRIt++) {
314     ResultPtr aResult = *aRIt;
315     GeomShapePtr aGeomShape = aResult->shape();
316     appendShapeIfVisible(theWorkshop, aResult, aGeomShape, theObjectShapes);
317   }
318 }
319
320 void PartSet_OperationPrs::getHighlightedShapes(ModuleBase_IWorkshop* theWorkshop,
321                                                 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
322 {
323   theObjectShapes.clear();
324
325   QList<ModuleBase_ViewerPrsPtr> aValues;
326   ModuleBase_IPropertyPanel* aPanel = theWorkshop->propertyPanel();
327   if (aPanel) {
328     ModuleBase_ModelWidget* aWidget = aPanel->activeWidget();
329     if (aWidget) {
330       aWidget->getHighlighted(aValues);
331     }
332   }
333
334   QList<GeomShapePtr> aShapes;
335   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIIt = aValues.begin(),
336                                               aILast = aValues.end();
337   for (; anIIt != aILast; anIIt++) {
338     ModuleBase_ViewerPrsPtr aPrs = *anIIt;
339     ObjectPtr anObject = aPrs->object();
340
341     GeomShapePtr aGeomShape = aPrs->shape();
342     if (!aGeomShape.get() || aGeomShape->isNull()) {
343       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
344       if (aResult.get()) {
345         aGeomShape = aResult->shape();
346       }
347     }
348     appendShapeIfVisible(theWorkshop, anObject, aGeomShape, theObjectShapes);
349   }
350 }
351
352
353 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
354 {
355   std::string anAttrType = theAttribute->attributeType();
356
357   return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
358          anAttrType == ModelAPI_AttributeRefList::typeId() ||
359          anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
360          anAttrType == ModelAPI_AttributeSelection::typeId() ||
361          anAttrType == ModelAPI_AttributeReference::typeId();
362 }