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