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