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"
16 #include <ModuleBase_IPropertyPanel.h>
17 #include <ModuleBase_ModelWidget.h>
19 #include <ModelAPI_Result.h>
20 #include <ModelAPI_Attribute.h>
21 #include <ModelAPI_AttributeRefAttr.h>
22 #include <ModelAPI_AttributeReference.h>
23 #include <ModelAPI_AttributeSelection.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_AttributeRefList.h>
26 #include <ModelAPI_Validator.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_ResultCompSolid.h>
30 #include <GeomAPI_IPresentable.h>
32 #include <StdPrs_WFDeflectionShape.hxx>
36 IMPLEMENT_STANDARD_HANDLE(PartSet_OperationPrs, ViewerData_AISShape);
37 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
39 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
40 : ViewerData_AISShape(TopoDS_Shape()), myFeature(FeaturePtr()), myWorkshop(theWorkshop)
42 myShapeColor = ModuleBase_Tools::color("Visualization", "construction_plane_color", "1,1,0");
43 myResultColor = ModuleBase_Tools::color("Visualization", "construction_plane_color", "0,1,0");
46 bool PartSet_OperationPrs::canActivate(const FeaturePtr& theFeature)
48 bool aHasSelectionAttribute = false;
50 std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
51 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
52 for (; anIt != aLast && !aHasSelectionAttribute; anIt++)
53 aHasSelectionAttribute = isSelectionAttribute(*anIt);
55 return aHasSelectionAttribute;
58 void PartSet_OperationPrs::setFeature(const FeaturePtr& theFeature)
60 myFeature = theFeature;
64 /*bool PartSet_OperationPrs::dependOn(const ObjectPtr& theResult)
66 return myFeatureShapes.contains(theResult);
69 void PartSet_OperationPrs::updateShapes()
71 myFeatureShapes.clear();
72 getFeatureShapes(myFeatureShapes);
74 myFeatureResults.clear();
76 myFeatureResults = myFeature->results();
79 bool PartSet_OperationPrs::hasShapes()
81 return !myFeatureShapes.empty() || !myFeatureResults.empty();
84 void PartSet_OperationPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
85 const Handle(Prs3d_Presentation)& thePresentation,
86 const Standard_Integer theMode)
90 // when the feature can not be visualized in the module, the operation preview should not
92 if (!myWorkshop->module()->canDisplayObject(myFeature))
95 Quantity_Color aColor(1., 1., 0., Quantity_TOC_RGB); // yellow
98 thePresentation->Clear();
99 XGUI_Displayer* aDisplayer = workshop()->displayer();
101 // create presentations on the base of the shapes
102 Handle(Prs3d_Drawer) aDrawer = Attributes();
104 QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = myFeatureShapes.begin(),
105 aLast = myFeatureShapes.end();
106 for (; anIt != aLast; anIt++) {
107 ObjectPtr anObject = anIt.key();
108 if (!isVisible(aDisplayer, anObject))
110 QList<GeomShapePtr> aShapes = anIt.value();
111 QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
112 for (; aShIt != aShLast; aShIt++) {
113 GeomShapePtr aGeomShape = *aShIt;
114 if (!aGeomShape.get())
116 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
117 // change deviation coefficient to provide more precise circle
118 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
119 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
123 aColor = Quantity_Color(0., 1., 0., Quantity_TOC_RGB); // green
126 std::list<ResultPtr>::const_iterator aRIt = myFeatureResults.begin(),
127 aRLast = myFeatureResults.end();
128 for (; aRIt != aRLast; aRIt++) {
129 ResultPtr aResult = *aRIt;
130 if (!isVisible(aDisplayer, aResult))
132 GeomShapePtr aGeomShape = aResult->shape();
133 if (!aGeomShape.get())
135 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
136 // change deviation coefficient to provide more precise circle
137 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
138 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
140 QList<ModuleBase_ViewerPrs> aValues;
141 ModuleBase_IPropertyPanel* aPanel = myWorkshop->propertyPanel();
143 ModuleBase_ModelWidget* aWidget = aPanel->activeWidget();
145 aWidget->getHighlighted(aValues);
149 Standard_Real aPreviousWidth = Width();
150 setWidth(aDrawer, aPreviousWidth+3);
151 Handle(AIS_InteractiveContext) aContext = GetContext();
152 Quantity_NameOfColor anHColor = aContext->HilightColor();
154 aColor = Quantity_Color(anHColor);
155 aColor = Quantity_Color((1. + aColor.Red())/2., (1. + aColor.Green())/2.,
156 (1. + aColor.Blue())/2., Quantity_TOC_RGB);
159 QList<ModuleBase_ViewerPrs>::const_iterator anIIt = aValues.begin(),
160 aILast = aValues.end();
161 for (; anIIt != aILast; anIIt++) {
162 ModuleBase_ViewerPrs aPrs = *anIIt;
163 ObjectPtr anObject = aPrs.object();
164 TopoDS_Shape aShape = aPrs.shape();
165 if (aShape.IsNull()) {
166 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
168 GeomShapePtr aGeomShape = aResult->shape();
169 if (aGeomShape.get())
170 aShape = aGeomShape->impl<TopoDS_Shape>();
173 if (!aShape.IsNull()) {
174 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
175 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
178 setWidth(aDrawer, aPreviousWidth);
181 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
182 const Standard_Integer aMode)
184 // the presentation is not used in the selection
187 bool PartSet_OperationPrs::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
189 bool aVisible = false;
190 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
191 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
192 if (aPrs.get() || aResult.get())
193 aVisible = theDisplayer->isVisible(theObject);
195 // check if all results of the feature are visible
196 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
197 std::list<ResultPtr> aResults = aFeature->results();
198 std::list<ResultPtr>::const_iterator aIt;
199 aVisible = !aResults.empty();
200 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
201 aVisible = aVisible && theDisplayer->isVisible(*aIt);
207 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
210 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
211 if (aComposite.get())
212 isSub = aComposite->isSub(theObject);
217 void addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
218 const FeaturePtr& theFeature,
219 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
221 if (theObject.get()) {
222 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
224 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
225 if (aCompsolidResult.get()) {
226 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
227 ResultPtr aSubResult = aCompsolidResult->subResult(i);
228 if (aSubResult.get()) {
230 addValue(aSubResult, aShape, theFeature, theObjectShapes);
238 GeomShapePtr aShape = theShape;
240 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
242 aShape = aResult->shape();
244 if (!isSubObject(theObject, theFeature)) {
245 if (theObjectShapes.contains(theObject))
246 theObjectShapes[theObject].append(aShape);
248 QList<GeomShapePtr> aShapes;
249 aShapes.append(aShape);
250 theObjectShapes[theObject] = aShapes;
256 void PartSet_OperationPrs::getFeatureShapes(QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
258 if (!myFeature.get())
261 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
263 QList<GeomShapePtr> aShapes;
264 std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
265 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
266 for (; anIt != aLast; anIt++) {
267 AttributePtr anAttribute = *anIt;
268 if (!isSelectionAttribute(anAttribute))
271 if (!aValidators->isCase(myFeature, anAttribute->id()))
272 continue; // this attribute is not participated in the current case
274 std::string anAttrType = anAttribute->attributeType();
276 if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
277 std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
278 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
279 for(int i = 0; i < aCurSelList->size(); i++) {
280 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
281 ResultPtr aResult = aSelAttribute->context();
282 GeomShapePtr aShape = aSelAttribute->value();
283 addValue(aResult, aShape, myFeature, theObjectShapes);
286 if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
287 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
288 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
289 for (int i = 0; i < aCurSelList->size(); i++) {
291 addValue(aCurSelList->object(i), aShape, myFeature, theObjectShapes);
297 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
298 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
299 if (anAttr->isObject()) {
300 anObject = anAttr->object();
303 aShape = PartSet_Tools::findShapeBy2DPoint(anAttr, myWorkshop);
304 // the distance point is not found if the point is selected in the 2nd time
305 // TODO: after debug, this check can be removed
308 anObject = anAttr->attr()->owner();
311 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
312 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
313 anObject = anAttr->context();
314 aShape = anAttr->value();
316 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
317 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
318 anObject = anAttr->value();
320 addValue(anObject, aShape, myFeature, theObjectShapes);
325 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
327 std::string anAttrType = theAttribute->attributeType();
329 /// workaround: start
330 /// this is a TEMPORARY correction in order to avoid crash by parameter edit
331 /// which is reproduced only on Linux Debian.
332 /// The possible reason is an empty AIS presentation visualized in the viewer.
333 /// The best solution is to analize whether the feature has shapes/results and only
334 /// in this case visualize custom presentation and hide it as soon as they disappear.
335 bool isAttributeArgument = theAttribute->isArgument();
336 if (!isAttributeArgument)
340 return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
341 anAttrType == ModelAPI_AttributeRefList::typeId() ||
342 anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
343 anAttrType == ModelAPI_AttributeSelection::typeId() ||
344 anAttrType == ModelAPI_AttributeReference::typeId();
347 XGUI_Workshop* PartSet_OperationPrs::workshop() const
349 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
350 return aConnector->workshop();