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_WFShape.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_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
61 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
62 : ViewerData_AISShape(TopoDS_Shape()), myWorkshop(theWorkshop), myUseAISWidth(false)
64 #ifdef DEBUG_OPERATION_PRS
65 qDebug("PartSet_OperationPrs::PartSet_OperationPrs");
67 myShapeColor = Quantity_Color(1, 1, 1, Quantity_TOC_RGB);
69 // first presentation for having correct Compute until presentation with shapes are set
70 gp_Pnt aPnt(0.0, 0.0, 0.0);
71 BRepBuilderAPI_MakeVertex aMaker(aPnt);
72 TopoDS_Vertex aVertex = aMaker.Vertex();
73 myShapeToPrsMap.Bind(aVertex, NULL);
76 bool PartSet_OperationPrs::hasShapes()
78 return !myShapeToPrsMap.IsEmpty();
81 void PartSet_OperationPrs::setShapeColor(const Quantity_Color& theColor)
83 myShapeColor = theColor;
86 void PartSet_OperationPrs::useAISWidth()
91 void PartSet_OperationPrs::Compute(
92 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
111 anIter(myShapeToPrsMap); 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_WFShape::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 =
174 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
175 if (aCompsolidResult.get()) {
176 if (aCompsolidResult->numberOfSubs() > 0) {
177 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
178 ResultPtr aSubResult = aCompsolidResult->subResult(i);
179 if (aSubResult.get()) {
181 addValue(aSubResult, aShape, theFeature, theWorkshop, theObjectShapes);
187 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
189 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
190 if (aFeature.get()) {
191 AttributeBooleanPtr aCopyAttr =
192 aFeature->data()->boolean(SketchPlugin_SketchEntity::COPY_ID());
193 if (aCopyAttr.get()) {
194 bool isCopy = aCopyAttr->value();
203 GeomShapePtr aShape = theShape;
205 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
207 aShape = aResult->shape();
209 if (!isSubObject(theObject, theFeature))
210 appendShapeIfVisible(theWorkshop, theObject, aShape, theObjectShapes);
214 void PartSet_OperationPrs::appendShapeIfVisible(ModuleBase_IWorkshop* theWorkshop,
215 const ObjectPtr& theObject,
216 GeomShapePtr theGeomShape,
217 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
219 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
220 if (XGUI_Displayer::isVisible(aDisplayer, theObject)) {
221 if (theGeomShape.get()) {
222 if (theObjectShapes.contains(theObject))
223 theObjectShapes[theObject].append(theGeomShape);
225 QList<GeomShapePtr> aShapes;
226 aShapes.append(theGeomShape);
227 theObjectShapes[theObject] = aShapes;
230 #ifdef DEBUG_EMPTY_SHAPE
231 qDebug(QString("Empty shape in result, result: %1")
232 .arg(ModuleBase_Tools::objectInfo(theObject)).toStdString().c_str());
238 void PartSet_OperationPrs::getFeatureShapes(const FeaturePtr& theFeature,
239 ModuleBase_IWorkshop* theWorkshop,
240 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
242 theObjectShapes.clear();
243 if (!theFeature.get())
246 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
248 QList<GeomShapePtr> aShapes;
249 std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
250 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
251 for (; anIt != aLast; anIt++) {
252 AttributePtr anAttribute = *anIt;
253 if (!isSelectionAttribute(anAttribute))
256 if (!aValidators->isCase(theFeature, anAttribute->id()))
257 continue; // this attribute is not participated in the current case
259 std::string anAttrType = anAttribute->attributeType();
261 if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
262 std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
263 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
264 for(int i = 0; i < aCurSelList->size(); i++) {
265 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
266 ResultPtr aResult = aSelAttribute->context();
267 GeomShapePtr aShape = aSelAttribute->value();
268 addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
271 if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
272 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
273 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
274 for (int i = 0; i < aCurSelList->size(); i++) {
275 ObjectPtr anObject = aCurSelList->object(i);
276 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
277 // if a feature is stored in the attribute, we should obtain the feature results
278 // e.g. feature rectangle uses parameters feature lines in the attribute
279 if (aFeature.get()) {
280 getResultShapes(aFeature, theWorkshop, theObjectShapes, false);
283 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
285 GeomShapePtr aShape = aResult->shape();
287 addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
295 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
296 AttributeRefAttrPtr anAttr =
297 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
298 if (anAttr->isObject()) {
299 anObject = anAttr->object();
302 AttributePtr anAttribute = anAttr->attr();
303 aShape = PartSet_Tools::findShapeBy2DPoint(anAttribute, theWorkshop);
304 // the distance point is not found if the point is selected in the 2nd time
305 // TODO: after debug, this check can be removed
308 anObject = anAttr->attr()->owner();
311 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
312 AttributeSelectionPtr anAttr =
313 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
314 anObject = anAttr->context();
315 aShape = anAttr->value();
317 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
318 AttributeReferencePtr anAttr =
319 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
320 anObject = anAttr->value();
322 addValue(anObject, aShape, theFeature, theWorkshop, theObjectShapes);
327 void PartSet_OperationPrs::getResultShapes(const FeaturePtr& theFeature,
328 ModuleBase_IWorkshop* theWorkshop,
329 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes,
330 const bool theListShouldBeCleared)
332 if (theListShouldBeCleared)
333 theObjectShapes.clear();
335 if (!theFeature.get())
338 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
340 std::list<ResultPtr> aResults;
341 ModelAPI_Tools::allResults(theFeature, aResults);
342 std::list<ResultPtr>::const_iterator aRIt = aResults.begin(),
343 aRLast = aResults.end();
344 for (; aRIt != aRLast; aRIt++) {
345 ResultPtr aResult = *aRIt;
346 GeomShapePtr aGeomShape = aResult->shape();
347 appendShapeIfVisible(theWorkshop, aResult, aGeomShape, theObjectShapes);
351 void PartSet_OperationPrs::getPresentationShapes(const FeaturePtr& theFeature,
352 ModuleBase_IWorkshop* theWorkshop,
353 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes,
354 const bool theListShouldBeCleared)
356 if (theListShouldBeCleared)
357 theObjectShapes.clear();
359 if (!theFeature.get() || !theFeature->data()->isValid()) // if feature is already removed
362 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
364 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theFeature);
368 AISObjectPtr anAIS = aPrs->getAISObject(aDisplayer->getAISObject(theFeature));
372 Handle(AIS_InteractiveObject) anAISPrs = anAIS->impl<Handle(AIS_InteractiveObject)>();
373 if (!anAISPrs.IsNull()) {
374 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(anAISPrs);
375 if (!aShapePrs.IsNull()) {
376 TopoDS_Shape aShape = aShapePrs->Shape();
377 if (!aShape.IsNull()) {
378 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape());
379 aGeomShape->setImpl(new TopoDS_Shape(aShape));
380 appendShapeIfVisible(theWorkshop, theFeature, aGeomShape, theObjectShapes);
386 void PartSet_OperationPrs::getHighlightedShapes(ModuleBase_IWorkshop* theWorkshop,
388 QList<GeomShapePtr> >& theObjectShapes)
390 theObjectShapes.clear();
392 QList<ModuleBase_ViewerPrsPtr> aValues;
393 ModuleBase_ModelWidget* anActiveWidget = theWorkshop->module()->activeWidget();
395 anActiveWidget->getHighlighted(aValues);
397 QList<GeomShapePtr> aShapes;
398 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIIt = aValues.begin(),
399 aILast = aValues.end();
400 for (; anIIt != aILast; anIIt++) {
401 ModuleBase_ViewerPrsPtr aPrs = *anIIt;
402 ObjectPtr anObject = aPrs->object();
404 GeomShapePtr aGeomShape = aPrs->shape();
405 if (!aGeomShape.get() || aGeomShape->isNull()) {
406 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
408 aGeomShape = aResult->shape();
411 appendShapeIfVisible(theWorkshop, anObject, aGeomShape, theObjectShapes);
416 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
418 std::string anAttrType = theAttribute->attributeType();
420 return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
421 anAttrType == ModelAPI_AttributeRefList::typeId() ||
422 anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
423 anAttrType == ModelAPI_AttributeSelection::typeId() ||
424 anAttrType == ModelAPI_AttributeReference::typeId();
427 void PartSet_OperationPrs::fillShapeList(
428 const QMap<ObjectPtr, QList<GeomShapePtr> >& theFeatureShapes,
429 ModuleBase_IWorkshop* theWorkshop,
430 NCollection_DataMap<TopoDS_Shape,
431 Handle(AIS_InteractiveObject)>& theShapeToPrsMap)
433 theShapeToPrsMap.Clear();
435 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
437 // create presentations on the base of the shapes
438 QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = theFeatureShapes.begin(),
439 aLast = theFeatureShapes.end();
440 for (; anIt != aLast; anIt++) {
441 ObjectPtr anObject = anIt.key();
442 QList<GeomShapePtr> aShapes = anIt.value();
443 QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
444 for (; aShIt != aShLast; aShIt++) {
445 GeomShapePtr aGeomShape = *aShIt;
446 // the shape should not be checked here on empty value because it should be checked in
447 // appendShapeIfVisible() on the step of filling theFeatureShapes list
448 // the reason is to avoid empty AIS object visualized in the viewer
449 //if (!aGeomShape.get()) continue;
450 TopoDS_Shape aShape = aGeomShape.get() ? aGeomShape->impl<TopoDS_Shape>() : TopoDS_Shape();
454 // change deviation coefficient to provide more precise circle
455 Handle(AIS_InteractiveObject) anIO;
456 AISObjectPtr anAISPtr = aDisplayer->getAISObject(anObject);
458 anIO = anAISPtr->impl<Handle(AIS_InteractiveObject)>();
459 theShapeToPrsMap.Bind(aShape, anIO);