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 void PartSet_OperationPrs::setFeature(const FeaturePtr& theFeature)
48 myFeature = theFeature;
51 void PartSet_OperationPrs::updateShapes()
53 myFeatureShapes.clear();
54 getFeatureShapes(myFeatureShapes);
56 myFeatureResults.clear();
58 myFeatureResults = myFeature->results();
61 bool PartSet_OperationPrs::hasShapes()
63 bool aHasShapes = !myFeatureShapes.empty();
65 // find a result which contains a shape
66 if (!aHasShapes && !myFeatureResults.empty()) {
67 std::list<ResultPtr>::const_iterator aRIt = myFeatureResults.begin(),
68 aRLast = myFeatureResults.end();
69 XGUI_Displayer* aDisplayer = workshop()->displayer();
70 for (; aRIt != aRLast; aRIt++) {
71 ResultPtr aResult = *aRIt;
72 if (!isVisible(aDisplayer, aResult))
74 GeomShapePtr aGeomShape = aResult->shape();
75 if (!aGeomShape.get())
77 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
86 void PartSet_OperationPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
87 const Handle(Prs3d_Presentation)& thePresentation,
88 const Standard_Integer theMode)
92 // when the feature can not be visualized in the module, the operation preview should not
94 if (!myWorkshop->module()->canDisplayObject(myFeature))
97 Quantity_Color aColor(1., 1., 0., Quantity_TOC_RGB); // yellow
100 thePresentation->Clear();
101 XGUI_Displayer* aDisplayer = workshop()->displayer();
103 // create presentations on the base of the shapes
104 Handle(Prs3d_Drawer) aDrawer = Attributes();
106 QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = myFeatureShapes.begin(),
107 aLast = myFeatureShapes.end();
108 for (; anIt != aLast; anIt++) {
109 ObjectPtr anObject = anIt.key();
110 if (!isVisible(aDisplayer, anObject))
112 QList<GeomShapePtr> aShapes = anIt.value();
113 QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
114 for (; aShIt != aShLast; aShIt++) {
115 GeomShapePtr aGeomShape = *aShIt;
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 aColor = Quantity_Color(0., 1., 0., Quantity_TOC_RGB); // green
128 std::list<ResultPtr>::const_iterator aRIt = myFeatureResults.begin(),
129 aRLast = myFeatureResults.end();
130 for (; aRIt != aRLast; aRIt++) {
131 ResultPtr aResult = *aRIt;
132 if (!isVisible(aDisplayer, aResult))
134 GeomShapePtr aGeomShape = aResult->shape();
135 if (!aGeomShape.get())
137 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
138 // change deviation coefficient to provide more precise circle
139 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
140 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
142 QList<ModuleBase_ViewerPrs> aValues;
143 ModuleBase_IPropertyPanel* aPanel = myWorkshop->propertyPanel();
145 ModuleBase_ModelWidget* aWidget = aPanel->activeWidget();
147 aWidget->getHighlighted(aValues);
151 Standard_Real aPreviousWidth = Width();
152 setWidth(aDrawer, aPreviousWidth+3);
153 Handle(AIS_InteractiveContext) aContext = GetContext();
154 Quantity_NameOfColor anHColor = aContext->HilightColor();
156 aColor = Quantity_Color(anHColor);
157 aColor = Quantity_Color((1. + aColor.Red())/2., (1. + aColor.Green())/2.,
158 (1. + aColor.Blue())/2., Quantity_TOC_RGB);
161 QList<ModuleBase_ViewerPrs>::const_iterator anIIt = aValues.begin(),
162 aILast = aValues.end();
163 for (; anIIt != aILast; anIIt++) {
164 ModuleBase_ViewerPrs aPrs = *anIIt;
165 ObjectPtr anObject = aPrs.object();
166 TopoDS_Shape aShape = aPrs.shape();
167 if (aShape.IsNull()) {
168 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
170 GeomShapePtr aGeomShape = aResult->shape();
171 if (aGeomShape.get())
172 aShape = aGeomShape->impl<TopoDS_Shape>();
175 if (!aShape.IsNull()) {
176 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
177 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
180 setWidth(aDrawer, aPreviousWidth);
183 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
184 const Standard_Integer aMode)
186 // the presentation is not used in the selection
189 bool PartSet_OperationPrs::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
191 bool aVisible = false;
192 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
193 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
194 if (aPrs.get() || aResult.get())
195 aVisible = theDisplayer->isVisible(theObject);
197 // check if all results of the feature are visible
198 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
199 std::list<ResultPtr> aResults = aFeature->results();
200 std::list<ResultPtr>::const_iterator aIt;
201 aVisible = !aResults.empty();
202 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
203 aVisible = aVisible && theDisplayer->isVisible(*aIt);
209 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
212 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
213 if (aComposite.get())
214 isSub = aComposite->isSub(theObject);
219 void addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
220 const FeaturePtr& theFeature,
221 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
223 if (theObject.get()) {
224 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
226 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
227 if (aCompsolidResult.get()) {
228 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
229 ResultPtr aSubResult = aCompsolidResult->subResult(i);
230 if (aSubResult.get()) {
232 addValue(aSubResult, aShape, theFeature, theObjectShapes);
240 GeomShapePtr aShape = theShape;
242 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
244 aShape = aResult->shape();
246 if (!isSubObject(theObject, theFeature)) {
247 if (theObjectShapes.contains(theObject))
248 theObjectShapes[theObject].append(aShape);
250 QList<GeomShapePtr> aShapes;
251 aShapes.append(aShape);
252 theObjectShapes[theObject] = aShapes;
258 void PartSet_OperationPrs::getFeatureShapes(QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
260 if (!myFeature.get())
263 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
265 QList<GeomShapePtr> aShapes;
266 std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
267 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
268 for (; anIt != aLast; anIt++) {
269 AttributePtr anAttribute = *anIt;
270 if (!isSelectionAttribute(anAttribute))
273 if (!aValidators->isCase(myFeature, anAttribute->id()))
274 continue; // this attribute is not participated in the current case
276 std::string anAttrType = anAttribute->attributeType();
278 if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
279 std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
280 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
281 for(int i = 0; i < aCurSelList->size(); i++) {
282 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
283 ResultPtr aResult = aSelAttribute->context();
284 GeomShapePtr aShape = aSelAttribute->value();
285 addValue(aResult, aShape, myFeature, theObjectShapes);
288 if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
289 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
290 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
291 for (int i = 0; i < aCurSelList->size(); i++) {
293 addValue(aCurSelList->object(i), aShape, myFeature, theObjectShapes);
299 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
300 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
301 if (anAttr->isObject()) {
302 anObject = anAttr->object();
305 aShape = PartSet_Tools::findShapeBy2DPoint(anAttr, myWorkshop);
306 // the distance point is not found if the point is selected in the 2nd time
307 // TODO: after debug, this check can be removed
310 anObject = anAttr->attr()->owner();
313 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
314 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
315 anObject = anAttr->context();
316 aShape = anAttr->value();
318 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
319 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
320 anObject = anAttr->value();
322 addValue(anObject, aShape, myFeature, theObjectShapes);
327 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
329 std::string anAttrType = theAttribute->attributeType();
331 return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
332 anAttrType == ModelAPI_AttributeRefList::typeId() ||
333 anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
334 anAttrType == ModelAPI_AttributeSelection::typeId() ||
335 anAttrType == ModelAPI_AttributeReference::typeId();
338 XGUI_Workshop* PartSet_OperationPrs::workshop() const
340 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
341 return aConnector->workshop();