Salome HOME
3204fa097413a71ff10e2b67a2e578ff8a8a5c08
[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_Events.h>
22 #include <ModelAPI_Result.h>
23 #include <ModelAPI_Attribute.h>
24 #include <ModelAPI_AttributeRefAttr.h>
25 #include <ModelAPI_AttributeReference.h>
26 #include <ModelAPI_AttributeSelection.h>
27 #include <ModelAPI_AttributeSelectionList.h>
28 #include <ModelAPI_AttributeRefList.h>
29 #include <ModelAPI_Validator.h>
30 #include <ModelAPI_Session.h>
31 #include <ModelAPI_ResultCompSolid.h>
32 #include <ModelAPI_Tools.h>
33
34 #include <Events_InfoMessage.h>
35 #include <Events_Loop.h>
36
37 #include <GeomAPI_IPresentable.h>
38
39 #include <StdPrs_WFDeflectionShape.hxx>
40
41 #include <QList>
42
43 #include <gp_Pnt.hxx>
44 #include <TopoDS_Vertex.hxx>
45 #include <BRepBuilderAPI_MakeVertex.hxx>
46 #include <BRep_Builder.hxx>
47 #include <TopoDS_Compound.hxx>
48
49 //#define DEBUG_EMPTY_SHAPE
50 //#define DEBUG_OPERATION_PRS
51
52 // multi-rotation/translation operation
53 //#define DEBUG_HIDE_COPY_ATTRIBUTE
54 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
55 #include <ModelAPI_AttributeBoolean.h>
56 #include <SketchPlugin_SketchEntity.h>
57 #endif
58
59 IMPLEMENT_STANDARD_HANDLE(PartSet_OperationPrs, ViewerData_AISShape);
60 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
61
62 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
63 : ViewerData_AISShape(TopoDS_Shape()), myWorkshop(theWorkshop), myUseAISWidth(false)
64 {
65 #ifdef DEBUG_OPERATION_PRS
66   qDebug("PartSet_OperationPrs::PartSet_OperationPrs");
67 #endif
68   myShapeColor = Quantity_Color(1, 1, 1, Quantity_TOC_RGB);
69
70   // first presentation for having correct Compute until presentation with shapes are set
71   gp_Pnt aPnt(0.0, 0.0, 0.0);
72   BRepBuilderAPI_MakeVertex aMaker(aPnt);
73   TopoDS_Vertex aVertex = aMaker.Vertex();
74   myShapeToPrsMap.Bind(aVertex, NULL);
75 }
76
77 bool PartSet_OperationPrs::hasShapes()
78 {
79   return !myShapeToPrsMap.IsEmpty();
80 }
81
82 void PartSet_OperationPrs::setShapeColor(const Quantity_Color& theColor)
83 {
84   myShapeColor = theColor;
85 }
86
87 void PartSet_OperationPrs::useAISWidth()
88 {
89   myUseAISWidth = true;
90 }
91
92 void PartSet_OperationPrs::Compute(
93             const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
94             const Handle(Prs3d_Presentation)& thePresentation, 
95             const Standard_Integer theMode)
96 {
97 #ifdef DEBUG_OPERATION_PRS
98   qDebug("PartSet_OperationPrs::Compute -- begin");
99 #endif
100
101   SetColor(myShapeColor);
102   thePresentation->Clear();
103   bool aReadyToDisplay = !myShapeToPrsMap.IsEmpty();
104
105   XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(myWorkshop)->displayer();
106   Handle(Prs3d_Drawer) aDrawer = Attributes();
107   // create presentations on the base of the shapes
108   BRep_Builder aBuilder;
109   TopoDS_Compound aComp;
110   aBuilder.MakeCompound(aComp);
111   for(NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>::Iterator 
112       anIter(myShapeToPrsMap); anIter.More(); anIter.Next()) {
113     const TopoDS_Shape& aShape = anIter.Key();
114     aBuilder.Add(aComp, aShape);
115     // change deviation coefficient to provide more precise circle
116     // as there is no result, the shape is processed to correct deviation. To be unified
117     ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
118
119     if (myUseAISWidth) {
120       Handle(AIS_InteractiveObject) anIO = anIter.Value();
121       if (!anIO.IsNull()) {
122         int aWidth = anIO->Width();
123         /// workaround for zero width. Else, there will be a crash
124         if (aWidth == 0) { // width returns of TSolid shape is zero
125           bool isDisplayed = !anIO->GetContext().IsNull();
126           aWidth = PartSet_Tools::getAISDefaultWidth();// default width value
127         }
128         setWidth(aDrawer, aWidth);
129       }
130     }
131     StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
132   }
133   Set(aComp);
134   if (!aReadyToDisplay) {
135     Events_InfoMessage("PartSet_OperationPrs", 
136       "An empty AIS presentation: PartSet_OperationPrs").send();
137     std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
138                 new Events_Message(Events_Loop::eventByName(EVENT_EMPTY_OPERATION_PRESENTATION)));
139     Events_Loop::loop()->send(aMsg);
140   }
141 #ifdef DEBUG_OPERATION_PRS
142   qDebug("PartSet_OperationPrs::Compute -- end");
143 #endif
144 }
145
146 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
147                                             const Standard_Integer aMode)
148 {
149   // the presentation is not used in the selection
150 }
151
152 NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>& PartSet_OperationPrs::shapesMap()
153 {
154   return myShapeToPrsMap;
155 }
156
157 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
158 {
159   bool isSub = false;
160   CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
161   if (aComposite.get())
162     isSub = aComposite->isSub(theObject);
163
164   return isSub;
165 }
166
167 void PartSet_OperationPrs::addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
168                                     const FeaturePtr& theFeature, ModuleBase_IWorkshop* theWorkshop,
169                                     QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
170 {
171   if (theObject.get()) {
172     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
173     if (aResult.get()) {
174       ResultCompSolidPtr aCompsolidResult = 
175         std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
176       if (aCompsolidResult.get()) {
177         if (aCompsolidResult->numberOfSubs() > 0) {
178           for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
179             ResultPtr aSubResult = aCompsolidResult->subResult(i);
180             if (aSubResult.get()) {
181               GeomShapePtr aShape;
182               addValue(aSubResult, aShape, theFeature, theWorkshop, theObjectShapes);
183             }
184           }
185           return;
186         }
187       }
188 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
189       else {
190         FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
191         if (aFeature.get()) {
192           AttributeBooleanPtr aCopyAttr = 
193             aFeature->data()->boolean(SketchPlugin_SketchEntity::COPY_ID());
194           if (aCopyAttr.get()) {
195             bool isCopy = aCopyAttr->value();
196             if (isCopy)
197               return;
198           }
199         }
200       }
201 #endif
202     }
203
204     GeomShapePtr aShape = theShape;
205     if (!aShape.get()) {
206       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
207       if (aResult.get())
208         aShape = aResult->shape();
209     }
210     if (!isSubObject(theObject, theFeature))
211       appendShapeIfVisible(theWorkshop, theObject, aShape, theObjectShapes);
212   }
213 }
214
215 void PartSet_OperationPrs::appendShapeIfVisible(ModuleBase_IWorkshop* theWorkshop,
216                               const ObjectPtr& theObject,
217                               GeomShapePtr theGeomShape,
218                               QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
219 {
220   XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
221   if (XGUI_Displayer::isVisible(aDisplayer, theObject)) {
222     if (theGeomShape.get()) {
223       if (theObjectShapes.contains(theObject))
224         theObjectShapes[theObject].append(theGeomShape);
225       else {
226         QList<GeomShapePtr> aShapes;
227         aShapes.append(theGeomShape);
228         theObjectShapes[theObject] = aShapes;
229       }
230     } else {
231   #ifdef DEBUG_EMPTY_SHAPE
232       qDebug(QString("Empty shape in result, result: %1")
233               .arg(ModuleBase_Tools::objectInfo(theObject)).toStdString().c_str());
234   #endif
235     }
236   }
237 }
238
239 void PartSet_OperationPrs::getFeatureShapes(const FeaturePtr& theFeature,
240                                             ModuleBase_IWorkshop* theWorkshop,
241                                             QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
242 {
243   theObjectShapes.clear();
244   if (!theFeature.get())
245     return;
246
247   ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
248
249   QList<GeomShapePtr> aShapes;
250   std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
251   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
252   for (; anIt != aLast; anIt++) {
253     AttributePtr anAttribute = *anIt;
254     if (!isSelectionAttribute(anAttribute))
255       continue;
256
257     if (!aValidators->isCase(theFeature, anAttribute->id()))
258       continue; // this attribute is not participated in the current case
259
260     std::string anAttrType = anAttribute->attributeType();
261
262     if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
263       std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList = 
264               std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
265       for(int i = 0; i < aCurSelList->size(); i++) {
266         std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
267         ResultPtr aResult = aSelAttribute->context();
268         GeomShapePtr aShape = aSelAttribute->value();
269         addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
270       }
271     }
272     if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
273       std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
274         std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
275       for (int i = 0; i < aCurSelList->size(); i++) {
276         ObjectPtr anObject = aCurSelList->object(i);
277         FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
278         // if a feature is stored in the attribute, we should obtain the feature results
279         // e.g. feature rectangle uses parameters feature lines in the attribute
280         if (aFeature.get()) {
281           getResultShapes(aFeature, theWorkshop, theObjectShapes, false);
282         }
283         else {
284           ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
285           if (aResult.get()) {
286             GeomShapePtr aShape = aResult->shape();
287             if (aShape.get())
288               addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
289           }
290         }
291       }
292     }
293     else {
294       ObjectPtr anObject;
295       GeomShapePtr aShape;
296       if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
297         AttributeRefAttrPtr anAttr = 
298           std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
299         if (anAttr->isObject()) {
300           anObject = anAttr->object();
301         }
302         else {
303           AttributePtr anAttribute = anAttr->attr();
304           aShape = PartSet_Tools::findShapeBy2DPoint(anAttribute, theWorkshop);
305           // the distance point is not found if the point is selected in the 2nd time
306           // TODO: after debug, this check can be removed
307           if (!aShape.get())
308             continue;
309           anObject = anAttr->attr()->owner();
310         }
311       }
312       if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
313         AttributeSelectionPtr anAttr = 
314           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 = 
320           std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
321         anObject = anAttr->value();
322       }
323       addValue(anObject, aShape, theFeature, theWorkshop, theObjectShapes);
324     }
325   }
326 }
327
328 void PartSet_OperationPrs::getResultShapes(const FeaturePtr& theFeature,
329                                            ModuleBase_IWorkshop* theWorkshop,
330                                            QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes,
331                                            const bool theListShouldBeCleared)
332 {
333   if (theListShouldBeCleared)
334     theObjectShapes.clear();
335
336   if (!theFeature.get())
337     return;
338
339   XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
340
341   std::list<ResultPtr> aResults;
342   ModelAPI_Tools::allResults(theFeature, aResults);
343   std::list<ResultPtr>::const_iterator aRIt = aResults.begin(),
344                                        aRLast = aResults.end();
345   for (; aRIt != aRLast; aRIt++) {
346     ResultPtr aResult = *aRIt;
347     GeomShapePtr aGeomShape = aResult->shape();
348     appendShapeIfVisible(theWorkshop, aResult, aGeomShape, theObjectShapes);
349   }
350 }
351
352 void PartSet_OperationPrs::getHighlightedShapes(ModuleBase_IWorkshop* theWorkshop,
353                                                 QMap<ObjectPtr, 
354                                                 QList<GeomShapePtr> >& theObjectShapes)
355 {
356   theObjectShapes.clear();
357
358   QList<ModuleBase_ViewerPrsPtr> aValues;
359   ModuleBase_ModelWidget* anActiveWidget = theWorkshop->module()->activeWidget();
360   if (anActiveWidget)
361     anActiveWidget->getHighlighted(aValues);
362
363   QList<GeomShapePtr> aShapes;
364   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIIt = aValues.begin(),
365                                               aILast = aValues.end();
366   for (; anIIt != aILast; anIIt++) {
367     ModuleBase_ViewerPrsPtr aPrs = *anIIt;
368     ObjectPtr anObject = aPrs->object();
369
370     GeomShapePtr aGeomShape = aPrs->shape();
371     if (!aGeomShape.get() || aGeomShape->isNull()) {
372       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
373       if (aResult.get()) {
374         aGeomShape = aResult->shape();
375       }
376     }
377     appendShapeIfVisible(theWorkshop, anObject, aGeomShape, theObjectShapes);
378   }
379 }
380
381
382 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
383 {
384   std::string anAttrType = theAttribute->attributeType();
385
386   return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
387          anAttrType == ModelAPI_AttributeRefList::typeId() ||
388          anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
389          anAttrType == ModelAPI_AttributeSelection::typeId() ||
390          anAttrType == ModelAPI_AttributeReference::typeId();
391 }
392
393 void PartSet_OperationPrs::fillShapeList(
394                             const QMap<ObjectPtr, QList<GeomShapePtr> >& theFeatureShapes,
395                             ModuleBase_IWorkshop* theWorkshop,
396                             NCollection_DataMap<TopoDS_Shape, 
397                             Handle(AIS_InteractiveObject)>& theShapeToPrsMap)
398 {
399   theShapeToPrsMap.Clear();
400
401   XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
402
403   // create presentations on the base of the shapes
404   QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = theFeatureShapes.begin(),
405                                                         aLast = theFeatureShapes.end();
406   for (; anIt != aLast; anIt++) {
407     ObjectPtr anObject = anIt.key();
408     QList<GeomShapePtr> aShapes = anIt.value();
409     QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
410     for (; aShIt != aShLast; aShIt++) {
411       GeomShapePtr aGeomShape = *aShIt;
412       // the shape should not be checked here on empty value because it should be checked in
413       // appendShapeIfVisible() on the step of filling theFeatureShapes list
414       // the reason is to avoid empty AIS object visualized in the viewer
415       //if (!aGeomShape.get()) continue;
416       TopoDS_Shape aShape = aGeomShape.get() ? aGeomShape->impl<TopoDS_Shape>() : TopoDS_Shape();
417       if (aShape.IsNull())
418         continue;
419
420       // change deviation coefficient to provide more precise circle
421       Handle(AIS_InteractiveObject) anIO;
422       AISObjectPtr anAISPtr = aDisplayer->getAISObject(anObject);
423       if (anAISPtr.get())
424         anIO = anAISPtr->impl<Handle(AIS_InteractiveObject)>();
425       theShapeToPrsMap.Bind(aShape, anIO);
426     }
427   }
428 }