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)
40 myShapeColor = ModuleBase_Tools::color("Visualization", "construction_plane_color", "1,1,0");
41 myResultColor = ModuleBase_Tools::color("Visualization", "construction_plane_color", "0,1,0");
44 bool PartSet_OperationPrs::canActivate(const FeaturePtr& theFeature)
46 bool aHasSelectionAttribute = false;
48 std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
49 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
50 for (; anIt != aLast && !aHasSelectionAttribute; anIt++)
51 aHasSelectionAttribute = isSelectionAttribute(*anIt);
53 return aHasSelectionAttribute;
56 void PartSet_OperationPrs::setFeature(const FeaturePtr& theFeature)
58 myFeature = theFeature;
62 /*bool PartSet_OperationPrs::dependOn(const ObjectPtr& theResult)
64 return myFeatureShapes.contains(theResult);
67 void PartSet_OperationPrs::updateShapes()
69 myFeatureShapes.clear();
70 getFeatureShapes(myFeatureShapes);
72 myFeatureResults.clear();
74 myFeatureResults = myFeature->results();
77 void PartSet_OperationPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
78 const Handle(Prs3d_Presentation)& thePresentation,
79 const Standard_Integer theMode)
81 Quantity_Color aColor(1., 1., 0., Quantity_TOC_RGB); // yellow
84 thePresentation->Clear();
85 XGUI_Displayer* aDisplayer = workshop()->displayer();
87 // create presentations on the base of the shapes
88 Handle(Prs3d_Drawer) aDrawer = Attributes();
89 QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = myFeatureShapes.begin(),
90 aLast = myFeatureShapes.end();
91 for (; anIt != aLast; anIt++) {
92 ObjectPtr anObject = anIt.key();
93 if (!isVisible(aDisplayer, anObject))
95 QList<GeomShapePtr> aShapes = anIt.value();
96 QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
97 for (; aShIt != aShLast; aShIt++) {
98 GeomShapePtr aGeomShape = *aShIt;
99 if (!aGeomShape.get())
101 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
102 // change deviation coefficient to provide more precise circle
103 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
104 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
108 aColor = Quantity_Color(0., 1., 0., Quantity_TOC_RGB); // green
111 std::list<ResultPtr>::const_iterator aRIt = myFeatureResults.begin(),
112 aRLast = myFeatureResults.end();
113 for (; aRIt != aRLast; aRIt++) {
114 ResultPtr aResult = *aRIt;
115 if (!isVisible(aDisplayer, aResult))
117 GeomShapePtr aGeomShape = aResult->shape();
118 if (!aGeomShape.get())
120 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
121 // change deviation coefficient to provide more precise circle
122 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
123 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
127 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
128 const Standard_Integer aMode)
130 // the presentation is not used in the selection
133 bool PartSet_OperationPrs::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
135 bool aVisible = false;
136 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
137 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
138 if (aPrs.get() || aResult.get())
139 aVisible = theDisplayer->isVisible(theObject);
141 // check if all results of the feature are visible
142 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
143 std::list<ResultPtr> aResults = aFeature->results();
144 std::list<ResultPtr>::const_iterator aIt;
145 aVisible = !aResults.empty();
146 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
147 aVisible = aVisible && theDisplayer->isVisible(*aIt);
153 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
156 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
157 if (aComposite.get())
158 isSub = aComposite->isSub(theObject);
163 void addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
164 const FeaturePtr& theFeature,
165 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
167 if (theObject.get()) {
168 GeomShapePtr aShape = theShape;
170 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
172 aShape = aResult->shape();
174 if (!isSubObject(theObject, theFeature)) {
175 if (theObjectShapes.contains(theObject))
176 theObjectShapes[theObject].append(aShape);
178 QList<GeomShapePtr> aShapes;
179 aShapes.append(aShape);
180 theObjectShapes[theObject] = aShapes;
186 void PartSet_OperationPrs::getFeatureShapes(QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
188 if (!myFeature.get())
191 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
193 QList<GeomShapePtr> aShapes;
194 std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
195 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
196 for (; anIt != aLast; anIt++) {
197 AttributePtr anAttribute = *anIt;
198 if (!isSelectionAttribute(anAttribute))
201 if (!aValidators->isCase(myFeature, anAttribute->id()))
202 continue; // this attribute is not participated in the current case
204 std::string anAttrType = anAttribute->attributeType();
206 if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
207 std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
208 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
209 for(int i = 0; i < aCurSelList->size(); i++) {
210 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
211 ResultPtr aResult = aSelAttribute->context();
212 GeomShapePtr aShape = aSelAttribute->value();
213 addValue(aResult, aShape, myFeature, theObjectShapes);
216 if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
217 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
218 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
219 for (int i = 0; i < aCurSelList->size(); i++) {
221 addValue(aCurSelList->object(i), aShape, myFeature, theObjectShapes);
227 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
228 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
229 if (anAttr->isObject()) {
230 anObject = anAttr->object();
233 aShape = PartSet_Tools::findShapeBy2DPoint(anAttr, myWorkshop);
234 // the distance point is not found if the point is selected in the 2nd time
235 // TODO: after debug, this check can be removed
238 anObject = anAttr->attr()->owner();
241 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
242 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
243 anObject = anAttr->context();
244 aShape = anAttr->value();
246 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
247 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
248 anObject = anAttr->value();
250 addValue(anObject, aShape, myFeature, theObjectShapes);
255 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
257 std::string anAttrType = theAttribute->attributeType();
259 return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
260 anAttrType == ModelAPI_AttributeRefList::typeId() ||
261 anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
262 anAttrType == ModelAPI_AttributeSelection::typeId() ||
263 anAttrType == ModelAPI_AttributeReference::typeId();
266 XGUI_Workshop* PartSet_OperationPrs::workshop() const
268 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
269 return aConnector->workshop();