1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PartSet_OperationPrs.cpp
4 // Created: 01 Jul 2015
5 // Author: Natalia ERMOLAEVA
7 #include "PartSet_OperationPrs.h"
8 #include "PartSet_Tools.h"
10 #include "XGUI_Workshop.h"
11 #include "XGUI_ModuleConnector.h"
12 #include "XGUI_Displayer.h"
14 #include <ModelAPI_Result.h>
15 #include <ModelAPI_Attribute.h>
16 #include <ModelAPI_AttributeRefAttr.h>
17 #include <ModelAPI_AttributeReference.h>
18 #include <ModelAPI_AttributeSelection.h>
19 #include <ModelAPI_AttributeSelectionList.h>
21 #include <GeomValidators_Tools.h>
23 #include <GeomAPI_IPresentable.h>
25 #include <StdPrs_WFDeflectionShape.hxx>
29 IMPLEMENT_STANDARD_HANDLE(PartSet_OperationPrs, ViewerData_AISShape);
30 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
32 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
33 : ViewerData_AISShape(TopoDS_Shape()), myFeature(FeaturePtr()), myWorkshop(theWorkshop)
37 bool PartSet_OperationPrs::canActivate(const FeaturePtr& theFeature)
39 bool aHasSelectionAttribute = false;
41 std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
42 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
43 for (; anIt != aLast && !aHasSelectionAttribute; anIt++)
44 aHasSelectionAttribute = isSelectionAttribute(*anIt);
46 return aHasSelectionAttribute;
49 void PartSet_OperationPrs::setFeature(const FeaturePtr& theFeature)
51 myFeature = theFeature;
55 bool PartSet_OperationPrs::dependOn(const ObjectPtr& theResult)
57 return myFeatureShapes.contains(theResult);
60 void PartSet_OperationPrs::updateShapes()
62 myFeatureShapes.clear();
63 getFeatureShapes(myFeatureShapes);
66 void PartSet_OperationPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
67 const Handle(Prs3d_Presentation)& thePresentation,
68 const Standard_Integer theMode)
70 thePresentation->Clear();
71 XGUI_Displayer* aDisplayer = workshop()->displayer();
73 // create presentations on the base of the shapes
74 Handle(Prs3d_Drawer) aDrawer = Attributes();
75 QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = myFeatureShapes.begin(),
76 aLast = myFeatureShapes.end();
77 for (; anIt != aLast; anIt++) {
78 ObjectPtr anObject = anIt.key();
79 if (!isVisible(aDisplayer, anObject))
81 QList<GeomShapePtr> aShapes = anIt.value();
82 QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
83 for (; aShIt != aShLast; aShIt++) {
84 GeomShapePtr aGeomShape = *aShIt;
85 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
86 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
91 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
92 const Standard_Integer aMode)
94 // the presentation is not used in the selection
97 bool PartSet_OperationPrs::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
99 bool aVisible = false;
100 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
101 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
102 if (aPrs.get() || aResult.get())
103 aVisible = theDisplayer->isVisible(theObject);
105 // check if all results of the feature are visible
106 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
107 std::list<ResultPtr> aResults = aFeature->results();
108 std::list<ResultPtr>::const_iterator aIt;
109 aVisible = !aResults.empty();
110 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
111 aVisible = aVisible && theDisplayer->isVisible(*aIt);
117 void addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
118 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
120 if (theObjectShapes.contains(theObject))
121 theObjectShapes[theObject].append(theShape);
123 QList<GeomShapePtr> aShapes;
124 aShapes.append(theShape);
125 theObjectShapes[theObject] = aShapes;
129 void PartSet_OperationPrs::getFeatureShapes(QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
131 if (!myFeature.get())
134 QList<GeomShapePtr> aShapes;
135 std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
136 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
137 for (; anIt != aLast; anIt++) {
138 AttributePtr anAttribute = *anIt;
139 if (!isSelectionAttribute(anAttribute))
142 std::string anAttrType = anAttribute->attributeType();
144 if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
145 std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
146 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
147 for(int i = 0; i < aCurSelList->size(); i++) {
148 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
149 ResultPtr aResult = aSelAttribute->context();
150 GeomShapePtr aShape = aSelAttribute->value();
152 aShape = aResult->shape();
153 addValue(aResult, aShape, theObjectShapes);
159 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
160 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
161 if (anAttr->isObject()) {
162 anObject = anAttr->object();
165 aShape = PartSet_Tools::findShapeBy2DPoint(anAttr, myWorkshop);
166 // the distance point is not found if the point is selected in the 2nd time
167 // TODO: after debug, this check can be removed
170 anObject = anAttr->attr()->owner();
173 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
174 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
175 anObject = anAttr->context();
176 aShape = anAttr->value();
178 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
179 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
180 anObject = anAttr->value();
183 if (anObject.get()) {
185 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
187 aShape = aResult->shape();
189 addValue(anObject, aShape, theObjectShapes);
195 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
197 std::string anAttrType = theAttribute->attributeType();
199 return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
200 anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
201 anAttrType == ModelAPI_AttributeSelection::typeId() ||
202 anAttrType == ModelAPI_AttributeReference::typeId();
205 XGUI_Workshop* PartSet_OperationPrs::workshop() const
207 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
208 return aConnector->workshop();