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