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