]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationPrs.cpp
Salome HOME
Visualize CompSolid sub-shapes when compsolid shape(e.g. through Object Browser)...
[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
14 #include "ModuleBase_Tools.h"
15 #include "ModuleBase_IModule.h"
16
17 #include <ModelAPI_Result.h>
18 #include <ModelAPI_Attribute.h>
19 #include <ModelAPI_AttributeRefAttr.h>
20 #include <ModelAPI_AttributeReference.h>
21 #include <ModelAPI_AttributeSelection.h>
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_AttributeRefList.h>
24 #include <ModelAPI_Validator.h>
25 #include <ModelAPI_Session.h>
26 #include <ModelAPI_ResultCompSolid.h>
27
28 #include <GeomValidators_Tools.h>
29
30 #include <GeomAPI_IPresentable.h>
31
32 #include <StdPrs_WFDeflectionShape.hxx>
33
34 #include <QList>
35
36 IMPLEMENT_STANDARD_HANDLE(PartSet_OperationPrs, ViewerData_AISShape);
37 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
38
39 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
40   : ViewerData_AISShape(TopoDS_Shape()), myFeature(FeaturePtr()), myWorkshop(theWorkshop)
41 {
42   myShapeColor = ModuleBase_Tools::color("Visualization", "construction_plane_color", "1,1,0");
43   myResultColor = ModuleBase_Tools::color("Visualization", "construction_plane_color", "0,1,0");
44 }
45
46 bool PartSet_OperationPrs::canActivate(const FeaturePtr& theFeature)
47 {
48   bool aHasSelectionAttribute = false;
49
50   std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
51   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
52   for (; anIt != aLast && !aHasSelectionAttribute; anIt++)
53     aHasSelectionAttribute = isSelectionAttribute(*anIt);
54
55   return aHasSelectionAttribute;
56 }
57
58 void PartSet_OperationPrs::setFeature(const FeaturePtr& theFeature)
59 {
60   myFeature = theFeature;
61   updateShapes();
62 }
63
64 /*bool PartSet_OperationPrs::dependOn(const ObjectPtr& theResult)
65 {
66   return myFeatureShapes.contains(theResult);
67 }*/
68
69 void PartSet_OperationPrs::updateShapes()
70 {
71   myFeatureShapes.clear();
72   getFeatureShapes(myFeatureShapes);
73
74   myFeatureResults.clear();
75   if (myFeature)
76     myFeatureResults = myFeature->results();
77 }
78
79 void PartSet_OperationPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
80                                    const Handle(Prs3d_Presentation)& thePresentation, 
81                                    const Standard_Integer theMode)
82 {
83   // when the feature can not be visualized in the module, the operation preview should not
84   // be visualized also
85   if (!myWorkshop->module()->canDisplayObject(myFeature))
86     return;
87
88   Quantity_Color aColor(1., 1., 0., Quantity_TOC_RGB); // yellow
89   SetColor(aColor);
90
91   thePresentation->Clear();
92   XGUI_Displayer* aDisplayer = workshop()->displayer();
93
94   // create presentations on the base of the shapes
95   Handle(Prs3d_Drawer) aDrawer = Attributes();
96   QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = myFeatureShapes.begin(),
97                                                         aLast = myFeatureShapes.end();
98   for (; anIt != aLast; anIt++) {
99     ObjectPtr anObject = anIt.key();
100     if (!isVisible(aDisplayer, anObject))
101       continue;
102     QList<GeomShapePtr> aShapes = anIt.value();
103     QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
104     for (; aShIt != aShLast; aShIt++) {
105       GeomShapePtr aGeomShape = *aShIt;
106       if (!aGeomShape.get())
107         continue;
108       TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
109       // change deviation coefficient to provide more precise circle
110       ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
111       StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
112     }
113   }
114
115   aColor = Quantity_Color(0., 1., 0., Quantity_TOC_RGB); // green
116   SetColor(aColor);
117
118   std::list<ResultPtr>::const_iterator aRIt = myFeatureResults.begin(),
119                                        aRLast = myFeatureResults.end();
120   for (; aRIt != aRLast; aRIt++) {
121     ResultPtr aResult = *aRIt;
122     if (!isVisible(aDisplayer, aResult))
123       continue;
124     GeomShapePtr aGeomShape = aResult->shape();
125     if (!aGeomShape.get())
126       continue;
127     TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
128     // change deviation coefficient to provide more precise circle
129     ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
130     StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
131   }
132 }
133
134 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
135                                             const Standard_Integer aMode)
136 {
137   // the presentation is not used in the selection
138 }
139
140 bool PartSet_OperationPrs::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
141 {
142   bool aVisible = false;
143   GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
144   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
145   if (aPrs.get() || aResult.get())
146     aVisible = theDisplayer->isVisible(theObject);
147   else {
148     // check if all results of the feature are visible
149     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
150     std::list<ResultPtr> aResults = aFeature->results();
151     std::list<ResultPtr>::const_iterator aIt;
152     aVisible = !aResults.empty();
153     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
154       aVisible = aVisible && theDisplayer->isVisible(*aIt);
155     }
156   }
157   return aVisible;
158 }
159
160 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
161 {
162   bool isSub = false;
163   CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
164   if (aComposite.get())
165     isSub = aComposite->isSub(theObject);
166
167   return isSub;
168 }
169
170 void addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
171               const FeaturePtr& theFeature,
172               QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
173 {
174   if (theObject.get()) {
175     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
176     if (aResult.get()) {
177       ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
178       if (aCompsolidResult.get()) {
179         for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
180           ResultPtr aSubResult = aCompsolidResult->subResult(i);
181           if (aSubResult.get()) {
182             GeomShapePtr aShape;
183             addValue(aSubResult, aShape, theFeature, theObjectShapes);
184           }
185         }
186         return;
187       }
188     }
189
190
191     GeomShapePtr aShape = theShape;
192     if (!aShape.get()) {
193       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
194       if (aResult.get())
195         aShape = aResult->shape();
196     }
197     if (!isSubObject(theObject, theFeature)) {
198       if (theObjectShapes.contains(theObject))
199         theObjectShapes[theObject].append(aShape);
200       else {
201         QList<GeomShapePtr> aShapes;
202         aShapes.append(aShape);
203         theObjectShapes[theObject] = aShapes;
204       }
205     }
206   }
207 }
208
209 void PartSet_OperationPrs::getFeatureShapes(QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
210 {
211   if (!myFeature.get())
212     return;
213
214   ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
215
216   QList<GeomShapePtr> aShapes;
217   std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
218   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
219   for (; anIt != aLast; anIt++) {
220     AttributePtr anAttribute = *anIt;
221     if (!isSelectionAttribute(anAttribute))
222       continue;
223
224     if (!aValidators->isCase(myFeature, anAttribute->id()))
225       continue; // this attribute is not participated in the current case
226
227     std::string anAttrType = anAttribute->attributeType();
228
229     if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
230       std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList = 
231               std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
232       for(int i = 0; i < aCurSelList->size(); i++) {
233         std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
234         ResultPtr aResult = aSelAttribute->context();
235         GeomShapePtr aShape = aSelAttribute->value();
236         addValue(aResult, aShape, myFeature, theObjectShapes);
237       }
238     }
239     if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
240       std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
241         std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
242       for (int i = 0; i < aCurSelList->size(); i++) {
243         GeomShapePtr aShape;
244         addValue(aCurSelList->object(i), aShape, myFeature, theObjectShapes);
245       }
246     }
247     else {
248       ObjectPtr anObject;
249       GeomShapePtr aShape;
250       if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
251         AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
252         if (anAttr->isObject()) {
253           anObject = anAttr->object();
254         }
255         else {
256           aShape = PartSet_Tools::findShapeBy2DPoint(anAttr, myWorkshop);
257           // the distance point is not found if the point is selected in the 2nd time
258           // TODO: after debug, this check can be removed
259           if (!aShape.get())
260             continue;
261           anObject = anAttr->attr()->owner();
262         }
263       }
264       if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
265         AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
266         anObject = anAttr->context();
267         aShape = anAttr->value();
268       }
269       if (anAttrType == ModelAPI_AttributeReference::typeId()) {
270         AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
271         anObject = anAttr->value();
272       }
273       addValue(anObject, aShape, myFeature, theObjectShapes);
274     }
275   }
276 }
277
278 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
279 {
280   std::string anAttrType = theAttribute->attributeType();
281
282   return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
283          anAttrType == ModelAPI_AttributeRefList::typeId() ||
284          anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
285          anAttrType == ModelAPI_AttributeSelection::typeId() ||
286          anAttrType == ModelAPI_AttributeReference::typeId();
287 }
288
289 XGUI_Workshop* PartSet_OperationPrs::workshop() const
290 {
291   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
292   return aConnector->workshop();
293 }