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