Salome HOME
Get rid of compilation warnings. Part II. MSVC warnings.
[modules/shaper.git] / src / PartSet / PartSet_OperationPrs.cpp
1 // Copyright (C) 2014-2020  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "PartSet_OperationPrs.h"
21 #include "PartSet_Tools.h"
22
23 #include "XGUI_Workshop.h"
24 #include "XGUI_ModuleConnector.h"
25 #include "XGUI_Displayer.h"
26 #include "XGUI_Tools.h"
27
28 #include "ModuleBase_Tools.h"
29 #include "ModuleBase_IModule.h"
30 #include <ModuleBase_IPropertyPanel.h>
31 #include <ModuleBase_ModelWidget.h>
32 #include <ModuleBase_ViewerPrs.h>
33
34 #include <ModelAPI_Events.h>
35 #include <ModelAPI_Result.h>
36 #include <ModelAPI_Attribute.h>
37 #include <ModelAPI_AttributeRefAttr.h>
38 #include <ModelAPI_AttributeReference.h>
39 #include <ModelAPI_AttributeSelection.h>
40 #include <ModelAPI_AttributeSelectionList.h>
41 #include <ModelAPI_AttributeRefList.h>
42 #include <ModelAPI_Validator.h>
43 #include <ModelAPI_Session.h>
44 #include <ModelAPI_ResultBody.h>
45 #include <ModelAPI_Tools.h>
46
47 #include <Events_InfoMessage.h>
48 #include <Events_Loop.h>
49
50 #include <GeomAPI_IPresentable.h>
51
52 #include <StdPrs_WFShape.hxx>
53
54 #include <QList>
55
56 #include <gp_Pnt.hxx>
57 #include <TopoDS_Vertex.hxx>
58 #include <BRepBuilderAPI_MakeVertex.hxx>
59 #include <BRep_Builder.hxx>
60 #include <TopoDS_Compound.hxx>
61 #include <Prs3d_IsoAspect.hxx>
62
63 //#define DEBUG_EMPTY_SHAPE
64 //#define DEBUG_OPERATION_PRS
65
66 // multi-rotation/translation operation
67 //#define DEBUG_HIDE_COPY_ATTRIBUTE
68 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
69 #include <ModelAPI_AttributeBoolean.h>
70 #include <SketchPlugin_SketchEntity.h>
71 #endif
72
73 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
74
75 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
76 : ViewerData_AISShape(TopoDS_Shape()), myWorkshop(theWorkshop), myUseAISWidth(false)
77 {
78 #ifdef DEBUG_OPERATION_PRS
79   qDebug("PartSet_OperationPrs::PartSet_OperationPrs");
80 #endif
81   myShapeColor = Quantity_Color(1, 1, 1, Quantity_TOC_RGB);
82
83   // first presentation for having correct Compute until presentation with shapes are set
84   gp_Pnt aPnt(0.0, 0.0, 0.0);
85   BRepBuilderAPI_MakeVertex aMaker(aPnt);
86   TopoDS_Vertex aVertex = aMaker.Vertex();
87   myShapeToPrsMap.Bind(aVertex, NULL);
88
89   Handle(Prs3d_Drawer) aDrawer = Attributes();
90   Handle(Prs3d_IsoAspect) aUIsoAspect = new Prs3d_IsoAspect(myShapeColor, Aspect_TOL_SOLID, 1, 0);
91   Handle(Prs3d_IsoAspect) aVIsoAspect = new Prs3d_IsoAspect(myShapeColor, Aspect_TOL_SOLID, 1, 0);
92   aDrawer->SetUIsoAspect(aUIsoAspect);
93   aDrawer->SetVIsoAspect(aVIsoAspect);
94   aDrawer->SetIsoOnPlane(false);
95   //aDrawer->SetTypeOfDeflection(Aspect_TOD_ABSOLUTE);
96 }
97
98 bool PartSet_OperationPrs::hasShapes()
99 {
100   return !myShapeToPrsMap.IsEmpty();
101 }
102
103 void PartSet_OperationPrs::setShapeColor(const Quantity_Color& theColor)
104 {
105   myShapeColor = theColor;
106 }
107
108 void PartSet_OperationPrs::useAISWidth()
109 {
110   myUseAISWidth = true;
111 }
112
113 void PartSet_OperationPrs::Compute(
114             const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
115             const Handle(Prs3d_Presentation)& thePresentation,
116             const Standard_Integer theMode)
117 {
118 #ifdef DEBUG_OPERATION_PRS
119   qDebug("PartSet_OperationPrs::Compute -- begin");
120 #endif
121
122   SetColor(myShapeColor);
123   thePresentation->Clear();
124   bool aReadyToDisplay = !myShapeToPrsMap.IsEmpty();
125
126   Handle(Prs3d_Drawer) aDrawer = Attributes();
127   // create presentations on the base of the shapes
128   BRep_Builder aBuilder;
129   TopoDS_Compound aComp;
130   aBuilder.MakeCompound(aComp);
131   for(NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>::Iterator
132       anIter(myShapeToPrsMap); anIter.More(); anIter.Next()) {
133     const TopoDS_Shape& aShape = anIter.Key();
134     aBuilder.Add(aComp, aShape);
135     // change deviation coefficient to provide more precise circle
136     // as there is no result, the shape is processed to correct deviation. To be unified
137     ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
138     ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, DynamicHilightAttributes());
139
140     if (myUseAISWidth) {
141       Handle(AIS_InteractiveObject) anIO = anIter.Value();
142       if (!anIO.IsNull()) {
143         int aWidth = anIO->Width();
144         /// workaround for zero width. Else, there will be a crash
145         if (aWidth == 0) { // width returns of TSolid shape is zero
146           aWidth = PartSet_Tools::getAISDefaultWidth();// default width value
147         }
148         setWidth(aDrawer, aWidth);
149       }
150     }
151     try {
152       StdPrs_WFShape::Add(thePresentation, aShape, aDrawer);
153     }
154     catch (...) {
155       return;
156     }
157   }
158   Set(aComp);
159   if (!aReadyToDisplay) {
160     Events_InfoMessage("PartSet_OperationPrs",
161       "An empty AIS presentation: PartSet_OperationPrs").send();
162     std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
163                 new Events_Message(Events_Loop::eventByName(EVENT_EMPTY_OPERATION_PRESENTATION)));
164     Events_Loop::loop()->send(aMsg);
165   }
166 #ifdef DEBUG_OPERATION_PRS
167   qDebug("PartSet_OperationPrs::Compute -- end");
168 #endif
169 }
170
171 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
172                                             const Standard_Integer aMode)
173 {
174   // the presentation is not used in the selection
175 }
176
177 NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>& PartSet_OperationPrs::shapesMap()
178 {
179   return myShapeToPrsMap;
180 }
181
182 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
183 {
184   bool isSub = false;
185   CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
186   if (aComposite.get())
187     isSub = aComposite->isSub(theObject);
188
189   return isSub;
190 }
191
192 void PartSet_OperationPrs::addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
193                                     const FeaturePtr& theFeature, ModuleBase_IWorkshop* theWorkshop,
194                                     QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
195 {
196   if (theObject.get()) {
197     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
198     if (aResult.get()) {
199       ResultBodyPtr aBodyResult =
200         std::dynamic_pointer_cast<ModelAPI_ResultBody>(theObject);
201       if (aBodyResult.get()) {
202         if (aBodyResult->numberOfSubs() > 0) {
203           for(int i = 0; i < aBodyResult->numberOfSubs(); i++) {
204             ResultPtr aSubResult = aBodyResult->subResult(i);
205             if (aSubResult.get()) {
206               GeomShapePtr aShape;
207               addValue(aSubResult, aShape, theFeature, theWorkshop, theObjectShapes);
208             }
209           }
210           return;
211         }
212       }
213 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
214       else {
215         FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
216         if (aFeature.get()) {
217           AttributeBooleanPtr aCopyAttr =
218             aFeature->data()->boolean(SketchPlugin_SketchEntity::COPY_ID());
219           if (aCopyAttr.get()) {
220             bool isCopy = aCopyAttr->value();
221             if (isCopy)
222               return;
223           }
224         }
225       }
226 #endif
227     }
228
229     GeomShapePtr aShape = theShape;
230     if (!aShape.get()) {
231       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
232       if (aRes.get())
233         aShape = aRes->shape();
234     }
235     if (!isSubObject(theObject, theFeature))
236       appendShapeIfVisible(theWorkshop, theObject, aShape, theObjectShapes);
237   }
238 }
239
240 void PartSet_OperationPrs::appendShapeIfVisible(ModuleBase_IWorkshop* theWorkshop,
241                               const ObjectPtr& theObject,
242                               GeomShapePtr theGeomShape,
243                               QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
244 {
245   //XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
246   //// VSV: Do not use isVisible checking because it can be used when state "Show Only" is ON
247   //if (XGUI_Displayer::isVisible(aDisplayer, theObject)) {
248     if (theGeomShape.get()) {
249       if (theObjectShapes.contains(theObject))
250         theObjectShapes[theObject].append(theGeomShape);
251       else {
252         QList<GeomShapePtr> aShapes;
253         aShapes.append(theGeomShape);
254         theObjectShapes[theObject] = aShapes;
255       }
256     } else {
257   #ifdef DEBUG_EMPTY_SHAPE
258       qDebug(QString("Empty shape in result, result: %1")
259               .arg(ModuleBase_Tools::objectInfo(theObject)).toStdString().c_str());
260   #endif
261     }
262   //}
263 }
264
265 void PartSet_OperationPrs::getFeatureShapes(const FeaturePtr& theFeature,
266                                             ModuleBase_IWorkshop* theWorkshop,
267                                             QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
268 {
269   theObjectShapes.clear();
270   if (!theFeature.get())
271     return;
272
273   ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
274
275   QList<GeomShapePtr> aShapes;
276   std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
277   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
278   for (; anIt != aLast; anIt++) {
279     AttributePtr anAttribute = *anIt;
280     if (!isSelectionAttribute(anAttribute))
281       continue;
282
283     if (!aValidators->isCase(theFeature, anAttribute->id()))
284       continue; // this attribute is not participated in the current case
285
286     std::string anAttrType = anAttribute->attributeType();
287
288     if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
289       std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
290               std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
291       for(int i = 0; i < aCurSelList->size(); i++) {
292         std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
293         ResultPtr aResult = aSelAttribute->context();
294         GeomShapePtr aShape = aSelAttribute->value();
295         addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
296       }
297     }
298     if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
299       std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
300         std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
301       for (int i = 0; i < aCurSelList->size(); i++) {
302         ObjectPtr anObject = aCurSelList->object(i);
303         FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
304         // if a feature is stored in the attribute, we should obtain the feature results
305         // e.g. feature rectangle uses parameters feature lines in the attribute
306         if (aFeature.get()) {
307           getResultShapes(aFeature, theWorkshop, theObjectShapes, false);
308         }
309         else {
310           ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
311           if (aResult.get()) {
312             GeomShapePtr aShape = aResult->shape();
313             if (aShape.get())
314               addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
315           }
316         }
317       }
318     }
319     else {
320       ObjectPtr anObject;
321       GeomShapePtr aShape;
322       if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
323         AttributeRefAttrPtr aRefAttr =
324           std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
325         if (aRefAttr->isObject()) {
326           anObject = aRefAttr->object();
327         }
328         else {
329           AttributePtr anAttr = aRefAttr->attr();
330           aShape = PartSet_Tools::findShapeBy2DPoint(anAttr, theWorkshop);
331           // the distance point is not found if the point is selected in the 2nd time
332           // TODO: after debug, this check can be removed
333           if (!aShape.get())
334             continue;
335           anObject = anAttr->owner();
336         }
337       }
338       if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
339         AttributeSelectionPtr anAttr =
340           std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
341         anObject = anAttr->context();
342         aShape = anAttr->value();
343       }
344       if (anAttrType == ModelAPI_AttributeReference::typeId()) {
345         AttributeReferencePtr anAttr =
346           std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
347         anObject = anAttr->value();
348       }
349       addValue(anObject, aShape, theFeature, theWorkshop, theObjectShapes);
350     }
351   }
352 }
353
354 void PartSet_OperationPrs::getResultShapes(const FeaturePtr& theFeature,
355                                            ModuleBase_IWorkshop* theWorkshop,
356                                            QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes,
357                                            const bool theListShouldBeCleared)
358 {
359   if (theListShouldBeCleared)
360     theObjectShapes.clear();
361
362   if (!theFeature.get())
363     return;
364
365   std::list<ResultPtr> aResults;
366   ModelAPI_Tools::allResults(theFeature, aResults);
367   std::list<ResultPtr>::const_iterator aRIt = aResults.begin(),
368                                        aRLast = aResults.end();
369   for (; aRIt != aRLast; aRIt++) {
370     ResultPtr aResult = *aRIt;
371     GeomShapePtr aGeomShape = aResult->shape();
372     appendShapeIfVisible(theWorkshop, aResult, aGeomShape, theObjectShapes);
373   }
374 }
375
376 void PartSet_OperationPrs::getPresentationShapes(const FeaturePtr& theFeature,
377                                            ModuleBase_IWorkshop* theWorkshop,
378                                            QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes,
379                                            const bool theListShouldBeCleared)
380 {
381   if (theListShouldBeCleared)
382     theObjectShapes.clear();
383
384   if (!theFeature.get() || !theFeature->data()->isValid()) // if feature is already removed
385     return;
386
387   XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
388
389   GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theFeature);
390   if (!aPrs.get())
391     return;
392
393   AISObjectPtr anAIS = aPrs->getAISObject(aDisplayer->getAISObject(theFeature));
394   if (!anAIS.get())
395     return;
396
397   Handle(AIS_InteractiveObject) anAISPrs = anAIS->impl<Handle(AIS_InteractiveObject)>();
398   if (!anAISPrs.IsNull()) {
399     Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(anAISPrs);
400     if (!aShapePrs.IsNull()) {
401       TopoDS_Shape aShape = aShapePrs->Shape();
402       if (!aShape.IsNull()) {
403         std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape());
404         aGeomShape->setImpl(new TopoDS_Shape(aShape));
405         appendShapeIfVisible(theWorkshop, theFeature, aGeomShape, theObjectShapes);
406       }
407     }
408   }
409 }
410
411 void PartSet_OperationPrs::getHighlightedShapes(ModuleBase_IWorkshop* theWorkshop,
412                                                 QMap<ObjectPtr,
413                                                 QList<GeomShapePtr> >& theObjectShapes)
414 {
415   theObjectShapes.clear();
416
417   QList<ModuleBase_ViewerPrsPtr> aValues;
418   ModuleBase_ModelWidget* anActiveWidget = theWorkshop->module()->activeWidget();
419   if (anActiveWidget)
420     anActiveWidget->getHighlighted(aValues);
421
422   QList<GeomShapePtr> aShapes;
423   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIIt = aValues.begin(),
424                                               aILast = aValues.end();
425   for (; anIIt != aILast; anIIt++) {
426     ModuleBase_ViewerPrsPtr aPrs = *anIIt;
427     ObjectPtr anObject = aPrs->object();
428
429     GeomShapePtr aGeomShape = aPrs->shape();
430     if (!aGeomShape.get() || aGeomShape->isNull()) {
431       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
432       if (aResult.get()) {
433         aGeomShape = aResult->shape();
434       }
435     }
436     appendShapeIfVisible(theWorkshop, anObject, aGeomShape, theObjectShapes);
437   }
438 }
439
440
441 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
442 {
443   std::string anAttrType = theAttribute->attributeType();
444
445   return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
446          anAttrType == ModelAPI_AttributeRefList::typeId() ||
447          anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
448          anAttrType == ModelAPI_AttributeSelection::typeId() ||
449          anAttrType == ModelAPI_AttributeReference::typeId();
450 }
451
452 void PartSet_OperationPrs::fillShapeList(
453                             const QMap<ObjectPtr, QList<GeomShapePtr> >& theFeatureShapes,
454                             ModuleBase_IWorkshop* theWorkshop,
455                             NCollection_DataMap<TopoDS_Shape,
456                             Handle(AIS_InteractiveObject)>& theShapeToPrsMap)
457 {
458   theShapeToPrsMap.Clear();
459
460   XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
461
462   // create presentations on the base of the shapes
463   QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = theFeatureShapes.begin(),
464                                                         aLast = theFeatureShapes.end();
465   for (; anIt != aLast; anIt++) {
466     ObjectPtr anObject = anIt.key();
467     QList<GeomShapePtr> aShapes = anIt.value();
468     QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
469     for (; aShIt != aShLast; aShIt++) {
470       GeomShapePtr aGeomShape = *aShIt;
471       // the shape should not be checked here on empty value because it should be checked in
472       // appendShapeIfVisible() on the step of filling theFeatureShapes list
473       // the reason is to avoid empty AIS object visualized in the viewer
474       //if (!aGeomShape.get()) continue;
475       TopoDS_Shape aShape = aGeomShape.get() ? aGeomShape->impl<TopoDS_Shape>() : TopoDS_Shape();
476       if (aShape.IsNull())
477         continue;
478
479       // change deviation coefficient to provide more precise circle
480       Handle(AIS_InteractiveObject) anIO;
481       AISObjectPtr anAISPtr = aDisplayer->getAISObject(anObject);
482       if (anAISPtr.get())
483         anIO = anAISPtr->impl<Handle(AIS_InteractiveObject)>();
484       theShapeToPrsMap.Bind(aShape, anIO);
485     }
486   }
487 }