]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationPrs.cpp
Salome HOME
1. Mirror/Rotation/Translation constraint: using RefList instead of SelectionList
[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
16 #include <ModelAPI_Result.h>
17 #include <ModelAPI_Attribute.h>
18 #include <ModelAPI_AttributeRefAttr.h>
19 #include <ModelAPI_AttributeReference.h>
20 #include <ModelAPI_AttributeSelection.h>
21 #include <ModelAPI_AttributeSelectionList.h>
22 #include <ModelAPI_AttributeRefList.h>
23
24
25 #include <GeomValidators_Tools.h>
26
27 #include <GeomAPI_IPresentable.h>
28
29 #include <StdPrs_WFDeflectionShape.hxx>
30
31 #include <QList>
32
33 IMPLEMENT_STANDARD_HANDLE(PartSet_OperationPrs, ViewerData_AISShape);
34 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
35
36 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
37   : ViewerData_AISShape(TopoDS_Shape()), myFeature(FeaturePtr()), myWorkshop(theWorkshop)
38 {
39 }
40
41 bool PartSet_OperationPrs::canActivate(const FeaturePtr& theFeature)
42 {
43   bool aHasSelectionAttribute = false;
44
45   std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
46   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
47   for (; anIt != aLast && !aHasSelectionAttribute; anIt++)
48     aHasSelectionAttribute = isSelectionAttribute(*anIt);
49
50   return aHasSelectionAttribute;
51 }
52
53 void PartSet_OperationPrs::setFeature(const FeaturePtr& theFeature)
54 {
55   myFeature = theFeature;
56   updateShapes();
57 }
58
59 bool PartSet_OperationPrs::dependOn(const ObjectPtr& theResult)
60 {
61   return myFeatureShapes.contains(theResult);
62 }
63
64 void PartSet_OperationPrs::updateShapes()
65 {
66   myFeatureShapes.clear();
67   getFeatureShapes(myFeatureShapes);
68 }
69
70 void PartSet_OperationPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
71                                    const Handle(Prs3d_Presentation)& thePresentation, 
72                                    const Standard_Integer theMode)
73 {
74   thePresentation->Clear();
75   XGUI_Displayer* aDisplayer = workshop()->displayer();
76
77   // create presentations on the base of the shapes
78   Handle(Prs3d_Drawer) aDrawer = Attributes();
79   QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = myFeatureShapes.begin(),
80                                                         aLast = myFeatureShapes.end();
81   for (; anIt != aLast; anIt++) {
82     ObjectPtr anObject = anIt.key();
83     if (!isVisible(aDisplayer, anObject))
84       continue;
85     QList<GeomShapePtr> aShapes = anIt.value();
86     QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
87     for (; aShIt != aShLast; aShIt++) {
88       GeomShapePtr aGeomShape = *aShIt;
89       if (!aGeomShape.get())
90         continue;
91       TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
92       // change deviation coefficient to provide more precise circle
93       ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
94       StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
95     }
96   }
97 }
98
99 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
100                                             const Standard_Integer aMode)
101 {
102   // the presentation is not used in the selection
103 }
104
105 bool PartSet_OperationPrs::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
106 {
107   bool aVisible = false;
108   GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
109   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
110   if (aPrs.get() || aResult.get())
111     aVisible = theDisplayer->isVisible(theObject);
112   else {
113     // check if all results of the feature are visible
114     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
115     std::list<ResultPtr> aResults = aFeature->results();
116     std::list<ResultPtr>::const_iterator aIt;
117     aVisible = !aResults.empty();
118     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
119       aVisible = aVisible && theDisplayer->isVisible(*aIt);
120     }
121   }
122   return aVisible;
123 }
124
125 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
126 {
127   bool isSub = false;
128   CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
129   if (aComposite.get())
130     isSub = aComposite->isSub(theObject);
131
132   return isSub;
133 }
134
135 void addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
136               const FeaturePtr& theFeature,
137               QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
138 {
139   if (theObject.get()) {
140     GeomShapePtr aShape = theShape;
141     if (!aShape.get()) {
142       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
143       if (aResult.get())
144         aShape = aResult->shape();
145     }
146     if (!isSubObject(theObject, theFeature)) {
147       if (theObjectShapes.contains(theObject))
148         theObjectShapes[theObject].append(aShape);
149       else {
150         QList<GeomShapePtr> aShapes;
151         aShapes.append(aShape);
152         theObjectShapes[theObject] = aShapes;
153       }
154     }
155   }
156 }
157
158 void PartSet_OperationPrs::getFeatureShapes(QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
159 {
160   if (!myFeature.get())
161     return;
162
163   QList<GeomShapePtr> aShapes;
164   std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
165   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
166   for (; anIt != aLast; anIt++) {
167     AttributePtr anAttribute = *anIt;
168     if (!isSelectionAttribute(anAttribute))
169       continue;
170
171     std::string anAttrType = anAttribute->attributeType();
172
173     if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
174       std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList = 
175               std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
176       for(int i = 0; i < aCurSelList->size(); i++) {
177         std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
178         ResultPtr aResult = aSelAttribute->context();
179         GeomShapePtr aShape = aSelAttribute->value();
180         addValue(aResult, aShape, myFeature, theObjectShapes);
181       }
182     }
183     if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
184       std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
185         std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
186       for (int i = 0; i < aCurSelList->size(); i++) {
187         GeomShapePtr aShape;
188         addValue(aCurSelList->object(i), aShape, myFeature, theObjectShapes);
189       }
190     }
191     else {
192       ObjectPtr anObject;
193       GeomShapePtr aShape;
194       if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
195         AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
196         if (anAttr->isObject()) {
197           anObject = anAttr->object();
198         }
199         else {
200           aShape = PartSet_Tools::findShapeBy2DPoint(anAttr, myWorkshop);
201           // the distance point is not found if the point is selected in the 2nd time
202           // TODO: after debug, this check can be removed
203           if (!aShape.get())
204             continue;
205           anObject = anAttr->attr()->owner();
206         }
207       }
208       if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
209         AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
210         anObject = anAttr->context();
211         aShape = anAttr->value();
212       }
213       if (anAttrType == ModelAPI_AttributeReference::typeId()) {
214         AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
215         anObject = anAttr->value();
216       }
217       addValue(anObject, aShape, myFeature, theObjectShapes);
218     }
219   }
220 }
221
222 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
223 {
224   std::string anAttrType = theAttribute->attributeType();
225
226   return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
227          anAttrType == ModelAPI_AttributeRefList::typeId() ||
228          anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
229          anAttrType == ModelAPI_AttributeSelection::typeId() ||
230          anAttrType == ModelAPI_AttributeReference::typeId();
231 }
232
233 XGUI_Workshop* PartSet_OperationPrs::workshop() const
234 {
235   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
236   return aConnector->workshop();
237 }