1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "PartSet_OperationPrs.h"
21 #include "PartSet_Tools.h"
23 #include "XGUI_Workshop.h"
24 #include "XGUI_ModuleConnector.h"
25 #include "XGUI_Displayer.h"
26 #include "XGUI_Tools.h"
28 #include "ModuleBase_Tools.h"
29 #include "ModuleBase_IModule.h"
30 #include <ModuleBase_IPropertyPanel.h>
31 #include <ModuleBase_ModelWidget.h>
32 #include <ModuleBase_ViewerPrs.h>
34 #include <ModelAPI_Events.h>
35 #include <ModelAPI_Result.h>
36 #include <ModelAPI_Attribute.h>
37 #include <ModelAPI_AttributeRefAttr.h>
38 #include <ModelAPI_AttributeReference.h>
39 #include <ModelAPI_AttributeSelection.h>
40 #include <ModelAPI_AttributeSelectionList.h>
41 #include <ModelAPI_AttributeRefList.h>
42 #include <ModelAPI_Validator.h>
43 #include <ModelAPI_Session.h>
44 #include <ModelAPI_ResultBody.h>
45 #include <ModelAPI_Tools.h>
47 #include <Events_InfoMessage.h>
48 #include <Events_Loop.h>
50 #include <GeomAPI_IPresentable.h>
52 #include <StdPrs_WFShape.hxx>
57 #include <TopoDS_Vertex.hxx>
58 #include <BRepBuilderAPI_MakeVertex.hxx>
59 #include <BRep_Builder.hxx>
60 #include <TopoDS_Compound.hxx>
61 #include <Prs3d_IsoAspect.hxx>
63 //#define DEBUG_EMPTY_SHAPE
64 //#define DEBUG_OPERATION_PRS
66 // multi-rotation/translation operation
67 //#define DEBUG_HIDE_COPY_ATTRIBUTE
68 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
69 #include <ModelAPI_AttributeBoolean.h>
70 #include <SketchPlugin_SketchEntity.h>
73 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
75 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
76 : ViewerData_AISShape(TopoDS_Shape()), myWorkshop(theWorkshop), myUseAISWidth(false)
78 #ifdef DEBUG_OPERATION_PRS
79 qDebug("PartSet_OperationPrs::PartSet_OperationPrs");
81 myShapeColor = Quantity_Color(1, 1, 1, Quantity_TOC_RGB);
83 // first presentation for having correct Compute until presentation with shapes are set
84 gp_Pnt aPnt(0.0, 0.0, 0.0);
85 BRepBuilderAPI_MakeVertex aMaker(aPnt);
86 TopoDS_Vertex aVertex = aMaker.Vertex();
87 myShapeToPrsMap.Bind(aVertex, NULL);
89 Handle(Prs3d_Drawer) aDrawer = Attributes();
90 Handle(Prs3d_IsoAspect) aUIsoAspect = new Prs3d_IsoAspect(myShapeColor, Aspect_TOL_SOLID, 1, 0);
91 Handle(Prs3d_IsoAspect) aVIsoAspect = new Prs3d_IsoAspect(myShapeColor, Aspect_TOL_SOLID, 1, 0);
92 aDrawer->SetUIsoAspect(aUIsoAspect);
93 aDrawer->SetVIsoAspect(aVIsoAspect);
94 aDrawer->SetIsoOnPlane(false);
97 bool PartSet_OperationPrs::hasShapes()
99 return !myShapeToPrsMap.IsEmpty();
102 void PartSet_OperationPrs::setShapeColor(const Quantity_Color& theColor)
104 myShapeColor = theColor;
107 void PartSet_OperationPrs::useAISWidth()
109 myUseAISWidth = true;
112 void PartSet_OperationPrs::Compute(
113 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
114 const Handle(Prs3d_Presentation)& thePresentation,
115 const Standard_Integer theMode)
117 #ifdef DEBUG_OPERATION_PRS
118 qDebug("PartSet_OperationPrs::Compute -- begin");
121 SetColor(myShapeColor);
122 thePresentation->Clear();
123 bool aReadyToDisplay = !myShapeToPrsMap.IsEmpty();
125 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(myWorkshop)->displayer();
126 Handle(Prs3d_Drawer) aDrawer = Attributes();
127 // create presentations on the base of the shapes
128 BRep_Builder aBuilder;
129 TopoDS_Compound aComp;
130 aBuilder.MakeCompound(aComp);
131 for(NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>::Iterator
132 anIter(myShapeToPrsMap); anIter.More(); anIter.Next()) {
133 const TopoDS_Shape& aShape = anIter.Key();
134 aBuilder.Add(aComp, aShape);
135 // change deviation coefficient to provide more precise circle
136 // as there is no result, the shape is processed to correct deviation. To be unified
137 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
138 Handle(Prs3d_Drawer) aHighlightDrawer = DynamicHilightAttributes();
139 if (!aHighlightDrawer.IsNull())
140 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aHighlightDrawer);
143 Handle(AIS_InteractiveObject) anIO = anIter.Value();
144 if (!anIO.IsNull()) {
145 int aWidth = anIO->Width();
146 /// workaround for zero width. Else, there will be a crash
147 if (aWidth == 0) { // width returns of TSolid shape is zero
148 bool isDisplayed = !anIO->GetContext().IsNull();
149 aWidth = PartSet_Tools::getAISDefaultWidth();// default width value
151 setWidth(aDrawer, aWidth);
155 StdPrs_WFShape::Add(thePresentation, aShape, aDrawer);
162 if (!aReadyToDisplay) {
163 Events_InfoMessage("PartSet_OperationPrs",
164 "An empty AIS presentation: PartSet_OperationPrs").send();
165 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
166 new Events_Message(Events_Loop::eventByName(EVENT_EMPTY_OPERATION_PRESENTATION)));
167 Events_Loop::loop()->send(aMsg);
169 #ifdef DEBUG_OPERATION_PRS
170 qDebug("PartSet_OperationPrs::Compute -- end");
174 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
175 const Standard_Integer aMode)
177 // the presentation is not used in the selection
180 NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>& PartSet_OperationPrs::shapesMap()
182 return myShapeToPrsMap;
185 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
188 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
189 if (aComposite.get())
190 isSub = aComposite->isSub(theObject);
195 void PartSet_OperationPrs::addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
196 const FeaturePtr& theFeature, ModuleBase_IWorkshop* theWorkshop,
197 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
199 if (theObject.get()) {
200 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
202 ResultBodyPtr aBodyResult =
203 std::dynamic_pointer_cast<ModelAPI_ResultBody>(theObject);
204 if (aBodyResult.get()) {
205 if (aBodyResult->numberOfSubs() > 0) {
206 for(int i = 0; i < aBodyResult->numberOfSubs(); i++) {
207 ResultPtr aSubResult = aBodyResult->subResult(i);
208 if (aSubResult.get()) {
210 addValue(aSubResult, aShape, theFeature, theWorkshop, theObjectShapes);
216 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
218 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
219 if (aFeature.get()) {
220 AttributeBooleanPtr aCopyAttr =
221 aFeature->data()->boolean(SketchPlugin_SketchEntity::COPY_ID());
222 if (aCopyAttr.get()) {
223 bool isCopy = aCopyAttr->value();
232 GeomShapePtr aShape = theShape;
234 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
236 aShape = aResult->shape();
238 if (!isSubObject(theObject, theFeature))
239 appendShapeIfVisible(theWorkshop, theObject, aShape, theObjectShapes);
243 void PartSet_OperationPrs::appendShapeIfVisible(ModuleBase_IWorkshop* theWorkshop,
244 const ObjectPtr& theObject,
245 GeomShapePtr theGeomShape,
246 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
248 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
249 // VSV: Do not use isVisible checking because it can be used when state "Show Only" is ON
250 //if (XGUI_Displayer::isVisible(aDisplayer, theObject)) {
251 if (theGeomShape.get()) {
252 if (theObjectShapes.contains(theObject))
253 theObjectShapes[theObject].append(theGeomShape);
255 QList<GeomShapePtr> aShapes;
256 aShapes.append(theGeomShape);
257 theObjectShapes[theObject] = aShapes;
260 #ifdef DEBUG_EMPTY_SHAPE
261 qDebug(QString("Empty shape in result, result: %1")
262 .arg(ModuleBase_Tools::objectInfo(theObject)).toStdString().c_str());
268 void PartSet_OperationPrs::getFeatureShapes(const FeaturePtr& theFeature,
269 ModuleBase_IWorkshop* theWorkshop,
270 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
272 theObjectShapes.clear();
273 if (!theFeature.get())
276 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
278 QList<GeomShapePtr> aShapes;
279 std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
280 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
281 for (; anIt != aLast; anIt++) {
282 AttributePtr anAttribute = *anIt;
283 if (!isSelectionAttribute(anAttribute))
286 if (!aValidators->isCase(theFeature, anAttribute->id()))
287 continue; // this attribute is not participated in the current case
289 std::string anAttrType = anAttribute->attributeType();
291 if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
292 std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
293 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
294 for(int i = 0; i < aCurSelList->size(); i++) {
295 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
296 ResultPtr aResult = aSelAttribute->context();
297 GeomShapePtr aShape = aSelAttribute->value();
298 addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
301 if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
302 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
303 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
304 for (int i = 0; i < aCurSelList->size(); i++) {
305 ObjectPtr anObject = aCurSelList->object(i);
306 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
307 // if a feature is stored in the attribute, we should obtain the feature results
308 // e.g. feature rectangle uses parameters feature lines in the attribute
309 if (aFeature.get()) {
310 getResultShapes(aFeature, theWorkshop, theObjectShapes, false);
313 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
315 GeomShapePtr aShape = aResult->shape();
317 addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
325 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
326 AttributeRefAttrPtr anAttr =
327 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
328 if (anAttr->isObject()) {
329 anObject = anAttr->object();
332 AttributePtr anAttribute = anAttr->attr();
333 aShape = PartSet_Tools::findShapeBy2DPoint(anAttribute, theWorkshop);
334 // the distance point is not found if the point is selected in the 2nd time
335 // TODO: after debug, this check can be removed
338 anObject = anAttr->attr()->owner();
341 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
342 AttributeSelectionPtr anAttr =
343 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
344 anObject = anAttr->context();
345 aShape = anAttr->value();
347 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
348 AttributeReferencePtr anAttr =
349 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
350 anObject = anAttr->value();
352 addValue(anObject, aShape, theFeature, theWorkshop, theObjectShapes);
357 void PartSet_OperationPrs::getResultShapes(const FeaturePtr& theFeature,
358 ModuleBase_IWorkshop* theWorkshop,
359 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes,
360 const bool theListShouldBeCleared)
362 if (theListShouldBeCleared)
363 theObjectShapes.clear();
365 if (!theFeature.get())
368 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
370 std::list<ResultPtr> aResults;
371 ModelAPI_Tools::allResults(theFeature, aResults);
372 std::list<ResultPtr>::const_iterator aRIt = aResults.begin(),
373 aRLast = aResults.end();
374 for (; aRIt != aRLast; aRIt++) {
375 ResultPtr aResult = *aRIt;
376 GeomShapePtr aGeomShape = aResult->shape();
377 appendShapeIfVisible(theWorkshop, aResult, aGeomShape, theObjectShapes);
381 void PartSet_OperationPrs::getPresentationShapes(const FeaturePtr& theFeature,
382 ModuleBase_IWorkshop* theWorkshop,
383 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes,
384 const bool theListShouldBeCleared)
386 if (theListShouldBeCleared)
387 theObjectShapes.clear();
389 if (!theFeature.get() || !theFeature->data()->isValid()) // if feature is already removed
392 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
394 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theFeature);
398 AISObjectPtr anAIS = aPrs->getAISObject(aDisplayer->getAISObject(theFeature));
402 Handle(AIS_InteractiveObject) anAISPrs = anAIS->impl<Handle(AIS_InteractiveObject)>();
403 if (!anAISPrs.IsNull()) {
404 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(anAISPrs);
405 if (!aShapePrs.IsNull()) {
406 TopoDS_Shape aShape = aShapePrs->Shape();
407 if (!aShape.IsNull()) {
408 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape());
409 aGeomShape->setImpl(new TopoDS_Shape(aShape));
410 appendShapeIfVisible(theWorkshop, theFeature, aGeomShape, theObjectShapes);
416 void PartSet_OperationPrs::getHighlightedShapes(ModuleBase_IWorkshop* theWorkshop,
418 QList<GeomShapePtr> >& theObjectShapes)
420 theObjectShapes.clear();
422 QList<ModuleBase_ViewerPrsPtr> aValues;
423 ModuleBase_ModelWidget* anActiveWidget = theWorkshop->module()->activeWidget();
425 anActiveWidget->getHighlighted(aValues);
427 QList<GeomShapePtr> aShapes;
428 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIIt = aValues.begin(),
429 aILast = aValues.end();
430 for (; anIIt != aILast; anIIt++) {
431 ModuleBase_ViewerPrsPtr aPrs = *anIIt;
432 ObjectPtr anObject = aPrs->object();
434 GeomShapePtr aGeomShape = aPrs->shape();
435 if (!aGeomShape.get() || aGeomShape->isNull()) {
436 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
438 aGeomShape = aResult->shape();
441 appendShapeIfVisible(theWorkshop, anObject, aGeomShape, theObjectShapes);
446 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
448 std::string anAttrType = theAttribute->attributeType();
450 return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
451 anAttrType == ModelAPI_AttributeRefList::typeId() ||
452 anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
453 anAttrType == ModelAPI_AttributeSelection::typeId() ||
454 anAttrType == ModelAPI_AttributeReference::typeId();
457 void PartSet_OperationPrs::fillShapeList(
458 const QMap<ObjectPtr, QList<GeomShapePtr> >& theFeatureShapes,
459 ModuleBase_IWorkshop* theWorkshop,
460 NCollection_DataMap<TopoDS_Shape,
461 Handle(AIS_InteractiveObject)>& theShapeToPrsMap)
463 theShapeToPrsMap.Clear();
465 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
467 // create presentations on the base of the shapes
468 QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = theFeatureShapes.begin(),
469 aLast = theFeatureShapes.end();
470 for (; anIt != aLast; anIt++) {
471 ObjectPtr anObject = anIt.key();
472 QList<GeomShapePtr> aShapes = anIt.value();
473 QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
474 for (; aShIt != aShLast; aShIt++) {
475 GeomShapePtr aGeomShape = *aShIt;
476 // the shape should not be checked here on empty value because it should be checked in
477 // appendShapeIfVisible() on the step of filling theFeatureShapes list
478 // the reason is to avoid empty AIS object visualized in the viewer
479 //if (!aGeomShape.get()) continue;
480 TopoDS_Shape aShape = aGeomShape.get() ? aGeomShape->impl<TopoDS_Shape>() : TopoDS_Shape();
484 // change deviation coefficient to provide more precise circle
485 Handle(AIS_InteractiveObject) anIO;
486 AISObjectPtr anAISPtr = aDisplayer->getAISObject(anObject);
488 anIO = anAISPtr->impl<Handle(AIS_InteractiveObject)>();
489 theShapeToPrsMap.Bind(aShape, anIO);