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