Salome HOME
#1119 Confirmation box for deleting parts
[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
20 #include <ModelAPI_Result.h>
21 #include <ModelAPI_Attribute.h>
22 #include <ModelAPI_AttributeRefAttr.h>
23 #include <ModelAPI_AttributeReference.h>
24 #include <ModelAPI_AttributeSelection.h>
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include <ModelAPI_AttributeRefList.h>
27 #include <ModelAPI_Validator.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_ResultCompSolid.h>
30
31 #include <Events_Error.h>
32
33 #include <GeomAPI_IPresentable.h>
34
35 #include <StdPrs_WFDeflectionShape.hxx>
36
37 #include <QList>
38
39 static const int AIS_DEFAULT_WIDTH = 2;
40
41 //#define DEBUG_EMPTY_SHAPE
42
43 // multi-rotation/translation operation
44 //#define DEBUG_HIDE_COPY_ATTRIBUTE
45 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
46 #include <ModelAPI_AttributeBoolean.h>
47 #include <SketchPlugin_SketchEntity.h>
48 #endif
49
50 IMPLEMENT_STANDARD_HANDLE(PartSet_OperationPrs, ViewerData_AISShape);
51 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
52
53 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
54 : ViewerData_AISShape(TopoDS_Shape()), myWorkshop(theWorkshop), myUseAISWidth(false)
55 {
56   myShapeColor = Quantity_Color(1, 1, 1, Quantity_TOC_RGB);
57 }
58
59 bool PartSet_OperationPrs::hasShapes()
60 {
61   return !myFeatureShapes.empty();
62 }
63
64 void PartSet_OperationPrs::setShapeColor(const Quantity_Color& theColor)
65 {
66   myShapeColor = theColor;
67 }
68
69 void PartSet_OperationPrs::useAISWidth()
70 {
71   myUseAISWidth = true;
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   SetColor(myShapeColor);
79   thePresentation->Clear();
80
81   XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(myWorkshop)->displayer();
82   Handle(Prs3d_Drawer) aDrawer = Attributes();
83
84   // create presentations on the base of the shapes
85   bool anEmptyAIS = true;
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     QList<GeomShapePtr> aShapes = anIt.value();
91     QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
92     for (; aShIt != aShLast; aShIt++) {
93       GeomShapePtr aGeomShape = *aShIt;
94       // the shape should not be checked here on empty value because it should be checked in
95       // appendShapeIfVisible() on the step of filling myFeatureShapes list
96       // the reason is to avoid empty AIS object visualized in the viewer
97       //if (!aGeomShape.get()) 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
102       if (myUseAISWidth) {
103         AISObjectPtr anAISPtr = aDisplayer->getAISObject(anObject);
104         if (anAISPtr.get()) {
105           Handle(AIS_InteractiveObject) anIO = anAISPtr->impl<Handle(AIS_InteractiveObject)>();
106           if (!anIO.IsNull()) {
107             int aWidth = anIO->Width();
108             /// workaround for zero width. Else, there will be a crash
109             if (aWidth == 0) { // width returns of TSolid shape is zero
110               bool isDisplayed = !anIO->GetContext().IsNull();
111               aWidth = AIS_DEFAULT_WIDTH;// default width value
112             }
113             setWidth(aDrawer, aWidth);
114           }
115         }
116       }
117       StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
118       if (anEmptyAIS)
119         anEmptyAIS = false;
120     }
121   }
122   if (anEmptyAIS)
123     Events_Error::throwException("An empty AIS presentation: PartSet_OperationPrs");
124 }
125
126 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
127                                             const Standard_Integer aMode)
128 {
129   // the presentation is not used in the selection
130 }
131
132 bool PartSet_OperationPrs::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
133 {
134   bool aVisible = false;
135   GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
136   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
137   if (aPrs.get() || aResult.get()) {
138     aVisible = theDisplayer->isVisible(theObject);
139     // compsolid is not visualized in the viewer, but should have presentation when all sub solids are
140     // visible. It is useful for highlight presentation where compsolid shape is selectable
141     if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultCompSolid::group()) {
142       ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
143       if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
144         bool anAllSubsVisible = aCompsolidResult->numberOfSubs() > 0;
145         for(int i = 0; i < aCompsolidResult->numberOfSubs() && anAllSubsVisible; i++) {
146           anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
147         }
148         aVisible = anAllSubsVisible;
149       }
150     }
151   }
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 PartSet_OperationPrs::addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
176                                     const FeaturePtr& theFeature, ModuleBase_IWorkshop* theWorkshop,
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         if (aCompsolidResult->numberOfSubs() > 0) {
185           for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
186             ResultPtr aSubResult = aCompsolidResult->subResult(i);
187             if (aSubResult.get()) {
188               GeomShapePtr aShape;
189               addValue(aSubResult, aShape, theFeature, theWorkshop, theObjectShapes);
190             }
191           }
192           return;
193         }
194       }
195 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
196       else {
197         FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
198         if (aFeature.get()) {
199           AttributeBooleanPtr aCopyAttr = aFeature->data()->boolean(SketchPlugin_SketchEntity::COPY_ID());
200           if (aCopyAttr.get()) {
201             bool isCopy = aCopyAttr->value();
202             if (isCopy)
203               return;
204           }
205         }
206       }
207 #endif
208     }
209
210     GeomShapePtr aShape = theShape;
211     if (!aShape.get()) {
212       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
213       if (aResult.get())
214         aShape = aResult->shape();
215     }
216     if (!isSubObject(theObject, theFeature))
217       appendShapeIfVisible(theWorkshop, theObject, aShape, theObjectShapes);
218   }
219 }
220
221 void PartSet_OperationPrs::appendShapeIfVisible(ModuleBase_IWorkshop* theWorkshop,
222                               const ObjectPtr& theObject,
223                               GeomShapePtr theGeomShape,
224                               QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
225 {
226   XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
227   if (isVisible(aDisplayer, theObject)) {
228     if (theGeomShape.get()) {
229       if (theObjectShapes.contains(theObject))
230         theObjectShapes[theObject].append(theGeomShape);
231       else {
232         QList<GeomShapePtr> aShapes;
233         aShapes.append(theGeomShape);
234         theObjectShapes[theObject] = aShapes;
235       }
236     } else {
237   #ifdef DEBUG_EMPTY_SHAPE
238       qDebug(QString("Empty shape in result, result: %1")
239               .arg(ModuleBase_Tools::objectInfo(theObject)).toStdString().c_str());
240   #endif
241     }
242   }
243 }
244
245 void PartSet_OperationPrs::getFeatureShapes(const FeaturePtr& theFeature,
246                                             ModuleBase_IWorkshop* theWorkshop,
247                                             QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
248 {
249   theObjectShapes.clear();
250   if (!theFeature.get())
251     return;
252
253   ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
254
255   QList<GeomShapePtr> aShapes;
256   std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
257   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
258   for (; anIt != aLast; anIt++) {
259     AttributePtr anAttribute = *anIt;
260     if (!isSelectionAttribute(anAttribute))
261       continue;
262
263     if (!aValidators->isCase(theFeature, anAttribute->id()))
264       continue; // this attribute is not participated in the current case
265
266     std::string anAttrType = anAttribute->attributeType();
267
268     if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
269       std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList = 
270               std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
271       for(int i = 0; i < aCurSelList->size(); i++) {
272         std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
273         ResultPtr aResult = aSelAttribute->context();
274         GeomShapePtr aShape = aSelAttribute->value();
275         addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
276       }
277     }
278     if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
279       std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
280         std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
281       for (int i = 0; i < aCurSelList->size(); i++) {
282         ObjectPtr anObject = aCurSelList->object(i);
283         FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
284         // if a feature is stored in the attribute, we should obtain the feature results
285         // e.g. feature rectangle uses parameters feature lines in the attribute
286         if (aFeature.get()) {
287           getResultShapes(aFeature, theWorkshop, theObjectShapes, false);
288         }
289         else {
290           ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
291           if (aResult.get()) {
292             GeomShapePtr aShape = aResult->shape();
293             if (aShape.get())
294               addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
295           }
296         }
297       }
298     }
299     else {
300       ObjectPtr anObject;
301       GeomShapePtr aShape;
302       if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
303         AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
304         if (anAttr->isObject()) {
305           anObject = anAttr->object();
306         }
307         else {
308           AttributePtr anAttribute = anAttr->attr();
309           aShape = PartSet_Tools::findShapeBy2DPoint(anAttribute, theWorkshop);
310           // the distance point is not found if the point is selected in the 2nd time
311           // TODO: after debug, this check can be removed
312           if (!aShape.get())
313             continue;
314           anObject = anAttr->attr()->owner();
315         }
316       }
317       if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
318         AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
319         anObject = anAttr->context();
320         aShape = anAttr->value();
321       }
322       if (anAttrType == ModelAPI_AttributeReference::typeId()) {
323         AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
324         anObject = anAttr->value();
325       }
326       addValue(anObject, aShape, theFeature, theWorkshop, theObjectShapes);
327     }
328   }
329 }
330
331 void PartSet_OperationPrs::getResultShapes(const FeaturePtr& theFeature,
332                                            ModuleBase_IWorkshop* theWorkshop,
333                                            QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes,
334                                            const bool theListShouldBeCleared)
335 {
336   if (theListShouldBeCleared)
337     theObjectShapes.clear();
338
339   if (!theFeature.get())
340     return;
341
342   XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
343
344   std::list<ResultPtr> aFeatureResults = theFeature->results();
345   std::list<ResultPtr>::const_iterator aRIt = aFeatureResults.begin(),
346                                        aRLast = aFeatureResults.end();
347   for (; aRIt != aRLast; aRIt++) {
348     ResultPtr aResult = *aRIt;
349     GeomShapePtr aGeomShape = aResult->shape();
350     appendShapeIfVisible(theWorkshop, aResult, aGeomShape, theObjectShapes);
351   }
352 }
353
354 void PartSet_OperationPrs::getHighlightedShapes(ModuleBase_IWorkshop* theWorkshop,
355                                                 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
356 {
357   theObjectShapes.clear();
358
359   QList<ModuleBase_ViewerPrs> aValues;
360   ModuleBase_IPropertyPanel* aPanel = theWorkshop->propertyPanel();
361   if (aPanel) {
362     ModuleBase_ModelWidget* aWidget = aPanel->activeWidget();
363     if (aWidget) {
364       aWidget->getHighlighted(aValues);
365     }
366   }
367
368   QList<GeomShapePtr> aShapes;
369   QList<ModuleBase_ViewerPrs>::const_iterator anIIt = aValues.begin(),
370                                               aILast = aValues.end();
371   for (; anIIt != aILast; anIIt++) {
372     ModuleBase_ViewerPrs aPrs = *anIIt;
373     ObjectPtr anObject = aPrs.object();
374
375     GeomShapePtr aGeomShape;
376
377     TopoDS_Shape aShape = aPrs.shape();
378     if (!aShape.IsNull()) {
379       aGeomShape = GeomShapePtr(new GeomAPI_Shape());
380       aGeomShape->setImpl(new TopoDS_Shape(aShape));
381     }
382     else {
383       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
384       if (aResult.get()) {
385         aGeomShape = aResult->shape();
386       }
387     }
388     appendShapeIfVisible(theWorkshop, anObject, aGeomShape, theObjectShapes);
389   }
390 }
391
392
393 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
394 {
395   std::string anAttrType = theAttribute->attributeType();
396
397   return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
398          anAttrType == ModelAPI_AttributeRefList::typeId() ||
399          anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
400          anAttrType == ModelAPI_AttributeSelection::typeId() ||
401          anAttrType == ModelAPI_AttributeReference::typeId();
402 }