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 !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_Error::throwException("An empty AIS presentation: PartSet_OperationPrs");
119 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
120 new Events_Message(Events_Loop::eventByName(EVENT_EMPTY_OPERATION_PRESENTATION)));
121 Events_Loop::loop()->send(aMsg);
125 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
126 const Standard_Integer aMode)
128 // the presentation is not used in the selection
131 NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>& PartSet_OperationPrs::shapesMap()
133 return myShapeToPrsMap;
136 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
139 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
140 if (aComposite.get())
141 isSub = aComposite->isSub(theObject);
146 void PartSet_OperationPrs::addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
147 const FeaturePtr& theFeature, ModuleBase_IWorkshop* theWorkshop,
148 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
150 if (theObject.get()) {
151 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
153 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
154 if (aCompsolidResult.get()) {
155 if (aCompsolidResult->numberOfSubs() > 0) {
156 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
157 ResultPtr aSubResult = aCompsolidResult->subResult(i);
158 if (aSubResult.get()) {
160 addValue(aSubResult, aShape, theFeature, theWorkshop, theObjectShapes);
166 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
168 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
169 if (aFeature.get()) {
170 AttributeBooleanPtr aCopyAttr = aFeature->data()->boolean(SketchPlugin_SketchEntity::COPY_ID());
171 if (aCopyAttr.get()) {
172 bool isCopy = aCopyAttr->value();
181 GeomShapePtr aShape = theShape;
183 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
185 aShape = aResult->shape();
187 if (!isSubObject(theObject, theFeature))
188 appendShapeIfVisible(theWorkshop, theObject, aShape, theObjectShapes);
192 void PartSet_OperationPrs::appendShapeIfVisible(ModuleBase_IWorkshop* theWorkshop,
193 const ObjectPtr& theObject,
194 GeomShapePtr theGeomShape,
195 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
197 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
198 if (XGUI_Displayer::isVisible(aDisplayer, theObject)) {
199 if (theGeomShape.get()) {
200 if (theObjectShapes.contains(theObject))
201 theObjectShapes[theObject].append(theGeomShape);
203 QList<GeomShapePtr> aShapes;
204 aShapes.append(theGeomShape);
205 theObjectShapes[theObject] = aShapes;
208 #ifdef DEBUG_EMPTY_SHAPE
209 qDebug(QString("Empty shape in result, result: %1")
210 .arg(ModuleBase_Tools::objectInfo(theObject)).toStdString().c_str());
216 void PartSet_OperationPrs::getFeatureShapes(const FeaturePtr& theFeature,
217 ModuleBase_IWorkshop* theWorkshop,
218 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
220 theObjectShapes.clear();
221 if (!theFeature.get())
224 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
226 QList<GeomShapePtr> aShapes;
227 std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
228 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
229 for (; anIt != aLast; anIt++) {
230 AttributePtr anAttribute = *anIt;
231 if (!isSelectionAttribute(anAttribute))
234 if (!aValidators->isCase(theFeature, anAttribute->id()))
235 continue; // this attribute is not participated in the current case
237 std::string anAttrType = anAttribute->attributeType();
239 if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
240 std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
241 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
242 for(int i = 0; i < aCurSelList->size(); i++) {
243 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
244 ResultPtr aResult = aSelAttribute->context();
245 GeomShapePtr aShape = aSelAttribute->value();
246 addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
249 if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
250 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
251 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
252 for (int i = 0; i < aCurSelList->size(); i++) {
253 ObjectPtr anObject = aCurSelList->object(i);
254 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
255 // if a feature is stored in the attribute, we should obtain the feature results
256 // e.g. feature rectangle uses parameters feature lines in the attribute
257 if (aFeature.get()) {
258 getResultShapes(aFeature, theWorkshop, theObjectShapes, false);
261 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
263 GeomShapePtr aShape = aResult->shape();
265 addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
273 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
274 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
275 if (anAttr->isObject()) {
276 anObject = anAttr->object();
279 AttributePtr anAttribute = anAttr->attr();
280 aShape = PartSet_Tools::findShapeBy2DPoint(anAttribute, theWorkshop);
281 // the distance point is not found if the point is selected in the 2nd time
282 // TODO: after debug, this check can be removed
285 anObject = anAttr->attr()->owner();
288 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
289 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
290 anObject = anAttr->context();
291 aShape = anAttr->value();
293 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
294 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
295 anObject = anAttr->value();
297 addValue(anObject, aShape, theFeature, theWorkshop, theObjectShapes);
302 void PartSet_OperationPrs::getResultShapes(const FeaturePtr& theFeature,
303 ModuleBase_IWorkshop* theWorkshop,
304 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes,
305 const bool theListShouldBeCleared)
307 if (theListShouldBeCleared)
308 theObjectShapes.clear();
310 if (!theFeature.get())
313 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
315 std::list<ResultPtr> aFeatureResults = theFeature->results();
316 std::list<ResultPtr>::const_iterator aRIt = aFeatureResults.begin(),
317 aRLast = aFeatureResults.end();
318 for (; aRIt != aRLast; aRIt++) {
319 ResultPtr aResult = *aRIt;
320 GeomShapePtr aGeomShape = aResult->shape();
321 appendShapeIfVisible(theWorkshop, aResult, aGeomShape, theObjectShapes);
325 void PartSet_OperationPrs::getHighlightedShapes(ModuleBase_IWorkshop* theWorkshop,
326 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
328 theObjectShapes.clear();
330 QList<ModuleBase_ViewerPrsPtr> aValues;
331 ModuleBase_IPropertyPanel* aPanel = theWorkshop->propertyPanel();
333 ModuleBase_ModelWidget* aWidget = aPanel->activeWidget();
335 aWidget->getHighlighted(aValues);
339 QList<GeomShapePtr> aShapes;
340 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIIt = aValues.begin(),
341 aILast = aValues.end();
342 for (; anIIt != aILast; anIIt++) {
343 ModuleBase_ViewerPrsPtr aPrs = *anIIt;
344 ObjectPtr anObject = aPrs->object();
346 GeomShapePtr aGeomShape = aPrs->shape();
347 if (!aGeomShape.get() || aGeomShape->isNull()) {
348 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
350 aGeomShape = aResult->shape();
353 appendShapeIfVisible(theWorkshop, anObject, aGeomShape, theObjectShapes);
358 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
360 std::string anAttrType = theAttribute->attributeType();
362 return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
363 anAttrType == ModelAPI_AttributeRefList::typeId() ||
364 anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
365 anAttrType == ModelAPI_AttributeSelection::typeId() ||
366 anAttrType == ModelAPI_AttributeReference::typeId();
369 void PartSet_OperationPrs::fillShapeList(const QMap<ObjectPtr, QList<GeomShapePtr> >& theFeatureShapes,
370 ModuleBase_IWorkshop* theWorkshop,
371 NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>& theShapeToPrsMap)
373 theShapeToPrsMap.Clear();
375 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
377 // create presentations on the base of the shapes
378 QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = theFeatureShapes.begin(),
379 aLast = theFeatureShapes.end();
380 for (; anIt != aLast; anIt++) {
381 ObjectPtr anObject = anIt.key();
382 QList<GeomShapePtr> aShapes = anIt.value();
383 QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
384 for (; aShIt != aShLast; aShIt++) {
385 GeomShapePtr aGeomShape = *aShIt;
386 // the shape should not be checked here on empty value because it should be checked in
387 // appendShapeIfVisible() on the step of filling theFeatureShapes list
388 // the reason is to avoid empty AIS object visualized in the viewer
389 //if (!aGeomShape.get()) continue;
390 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;
396 AISObjectPtr anAISPtr = aDisplayer->getAISObject(anObject);
398 anIO = anAISPtr->impl<Handle(AIS_InteractiveObject)>();
399 theShapeToPrsMap.Bind(aShape, anIO);