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 static const int AIS_DEFAULT_WIDTH = 2;
38 IMPLEMENT_STANDARD_HANDLE(PartSet_OperationPrs, ViewerData_AISShape);
39 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
41 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
42 : ViewerData_AISShape(TopoDS_Shape()), myWorkshop(theWorkshop), myUseAISWidth(false)
44 myShapeColor = Quantity_Color(1, 1, 1, Quantity_TOC_RGB);
47 bool PartSet_OperationPrs::hasShapes()
49 return !myFeatureShapes.empty();
52 void PartSet_OperationPrs::setShapeColor(const Quantity_Color& theColor)
54 myShapeColor = theColor;
57 void PartSet_OperationPrs::useAISWidth()
62 void PartSet_OperationPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
63 const Handle(Prs3d_Presentation)& thePresentation,
64 const Standard_Integer theMode)
66 SetColor(myShapeColor);
67 thePresentation->Clear();
69 XGUI_Displayer* aDisplayer = workshop(myWorkshop)->displayer();
70 Handle(Prs3d_Drawer) aDrawer = Attributes();
72 // create presentations on the base of the shapes
73 QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = myFeatureShapes.begin(),
74 aLast = myFeatureShapes.end();
75 for (; anIt != aLast; anIt++) {
76 ObjectPtr anObject = anIt.key();
77 if (!isVisible(aDisplayer, anObject))
79 QList<GeomShapePtr> aShapes = anIt.value();
80 QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
81 for (; aShIt != aShLast; aShIt++) {
82 GeomShapePtr aGeomShape = *aShIt;
83 if (!aGeomShape.get())
85 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
86 // change deviation coefficient to provide more precise circle
87 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
90 AISObjectPtr anAISPtr = aDisplayer->getAISObject(anObject);
92 Handle(AIS_InteractiveObject) anIO = anAISPtr->impl<Handle(AIS_InteractiveObject)>();
94 int aWidth = anIO->Width();
95 /// workaround for zero width. Else, there will be a crash
96 if (aWidth == 0) { // width returns of TSolid shape is zero
97 bool isDisplayed = !anIO->GetContext().IsNull();
98 aWidth = AIS_DEFAULT_WIDTH;// default width value
100 setWidth(aDrawer, aWidth);
104 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
109 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
110 const Standard_Integer aMode)
112 // the presentation is not used in the selection
115 bool PartSet_OperationPrs::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
117 bool aVisible = false;
118 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
119 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
120 if (aPrs.get() || aResult.get())
121 aVisible = theDisplayer->isVisible(theObject);
123 // check if all results of the feature are visible
124 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
125 std::list<ResultPtr> aResults = aFeature->results();
126 std::list<ResultPtr>::const_iterator aIt;
127 aVisible = !aResults.empty();
128 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
129 aVisible = aVisible && theDisplayer->isVisible(*aIt);
135 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
138 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
139 if (aComposite.get())
140 isSub = aComposite->isSub(theObject);
145 void addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
146 const FeaturePtr& theFeature,
147 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
149 if (theObject.get()) {
150 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
152 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
153 if (aCompsolidResult.get()) {
154 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
155 ResultPtr aSubResult = aCompsolidResult->subResult(i);
156 if (aSubResult.get()) {
158 addValue(aSubResult, aShape, theFeature, theObjectShapes);
166 GeomShapePtr aShape = theShape;
168 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
170 aShape = aResult->shape();
172 if (!isSubObject(theObject, theFeature)) {
173 if (theObjectShapes.contains(theObject))
174 theObjectShapes[theObject].append(aShape);
176 QList<GeomShapePtr> aShapes;
177 aShapes.append(aShape);
178 theObjectShapes[theObject] = aShapes;
184 void PartSet_OperationPrs::getFeatureShapes(const FeaturePtr& theFeature,
185 ModuleBase_IWorkshop* theWorkshop,
186 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
188 theObjectShapes.clear();
189 if (!theFeature.get())
192 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
194 QList<GeomShapePtr> aShapes;
195 std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
196 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
197 for (; anIt != aLast; anIt++) {
198 AttributePtr anAttribute = *anIt;
199 if (!isSelectionAttribute(anAttribute))
202 if (!aValidators->isCase(theFeature, anAttribute->id()))
203 continue; // this attribute is not participated in the current case
205 std::string anAttrType = anAttribute->attributeType();
207 if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
208 std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
209 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
210 for(int i = 0; i < aCurSelList->size(); i++) {
211 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
212 ResultPtr aResult = aSelAttribute->context();
213 GeomShapePtr aShape = aSelAttribute->value();
214 addValue(aResult, aShape, theFeature, theObjectShapes);
217 if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
218 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
219 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
220 for (int i = 0; i < aCurSelList->size(); i++) {
222 addValue(aCurSelList->object(i), aShape, theFeature, theObjectShapes);
228 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
229 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
230 if (anAttr->isObject()) {
231 anObject = anAttr->object();
234 aShape = PartSet_Tools::findShapeBy2DPoint(anAttr, theWorkshop);
235 // the distance point is not found if the point is selected in the 2nd time
236 // TODO: after debug, this check can be removed
239 anObject = anAttr->attr()->owner();
242 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
243 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
244 anObject = anAttr->context();
245 aShape = anAttr->value();
247 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
248 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
249 anObject = anAttr->value();
251 addValue(anObject, aShape, theFeature, theObjectShapes);
256 void PartSet_OperationPrs::getResultShapes(const FeaturePtr& theFeature,
257 ModuleBase_IWorkshop* theWorkshop,
258 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
260 theObjectShapes.clear();
262 if (!theFeature.get())
265 XGUI_Displayer* aDisplayer = workshop(theWorkshop)->displayer();
267 std::list<ResultPtr> aFeatureResults = theFeature->results();
268 std::list<ResultPtr>::const_iterator aRIt = aFeatureResults.begin(),
269 aRLast = aFeatureResults.end();
270 for (; aRIt != aRLast; aRIt++) {
271 ResultPtr aResult = *aRIt;
272 if (!isVisible(aDisplayer, aResult))
274 GeomShapePtr aGeomShape = aResult->shape();
275 if (!aGeomShape.get())
278 if (theObjectShapes.contains(aResult))
279 theObjectShapes[aResult].append(aGeomShape);
281 QList<GeomShapePtr> aShapes;
282 aShapes.append(aGeomShape);
283 theObjectShapes[aResult] = aShapes;
288 void PartSet_OperationPrs::getHighlightedShapes(ModuleBase_IWorkshop* theWorkshop,
289 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
291 theObjectShapes.clear();
293 QList<ModuleBase_ViewerPrs> aValues;
294 ModuleBase_IPropertyPanel* aPanel = theWorkshop->propertyPanel();
296 ModuleBase_ModelWidget* aWidget = aPanel->activeWidget();
298 aWidget->getHighlighted(aValues);
302 QList<GeomShapePtr> aShapes;
303 QList<ModuleBase_ViewerPrs>::const_iterator anIIt = aValues.begin(),
304 aILast = aValues.end();
305 for (; anIIt != aILast; anIIt++) {
306 ModuleBase_ViewerPrs aPrs = *anIIt;
307 ObjectPtr anObject = aPrs.object();
309 GeomShapePtr aGeomShape;
311 TopoDS_Shape aShape = aPrs.shape();
312 if (!aShape.IsNull()) {
313 aGeomShape = GeomShapePtr(new GeomAPI_Shape());
314 aGeomShape->setImpl(new TopoDS_Shape(aShape));
317 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
319 aGeomShape = aResult->shape();
323 if (theObjectShapes.contains(anObject))
324 theObjectShapes[anObject].append(aGeomShape);
326 QList<GeomShapePtr> aShapes;
327 aShapes.append(aGeomShape);
328 theObjectShapes[anObject] = aShapes;
334 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
336 std::string anAttrType = theAttribute->attributeType();
338 return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
339 anAttrType == ModelAPI_AttributeRefList::typeId() ||
340 anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
341 anAttrType == ModelAPI_AttributeSelection::typeId() ||
342 anAttrType == ModelAPI_AttributeReference::typeId();
345 XGUI_Workshop* PartSet_OperationPrs::workshop(ModuleBase_IWorkshop* theWorkshop)
347 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
348 return aConnector->workshop();