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>
43 #include <TopoDS_Vertex.hxx>
44 #include <BRepBuilderAPI_MakeVertex.hxx>
46 //#define DEBUG_EMPTY_SHAPE
48 // multi-rotation/translation operation
49 //#define DEBUG_HIDE_COPY_ATTRIBUTE
50 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
51 #include <ModelAPI_AttributeBoolean.h>
52 #include <SketchPlugin_SketchEntity.h>
55 IMPLEMENT_STANDARD_HANDLE(PartSet_OperationPrs, ViewerData_AISShape);
56 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
58 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
59 : ViewerData_AISShape(TopoDS_Shape()), myWorkshop(theWorkshop), myUseAISWidth(false)
61 myShapeColor = Quantity_Color(1, 1, 1, Quantity_TOC_RGB);
63 // first presentation for having correct Compute until presentation with shapes are set
64 gp_Pnt aPnt(0.0, 0.0, 0.0);
65 BRepBuilderAPI_MakeVertex aMaker(aPnt);
66 TopoDS_Vertex aVertex = aMaker.Vertex();
67 myShapeToPrsMap.Bind(aVertex, NULL);
70 bool PartSet_OperationPrs::hasShapes()
72 return !myFeatureShapes.empty();
75 void PartSet_OperationPrs::setShapeColor(const Quantity_Color& theColor)
77 myShapeColor = theColor;
80 void PartSet_OperationPrs::useAISWidth()
85 void PartSet_OperationPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
86 const Handle(Prs3d_Presentation)& thePresentation,
87 const Standard_Integer theMode)
89 SetColor(myShapeColor);
90 thePresentation->Clear();
92 NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)> aShapeToPrsMap;
93 fillShapeList(myFeatureShapes, aShapeToPrsMap);
95 bool aReadyToDisplay = !aShapeToPrsMap.IsEmpty();
96 if (aReadyToDisplay) {
97 myShapeToPrsMap.Clear();
98 myShapeToPrsMap.Assign(aShapeToPrsMap);
101 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(myWorkshop)->displayer();
102 Handle(Prs3d_Drawer) aDrawer = Attributes();
103 // create presentations on the base of the shapes
104 for(NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>::Iterator anIter(myShapeToPrsMap);
105 anIter.More(); anIter.Next()) {
106 const TopoDS_Shape& aShape = anIter.Key();
107 // change deviation coefficient to provide more precise circle
108 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
111 Handle(AIS_InteractiveObject) anIO = anIter.Value();
112 if (!anIO.IsNull()) {
113 int aWidth = anIO->Width();
114 /// workaround for zero width. Else, there will be a crash
115 if (aWidth == 0) { // width returns of TSolid shape is zero
116 bool isDisplayed = !anIO->GetContext().IsNull();
117 aWidth = PartSet_Tools::getAISDefaultWidth();// default width value
119 setWidth(aDrawer, aWidth);
122 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
125 if (!aReadyToDisplay) {
126 Events_Error::throwException("An empty AIS presentation: PartSet_OperationPrs");
127 //std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
128 // new Events_Message(Events_Loop::eventByName(EVENT_EMPTY_OPERATION_PRESENTATION)));
129 //Events_Loop::loop()->send(aMsg);
133 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
134 const Standard_Integer aMode)
136 // the presentation is not used in the selection
139 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
142 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
143 if (aComposite.get())
144 isSub = aComposite->isSub(theObject);
149 void PartSet_OperationPrs::addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
150 const FeaturePtr& theFeature, ModuleBase_IWorkshop* theWorkshop,
151 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
153 if (theObject.get()) {
154 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
156 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
157 if (aCompsolidResult.get()) {
158 if (aCompsolidResult->numberOfSubs() > 0) {
159 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
160 ResultPtr aSubResult = aCompsolidResult->subResult(i);
161 if (aSubResult.get()) {
163 addValue(aSubResult, aShape, theFeature, theWorkshop, theObjectShapes);
169 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
171 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
172 if (aFeature.get()) {
173 AttributeBooleanPtr aCopyAttr = aFeature->data()->boolean(SketchPlugin_SketchEntity::COPY_ID());
174 if (aCopyAttr.get()) {
175 bool isCopy = aCopyAttr->value();
184 GeomShapePtr aShape = theShape;
186 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
188 aShape = aResult->shape();
190 if (!isSubObject(theObject, theFeature))
191 appendShapeIfVisible(theWorkshop, theObject, aShape, theObjectShapes);
195 void PartSet_OperationPrs::appendShapeIfVisible(ModuleBase_IWorkshop* theWorkshop,
196 const ObjectPtr& theObject,
197 GeomShapePtr theGeomShape,
198 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
200 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
201 if (XGUI_Displayer::isVisible(aDisplayer, theObject)) {
202 if (theGeomShape.get()) {
203 if (theObjectShapes.contains(theObject))
204 theObjectShapes[theObject].append(theGeomShape);
206 QList<GeomShapePtr> aShapes;
207 aShapes.append(theGeomShape);
208 theObjectShapes[theObject] = aShapes;
211 #ifdef DEBUG_EMPTY_SHAPE
212 qDebug(QString("Empty shape in result, result: %1")
213 .arg(ModuleBase_Tools::objectInfo(theObject)).toStdString().c_str());
219 void PartSet_OperationPrs::getFeatureShapes(const FeaturePtr& theFeature,
220 ModuleBase_IWorkshop* theWorkshop,
221 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
223 theObjectShapes.clear();
224 if (!theFeature.get())
227 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
229 QList<GeomShapePtr> aShapes;
230 std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
231 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
232 for (; anIt != aLast; anIt++) {
233 AttributePtr anAttribute = *anIt;
234 if (!isSelectionAttribute(anAttribute))
237 if (!aValidators->isCase(theFeature, anAttribute->id()))
238 continue; // this attribute is not participated in the current case
240 std::string anAttrType = anAttribute->attributeType();
242 if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
243 std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
244 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
245 for(int i = 0; i < aCurSelList->size(); i++) {
246 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
247 ResultPtr aResult = aSelAttribute->context();
248 GeomShapePtr aShape = aSelAttribute->value();
249 addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
252 if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
253 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
254 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
255 for (int i = 0; i < aCurSelList->size(); i++) {
256 ObjectPtr anObject = aCurSelList->object(i);
257 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
258 // if a feature is stored in the attribute, we should obtain the feature results
259 // e.g. feature rectangle uses parameters feature lines in the attribute
260 if (aFeature.get()) {
261 getResultShapes(aFeature, theWorkshop, theObjectShapes, false);
264 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
266 GeomShapePtr aShape = aResult->shape();
268 addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
276 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
277 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
278 if (anAttr->isObject()) {
279 anObject = anAttr->object();
282 AttributePtr anAttribute = anAttr->attr();
283 aShape = PartSet_Tools::findShapeBy2DPoint(anAttribute, theWorkshop);
284 // the distance point is not found if the point is selected in the 2nd time
285 // TODO: after debug, this check can be removed
288 anObject = anAttr->attr()->owner();
291 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
292 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
293 anObject = anAttr->context();
294 aShape = anAttr->value();
296 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
297 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
298 anObject = anAttr->value();
300 addValue(anObject, aShape, theFeature, theWorkshop, theObjectShapes);
305 void PartSet_OperationPrs::getResultShapes(const FeaturePtr& theFeature,
306 ModuleBase_IWorkshop* theWorkshop,
307 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes,
308 const bool theListShouldBeCleared)
310 if (theListShouldBeCleared)
311 theObjectShapes.clear();
313 if (!theFeature.get())
316 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
318 std::list<ResultPtr> aFeatureResults = theFeature->results();
319 std::list<ResultPtr>::const_iterator aRIt = aFeatureResults.begin(),
320 aRLast = aFeatureResults.end();
321 for (; aRIt != aRLast; aRIt++) {
322 ResultPtr aResult = *aRIt;
323 GeomShapePtr aGeomShape = aResult->shape();
324 appendShapeIfVisible(theWorkshop, aResult, aGeomShape, theObjectShapes);
328 void PartSet_OperationPrs::getHighlightedShapes(ModuleBase_IWorkshop* theWorkshop,
329 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
331 theObjectShapes.clear();
333 QList<ModuleBase_ViewerPrsPtr> aValues;
334 ModuleBase_IPropertyPanel* aPanel = theWorkshop->propertyPanel();
336 ModuleBase_ModelWidget* aWidget = aPanel->activeWidget();
338 aWidget->getHighlighted(aValues);
342 QList<GeomShapePtr> aShapes;
343 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIIt = aValues.begin(),
344 aILast = aValues.end();
345 for (; anIIt != aILast; anIIt++) {
346 ModuleBase_ViewerPrsPtr aPrs = *anIIt;
347 ObjectPtr anObject = aPrs->object();
349 GeomShapePtr aGeomShape = aPrs->shape();
350 if (!aGeomShape.get() || aGeomShape->isNull()) {
351 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
353 aGeomShape = aResult->shape();
356 appendShapeIfVisible(theWorkshop, anObject, aGeomShape, theObjectShapes);
361 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
363 std::string anAttrType = theAttribute->attributeType();
365 return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
366 anAttrType == ModelAPI_AttributeRefList::typeId() ||
367 anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
368 anAttrType == ModelAPI_AttributeSelection::typeId() ||
369 anAttrType == ModelAPI_AttributeReference::typeId();
372 void PartSet_OperationPrs::fillShapeList(const QMap<ObjectPtr, QList<GeomShapePtr> >& theFeatureShapes,
373 NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>& theShapeToPrsMap)
375 theShapeToPrsMap.Clear();
377 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(myWorkshop)->displayer();
378 Handle(Prs3d_Drawer) aDrawer = Attributes();
380 // create presentations on the base of the shapes
381 QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = myFeatureShapes.begin(),
382 aLast = myFeatureShapes.end();
383 for (; anIt != aLast; anIt++) {
384 ObjectPtr anObject = anIt.key();
385 QList<GeomShapePtr> aShapes = anIt.value();
386 QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
387 for (; aShIt != aShLast; aShIt++) {
388 GeomShapePtr aGeomShape = *aShIt;
389 // the shape should not be checked here on empty value because it should be checked in
390 // appendShapeIfVisible() on the step of filling myFeatureShapes list
391 // the reason is to avoid empty AIS object visualized in the viewer
392 //if (!aGeomShape.get()) continue;
393 TopoDS_Shape aShape = aGeomShape.get() ? aGeomShape->impl<TopoDS_Shape>() : TopoDS_Shape();
394 // change deviation coefficient to provide more precise circle
395 Handle(AIS_InteractiveObject) anIO;
397 AISObjectPtr anAISPtr = aDisplayer->getAISObject(anObject);
399 anIO = anAISPtr->impl<Handle(AIS_InteractiveObject)>();
401 theShapeToPrsMap.Bind(aShape, anIO);