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),
43 myShapeColor = ModuleBase_Tools::color("Visualization", "construction_plane_color", "1,1,0");
44 myResultColor = ModuleBase_Tools::color("Visualization", "construction_plane_color", "0,1,0");
47 void PartSet_OperationPrs::setFeature(const FeaturePtr& theFeature)
49 myFeature = theFeature;
52 void PartSet_OperationPrs::updateShapes()
54 myFeatureResults.clear();
56 myFeatureResults = myFeature->results();
59 bool PartSet_OperationPrs::hasShapes()
61 bool aHasShapes = !myFeatureShapes.empty();
63 // find a result which contains a shape
64 if (!aHasShapes && !myFeatureResults.empty()) {
65 std::list<ResultPtr>::const_iterator aRIt = myFeatureResults.begin(),
66 aRLast = myFeatureResults.end();
67 XGUI_Displayer* aDisplayer = workshop()->displayer();
68 for (; aRIt != aRLast; aRIt++) {
69 ResultPtr aResult = *aRIt;
70 if (!isVisible(aDisplayer, aResult))
72 GeomShapePtr aGeomShape = aResult->shape();
73 if (!aGeomShape.get())
75 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
84 void PartSet_OperationPrs::setColors(const Quantity_Color& theShapeColor, const Quantity_Color& theResultColor)
86 myShapeColor = theShapeColor;
87 myResultColor = theResultColor;
90 void PartSet_OperationPrs::useAISWidth()
95 void PartSet_OperationPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
96 const Handle(Prs3d_Presentation)& thePresentation,
97 const Standard_Integer theMode)
101 // when the feature can not be visualized in the module, the operation preview should not
102 // be visualized also
103 if (!myWorkshop->module()->canDisplayObject(myFeature))
106 SetColor(myShapeColor);
107 thePresentation->Clear();
108 XGUI_Displayer* aDisplayer = workshop()->displayer();
110 Handle(Prs3d_Drawer) aDrawer = Attributes();
112 // create presentations on the base of the shapes
113 QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = myFeatureShapes.begin(),
114 aLast = myFeatureShapes.end();
115 for (; anIt != aLast; anIt++) {
116 ObjectPtr anObject = anIt.key();
117 if (!isVisible(aDisplayer, anObject))
119 QList<GeomShapePtr> aShapes = anIt.value();
120 QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
121 for (; aShIt != aShLast; aShIt++) {
122 GeomShapePtr aGeomShape = *aShIt;
123 if (!aGeomShape.get())
125 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
126 // change deviation coefficient to provide more precise circle
127 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
130 AISObjectPtr anAISPtr = aDisplayer->getAISObject(anObject);
131 if (anAISPtr.get()) {
132 Handle(AIS_InteractiveObject) anIO = anAISPtr->impl<Handle(AIS_InteractiveObject)>();
134 setWidth(aDrawer, anIO->Width());
137 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
141 // create presentations on the base of the results
142 SetColor(myResultColor);
143 std::list<ResultPtr>::const_iterator aRIt = myFeatureResults.begin(),
144 aRLast = myFeatureResults.end();
145 for (; aRIt != aRLast; aRIt++) {
146 ResultPtr aResult = *aRIt;
147 if (!isVisible(aDisplayer, aResult))
149 GeomShapePtr aGeomShape = aResult->shape();
150 if (!aGeomShape.get())
152 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
153 // change deviation coefficient to provide more precise circle
154 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
155 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
157 QList<ModuleBase_ViewerPrs> aValues;
158 ModuleBase_IPropertyPanel* aPanel = myWorkshop->propertyPanel();
160 ModuleBase_ModelWidget* aWidget = aPanel->activeWidget();
162 aWidget->getHighlighted(aValues);
167 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
168 const Standard_Integer aMode)
170 // the presentation is not used in the selection
173 bool PartSet_OperationPrs::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
175 bool aVisible = false;
176 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
177 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
178 if (aPrs.get() || aResult.get())
179 aVisible = theDisplayer->isVisible(theObject);
181 // check if all results of the feature are visible
182 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
183 std::list<ResultPtr> aResults = aFeature->results();
184 std::list<ResultPtr>::const_iterator aIt;
185 aVisible = !aResults.empty();
186 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
187 aVisible = aVisible && theDisplayer->isVisible(*aIt);
193 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
196 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
197 if (aComposite.get())
198 isSub = aComposite->isSub(theObject);
203 void addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
204 const FeaturePtr& theFeature,
205 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
207 if (theObject.get()) {
208 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
210 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
211 if (aCompsolidResult.get()) {
212 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
213 ResultPtr aSubResult = aCompsolidResult->subResult(i);
214 if (aSubResult.get()) {
216 addValue(aSubResult, aShape, theFeature, theObjectShapes);
224 GeomShapePtr aShape = theShape;
226 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
228 aShape = aResult->shape();
230 if (!isSubObject(theObject, theFeature)) {
231 if (theObjectShapes.contains(theObject))
232 theObjectShapes[theObject].append(aShape);
234 QList<GeomShapePtr> aShapes;
235 aShapes.append(aShape);
236 theObjectShapes[theObject] = aShapes;
242 void PartSet_OperationPrs::getFeatureShapes(const FeaturePtr& theFeature,
243 ModuleBase_IWorkshop* theWorkshop,
244 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
246 theObjectShapes.clear();
247 if (!theFeature.get())
250 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
252 QList<GeomShapePtr> aShapes;
253 std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
254 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
255 for (; anIt != aLast; anIt++) {
256 AttributePtr anAttribute = *anIt;
257 if (!isSelectionAttribute(anAttribute))
260 if (!aValidators->isCase(theFeature, anAttribute->id()))
261 continue; // this attribute is not participated in the current case
263 std::string anAttrType = anAttribute->attributeType();
265 if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
266 std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
267 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
268 for(int i = 0; i < aCurSelList->size(); i++) {
269 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
270 ResultPtr aResult = aSelAttribute->context();
271 GeomShapePtr aShape = aSelAttribute->value();
272 addValue(aResult, aShape, theFeature, theObjectShapes);
275 if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
276 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
277 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
278 for (int i = 0; i < aCurSelList->size(); i++) {
280 addValue(aCurSelList->object(i), aShape, theFeature, theObjectShapes);
286 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
287 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
288 if (anAttr->isObject()) {
289 anObject = anAttr->object();
292 aShape = PartSet_Tools::findShapeBy2DPoint(anAttr, theWorkshop);
293 // the distance point is not found if the point is selected in the 2nd time
294 // TODO: after debug, this check can be removed
297 anObject = anAttr->attr()->owner();
300 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
301 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
302 anObject = anAttr->context();
303 aShape = anAttr->value();
305 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
306 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
307 anObject = anAttr->value();
309 addValue(anObject, aShape, theFeature, theObjectShapes);
314 void PartSet_OperationPrs::getHighlightedShapes(ModuleBase_IWorkshop* theWorkshop,
315 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
317 theObjectShapes.clear();
319 QList<ModuleBase_ViewerPrs> aValues;
320 ModuleBase_IPropertyPanel* aPanel = theWorkshop->propertyPanel();
322 ModuleBase_ModelWidget* aWidget = aPanel->activeWidget();
324 aWidget->getHighlighted(aValues);
328 QList<GeomShapePtr> aShapes;
329 QList<ModuleBase_ViewerPrs>::const_iterator anIIt = aValues.begin(),
330 aILast = aValues.end();
331 for (; anIIt != aILast; anIIt++) {
332 ModuleBase_ViewerPrs aPrs = *anIIt;
333 ObjectPtr anObject = aPrs.object();
335 GeomShapePtr aGeomShape;
337 TopoDS_Shape aShape = aPrs.shape();
338 if (!aShape.IsNull()) {
339 aGeomShape = GeomShapePtr(new GeomAPI_Shape());
340 aGeomShape->setImpl(new TopoDS_Shape(aShape));
343 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
345 aGeomShape = aResult->shape();
349 if (theObjectShapes.contains(anObject))
350 theObjectShapes[anObject].append(aGeomShape);
352 QList<GeomShapePtr> aShapes;
353 aShapes.append(aGeomShape);
354 theObjectShapes[anObject] = aShapes;
360 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
362 std::string anAttrType = theAttribute->attributeType();
364 return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
365 anAttrType == ModelAPI_AttributeRefList::typeId() ||
366 anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
367 anAttrType == ModelAPI_AttributeSelection::typeId() ||
368 anAttrType == ModelAPI_AttributeReference::typeId();
371 XGUI_Workshop* PartSet_OperationPrs::workshop() const
373 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
374 return aConnector->workshop();