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