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>
23 #include <ModelAPI_Validator.h>
24 #include <ModelAPI_Session.h>
26 #include <GeomValidators_Tools.h>
28 #include <GeomAPI_IPresentable.h>
30 #include <StdPrs_WFDeflectionShape.hxx>
34 IMPLEMENT_STANDARD_HANDLE(PartSet_OperationPrs, ViewerData_AISShape);
35 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
37 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
38 : ViewerData_AISShape(TopoDS_Shape()), myFeature(FeaturePtr()), myWorkshop(theWorkshop)
42 bool PartSet_OperationPrs::canActivate(const FeaturePtr& theFeature)
44 bool aHasSelectionAttribute = false;
46 std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
47 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
48 for (; anIt != aLast && !aHasSelectionAttribute; anIt++)
49 aHasSelectionAttribute = isSelectionAttribute(*anIt);
51 return aHasSelectionAttribute;
54 void PartSet_OperationPrs::setFeature(const FeaturePtr& theFeature)
56 myFeature = theFeature;
60 /*bool PartSet_OperationPrs::dependOn(const ObjectPtr& theResult)
62 return myFeatureShapes.contains(theResult);
65 void PartSet_OperationPrs::updateShapes()
67 myFeatureShapes.clear();
68 getFeatureShapes(myFeatureShapes);
70 myFeatureResults.clear();
72 myFeatureResults = myFeature->results();
75 void PartSet_OperationPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
76 const Handle(Prs3d_Presentation)& thePresentation,
77 const Standard_Integer theMode)
79 Quantity_Color aColor(1., 1., 0., Quantity_TOC_RGB); // yellow
82 thePresentation->Clear();
83 XGUI_Displayer* aDisplayer = workshop()->displayer();
85 // create presentations on the base of the shapes
86 Handle(Prs3d_Drawer) aDrawer = Attributes();
87 QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = myFeatureShapes.begin(),
88 aLast = myFeatureShapes.end();
89 for (; anIt != aLast; anIt++) {
90 ObjectPtr anObject = anIt.key();
91 if (!isVisible(aDisplayer, anObject))
93 QList<GeomShapePtr> aShapes = anIt.value();
94 QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
95 for (; aShIt != aShLast; aShIt++) {
96 GeomShapePtr aGeomShape = *aShIt;
97 if (!aGeomShape.get())
99 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
100 // change deviation coefficient to provide more precise circle
101 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
102 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
106 aColor = Quantity_Color(0., 1., 0., Quantity_TOC_RGB); // green
109 std::list<ResultPtr>::const_iterator aRIt = myFeatureResults.begin(),
110 aRLast = myFeatureResults.end();
111 for (; aRIt != aRLast; aRIt++) {
112 ResultPtr aResult = *aRIt;
113 if (!isVisible(aDisplayer, aResult))
115 GeomShapePtr aGeomShape = aResult->shape();
116 if (!aGeomShape.get())
118 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
119 // change deviation coefficient to provide more precise circle
120 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
121 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
125 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
126 const Standard_Integer aMode)
128 // the presentation is not used in the selection
131 bool PartSet_OperationPrs::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
133 bool aVisible = false;
134 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
135 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
136 if (aPrs.get() || aResult.get())
137 aVisible = theDisplayer->isVisible(theObject);
139 // check if all results of the feature are visible
140 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
141 std::list<ResultPtr> aResults = aFeature->results();
142 std::list<ResultPtr>::const_iterator aIt;
143 aVisible = !aResults.empty();
144 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
145 aVisible = aVisible && theDisplayer->isVisible(*aIt);
151 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
154 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
155 if (aComposite.get())
156 isSub = aComposite->isSub(theObject);
161 void addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
162 const FeaturePtr& theFeature,
163 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
165 if (theObject.get()) {
166 GeomShapePtr aShape = theShape;
168 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
170 aShape = aResult->shape();
172 if (!isSubObject(theObject, theFeature)) {
173 if (theObjectShapes.contains(theObject))
174 theObjectShapes[theObject].append(aShape);
176 QList<GeomShapePtr> aShapes;
177 aShapes.append(aShape);
178 theObjectShapes[theObject] = aShapes;
184 void PartSet_OperationPrs::getFeatureShapes(QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
186 if (!myFeature.get())
189 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
191 QList<GeomShapePtr> aShapes;
192 std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
193 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
194 for (; anIt != aLast; anIt++) {
195 AttributePtr anAttribute = *anIt;
196 if (!isSelectionAttribute(anAttribute))
199 if (!aValidators->isCase(myFeature, anAttribute->id()))
200 continue; // this attribute is not participated in the current case
202 std::string anAttrType = anAttribute->attributeType();
204 if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
205 std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
206 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
207 for(int i = 0; i < aCurSelList->size(); i++) {
208 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
209 ResultPtr aResult = aSelAttribute->context();
210 GeomShapePtr aShape = aSelAttribute->value();
211 addValue(aResult, aShape, myFeature, theObjectShapes);
214 if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
215 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
216 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
217 for (int i = 0; i < aCurSelList->size(); i++) {
219 addValue(aCurSelList->object(i), aShape, myFeature, theObjectShapes);
225 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
226 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
227 if (anAttr->isObject()) {
228 anObject = anAttr->object();
231 aShape = PartSet_Tools::findShapeBy2DPoint(anAttr, myWorkshop);
232 // the distance point is not found if the point is selected in the 2nd time
233 // TODO: after debug, this check can be removed
236 anObject = anAttr->attr()->owner();
239 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
240 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
241 anObject = anAttr->context();
242 aShape = anAttr->value();
244 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
245 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
246 anObject = anAttr->value();
248 addValue(anObject, aShape, myFeature, theObjectShapes);
253 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
255 std::string anAttrType = theAttribute->attributeType();
257 return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
258 anAttrType == ModelAPI_AttributeRefList::typeId() ||
259 anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
260 anAttrType == ModelAPI_AttributeSelection::typeId() ||
261 anAttrType == ModelAPI_AttributeReference::typeId();
264 XGUI_Workshop* PartSet_OperationPrs::workshop() const
266 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
267 return aConnector->workshop();