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>
32 #include <ModelAPI_Tools.h>
34 #include <Events_InfoMessage.h>
35 #include <Events_Loop.h>
37 #include <GeomAPI_IPresentable.h>
39 #include <StdPrs_WFDeflectionShape.hxx>
44 #include <TopoDS_Vertex.hxx>
45 #include <BRepBuilderAPI_MakeVertex.hxx>
46 #include <BRep_Builder.hxx>
47 #include <TopoDS_Compound.hxx>
49 //#define DEBUG_EMPTY_SHAPE
50 //#define DEBUG_OPERATION_PRS
52 // multi-rotation/translation operation
53 //#define DEBUG_HIDE_COPY_ATTRIBUTE
54 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
55 #include <ModelAPI_AttributeBoolean.h>
56 #include <SketchPlugin_SketchEntity.h>
59 IMPLEMENT_STANDARD_HANDLE(PartSet_OperationPrs, ViewerData_AISShape);
60 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
62 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
63 : ViewerData_AISShape(TopoDS_Shape()), myWorkshop(theWorkshop), myUseAISWidth(false)
65 #ifdef DEBUG_OPERATION_PRS
66 qDebug("PartSet_OperationPrs::PartSet_OperationPrs");
68 myShapeColor = Quantity_Color(1, 1, 1, Quantity_TOC_RGB);
70 // first presentation for having correct Compute until presentation with shapes are set
71 gp_Pnt aPnt(0.0, 0.0, 0.0);
72 BRepBuilderAPI_MakeVertex aMaker(aPnt);
73 TopoDS_Vertex aVertex = aMaker.Vertex();
74 myShapeToPrsMap.Bind(aVertex, NULL);
77 bool PartSet_OperationPrs::hasShapes()
79 return !myShapeToPrsMap.IsEmpty();
82 void PartSet_OperationPrs::setShapeColor(const Quantity_Color& theColor)
84 myShapeColor = theColor;
87 void PartSet_OperationPrs::useAISWidth()
92 void PartSet_OperationPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
93 const Handle(Prs3d_Presentation)& thePresentation,
94 const Standard_Integer theMode)
96 #ifdef DEBUG_OPERATION_PRS
97 qDebug("PartSet_OperationPrs::Compute -- begin");
100 SetColor(myShapeColor);
101 thePresentation->Clear();
102 bool aReadyToDisplay = !myShapeToPrsMap.IsEmpty();
104 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(myWorkshop)->displayer();
105 Handle(Prs3d_Drawer) aDrawer = Attributes();
106 // create presentations on the base of the shapes
107 BRep_Builder aBuilder;
108 TopoDS_Compound aComp;
109 aBuilder.MakeCompound(aComp);
110 for(NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>::Iterator anIter(myShapeToPrsMap);
111 anIter.More(); anIter.Next()) {
112 const TopoDS_Shape& aShape = anIter.Key();
113 aBuilder.Add(aComp, aShape);
114 // change deviation coefficient to provide more precise circle
115 // as there is no result, the shape is processed to correct deviation. To be unified
116 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
119 Handle(AIS_InteractiveObject) anIO = anIter.Value();
120 if (!anIO.IsNull()) {
121 int aWidth = anIO->Width();
122 /// workaround for zero width. Else, there will be a crash
123 if (aWidth == 0) { // width returns of TSolid shape is zero
124 bool isDisplayed = !anIO->GetContext().IsNull();
125 aWidth = PartSet_Tools::getAISDefaultWidth();// default width value
127 setWidth(aDrawer, aWidth);
130 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
133 if (!aReadyToDisplay) {
134 Events_InfoMessage("PartSet_OperationPrs",
135 "An empty AIS presentation: PartSet_OperationPrs").send();
136 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
137 new Events_Message(Events_Loop::eventByName(EVENT_EMPTY_OPERATION_PRESENTATION)));
138 Events_Loop::loop()->send(aMsg);
140 #ifdef DEBUG_OPERATION_PRS
141 qDebug("PartSet_OperationPrs::Compute -- end");
145 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
146 const Standard_Integer aMode)
148 // the presentation is not used in the selection
151 NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>& PartSet_OperationPrs::shapesMap()
153 return myShapeToPrsMap;
156 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
159 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
160 if (aComposite.get())
161 isSub = aComposite->isSub(theObject);
166 void PartSet_OperationPrs::addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
167 const FeaturePtr& theFeature, ModuleBase_IWorkshop* theWorkshop,
168 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
170 if (theObject.get()) {
171 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
173 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
174 if (aCompsolidResult.get()) {
175 if (aCompsolidResult->numberOfSubs() > 0) {
176 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
177 ResultPtr aSubResult = aCompsolidResult->subResult(i);
178 if (aSubResult.get()) {
180 addValue(aSubResult, aShape, theFeature, theWorkshop, theObjectShapes);
186 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
188 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
189 if (aFeature.get()) {
190 AttributeBooleanPtr aCopyAttr = aFeature->data()->boolean(SketchPlugin_SketchEntity::COPY_ID());
191 if (aCopyAttr.get()) {
192 bool isCopy = aCopyAttr->value();
201 GeomShapePtr aShape = theShape;
203 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
205 aShape = aResult->shape();
207 if (!isSubObject(theObject, theFeature))
208 appendShapeIfVisible(theWorkshop, theObject, aShape, theObjectShapes);
212 void PartSet_OperationPrs::appendShapeIfVisible(ModuleBase_IWorkshop* theWorkshop,
213 const ObjectPtr& theObject,
214 GeomShapePtr theGeomShape,
215 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
217 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
218 if (XGUI_Displayer::isVisible(aDisplayer, theObject)) {
219 if (theGeomShape.get()) {
220 if (theObjectShapes.contains(theObject))
221 theObjectShapes[theObject].append(theGeomShape);
223 QList<GeomShapePtr> aShapes;
224 aShapes.append(theGeomShape);
225 theObjectShapes[theObject] = aShapes;
228 #ifdef DEBUG_EMPTY_SHAPE
229 qDebug(QString("Empty shape in result, result: %1")
230 .arg(ModuleBase_Tools::objectInfo(theObject)).toStdString().c_str());
236 void PartSet_OperationPrs::getFeatureShapes(const FeaturePtr& theFeature,
237 ModuleBase_IWorkshop* theWorkshop,
238 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
240 theObjectShapes.clear();
241 if (!theFeature.get())
244 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
246 QList<GeomShapePtr> aShapes;
247 std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
248 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
249 for (; anIt != aLast; anIt++) {
250 AttributePtr anAttribute = *anIt;
251 if (!isSelectionAttribute(anAttribute))
254 if (!aValidators->isCase(theFeature, anAttribute->id()))
255 continue; // this attribute is not participated in the current case
257 std::string anAttrType = anAttribute->attributeType();
259 if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
260 std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
261 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
262 for(int i = 0; i < aCurSelList->size(); i++) {
263 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
264 ResultPtr aResult = aSelAttribute->context();
265 GeomShapePtr aShape = aSelAttribute->value();
266 addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
269 if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
270 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
271 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
272 for (int i = 0; i < aCurSelList->size(); i++) {
273 ObjectPtr anObject = aCurSelList->object(i);
274 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
275 // if a feature is stored in the attribute, we should obtain the feature results
276 // e.g. feature rectangle uses parameters feature lines in the attribute
277 if (aFeature.get()) {
278 getResultShapes(aFeature, theWorkshop, theObjectShapes, false);
281 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
283 GeomShapePtr aShape = aResult->shape();
285 addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
293 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
294 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
295 if (anAttr->isObject()) {
296 anObject = anAttr->object();
299 AttributePtr anAttribute = anAttr->attr();
300 aShape = PartSet_Tools::findShapeBy2DPoint(anAttribute, theWorkshop);
301 // the distance point is not found if the point is selected in the 2nd time
302 // TODO: after debug, this check can be removed
305 anObject = anAttr->attr()->owner();
308 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
309 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
310 anObject = anAttr->context();
311 aShape = anAttr->value();
313 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
314 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
315 anObject = anAttr->value();
317 addValue(anObject, aShape, theFeature, theWorkshop, theObjectShapes);
322 void PartSet_OperationPrs::getResultShapes(const FeaturePtr& theFeature,
323 ModuleBase_IWorkshop* theWorkshop,
324 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes,
325 const bool theListShouldBeCleared)
327 if (theListShouldBeCleared)
328 theObjectShapes.clear();
330 if (!theFeature.get())
333 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
335 std::list<ResultPtr> aResults;
336 ModelAPI_Tools::allResults(theFeature, aResults);
337 std::list<ResultPtr>::const_iterator aRIt = aResults.begin(),
338 aRLast = aResults.end();
339 for (; aRIt != aRLast; aRIt++) {
340 ResultPtr aResult = *aRIt;
341 GeomShapePtr aGeomShape = aResult->shape();
342 appendShapeIfVisible(theWorkshop, aResult, aGeomShape, theObjectShapes);
346 void PartSet_OperationPrs::getHighlightedShapes(ModuleBase_IWorkshop* theWorkshop,
347 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
349 theObjectShapes.clear();
351 QList<ModuleBase_ViewerPrsPtr> aValues;
352 ModuleBase_ModelWidget* anActiveWidget = theWorkshop->module()->activeWidget();
354 anActiveWidget->getHighlighted(aValues);
356 QList<GeomShapePtr> aShapes;
357 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIIt = aValues.begin(),
358 aILast = aValues.end();
359 for (; anIIt != aILast; anIIt++) {
360 ModuleBase_ViewerPrsPtr aPrs = *anIIt;
361 ObjectPtr anObject = aPrs->object();
363 GeomShapePtr aGeomShape = aPrs->shape();
364 if (!aGeomShape.get() || aGeomShape->isNull()) {
365 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
367 aGeomShape = aResult->shape();
370 appendShapeIfVisible(theWorkshop, anObject, aGeomShape, theObjectShapes);
375 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
377 std::string anAttrType = theAttribute->attributeType();
379 return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
380 anAttrType == ModelAPI_AttributeRefList::typeId() ||
381 anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
382 anAttrType == ModelAPI_AttributeSelection::typeId() ||
383 anAttrType == ModelAPI_AttributeReference::typeId();
386 void PartSet_OperationPrs::fillShapeList(const QMap<ObjectPtr, QList<GeomShapePtr> >& theFeatureShapes,
387 ModuleBase_IWorkshop* theWorkshop,
388 NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>& theShapeToPrsMap)
390 theShapeToPrsMap.Clear();
392 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
394 // create presentations on the base of the shapes
395 QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = theFeatureShapes.begin(),
396 aLast = theFeatureShapes.end();
397 for (; anIt != aLast; anIt++) {
398 ObjectPtr anObject = anIt.key();
399 QList<GeomShapePtr> aShapes = anIt.value();
400 QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
401 for (; aShIt != aShLast; aShIt++) {
402 GeomShapePtr aGeomShape = *aShIt;
403 // the shape should not be checked here on empty value because it should be checked in
404 // appendShapeIfVisible() on the step of filling theFeatureShapes list
405 // the reason is to avoid empty AIS object visualized in the viewer
406 //if (!aGeomShape.get()) continue;
407 TopoDS_Shape aShape = aGeomShape.get() ? aGeomShape->impl<TopoDS_Shape>() : TopoDS_Shape();
411 // change deviation coefficient to provide more precise circle
412 Handle(AIS_InteractiveObject) anIO;
413 AISObjectPtr anAISPtr = aDisplayer->getAISObject(anObject);
415 anIO = anAISPtr->impl<Handle(AIS_InteractiveObject)>();
416 theShapeToPrsMap.Bind(aShape, anIO);