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);
122 // compsolid is not visualized in the viewer, but should have presentation when all sub solids are
123 // visible. It is useful for highlight presentation where compsolid shape is selectable
124 if (!aVisible && aResult->groupName() == ModelAPI_ResultCompSolid::group()) {
125 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
126 if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
127 bool anAllSubsVisible = aCompsolidResult->numberOfSubs() > 0;
128 for(int i = 0; i < aCompsolidResult->numberOfSubs() && anAllSubsVisible; i++) {
129 anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
131 aVisible = anAllSubsVisible;
136 // check if all results of the feature are visible
137 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
138 std::list<ResultPtr> aResults = aFeature->results();
139 std::list<ResultPtr>::const_iterator aIt;
140 aVisible = !aResults.empty();
141 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
142 aVisible = aVisible && theDisplayer->isVisible(*aIt);
148 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
151 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
152 if (aComposite.get())
153 isSub = aComposite->isSub(theObject);
158 void addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
159 const FeaturePtr& theFeature,
160 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
162 if (theObject.get()) {
163 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
165 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
166 if (aCompsolidResult.get()) {
167 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
168 ResultPtr aSubResult = aCompsolidResult->subResult(i);
169 if (aSubResult.get()) {
171 addValue(aSubResult, aShape, theFeature, theObjectShapes);
179 GeomShapePtr aShape = theShape;
181 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
183 aShape = aResult->shape();
185 if (!isSubObject(theObject, theFeature)) {
186 if (theObjectShapes.contains(theObject))
187 theObjectShapes[theObject].append(aShape);
189 QList<GeomShapePtr> aShapes;
190 aShapes.append(aShape);
191 theObjectShapes[theObject] = aShapes;
197 void PartSet_OperationPrs::getFeatureShapes(const FeaturePtr& theFeature,
198 ModuleBase_IWorkshop* theWorkshop,
199 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
201 theObjectShapes.clear();
202 if (!theFeature.get())
205 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
207 QList<GeomShapePtr> aShapes;
208 std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
209 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
210 for (; anIt != aLast; anIt++) {
211 AttributePtr anAttribute = *anIt;
212 if (!isSelectionAttribute(anAttribute))
215 if (!aValidators->isCase(theFeature, anAttribute->id()))
216 continue; // this attribute is not participated in the current case
218 std::string anAttrType = anAttribute->attributeType();
220 if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
221 std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
222 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
223 for(int i = 0; i < aCurSelList->size(); i++) {
224 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
225 ResultPtr aResult = aSelAttribute->context();
226 GeomShapePtr aShape = aSelAttribute->value();
227 addValue(aResult, aShape, theFeature, theObjectShapes);
230 if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
231 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
232 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
233 for (int i = 0; i < aCurSelList->size(); i++) {
235 addValue(aCurSelList->object(i), aShape, theFeature, theObjectShapes);
241 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
242 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
243 if (anAttr->isObject()) {
244 anObject = anAttr->object();
247 aShape = PartSet_Tools::findShapeBy2DPoint(anAttr, theWorkshop);
248 // the distance point is not found if the point is selected in the 2nd time
249 // TODO: after debug, this check can be removed
252 anObject = anAttr->attr()->owner();
255 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
256 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
257 anObject = anAttr->context();
258 aShape = anAttr->value();
260 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
261 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
262 anObject = anAttr->value();
264 addValue(anObject, aShape, theFeature, theObjectShapes);
269 void PartSet_OperationPrs::getResultShapes(const FeaturePtr& theFeature,
270 ModuleBase_IWorkshop* theWorkshop,
271 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
273 theObjectShapes.clear();
275 if (!theFeature.get())
278 XGUI_Displayer* aDisplayer = workshop(theWorkshop)->displayer();
280 std::list<ResultPtr> aFeatureResults = theFeature->results();
281 std::list<ResultPtr>::const_iterator aRIt = aFeatureResults.begin(),
282 aRLast = aFeatureResults.end();
283 for (; aRIt != aRLast; aRIt++) {
284 ResultPtr aResult = *aRIt;
285 if (!isVisible(aDisplayer, aResult))
287 GeomShapePtr aGeomShape = aResult->shape();
288 if (!aGeomShape.get())
291 if (theObjectShapes.contains(aResult))
292 theObjectShapes[aResult].append(aGeomShape);
294 QList<GeomShapePtr> aShapes;
295 aShapes.append(aGeomShape);
296 theObjectShapes[aResult] = aShapes;
301 void PartSet_OperationPrs::getHighlightedShapes(ModuleBase_IWorkshop* theWorkshop,
302 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
304 theObjectShapes.clear();
306 QList<ModuleBase_ViewerPrs> aValues;
307 ModuleBase_IPropertyPanel* aPanel = theWorkshop->propertyPanel();
309 ModuleBase_ModelWidget* aWidget = aPanel->activeWidget();
311 aWidget->getHighlighted(aValues);
315 QList<GeomShapePtr> aShapes;
316 QList<ModuleBase_ViewerPrs>::const_iterator anIIt = aValues.begin(),
317 aILast = aValues.end();
318 for (; anIIt != aILast; anIIt++) {
319 ModuleBase_ViewerPrs aPrs = *anIIt;
320 ObjectPtr anObject = aPrs.object();
322 GeomShapePtr aGeomShape;
324 TopoDS_Shape aShape = aPrs.shape();
325 if (!aShape.IsNull()) {
326 aGeomShape = GeomShapePtr(new GeomAPI_Shape());
327 aGeomShape->setImpl(new TopoDS_Shape(aShape));
330 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
332 aGeomShape = aResult->shape();
336 if (theObjectShapes.contains(anObject))
337 theObjectShapes[anObject].append(aGeomShape);
339 QList<GeomShapePtr> aShapes;
340 aShapes.append(aGeomShape);
341 theObjectShapes[anObject] = aShapes;
347 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
349 std::string anAttrType = theAttribute->attributeType();
351 return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
352 anAttrType == ModelAPI_AttributeRefList::typeId() ||
353 anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
354 anAttrType == ModelAPI_AttributeSelection::typeId() ||
355 anAttrType == ModelAPI_AttributeReference::typeId();
358 XGUI_Workshop* PartSet_OperationPrs::workshop(ModuleBase_IWorkshop* theWorkshop)
360 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
361 return aConnector->workshop();