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 bool aReadyToDisplay = !myShapeToPrsMap.IsEmpty();
86 if (aReadyToDisplay) {
87 myShapeToPrsMap.Clear();
88 myShapeToPrsMap.Assign(aShapeToPrsMap);
91 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(myWorkshop)->displayer();
92 Handle(Prs3d_Drawer) aDrawer = Attributes();
93 // create presentations on the base of the shapes
94 for(NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>::Iterator anIter(myShapeToPrsMap);
95 anIter.More(); anIter.Next()) {
96 const TopoDS_Shape& aShape = anIter.Key();
97 // change deviation coefficient to provide more precise circle
98 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
101 Handle(AIS_InteractiveObject) anIO = anIter.Value();
102 if (!anIO.IsNull()) {
103 int aWidth = anIO->Width();
104 /// workaround for zero width. Else, there will be a crash
105 if (aWidth == 0) { // width returns of TSolid shape is zero
106 bool isDisplayed = !anIO->GetContext().IsNull();
107 aWidth = PartSet_Tools::getAISDefaultWidth();// default width value
109 setWidth(aDrawer, aWidth);
112 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
115 if (!aReadyToDisplay) {
116 //Events_Error::throwException("An empty AIS presentation: PartSet_OperationPrs");
117 //std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
118 // new Events_Message(Events_Loop::eventByName(EVENT_EMPTY_OPERATION_PRESENTATION)));
119 //Events_Loop::loop()->send(aMsg);
123 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
124 const Standard_Integer aMode)
126 // the presentation is not used in the selection
129 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
132 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
133 if (aComposite.get())
134 isSub = aComposite->isSub(theObject);
139 void PartSet_OperationPrs::addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
140 const FeaturePtr& theFeature, ModuleBase_IWorkshop* theWorkshop,
141 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
143 if (theObject.get()) {
144 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
146 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
147 if (aCompsolidResult.get()) {
148 if (aCompsolidResult->numberOfSubs() > 0) {
149 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
150 ResultPtr aSubResult = aCompsolidResult->subResult(i);
151 if (aSubResult.get()) {
153 addValue(aSubResult, aShape, theFeature, theWorkshop, theObjectShapes);
159 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
161 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
162 if (aFeature.get()) {
163 AttributeBooleanPtr aCopyAttr = aFeature->data()->boolean(SketchPlugin_SketchEntity::COPY_ID());
164 if (aCopyAttr.get()) {
165 bool isCopy = aCopyAttr->value();
174 GeomShapePtr aShape = theShape;
176 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
178 aShape = aResult->shape();
180 if (!isSubObject(theObject, theFeature))
181 appendShapeIfVisible(theWorkshop, theObject, aShape, theObjectShapes);
185 void PartSet_OperationPrs::appendShapeIfVisible(ModuleBase_IWorkshop* theWorkshop,
186 const ObjectPtr& theObject,
187 GeomShapePtr theGeomShape,
188 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
190 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
191 if (XGUI_Displayer::isVisible(aDisplayer, theObject)) {
192 if (theGeomShape.get()) {
193 if (theObjectShapes.contains(theObject))
194 theObjectShapes[theObject].append(theGeomShape);
196 QList<GeomShapePtr> aShapes;
197 aShapes.append(theGeomShape);
198 theObjectShapes[theObject] = aShapes;
201 #ifdef DEBUG_EMPTY_SHAPE
202 qDebug(QString("Empty shape in result, result: %1")
203 .arg(ModuleBase_Tools::objectInfo(theObject)).toStdString().c_str());
209 void PartSet_OperationPrs::getFeatureShapes(const FeaturePtr& theFeature,
210 ModuleBase_IWorkshop* theWorkshop,
211 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
213 theObjectShapes.clear();
214 if (!theFeature.get())
217 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
219 QList<GeomShapePtr> aShapes;
220 std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
221 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
222 for (; anIt != aLast; anIt++) {
223 AttributePtr anAttribute = *anIt;
224 if (!isSelectionAttribute(anAttribute))
227 if (!aValidators->isCase(theFeature, anAttribute->id()))
228 continue; // this attribute is not participated in the current case
230 std::string anAttrType = anAttribute->attributeType();
232 if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
233 std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
234 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
235 for(int i = 0; i < aCurSelList->size(); i++) {
236 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
237 ResultPtr aResult = aSelAttribute->context();
238 GeomShapePtr aShape = aSelAttribute->value();
239 addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
242 if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
243 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
244 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
245 for (int i = 0; i < aCurSelList->size(); i++) {
246 ObjectPtr anObject = aCurSelList->object(i);
247 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
248 // if a feature is stored in the attribute, we should obtain the feature results
249 // e.g. feature rectangle uses parameters feature lines in the attribute
250 if (aFeature.get()) {
251 getResultShapes(aFeature, theWorkshop, theObjectShapes, false);
254 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
256 GeomShapePtr aShape = aResult->shape();
258 addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
266 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
267 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
268 if (anAttr->isObject()) {
269 anObject = anAttr->object();
272 AttributePtr anAttribute = anAttr->attr();
273 aShape = PartSet_Tools::findShapeBy2DPoint(anAttribute, theWorkshop);
274 // the distance point is not found if the point is selected in the 2nd time
275 // TODO: after debug, this check can be removed
278 anObject = anAttr->attr()->owner();
281 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
282 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
283 anObject = anAttr->context();
284 aShape = anAttr->value();
286 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
287 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
288 anObject = anAttr->value();
290 addValue(anObject, aShape, theFeature, theWorkshop, theObjectShapes);
295 void PartSet_OperationPrs::getResultShapes(const FeaturePtr& theFeature,
296 ModuleBase_IWorkshop* theWorkshop,
297 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes,
298 const bool theListShouldBeCleared)
300 if (theListShouldBeCleared)
301 theObjectShapes.clear();
303 if (!theFeature.get())
306 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
308 std::list<ResultPtr> aFeatureResults = theFeature->results();
309 std::list<ResultPtr>::const_iterator aRIt = aFeatureResults.begin(),
310 aRLast = aFeatureResults.end();
311 for (; aRIt != aRLast; aRIt++) {
312 ResultPtr aResult = *aRIt;
313 GeomShapePtr aGeomShape = aResult->shape();
314 appendShapeIfVisible(theWorkshop, aResult, aGeomShape, theObjectShapes);
318 void PartSet_OperationPrs::getHighlightedShapes(ModuleBase_IWorkshop* theWorkshop,
319 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
321 theObjectShapes.clear();
323 QList<ModuleBase_ViewerPrsPtr> aValues;
324 ModuleBase_IPropertyPanel* aPanel = theWorkshop->propertyPanel();
326 ModuleBase_ModelWidget* aWidget = aPanel->activeWidget();
328 aWidget->getHighlighted(aValues);
332 QList<GeomShapePtr> aShapes;
333 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIIt = aValues.begin(),
334 aILast = aValues.end();
335 for (; anIIt != aILast; anIIt++) {
336 ModuleBase_ViewerPrsPtr aPrs = *anIIt;
337 ObjectPtr anObject = aPrs->object();
339 GeomShapePtr aGeomShape = aPrs->shape();
340 if (!aGeomShape.get() || aGeomShape->isNull()) {
341 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
343 aGeomShape = aResult->shape();
346 appendShapeIfVisible(theWorkshop, anObject, aGeomShape, theObjectShapes);
351 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
353 std::string anAttrType = theAttribute->attributeType();
355 return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
356 anAttrType == ModelAPI_AttributeRefList::typeId() ||
357 anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
358 anAttrType == ModelAPI_AttributeSelection::typeId() ||
359 anAttrType == ModelAPI_AttributeReference::typeId();
362 void PartSet_OperationPrs::fillShapeList(const QMap<ObjectPtr, QList<GeomShapePtr> >& theFeatureShapes,
363 NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>& theShapeToPrsMap)
365 theShapeToPrsMap.Clear();
367 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(myWorkshop)->displayer();
368 Handle(Prs3d_Drawer) aDrawer = Attributes();
370 // create presentations on the base of the shapes
371 QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = myFeatureShapes.begin(),
372 aLast = myFeatureShapes.end();
373 for (; anIt != aLast; anIt++) {
374 ObjectPtr anObject = anIt.key();
375 QList<GeomShapePtr> aShapes = anIt.value();
376 QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
377 for (; aShIt != aShLast; aShIt++) {
378 GeomShapePtr aGeomShape = *aShIt;
379 // the shape should not be checked here on empty value because it should be checked in
380 // appendShapeIfVisible() on the step of filling myFeatureShapes list
381 // the reason is to avoid empty AIS object visualized in the viewer
382 //if (!aGeomShape.get()) continue;
383 TopoDS_Shape aShape = aGeomShape.get() ? aGeomShape->impl<TopoDS_Shape>() : TopoDS_Shape();
384 // change deviation coefficient to provide more precise circle
385 Handle(AIS_InteractiveObject) anIO;
387 AISObjectPtr anAISPtr = aDisplayer->getAISObject(anObject);
389 anIO = anAISPtr->impl<Handle(AIS_InteractiveObject)>();
391 theShapeToPrsMap.Bind(aShape, anIO);