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 <Events_Error.h>
32 #include <GeomAPI_IPresentable.h>
34 #include <StdPrs_WFDeflectionShape.hxx>
38 static const int AIS_DEFAULT_WIDTH = 2;
40 IMPLEMENT_STANDARD_HANDLE(PartSet_OperationPrs, ViewerData_AISShape);
41 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
43 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
44 : ViewerData_AISShape(TopoDS_Shape()), myWorkshop(theWorkshop), myUseAISWidth(false)
46 myShapeColor = Quantity_Color(1, 1, 1, Quantity_TOC_RGB);
49 bool PartSet_OperationPrs::hasShapes()
51 return !myFeatureShapes.empty();
54 void PartSet_OperationPrs::setShapeColor(const Quantity_Color& theColor)
56 myShapeColor = theColor;
59 void PartSet_OperationPrs::useAISWidth()
64 void PartSet_OperationPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
65 const Handle(Prs3d_Presentation)& thePresentation,
66 const Standard_Integer theMode)
68 SetColor(myShapeColor);
69 thePresentation->Clear();
71 XGUI_Displayer* aDisplayer = workshop(myWorkshop)->displayer();
72 Handle(Prs3d_Drawer) aDrawer = Attributes();
74 // create presentations on the base of the shapes
75 bool anEmptyAIS = true;
76 QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = myFeatureShapes.begin(),
77 aLast = myFeatureShapes.end();
78 for (; anIt != aLast; anIt++) {
79 ObjectPtr anObject = anIt.key();
80 if (!isVisible(aDisplayer, anObject))
82 QList<GeomShapePtr> aShapes = anIt.value();
83 QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
84 for (; aShIt != aShLast; aShIt++) {
85 GeomShapePtr aGeomShape = *aShIt;
86 if (!aGeomShape.get())
88 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
89 // change deviation coefficient to provide more precise circle
90 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
93 AISObjectPtr anAISPtr = aDisplayer->getAISObject(anObject);
95 Handle(AIS_InteractiveObject) anIO = anAISPtr->impl<Handle(AIS_InteractiveObject)>();
97 int aWidth = anIO->Width();
98 /// workaround for zero width. Else, there will be a crash
99 if (aWidth == 0) { // width returns of TSolid shape is zero
100 bool isDisplayed = !anIO->GetContext().IsNull();
101 aWidth = AIS_DEFAULT_WIDTH;// default width value
103 setWidth(aDrawer, aWidth);
107 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
113 Events_Error::throwException("An empty AIS presentation: PartSet_OperationPrs");
116 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
117 const Standard_Integer aMode)
119 // the presentation is not used in the selection
122 bool PartSet_OperationPrs::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
124 bool aVisible = false;
125 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
126 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
127 if (aPrs.get() || aResult.get()) {
128 aVisible = theDisplayer->isVisible(theObject);
129 // compsolid is not visualized in the viewer, but should have presentation when all sub solids are
130 // visible. It is useful for highlight presentation where compsolid shape is selectable
131 if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultCompSolid::group()) {
132 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
133 if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
134 bool anAllSubsVisible = aCompsolidResult->numberOfSubs() > 0;
135 for(int i = 0; i < aCompsolidResult->numberOfSubs() && anAllSubsVisible; i++) {
136 anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
138 aVisible = anAllSubsVisible;
143 // check if all results of the feature are visible
144 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
145 std::list<ResultPtr> aResults = aFeature->results();
146 std::list<ResultPtr>::const_iterator aIt;
147 aVisible = !aResults.empty();
148 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
149 aVisible = aVisible && theDisplayer->isVisible(*aIt);
155 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
158 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
159 if (aComposite.get())
160 isSub = aComposite->isSub(theObject);
165 void addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
166 const FeaturePtr& theFeature,
167 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
169 if (theObject.get()) {
170 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
172 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
173 if (aCompsolidResult.get()) {
174 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
175 ResultPtr aSubResult = aCompsolidResult->subResult(i);
176 if (aSubResult.get()) {
178 addValue(aSubResult, aShape, theFeature, theObjectShapes);
186 GeomShapePtr aShape = theShape;
188 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
190 aShape = aResult->shape();
192 if (!isSubObject(theObject, theFeature)) {
193 if (theObjectShapes.contains(theObject))
194 theObjectShapes[theObject].append(aShape);
196 QList<GeomShapePtr> aShapes;
197 aShapes.append(aShape);
198 theObjectShapes[theObject] = aShapes;
204 void PartSet_OperationPrs::getFeatureShapes(const FeaturePtr& theFeature,
205 ModuleBase_IWorkshop* theWorkshop,
206 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
208 theObjectShapes.clear();
209 if (!theFeature.get())
212 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
214 QList<GeomShapePtr> aShapes;
215 std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
216 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
217 for (; anIt != aLast; anIt++) {
218 AttributePtr anAttribute = *anIt;
219 if (!isSelectionAttribute(anAttribute))
222 if (!aValidators->isCase(theFeature, anAttribute->id()))
223 continue; // this attribute is not participated in the current case
225 std::string anAttrType = anAttribute->attributeType();
227 if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
228 std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
229 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
230 for(int i = 0; i < aCurSelList->size(); i++) {
231 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
232 ResultPtr aResult = aSelAttribute->context();
233 GeomShapePtr aShape = aSelAttribute->value();
234 addValue(aResult, aShape, theFeature, theObjectShapes);
237 if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
238 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
239 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
240 for (int i = 0; i < aCurSelList->size(); i++) {
242 addValue(aCurSelList->object(i), aShape, theFeature, theObjectShapes);
248 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
249 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
250 if (anAttr->isObject()) {
251 anObject = anAttr->object();
254 aShape = PartSet_Tools::findShapeBy2DPoint(anAttr, theWorkshop);
255 // the distance point is not found if the point is selected in the 2nd time
256 // TODO: after debug, this check can be removed
259 anObject = anAttr->attr()->owner();
262 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
263 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
264 anObject = anAttr->context();
265 aShape = anAttr->value();
267 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
268 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
269 anObject = anAttr->value();
271 addValue(anObject, aShape, theFeature, theObjectShapes);
276 void PartSet_OperationPrs::getResultShapes(const FeaturePtr& theFeature,
277 ModuleBase_IWorkshop* theWorkshop,
278 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
280 theObjectShapes.clear();
282 if (!theFeature.get())
285 XGUI_Displayer* aDisplayer = workshop(theWorkshop)->displayer();
287 std::list<ResultPtr> aFeatureResults = theFeature->results();
288 std::list<ResultPtr>::const_iterator aRIt = aFeatureResults.begin(),
289 aRLast = aFeatureResults.end();
290 for (; aRIt != aRLast; aRIt++) {
291 ResultPtr aResult = *aRIt;
292 if (!isVisible(aDisplayer, aResult))
294 GeomShapePtr aGeomShape = aResult->shape();
295 if (!aGeomShape.get())
298 if (theObjectShapes.contains(aResult))
299 theObjectShapes[aResult].append(aGeomShape);
301 QList<GeomShapePtr> aShapes;
302 aShapes.append(aGeomShape);
303 theObjectShapes[aResult] = aShapes;
308 void PartSet_OperationPrs::getHighlightedShapes(ModuleBase_IWorkshop* theWorkshop,
309 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
311 theObjectShapes.clear();
313 QList<ModuleBase_ViewerPrs> aValues;
314 ModuleBase_IPropertyPanel* aPanel = theWorkshop->propertyPanel();
316 ModuleBase_ModelWidget* aWidget = aPanel->activeWidget();
318 aWidget->getHighlighted(aValues);
322 QList<GeomShapePtr> aShapes;
323 QList<ModuleBase_ViewerPrs>::const_iterator anIIt = aValues.begin(),
324 aILast = aValues.end();
325 for (; anIIt != aILast; anIIt++) {
326 ModuleBase_ViewerPrs aPrs = *anIIt;
327 ObjectPtr anObject = aPrs.object();
329 GeomShapePtr aGeomShape;
331 TopoDS_Shape aShape = aPrs.shape();
332 if (!aShape.IsNull()) {
333 aGeomShape = GeomShapePtr(new GeomAPI_Shape());
334 aGeomShape->setImpl(new TopoDS_Shape(aShape));
337 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
339 aGeomShape = aResult->shape();
343 if (theObjectShapes.contains(anObject))
344 theObjectShapes[anObject].append(aGeomShape);
346 QList<GeomShapePtr> aShapes;
347 aShapes.append(aGeomShape);
348 theObjectShapes[anObject] = aShapes;
354 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
356 std::string anAttrType = theAttribute->attributeType();
358 return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
359 anAttrType == ModelAPI_AttributeRefList::typeId() ||
360 anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
361 anAttrType == ModelAPI_AttributeSelection::typeId() ||
362 anAttrType == ModelAPI_AttributeReference::typeId();
365 XGUI_Workshop* PartSet_OperationPrs::workshop(ModuleBase_IWorkshop* theWorkshop)
367 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
368 return aConnector->workshop();