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"
13 #include "XGUI_Tools.h"
15 #include "ModuleBase_Tools.h"
16 #include "ModuleBase_IModule.h"
17 #include <ModuleBase_IPropertyPanel.h>
18 #include <ModuleBase_ModelWidget.h>
19 #include <ModuleBase_ViewerPrs.h>
21 #include <ModelAPI_Events.h>
22 #include <ModelAPI_Result.h>
23 #include <ModelAPI_Attribute.h>
24 #include <ModelAPI_AttributeRefAttr.h>
25 #include <ModelAPI_AttributeReference.h>
26 #include <ModelAPI_AttributeSelection.h>
27 #include <ModelAPI_AttributeSelectionList.h>
28 #include <ModelAPI_AttributeRefList.h>
29 #include <ModelAPI_Validator.h>
30 #include <ModelAPI_Session.h>
31 #include <ModelAPI_ResultCompSolid.h>
33 #include <Events_Error.h>
34 #include <Events_Loop.h>
36 #include <GeomAPI_IPresentable.h>
38 #include <StdPrs_WFDeflectionShape.hxx>
42 //#define DEBUG_EMPTY_SHAPE
44 // multi-rotation/translation operation
45 //#define DEBUG_HIDE_COPY_ATTRIBUTE
46 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
47 #include <ModelAPI_AttributeBoolean.h>
48 #include <SketchPlugin_SketchEntity.h>
51 IMPLEMENT_STANDARD_HANDLE(PartSet_OperationPrs, ViewerData_AISShape);
52 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
54 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
55 : ViewerData_AISShape(TopoDS_Shape()), myWorkshop(theWorkshop), myUseAISWidth(false)
57 myShapeColor = Quantity_Color(1, 1, 1, Quantity_TOC_RGB);
60 bool PartSet_OperationPrs::hasShapes()
62 return !myFeatureShapes.empty();
65 void PartSet_OperationPrs::setShapeColor(const Quantity_Color& theColor)
67 myShapeColor = theColor;
70 void PartSet_OperationPrs::useAISWidth()
75 void PartSet_OperationPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
76 const Handle(Prs3d_Presentation)& thePresentation,
77 const Standard_Integer theMode)
79 SetColor(myShapeColor);
80 thePresentation->Clear();
82 NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)> aShapeToPrsMap;
83 fillShapeList(myFeatureShapes, aShapeToPrsMap);
85 if (!aShapeToPrsMap.IsEmpty()) {
86 myShapeToPrsMap.Clear();
87 myShapeToPrsMap.Assign(aShapeToPrsMap);
89 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(myWorkshop)->displayer();
90 Handle(Prs3d_Drawer) aDrawer = Attributes();
91 // create presentations on the base of the shapes
92 for(NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>::Iterator anIter(myShapeToPrsMap);
93 anIter.More(); anIter.Next()) {
94 const TopoDS_Shape& aShape = anIter.Key();
95 // change deviation coefficient to provide more precise circle
96 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
99 Handle(AIS_InteractiveObject) anIO = anIter.Value();
100 if (!anIO.IsNull()) {
101 int aWidth = anIO->Width();
102 /// workaround for zero width. Else, there will be a crash
103 if (aWidth == 0) { // width returns of TSolid shape is zero
104 bool isDisplayed = !anIO->GetContext().IsNull();
105 aWidth = PartSet_Tools::getAISDefaultWidth();// default width value
107 setWidth(aDrawer, aWidth);
110 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
113 if (myShapeToPrsMap.IsEmpty()) {
114 Events_Error::throwException("An empty AIS presentation: PartSet_OperationPrs");
116 //std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
117 // new Events_Message(Events_Loop::eventByName(EVENT_EMPTY_OPERATION_PRESENTATION)));
118 //Events_Loop::loop()->send(aMsg);
122 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
123 const Standard_Integer aMode)
125 // the presentation is not used in the selection
128 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
131 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
132 if (aComposite.get())
133 isSub = aComposite->isSub(theObject);
138 void PartSet_OperationPrs::addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
139 const FeaturePtr& theFeature, ModuleBase_IWorkshop* theWorkshop,
140 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
142 if (theObject.get()) {
143 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
145 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
146 if (aCompsolidResult.get()) {
147 if (aCompsolidResult->numberOfSubs() > 0) {
148 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
149 ResultPtr aSubResult = aCompsolidResult->subResult(i);
150 if (aSubResult.get()) {
152 addValue(aSubResult, aShape, theFeature, theWorkshop, theObjectShapes);
158 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
160 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
161 if (aFeature.get()) {
162 AttributeBooleanPtr aCopyAttr = aFeature->data()->boolean(SketchPlugin_SketchEntity::COPY_ID());
163 if (aCopyAttr.get()) {
164 bool isCopy = aCopyAttr->value();
173 GeomShapePtr aShape = theShape;
175 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
177 aShape = aResult->shape();
179 if (!isSubObject(theObject, theFeature))
180 appendShapeIfVisible(theWorkshop, theObject, aShape, theObjectShapes);
184 void PartSet_OperationPrs::appendShapeIfVisible(ModuleBase_IWorkshop* theWorkshop,
185 const ObjectPtr& theObject,
186 GeomShapePtr theGeomShape,
187 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
189 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
190 if (XGUI_Displayer::isVisible(aDisplayer, theObject)) {
191 if (theGeomShape.get()) {
192 if (theObjectShapes.contains(theObject))
193 theObjectShapes[theObject].append(theGeomShape);
195 QList<GeomShapePtr> aShapes;
196 aShapes.append(theGeomShape);
197 theObjectShapes[theObject] = aShapes;
200 #ifdef DEBUG_EMPTY_SHAPE
201 qDebug(QString("Empty shape in result, result: %1")
202 .arg(ModuleBase_Tools::objectInfo(theObject)).toStdString().c_str());
208 void PartSet_OperationPrs::getFeatureShapes(const FeaturePtr& theFeature,
209 ModuleBase_IWorkshop* theWorkshop,
210 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
212 theObjectShapes.clear();
213 if (!theFeature.get())
216 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
218 QList<GeomShapePtr> aShapes;
219 std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
220 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
221 for (; anIt != aLast; anIt++) {
222 AttributePtr anAttribute = *anIt;
223 if (!isSelectionAttribute(anAttribute))
226 if (!aValidators->isCase(theFeature, anAttribute->id()))
227 continue; // this attribute is not participated in the current case
229 std::string anAttrType = anAttribute->attributeType();
231 if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
232 std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
233 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
234 for(int i = 0; i < aCurSelList->size(); i++) {
235 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
236 ResultPtr aResult = aSelAttribute->context();
237 GeomShapePtr aShape = aSelAttribute->value();
238 addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
241 if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
242 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
243 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
244 for (int i = 0; i < aCurSelList->size(); i++) {
245 ObjectPtr anObject = aCurSelList->object(i);
246 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
247 // if a feature is stored in the attribute, we should obtain the feature results
248 // e.g. feature rectangle uses parameters feature lines in the attribute
249 if (aFeature.get()) {
250 getResultShapes(aFeature, theWorkshop, theObjectShapes, false);
253 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
255 GeomShapePtr aShape = aResult->shape();
257 addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
265 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
266 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
267 if (anAttr->isObject()) {
268 anObject = anAttr->object();
271 AttributePtr anAttribute = anAttr->attr();
272 aShape = PartSet_Tools::findShapeBy2DPoint(anAttribute, theWorkshop);
273 // the distance point is not found if the point is selected in the 2nd time
274 // TODO: after debug, this check can be removed
277 anObject = anAttr->attr()->owner();
280 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
281 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
282 anObject = anAttr->context();
283 aShape = anAttr->value();
285 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
286 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
287 anObject = anAttr->value();
289 addValue(anObject, aShape, theFeature, theWorkshop, theObjectShapes);
294 void PartSet_OperationPrs::getResultShapes(const FeaturePtr& theFeature,
295 ModuleBase_IWorkshop* theWorkshop,
296 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes,
297 const bool theListShouldBeCleared)
299 if (theListShouldBeCleared)
300 theObjectShapes.clear();
302 if (!theFeature.get())
305 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
307 std::list<ResultPtr> aFeatureResults = theFeature->results();
308 std::list<ResultPtr>::const_iterator aRIt = aFeatureResults.begin(),
309 aRLast = aFeatureResults.end();
310 for (; aRIt != aRLast; aRIt++) {
311 ResultPtr aResult = *aRIt;
312 GeomShapePtr aGeomShape = aResult->shape();
313 appendShapeIfVisible(theWorkshop, aResult, aGeomShape, theObjectShapes);
317 void PartSet_OperationPrs::getHighlightedShapes(ModuleBase_IWorkshop* theWorkshop,
318 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
320 theObjectShapes.clear();
322 QList<ModuleBase_ViewerPrsPtr> aValues;
323 ModuleBase_IPropertyPanel* aPanel = theWorkshop->propertyPanel();
325 ModuleBase_ModelWidget* aWidget = aPanel->activeWidget();
327 aWidget->getHighlighted(aValues);
331 QList<GeomShapePtr> aShapes;
332 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIIt = aValues.begin(),
333 aILast = aValues.end();
334 for (; anIIt != aILast; anIIt++) {
335 ModuleBase_ViewerPrsPtr aPrs = *anIIt;
336 ObjectPtr anObject = aPrs->object();
338 GeomShapePtr aGeomShape = aPrs->shape();
339 if (!aGeomShape.get() || aGeomShape->isNull()) {
340 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
342 aGeomShape = aResult->shape();
345 appendShapeIfVisible(theWorkshop, anObject, aGeomShape, theObjectShapes);
350 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
352 std::string anAttrType = theAttribute->attributeType();
354 return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
355 anAttrType == ModelAPI_AttributeRefList::typeId() ||
356 anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
357 anAttrType == ModelAPI_AttributeSelection::typeId() ||
358 anAttrType == ModelAPI_AttributeReference::typeId();
361 void PartSet_OperationPrs::fillShapeList(const QMap<ObjectPtr, QList<GeomShapePtr> >& theFeatureShapes,
362 NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>& theShapeToPrsMap)
364 theShapeToPrsMap.Clear();
366 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(myWorkshop)->displayer();
367 Handle(Prs3d_Drawer) aDrawer = Attributes();
369 // create presentations on the base of the shapes
370 QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = myFeatureShapes.begin(),
371 aLast = myFeatureShapes.end();
372 for (; anIt != aLast; anIt++) {
373 ObjectPtr anObject = anIt.key();
374 QList<GeomShapePtr> aShapes = anIt.value();
375 QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
376 for (; aShIt != aShLast; aShIt++) {
377 GeomShapePtr aGeomShape = *aShIt;
378 // the shape should not be checked here on empty value because it should be checked in
379 // appendShapeIfVisible() on the step of filling myFeatureShapes list
380 // the reason is to avoid empty AIS object visualized in the viewer
381 //if (!aGeomShape.get()) continue;
382 TopoDS_Shape aShape = aGeomShape.get() ? aGeomShape->impl<TopoDS_Shape>() : TopoDS_Shape();
383 // change deviation coefficient to provide more precise circle
384 Handle(AIS_InteractiveObject) anIO;
386 AISObjectPtr anAISPtr = aDisplayer->getAISObject(anObject);
388 anIO = anAISPtr->impl<Handle(AIS_InteractiveObject)>();
390 theShapeToPrsMap.Bind(aShape, anIO);