]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationPrs.cpp
Salome HOME
#1371 Using auxilliary Sketch elements in any Feature: code improvement to separate...
[modules/shaper.git] / src / PartSet / PartSet_OperationPrs.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_OperationPrs.cpp
4 // Created:     01 Jul 2015
5 // Author:      Natalia ERMOLAEVA
6
7 #include "PartSet_OperationPrs.h"
8 #include "PartSet_Tools.h"
9
10 #include "XGUI_Workshop.h"
11 #include "XGUI_ModuleConnector.h"
12 #include "XGUI_Displayer.h"
13 #include "XGUI_Tools.h"
14
15 #include "ModuleBase_Tools.h"
16 #include "ModuleBase_IModule.h"
17 #include <ModuleBase_IPropertyPanel.h>
18 #include <ModuleBase_ModelWidget.h>
19
20 #include <ModelAPI_Result.h>
21 #include <ModelAPI_Attribute.h>
22 #include <ModelAPI_AttributeRefAttr.h>
23 #include <ModelAPI_AttributeReference.h>
24 #include <ModelAPI_AttributeSelection.h>
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include <ModelAPI_AttributeRefList.h>
27 #include <ModelAPI_Validator.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_ResultCompSolid.h>
30
31 #include <Events_Error.h>
32
33 #include <GeomAPI_IPresentable.h>
34
35 #include <StdPrs_WFDeflectionShape.hxx>
36
37 #include <QList>
38
39 static const int AIS_DEFAULT_WIDTH = 2;
40
41 //#define DEBUG_EMPTY_SHAPE
42
43 // multi-rotation/translation operation
44 //#define DEBUG_HIDE_COPY_ATTRIBUTE
45 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
46 #include <ModelAPI_AttributeBoolean.h>
47 #include <SketchPlugin_SketchEntity.h>
48 #endif
49
50 IMPLEMENT_STANDARD_HANDLE(PartSet_OperationPrs, ViewerData_AISShape);
51 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
52
53 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
54 : ViewerData_AISShape(TopoDS_Shape()), myWorkshop(theWorkshop), myUseAISWidth(false)
55 {
56   myShapeColor = Quantity_Color(1, 1, 1, Quantity_TOC_RGB);
57 }
58
59 bool PartSet_OperationPrs::hasShapes()
60 {
61   return !myFeatureShapes.empty();
62 }
63
64 void PartSet_OperationPrs::setShapeColor(const Quantity_Color& theColor)
65 {
66   myShapeColor = theColor;
67 }
68
69 void PartSet_OperationPrs::useAISWidth()
70 {
71   myUseAISWidth = true;
72 }
73
74 void PartSet_OperationPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
75                                    const Handle(Prs3d_Presentation)& thePresentation, 
76                                    const Standard_Integer theMode)
77 {
78   SetColor(myShapeColor);
79   thePresentation->Clear();
80
81   XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(myWorkshop)->displayer();
82   Handle(Prs3d_Drawer) aDrawer = Attributes();
83
84   // create presentations on the base of the shapes
85   bool anEmptyAIS = true;
86   QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = myFeatureShapes.begin(),
87                                                         aLast = myFeatureShapes.end();
88   for (; anIt != aLast; anIt++) {
89     ObjectPtr anObject = anIt.key();
90     QList<GeomShapePtr> aShapes = anIt.value();
91     QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
92     for (; aShIt != aShLast; aShIt++) {
93       GeomShapePtr aGeomShape = *aShIt;
94       // the shape should not be checked here on empty value because it should be checked in
95       // appendShapeIfVisible() on the step of filling myFeatureShapes list
96       // the reason is to avoid empty AIS object visualized in the viewer
97       //if (!aGeomShape.get()) continue;
98       TopoDS_Shape aShape = aGeomShape.get() ? aGeomShape->impl<TopoDS_Shape>() : TopoDS_Shape();
99       // change deviation coefficient to provide more precise circle
100       ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
101
102       if (myUseAISWidth) {
103         AISObjectPtr anAISPtr = aDisplayer->getAISObject(anObject);
104         if (anAISPtr.get()) {
105           Handle(AIS_InteractiveObject) anIO = anAISPtr->impl<Handle(AIS_InteractiveObject)>();
106           if (!anIO.IsNull()) {
107             int aWidth = anIO->Width();
108             /// workaround for zero width. Else, there will be a crash
109             if (aWidth == 0) { // width returns of TSolid shape is zero
110               bool isDisplayed = !anIO->GetContext().IsNull();
111               aWidth = AIS_DEFAULT_WIDTH;// default width value
112             }
113             setWidth(aDrawer, aWidth);
114           }
115         }
116       }
117       StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
118       if (anEmptyAIS)
119         anEmptyAIS = false;
120     }
121   }
122   if (anEmptyAIS)
123     Events_Error::throwException("An empty AIS presentation: PartSet_OperationPrs");
124 }
125
126 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
127                                             const Standard_Integer aMode)
128 {
129   // the presentation is not used in the selection
130 }
131
132 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
133 {
134   bool isSub = false;
135   CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
136   if (aComposite.get())
137     isSub = aComposite->isSub(theObject);
138
139   return isSub;
140 }
141
142 void PartSet_OperationPrs::addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
143                                     const FeaturePtr& theFeature, ModuleBase_IWorkshop* theWorkshop,
144                                     QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
145 {
146   if (theObject.get()) {
147     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
148     if (aResult.get()) {
149       ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
150       if (aCompsolidResult.get()) {
151         if (aCompsolidResult->numberOfSubs() > 0) {
152           for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
153             ResultPtr aSubResult = aCompsolidResult->subResult(i);
154             if (aSubResult.get()) {
155               GeomShapePtr aShape;
156               addValue(aSubResult, aShape, theFeature, theWorkshop, theObjectShapes);
157             }
158           }
159           return;
160         }
161       }
162 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
163       else {
164         FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
165         if (aFeature.get()) {
166           AttributeBooleanPtr aCopyAttr = aFeature->data()->boolean(SketchPlugin_SketchEntity::COPY_ID());
167           if (aCopyAttr.get()) {
168             bool isCopy = aCopyAttr->value();
169             if (isCopy)
170               return;
171           }
172         }
173       }
174 #endif
175     }
176
177     GeomShapePtr aShape = theShape;
178     if (!aShape.get()) {
179       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
180       if (aResult.get())
181         aShape = aResult->shape();
182     }
183     if (!isSubObject(theObject, theFeature))
184       appendShapeIfVisible(theWorkshop, theObject, aShape, theObjectShapes);
185   }
186 }
187
188 void PartSet_OperationPrs::appendShapeIfVisible(ModuleBase_IWorkshop* theWorkshop,
189                               const ObjectPtr& theObject,
190                               GeomShapePtr theGeomShape,
191                               QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
192 {
193   XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
194   if (XGUI_Displayer::isVisible(aDisplayer, theObject)) {
195     if (theGeomShape.get()) {
196       if (theObjectShapes.contains(theObject))
197         theObjectShapes[theObject].append(theGeomShape);
198       else {
199         QList<GeomShapePtr> aShapes;
200         aShapes.append(theGeomShape);
201         theObjectShapes[theObject] = aShapes;
202       }
203     } else {
204   #ifdef DEBUG_EMPTY_SHAPE
205       qDebug(QString("Empty shape in result, result: %1")
206               .arg(ModuleBase_Tools::objectInfo(theObject)).toStdString().c_str());
207   #endif
208     }
209   }
210 }
211
212 void PartSet_OperationPrs::getFeatureShapes(const FeaturePtr& theFeature,
213                                             ModuleBase_IWorkshop* theWorkshop,
214                                             QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
215 {
216   theObjectShapes.clear();
217   if (!theFeature.get())
218     return;
219
220   ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
221
222   QList<GeomShapePtr> aShapes;
223   std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
224   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
225   for (; anIt != aLast; anIt++) {
226     AttributePtr anAttribute = *anIt;
227     if (!isSelectionAttribute(anAttribute))
228       continue;
229
230     if (!aValidators->isCase(theFeature, anAttribute->id()))
231       continue; // this attribute is not participated in the current case
232
233     std::string anAttrType = anAttribute->attributeType();
234
235     if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
236       std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList = 
237               std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
238       for(int i = 0; i < aCurSelList->size(); i++) {
239         std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
240         ResultPtr aResult = aSelAttribute->context();
241         GeomShapePtr aShape = aSelAttribute->value();
242         addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
243       }
244     }
245     if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
246       std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
247         std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
248       for (int i = 0; i < aCurSelList->size(); i++) {
249         ObjectPtr anObject = aCurSelList->object(i);
250         FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
251         // if a feature is stored in the attribute, we should obtain the feature results
252         // e.g. feature rectangle uses parameters feature lines in the attribute
253         if (aFeature.get()) {
254           getResultShapes(aFeature, theWorkshop, theObjectShapes, false);
255         }
256         else {
257           ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
258           if (aResult.get()) {
259             GeomShapePtr aShape = aResult->shape();
260             if (aShape.get())
261               addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
262           }
263         }
264       }
265     }
266     else {
267       ObjectPtr anObject;
268       GeomShapePtr aShape;
269       if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
270         AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
271         if (anAttr->isObject()) {
272           anObject = anAttr->object();
273         }
274         else {
275           AttributePtr anAttribute = anAttr->attr();
276           aShape = PartSet_Tools::findShapeBy2DPoint(anAttribute, theWorkshop);
277           // the distance point is not found if the point is selected in the 2nd time
278           // TODO: after debug, this check can be removed
279           if (!aShape.get())
280             continue;
281           anObject = anAttr->attr()->owner();
282         }
283       }
284       if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
285         AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
286         anObject = anAttr->context();
287         aShape = anAttr->value();
288       }
289       if (anAttrType == ModelAPI_AttributeReference::typeId()) {
290         AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
291         anObject = anAttr->value();
292       }
293       addValue(anObject, aShape, theFeature, theWorkshop, theObjectShapes);
294     }
295   }
296 }
297
298 void PartSet_OperationPrs::getResultShapes(const FeaturePtr& theFeature,
299                                            ModuleBase_IWorkshop* theWorkshop,
300                                            QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes,
301                                            const bool theListShouldBeCleared)
302 {
303   if (theListShouldBeCleared)
304     theObjectShapes.clear();
305
306   if (!theFeature.get())
307     return;
308
309   XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
310
311   std::list<ResultPtr> aFeatureResults = theFeature->results();
312   std::list<ResultPtr>::const_iterator aRIt = aFeatureResults.begin(),
313                                        aRLast = aFeatureResults.end();
314   for (; aRIt != aRLast; aRIt++) {
315     ResultPtr aResult = *aRIt;
316     GeomShapePtr aGeomShape = aResult->shape();
317     appendShapeIfVisible(theWorkshop, aResult, aGeomShape, theObjectShapes);
318   }
319 }
320
321 void PartSet_OperationPrs::getHighlightedShapes(ModuleBase_IWorkshop* theWorkshop,
322                                                 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
323 {
324   theObjectShapes.clear();
325
326   QList<ModuleBase_ViewerPrs> aValues;
327   ModuleBase_IPropertyPanel* aPanel = theWorkshop->propertyPanel();
328   if (aPanel) {
329     ModuleBase_ModelWidget* aWidget = aPanel->activeWidget();
330     if (aWidget) {
331       aWidget->getHighlighted(aValues);
332     }
333   }
334
335   QList<GeomShapePtr> aShapes;
336   QList<ModuleBase_ViewerPrs>::const_iterator anIIt = aValues.begin(),
337                                               aILast = aValues.end();
338   for (; anIIt != aILast; anIIt++) {
339     ModuleBase_ViewerPrs aPrs = *anIIt;
340     ObjectPtr anObject = aPrs.object();
341
342     GeomShapePtr aGeomShape = aPrs.shape();
343     if (!aGeomShape.get() || aGeomShape->isNull()) {
344       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
345       if (aResult.get()) {
346         aGeomShape = aResult->shape();
347       }
348     }
349     appendShapeIfVisible(theWorkshop, anObject, aGeomShape, theObjectShapes);
350   }
351 }
352
353
354 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
355 {
356   std::string anAttrType = theAttribute->attributeType();
357
358   return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
359          anAttrType == ModelAPI_AttributeRefList::typeId() ||
360          anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
361          anAttrType == ModelAPI_AttributeSelection::typeId() ||
362          anAttrType == ModelAPI_AttributeReference::typeId();
363 }