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"
15 #include "ModuleBase_IModule.h"
17 #include <ModelAPI_Result.h>
18 #include <ModelAPI_Attribute.h>
19 #include <ModelAPI_AttributeRefAttr.h>
20 #include <ModelAPI_AttributeReference.h>
21 #include <ModelAPI_AttributeSelection.h>
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_AttributeRefList.h>
24 #include <ModelAPI_Validator.h>
25 #include <ModelAPI_Session.h>
27 #include <GeomValidators_Tools.h>
29 #include <GeomAPI_IPresentable.h>
31 #include <StdPrs_WFDeflectionShape.hxx>
35 IMPLEMENT_STANDARD_HANDLE(PartSet_OperationPrs, ViewerData_AISShape);
36 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
38 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
39 : ViewerData_AISShape(TopoDS_Shape()), myFeature(FeaturePtr()), myWorkshop(theWorkshop)
41 myShapeColor = ModuleBase_Tools::color("Visualization", "construction_plane_color", "1,1,0");
42 myResultColor = ModuleBase_Tools::color("Visualization", "construction_plane_color", "0,1,0");
45 bool PartSet_OperationPrs::canActivate(const FeaturePtr& theFeature)
47 bool aHasSelectionAttribute = false;
49 std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
50 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
51 for (; anIt != aLast && !aHasSelectionAttribute; anIt++)
52 aHasSelectionAttribute = isSelectionAttribute(*anIt);
54 return aHasSelectionAttribute;
57 void PartSet_OperationPrs::setFeature(const FeaturePtr& theFeature)
59 myFeature = theFeature;
63 /*bool PartSet_OperationPrs::dependOn(const ObjectPtr& theResult)
65 return myFeatureShapes.contains(theResult);
68 void PartSet_OperationPrs::updateShapes()
70 myFeatureShapes.clear();
71 getFeatureShapes(myFeatureShapes);
73 myFeatureResults.clear();
75 myFeatureResults = myFeature->results();
78 void PartSet_OperationPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
79 const Handle(Prs3d_Presentation)& thePresentation,
80 const Standard_Integer theMode)
82 // when the feature can not be visualized in the module, the operation preview should not
84 if (!myWorkshop->module()->canDisplayObject(myFeature))
87 Quantity_Color aColor(1., 1., 0., Quantity_TOC_RGB); // yellow
90 thePresentation->Clear();
91 XGUI_Displayer* aDisplayer = workshop()->displayer();
93 // create presentations on the base of the shapes
94 Handle(Prs3d_Drawer) aDrawer = Attributes();
95 QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = myFeatureShapes.begin(),
96 aLast = myFeatureShapes.end();
97 for (; anIt != aLast; anIt++) {
98 ObjectPtr anObject = anIt.key();
99 if (!isVisible(aDisplayer, anObject))
101 QList<GeomShapePtr> aShapes = anIt.value();
102 QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
103 for (; aShIt != aShLast; aShIt++) {
104 GeomShapePtr aGeomShape = *aShIt;
105 if (!aGeomShape.get())
107 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
108 // change deviation coefficient to provide more precise circle
109 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
110 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
114 aColor = Quantity_Color(0., 1., 0., Quantity_TOC_RGB); // green
117 std::list<ResultPtr>::const_iterator aRIt = myFeatureResults.begin(),
118 aRLast = myFeatureResults.end();
119 for (; aRIt != aRLast; aRIt++) {
120 ResultPtr aResult = *aRIt;
121 if (!isVisible(aDisplayer, aResult))
123 GeomShapePtr aGeomShape = aResult->shape();
124 if (!aGeomShape.get())
126 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
127 // change deviation coefficient to provide more precise circle
128 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
129 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
133 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
134 const Standard_Integer aMode)
136 // the presentation is not used in the selection
139 bool PartSet_OperationPrs::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
141 bool aVisible = false;
142 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
143 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
144 if (aPrs.get() || aResult.get())
145 aVisible = theDisplayer->isVisible(theObject);
147 // check if all results of the feature are visible
148 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
149 std::list<ResultPtr> aResults = aFeature->results();
150 std::list<ResultPtr>::const_iterator aIt;
151 aVisible = !aResults.empty();
152 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
153 aVisible = aVisible && theDisplayer->isVisible(*aIt);
159 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
162 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
163 if (aComposite.get())
164 isSub = aComposite->isSub(theObject);
169 void addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
170 const FeaturePtr& theFeature,
171 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
173 if (theObject.get()) {
174 GeomShapePtr aShape = theShape;
176 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
178 aShape = aResult->shape();
180 if (!isSubObject(theObject, theFeature)) {
181 if (theObjectShapes.contains(theObject))
182 theObjectShapes[theObject].append(aShape);
184 QList<GeomShapePtr> aShapes;
185 aShapes.append(aShape);
186 theObjectShapes[theObject] = aShapes;
192 void PartSet_OperationPrs::getFeatureShapes(QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
194 if (!myFeature.get())
197 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
199 QList<GeomShapePtr> aShapes;
200 std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
201 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
202 for (; anIt != aLast; anIt++) {
203 AttributePtr anAttribute = *anIt;
204 if (!isSelectionAttribute(anAttribute))
207 if (!aValidators->isCase(myFeature, anAttribute->id()))
208 continue; // this attribute is not participated in the current case
210 std::string anAttrType = anAttribute->attributeType();
212 if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
213 std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
214 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
215 for(int i = 0; i < aCurSelList->size(); i++) {
216 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
217 ResultPtr aResult = aSelAttribute->context();
218 GeomShapePtr aShape = aSelAttribute->value();
219 addValue(aResult, aShape, myFeature, theObjectShapes);
222 if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
223 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
224 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
225 for (int i = 0; i < aCurSelList->size(); i++) {
227 addValue(aCurSelList->object(i), aShape, myFeature, theObjectShapes);
233 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
234 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
235 if (anAttr->isObject()) {
236 anObject = anAttr->object();
239 aShape = PartSet_Tools::findShapeBy2DPoint(anAttr, myWorkshop);
240 // the distance point is not found if the point is selected in the 2nd time
241 // TODO: after debug, this check can be removed
244 anObject = anAttr->attr()->owner();
247 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
248 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
249 anObject = anAttr->context();
250 aShape = anAttr->value();
252 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
253 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
254 anObject = anAttr->value();
256 addValue(anObject, aShape, myFeature, theObjectShapes);
261 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
263 std::string anAttrType = theAttribute->attributeType();
265 return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
266 anAttrType == ModelAPI_AttributeRefList::typeId() ||
267 anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
268 anAttrType == ModelAPI_AttributeSelection::typeId() ||
269 anAttrType == ModelAPI_AttributeReference::typeId();
272 XGUI_Workshop* PartSet_OperationPrs::workshop() const
274 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
275 return aConnector->workshop();