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