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_InfoMessage.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 !myShapeToPrsMap.IsEmpty();
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();
91 bool aReadyToDisplay = !myShapeToPrsMap.IsEmpty();
93 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(myWorkshop)->displayer();
94 Handle(Prs3d_Drawer) aDrawer = Attributes();
95 // create presentations on the base of the shapes
96 for(NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>::Iterator anIter(myShapeToPrsMap);
97 anIter.More(); anIter.Next()) {
98 const TopoDS_Shape& aShape = anIter.Key();
99 // change deviation coefficient to provide more precise circle
100 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
103 Handle(AIS_InteractiveObject) anIO = anIter.Value();
104 if (!anIO.IsNull()) {
105 int aWidth = anIO->Width();
106 /// workaround for zero width. Else, there will be a crash
107 if (aWidth == 0) { // width returns of TSolid shape is zero
108 bool isDisplayed = !anIO->GetContext().IsNull();
109 aWidth = PartSet_Tools::getAISDefaultWidth();// default width value
111 setWidth(aDrawer, aWidth);
114 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
117 if (!aReadyToDisplay) {
118 Events_InfoMessage("PartSet_OperationPrs",
119 "An empty AIS presentation: PartSet_OperationPrs").send();
120 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
121 new Events_Message(Events_Loop::eventByName(EVENT_EMPTY_OPERATION_PRESENTATION)));
122 Events_Loop::loop()->send(aMsg);
126 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
127 const Standard_Integer aMode)
129 // the presentation is not used in the selection
132 NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>& PartSet_OperationPrs::shapesMap()
134 return myShapeToPrsMap;
137 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
140 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
141 if (aComposite.get())
142 isSub = aComposite->isSub(theObject);
147 void PartSet_OperationPrs::addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
148 const FeaturePtr& theFeature, ModuleBase_IWorkshop* theWorkshop,
149 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
151 if (theObject.get()) {
152 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
154 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
155 if (aCompsolidResult.get()) {
156 if (aCompsolidResult->numberOfSubs() > 0) {
157 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
158 ResultPtr aSubResult = aCompsolidResult->subResult(i);
159 if (aSubResult.get()) {
161 addValue(aSubResult, aShape, theFeature, theWorkshop, theObjectShapes);
167 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
169 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
170 if (aFeature.get()) {
171 AttributeBooleanPtr aCopyAttr = aFeature->data()->boolean(SketchPlugin_SketchEntity::COPY_ID());
172 if (aCopyAttr.get()) {
173 bool isCopy = aCopyAttr->value();
182 GeomShapePtr aShape = theShape;
184 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
186 aShape = aResult->shape();
188 if (!isSubObject(theObject, theFeature))
189 appendShapeIfVisible(theWorkshop, theObject, aShape, theObjectShapes);
193 void PartSet_OperationPrs::appendShapeIfVisible(ModuleBase_IWorkshop* theWorkshop,
194 const ObjectPtr& theObject,
195 GeomShapePtr theGeomShape,
196 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
198 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
199 if (XGUI_Displayer::isVisible(aDisplayer, theObject)) {
200 if (theGeomShape.get()) {
201 if (theObjectShapes.contains(theObject))
202 theObjectShapes[theObject].append(theGeomShape);
204 QList<GeomShapePtr> aShapes;
205 aShapes.append(theGeomShape);
206 theObjectShapes[theObject] = aShapes;
209 #ifdef DEBUG_EMPTY_SHAPE
210 qDebug(QString("Empty shape in result, result: %1")
211 .arg(ModuleBase_Tools::objectInfo(theObject)).toStdString().c_str());
217 void PartSet_OperationPrs::getFeatureShapes(const FeaturePtr& theFeature,
218 ModuleBase_IWorkshop* theWorkshop,
219 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
221 theObjectShapes.clear();
222 if (!theFeature.get())
225 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
227 QList<GeomShapePtr> aShapes;
228 std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
229 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
230 for (; anIt != aLast; anIt++) {
231 AttributePtr anAttribute = *anIt;
232 if (!isSelectionAttribute(anAttribute))
235 if (!aValidators->isCase(theFeature, anAttribute->id()))
236 continue; // this attribute is not participated in the current case
238 std::string anAttrType = anAttribute->attributeType();
240 if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
241 std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
242 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
243 for(int i = 0; i < aCurSelList->size(); i++) {
244 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
245 ResultPtr aResult = aSelAttribute->context();
246 GeomShapePtr aShape = aSelAttribute->value();
247 addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
250 if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
251 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
252 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
253 for (int i = 0; i < aCurSelList->size(); i++) {
254 ObjectPtr anObject = aCurSelList->object(i);
255 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
256 // if a feature is stored in the attribute, we should obtain the feature results
257 // e.g. feature rectangle uses parameters feature lines in the attribute
258 if (aFeature.get()) {
259 getResultShapes(aFeature, theWorkshop, theObjectShapes, false);
262 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
264 GeomShapePtr aShape = aResult->shape();
266 addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
274 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
275 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
276 if (anAttr->isObject()) {
277 anObject = anAttr->object();
280 AttributePtr anAttribute = anAttr->attr();
281 aShape = PartSet_Tools::findShapeBy2DPoint(anAttribute, theWorkshop);
282 // the distance point is not found if the point is selected in the 2nd time
283 // TODO: after debug, this check can be removed
286 anObject = anAttr->attr()->owner();
289 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
290 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
291 anObject = anAttr->context();
292 aShape = anAttr->value();
294 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
295 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
296 anObject = anAttr->value();
298 addValue(anObject, aShape, theFeature, theWorkshop, theObjectShapes);
303 void PartSet_OperationPrs::getResultShapes(const FeaturePtr& theFeature,
304 ModuleBase_IWorkshop* theWorkshop,
305 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes,
306 const bool theListShouldBeCleared)
308 if (theListShouldBeCleared)
309 theObjectShapes.clear();
311 if (!theFeature.get())
314 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
316 std::list<ResultPtr> aFeatureResults = theFeature->results();
317 std::list<ResultPtr>::const_iterator aRIt = aFeatureResults.begin(),
318 aRLast = aFeatureResults.end();
319 for (; aRIt != aRLast; aRIt++) {
320 ResultPtr aResult = *aRIt;
321 GeomShapePtr aGeomShape = aResult->shape();
322 appendShapeIfVisible(theWorkshop, aResult, aGeomShape, theObjectShapes);
326 void PartSet_OperationPrs::getHighlightedShapes(ModuleBase_IWorkshop* theWorkshop,
327 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
329 theObjectShapes.clear();
331 QList<ModuleBase_ViewerPrsPtr> aValues;
332 ModuleBase_IPropertyPanel* aPanel = theWorkshop->propertyPanel();
334 ModuleBase_ModelWidget* aWidget = aPanel->activeWidget();
336 aWidget->getHighlighted(aValues);
340 QList<GeomShapePtr> aShapes;
341 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIIt = aValues.begin(),
342 aILast = aValues.end();
343 for (; anIIt != aILast; anIIt++) {
344 ModuleBase_ViewerPrsPtr aPrs = *anIIt;
345 ObjectPtr anObject = aPrs->object();
347 GeomShapePtr aGeomShape = aPrs->shape();
348 if (!aGeomShape.get() || aGeomShape->isNull()) {
349 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
351 aGeomShape = aResult->shape();
354 appendShapeIfVisible(theWorkshop, anObject, aGeomShape, theObjectShapes);
359 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
361 std::string anAttrType = theAttribute->attributeType();
363 return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
364 anAttrType == ModelAPI_AttributeRefList::typeId() ||
365 anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
366 anAttrType == ModelAPI_AttributeSelection::typeId() ||
367 anAttrType == ModelAPI_AttributeReference::typeId();
370 void PartSet_OperationPrs::fillShapeList(const QMap<ObjectPtr, QList<GeomShapePtr> >& theFeatureShapes,
371 ModuleBase_IWorkshop* theWorkshop,
372 NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>& theShapeToPrsMap)
374 theShapeToPrsMap.Clear();
376 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
378 // create presentations on the base of the shapes
379 QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = theFeatureShapes.begin(),
380 aLast = theFeatureShapes.end();
381 for (; anIt != aLast; anIt++) {
382 ObjectPtr anObject = anIt.key();
383 QList<GeomShapePtr> aShapes = anIt.value();
384 QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
385 for (; aShIt != aShLast; aShIt++) {
386 GeomShapePtr aGeomShape = *aShIt;
387 // the shape should not be checked here on empty value because it should be checked in
388 // appendShapeIfVisible() on the step of filling theFeatureShapes list
389 // the reason is to avoid empty AIS object visualized in the viewer
390 //if (!aGeomShape.get()) continue;
391 TopoDS_Shape aShape = aGeomShape.get() ? aGeomShape->impl<TopoDS_Shape>() : TopoDS_Shape();
395 // change deviation coefficient to provide more precise circle
396 Handle(AIS_InteractiveObject) anIO;
397 AISObjectPtr anAISPtr = aDisplayer->getAISObject(anObject);
399 anIO = anAISPtr->impl<Handle(AIS_InteractiveObject)>();
400 theShapeToPrsMap.Bind(aShape, anIO);