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 "ModuleBase_Tools.h"
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>
23 #include <GeomValidators_Tools.h>
25 #include <GeomAPI_IPresentable.h>
27 #include <StdPrs_WFDeflectionShape.hxx>
31 IMPLEMENT_STANDARD_HANDLE(PartSet_OperationPrs, ViewerData_AISShape);
32 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
34 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
35 : ViewerData_AISShape(TopoDS_Shape()), myFeature(FeaturePtr()), myWorkshop(theWorkshop)
39 bool PartSet_OperationPrs::canActivate(const FeaturePtr& theFeature)
41 bool aHasSelectionAttribute = false;
43 std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
44 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
45 for (; anIt != aLast && !aHasSelectionAttribute; anIt++)
46 aHasSelectionAttribute = isSelectionAttribute(*anIt);
48 return aHasSelectionAttribute;
51 void PartSet_OperationPrs::setFeature(const FeaturePtr& theFeature)
53 myFeature = theFeature;
57 bool PartSet_OperationPrs::dependOn(const ObjectPtr& theResult)
59 return myFeatureShapes.contains(theResult);
62 void PartSet_OperationPrs::updateShapes()
64 myFeatureShapes.clear();
65 getFeatureShapes(myFeatureShapes);
68 void PartSet_OperationPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
69 const Handle(Prs3d_Presentation)& thePresentation,
70 const Standard_Integer theMode)
72 thePresentation->Clear();
73 XGUI_Displayer* aDisplayer = workshop()->displayer();
75 // create presentations on the base of the shapes
76 Handle(Prs3d_Drawer) aDrawer = Attributes();
77 QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = myFeatureShapes.begin(),
78 aLast = myFeatureShapes.end();
79 for (; anIt != aLast; anIt++) {
80 ObjectPtr anObject = anIt.key();
81 if (!isVisible(aDisplayer, anObject))
83 QList<GeomShapePtr> aShapes = anIt.value();
84 QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
85 for (; aShIt != aShLast; aShIt++) {
86 GeomShapePtr aGeomShape = *aShIt;
87 if (!aGeomShape.get())
89 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
90 // change deviation coefficient to provide more precise circle
91 //aDrawer->SetDeviationCoefficient(ModuleBase_Tools::defaultDeviationCoefficient());
92 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
97 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
98 const Standard_Integer aMode)
100 // the presentation is not used in the selection
103 bool PartSet_OperationPrs::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
105 bool aVisible = false;
106 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
107 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
108 if (aPrs.get() || aResult.get())
109 aVisible = theDisplayer->isVisible(theObject);
111 // check if all results of the feature are visible
112 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
113 std::list<ResultPtr> aResults = aFeature->results();
114 std::list<ResultPtr>::const_iterator aIt;
115 aVisible = !aResults.empty();
116 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
117 aVisible = aVisible && theDisplayer->isVisible(*aIt);
123 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
126 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
127 if (aComposite.get())
128 isSub = aComposite->isSub(theObject);
133 void addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
134 const FeaturePtr& theFeature,
135 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
137 if (theObject.get()) {
138 GeomShapePtr aShape = theShape;
140 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
142 aShape = aResult->shape();
144 if (!isSubObject(theObject, theFeature)) {
145 if (theObjectShapes.contains(theObject))
146 theObjectShapes[theObject].append(aShape);
148 QList<GeomShapePtr> aShapes;
149 aShapes.append(aShape);
150 theObjectShapes[theObject] = aShapes;
156 void PartSet_OperationPrs::getFeatureShapes(QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
158 if (!myFeature.get())
161 QList<GeomShapePtr> aShapes;
162 std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
163 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
164 for (; anIt != aLast; anIt++) {
165 AttributePtr anAttribute = *anIt;
166 if (!isSelectionAttribute(anAttribute))
169 std::string anAttrType = anAttribute->attributeType();
171 if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
172 std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
173 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
174 for(int i = 0; i < aCurSelList->size(); i++) {
175 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
176 ResultPtr aResult = aSelAttribute->context();
177 GeomShapePtr aShape = aSelAttribute->value();
178 addValue(aResult, aShape, myFeature, theObjectShapes);
184 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
185 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
186 if (anAttr->isObject()) {
187 anObject = anAttr->object();
190 aShape = PartSet_Tools::findShapeBy2DPoint(anAttr, myWorkshop);
191 // the distance point is not found if the point is selected in the 2nd time
192 // TODO: after debug, this check can be removed
195 anObject = anAttr->attr()->owner();
198 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
199 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
200 anObject = anAttr->context();
201 aShape = anAttr->value();
203 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
204 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
205 anObject = anAttr->value();
207 addValue(anObject, aShape, myFeature, theObjectShapes);
212 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
214 std::string anAttrType = theAttribute->attributeType();
216 return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
217 anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
218 anAttrType == ModelAPI_AttributeSelection::typeId() ||
219 anAttrType == ModelAPI_AttributeReference::typeId();
222 XGUI_Workshop* PartSet_OperationPrs::workshop() const
224 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
225 return aConnector->workshop();