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>
22 #include <ModelAPI_AttributeRefList.h>
25 #include <GeomValidators_Tools.h>
27 #include <GeomAPI_IPresentable.h>
29 #include <StdPrs_WFDeflectionShape.hxx>
33 IMPLEMENT_STANDARD_HANDLE(PartSet_OperationPrs, ViewerData_AISShape);
34 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
36 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
37 : ViewerData_AISShape(TopoDS_Shape()), myFeature(FeaturePtr()), myWorkshop(theWorkshop)
41 bool PartSet_OperationPrs::canActivate(const FeaturePtr& theFeature)
43 bool aHasSelectionAttribute = false;
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);
50 return aHasSelectionAttribute;
53 void PartSet_OperationPrs::setFeature(const FeaturePtr& theFeature)
55 myFeature = theFeature;
59 bool PartSet_OperationPrs::dependOn(const ObjectPtr& theResult)
61 return myFeatureShapes.contains(theResult);
64 void PartSet_OperationPrs::updateShapes()
66 myFeatureShapes.clear();
67 getFeatureShapes(myFeatureShapes);
69 myFeatureResults.clear();
71 myFeatureResults = myFeature->results();
74 void PartSet_OperationPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
75 const Handle(Prs3d_Presentation)& thePresentation,
76 const Standard_Integer theMode)
78 Quantity_Color aColor(1., 1., 0., Quantity_TOC_RGB); // yellow
81 thePresentation->Clear();
82 XGUI_Displayer* aDisplayer = workshop()->displayer();
84 // create presentations on the base of the shapes
85 Handle(Prs3d_Drawer) aDrawer = Attributes();
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 if (!isVisible(aDisplayer, anObject))
92 QList<GeomShapePtr> aShapes = anIt.value();
93 QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
94 for (; aShIt != aShLast; aShIt++) {
95 GeomShapePtr aGeomShape = *aShIt;
96 if (!aGeomShape.get())
98 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
99 // change deviation coefficient to provide more precise circle
100 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
101 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
105 aColor = Quantity_Color(0., 1., 0., Quantity_TOC_RGB); // green
108 std::list<ResultPtr>::const_iterator aRIt = myFeatureResults.begin(),
109 aRLast = myFeatureResults.end();
110 for (; aRIt != aRLast; aRIt++) {
111 ResultPtr aResult = *aRIt;
112 if (!isVisible(aDisplayer, aResult))
114 GeomShapePtr aGeomShape = aResult->shape();
115 if (!aGeomShape.get())
117 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
118 // change deviation coefficient to provide more precise circle
119 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
120 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
124 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
125 const Standard_Integer aMode)
127 // the presentation is not used in the selection
130 bool PartSet_OperationPrs::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
132 bool aVisible = false;
133 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
134 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
135 if (aPrs.get() || aResult.get())
136 aVisible = theDisplayer->isVisible(theObject);
138 // check if all results of the feature are visible
139 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
140 std::list<ResultPtr> aResults = aFeature->results();
141 std::list<ResultPtr>::const_iterator aIt;
142 aVisible = !aResults.empty();
143 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
144 aVisible = aVisible && theDisplayer->isVisible(*aIt);
150 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
153 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
154 if (aComposite.get())
155 isSub = aComposite->isSub(theObject);
160 void addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
161 const FeaturePtr& theFeature,
162 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
164 if (theObject.get()) {
165 GeomShapePtr aShape = theShape;
167 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
169 aShape = aResult->shape();
171 if (!isSubObject(theObject, theFeature)) {
172 if (theObjectShapes.contains(theObject))
173 theObjectShapes[theObject].append(aShape);
175 QList<GeomShapePtr> aShapes;
176 aShapes.append(aShape);
177 theObjectShapes[theObject] = aShapes;
183 void PartSet_OperationPrs::getFeatureShapes(QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
185 if (!myFeature.get())
188 QList<GeomShapePtr> aShapes;
189 std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
190 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
191 for (; anIt != aLast; anIt++) {
192 AttributePtr anAttribute = *anIt;
193 if (!isSelectionAttribute(anAttribute))
196 std::string anAttrType = anAttribute->attributeType();
198 if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
199 std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
200 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
201 for(int i = 0; i < aCurSelList->size(); i++) {
202 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
203 ResultPtr aResult = aSelAttribute->context();
204 GeomShapePtr aShape = aSelAttribute->value();
205 addValue(aResult, aShape, myFeature, theObjectShapes);
208 if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
209 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
210 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
211 for (int i = 0; i < aCurSelList->size(); i++) {
213 addValue(aCurSelList->object(i), aShape, myFeature, theObjectShapes);
219 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
220 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
221 if (anAttr->isObject()) {
222 anObject = anAttr->object();
225 aShape = PartSet_Tools::findShapeBy2DPoint(anAttr, myWorkshop);
226 // the distance point is not found if the point is selected in the 2nd time
227 // TODO: after debug, this check can be removed
230 anObject = anAttr->attr()->owner();
233 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
234 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
235 anObject = anAttr->context();
236 aShape = anAttr->value();
238 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
239 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
240 anObject = anAttr->value();
242 addValue(anObject, aShape, myFeature, theObjectShapes);
247 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
249 std::string anAttrType = theAttribute->attributeType();
251 return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
252 anAttrType == ModelAPI_AttributeRefList::typeId() ||
253 anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
254 anAttrType == ModelAPI_AttributeSelection::typeId() ||
255 anAttrType == ModelAPI_AttributeReference::typeId();
258 XGUI_Workshop* PartSet_OperationPrs::workshop() const
260 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
261 return aConnector->workshop();