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(
93 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
94 const Handle(Prs3d_Presentation)& thePresentation,
95 const Standard_Integer theMode)
97 #ifdef DEBUG_OPERATION_PRS
98 qDebug("PartSet_OperationPrs::Compute -- begin");
101 SetColor(myShapeColor);
102 thePresentation->Clear();
103 bool aReadyToDisplay = !myShapeToPrsMap.IsEmpty();
105 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(myWorkshop)->displayer();
106 Handle(Prs3d_Drawer) aDrawer = Attributes();
107 // create presentations on the base of the shapes
108 BRep_Builder aBuilder;
109 TopoDS_Compound aComp;
110 aBuilder.MakeCompound(aComp);
111 for(NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>::Iterator
112 anIter(myShapeToPrsMap); anIter.More(); anIter.Next()) {
113 const TopoDS_Shape& aShape = anIter.Key();
114 aBuilder.Add(aComp, aShape);
115 // change deviation coefficient to provide more precise circle
116 // as there is no result, the shape is processed to correct deviation. To be unified
117 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
120 Handle(AIS_InteractiveObject) anIO = anIter.Value();
121 if (!anIO.IsNull()) {
122 int aWidth = anIO->Width();
123 /// workaround for zero width. Else, there will be a crash
124 if (aWidth == 0) { // width returns of TSolid shape is zero
125 bool isDisplayed = !anIO->GetContext().IsNull();
126 aWidth = PartSet_Tools::getAISDefaultWidth();// default width value
128 setWidth(aDrawer, aWidth);
131 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
134 if (!aReadyToDisplay) {
135 Events_InfoMessage("PartSet_OperationPrs",
136 "An empty AIS presentation: PartSet_OperationPrs").send();
137 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
138 new Events_Message(Events_Loop::eventByName(EVENT_EMPTY_OPERATION_PRESENTATION)));
139 Events_Loop::loop()->send(aMsg);
141 #ifdef DEBUG_OPERATION_PRS
142 qDebug("PartSet_OperationPrs::Compute -- end");
146 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
147 const Standard_Integer aMode)
149 // the presentation is not used in the selection
152 NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>& PartSet_OperationPrs::shapesMap()
154 return myShapeToPrsMap;
157 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
160 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
161 if (aComposite.get())
162 isSub = aComposite->isSub(theObject);
167 void PartSet_OperationPrs::addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
168 const FeaturePtr& theFeature, ModuleBase_IWorkshop* theWorkshop,
169 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
171 if (theObject.get()) {
172 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
174 ResultCompSolidPtr aCompsolidResult =
175 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
176 if (aCompsolidResult.get()) {
177 if (aCompsolidResult->numberOfSubs() > 0) {
178 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
179 ResultPtr aSubResult = aCompsolidResult->subResult(i);
180 if (aSubResult.get()) {
182 addValue(aSubResult, aShape, theFeature, theWorkshop, theObjectShapes);
188 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
190 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
191 if (aFeature.get()) {
192 AttributeBooleanPtr aCopyAttr =
193 aFeature->data()->boolean(SketchPlugin_SketchEntity::COPY_ID());
194 if (aCopyAttr.get()) {
195 bool isCopy = aCopyAttr->value();
204 GeomShapePtr aShape = theShape;
206 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
208 aShape = aResult->shape();
210 if (!isSubObject(theObject, theFeature))
211 appendShapeIfVisible(theWorkshop, theObject, aShape, theObjectShapes);
215 void PartSet_OperationPrs::appendShapeIfVisible(ModuleBase_IWorkshop* theWorkshop,
216 const ObjectPtr& theObject,
217 GeomShapePtr theGeomShape,
218 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
220 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
221 if (XGUI_Displayer::isVisible(aDisplayer, theObject)) {
222 if (theGeomShape.get()) {
223 if (theObjectShapes.contains(theObject))
224 theObjectShapes[theObject].append(theGeomShape);
226 QList<GeomShapePtr> aShapes;
227 aShapes.append(theGeomShape);
228 theObjectShapes[theObject] = aShapes;
231 #ifdef DEBUG_EMPTY_SHAPE
232 qDebug(QString("Empty shape in result, result: %1")
233 .arg(ModuleBase_Tools::objectInfo(theObject)).toStdString().c_str());
239 void PartSet_OperationPrs::getFeatureShapes(const FeaturePtr& theFeature,
240 ModuleBase_IWorkshop* theWorkshop,
241 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
243 theObjectShapes.clear();
244 if (!theFeature.get())
247 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
249 QList<GeomShapePtr> aShapes;
250 std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
251 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
252 for (; anIt != aLast; anIt++) {
253 AttributePtr anAttribute = *anIt;
254 if (!isSelectionAttribute(anAttribute))
257 if (!aValidators->isCase(theFeature, anAttribute->id()))
258 continue; // this attribute is not participated in the current case
260 std::string anAttrType = anAttribute->attributeType();
262 if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
263 std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
264 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
265 for(int i = 0; i < aCurSelList->size(); i++) {
266 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
267 ResultPtr aResult = aSelAttribute->context();
268 GeomShapePtr aShape = aSelAttribute->value();
269 addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
272 if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
273 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
274 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
275 for (int i = 0; i < aCurSelList->size(); i++) {
276 ObjectPtr anObject = aCurSelList->object(i);
277 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
278 // if a feature is stored in the attribute, we should obtain the feature results
279 // e.g. feature rectangle uses parameters feature lines in the attribute
280 if (aFeature.get()) {
281 getResultShapes(aFeature, theWorkshop, theObjectShapes, false);
284 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
286 GeomShapePtr aShape = aResult->shape();
288 addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
296 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
297 AttributeRefAttrPtr anAttr =
298 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
299 if (anAttr->isObject()) {
300 anObject = anAttr->object();
303 AttributePtr anAttribute = anAttr->attr();
304 aShape = PartSet_Tools::findShapeBy2DPoint(anAttribute, theWorkshop);
305 // the distance point is not found if the point is selected in the 2nd time
306 // TODO: after debug, this check can be removed
309 anObject = anAttr->attr()->owner();
312 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
313 AttributeSelectionPtr anAttr =
314 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
315 anObject = anAttr->context();
316 aShape = anAttr->value();
318 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
319 AttributeReferencePtr anAttr =
320 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
321 anObject = anAttr->value();
323 addValue(anObject, aShape, theFeature, theWorkshop, theObjectShapes);
328 void PartSet_OperationPrs::getResultShapes(const FeaturePtr& theFeature,
329 ModuleBase_IWorkshop* theWorkshop,
330 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes,
331 const bool theListShouldBeCleared)
333 if (theListShouldBeCleared)
334 theObjectShapes.clear();
336 if (!theFeature.get())
339 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
341 std::list<ResultPtr> aResults;
342 ModelAPI_Tools::allResults(theFeature, aResults);
343 std::list<ResultPtr>::const_iterator aRIt = aResults.begin(),
344 aRLast = aResults.end();
345 for (; aRIt != aRLast; aRIt++) {
346 ResultPtr aResult = *aRIt;
347 GeomShapePtr aGeomShape = aResult->shape();
348 appendShapeIfVisible(theWorkshop, aResult, aGeomShape, theObjectShapes);
352 void PartSet_OperationPrs::getHighlightedShapes(ModuleBase_IWorkshop* theWorkshop,
354 QList<GeomShapePtr> >& theObjectShapes)
356 theObjectShapes.clear();
358 QList<ModuleBase_ViewerPrsPtr> aValues;
359 ModuleBase_ModelWidget* anActiveWidget = theWorkshop->module()->activeWidget();
361 anActiveWidget->getHighlighted(aValues);
363 QList<GeomShapePtr> aShapes;
364 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIIt = aValues.begin(),
365 aILast = aValues.end();
366 for (; anIIt != aILast; anIIt++) {
367 ModuleBase_ViewerPrsPtr aPrs = *anIIt;
368 ObjectPtr anObject = aPrs->object();
370 GeomShapePtr aGeomShape = aPrs->shape();
371 if (!aGeomShape.get() || aGeomShape->isNull()) {
372 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
374 aGeomShape = aResult->shape();
377 appendShapeIfVisible(theWorkshop, anObject, aGeomShape, theObjectShapes);
382 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
384 std::string anAttrType = theAttribute->attributeType();
386 return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
387 anAttrType == ModelAPI_AttributeRefList::typeId() ||
388 anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
389 anAttrType == ModelAPI_AttributeSelection::typeId() ||
390 anAttrType == ModelAPI_AttributeReference::typeId();
393 void PartSet_OperationPrs::fillShapeList(
394 const QMap<ObjectPtr, QList<GeomShapePtr> >& theFeatureShapes,
395 ModuleBase_IWorkshop* theWorkshop,
396 NCollection_DataMap<TopoDS_Shape,
397 Handle(AIS_InteractiveObject)>& theShapeToPrsMap)
399 theShapeToPrsMap.Clear();
401 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
403 // create presentations on the base of the shapes
404 QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = theFeatureShapes.begin(),
405 aLast = theFeatureShapes.end();
406 for (; anIt != aLast; anIt++) {
407 ObjectPtr anObject = anIt.key();
408 QList<GeomShapePtr> aShapes = anIt.value();
409 QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
410 for (; aShIt != aShLast; aShIt++) {
411 GeomShapePtr aGeomShape = *aShIt;
412 // the shape should not be checked here on empty value because it should be checked in
413 // appendShapeIfVisible() on the step of filling theFeatureShapes list
414 // the reason is to avoid empty AIS object visualized in the viewer
415 //if (!aGeomShape.get()) continue;
416 TopoDS_Shape aShape = aGeomShape.get() ? aGeomShape->impl<TopoDS_Shape>() : TopoDS_Shape();
420 // change deviation coefficient to provide more precise circle
421 Handle(AIS_InteractiveObject) anIO;
422 AISObjectPtr anAISPtr = aDisplayer->getAISObject(anObject);
424 anIO = anAISPtr->impl<Handle(AIS_InteractiveObject)>();
425 theShapeToPrsMap.Bind(aShape, anIO);