1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "PartSet_OperationPrs.h"
22 #include "PartSet_Tools.h"
24 #include "XGUI_Workshop.h"
25 #include "XGUI_ModuleConnector.h"
26 #include "XGUI_Displayer.h"
27 #include "XGUI_Tools.h"
29 #include "ModuleBase_Tools.h"
30 #include "ModuleBase_IModule.h"
31 #include <ModuleBase_IPropertyPanel.h>
32 #include <ModuleBase_ModelWidget.h>
33 #include <ModuleBase_ViewerPrs.h>
35 #include <ModelAPI_Events.h>
36 #include <ModelAPI_Result.h>
37 #include <ModelAPI_Attribute.h>
38 #include <ModelAPI_AttributeRefAttr.h>
39 #include <ModelAPI_AttributeReference.h>
40 #include <ModelAPI_AttributeSelection.h>
41 #include <ModelAPI_AttributeSelectionList.h>
42 #include <ModelAPI_AttributeRefList.h>
43 #include <ModelAPI_Validator.h>
44 #include <ModelAPI_Session.h>
45 #include <ModelAPI_ResultCompSolid.h>
46 #include <ModelAPI_Tools.h>
48 #include <Events_InfoMessage.h>
49 #include <Events_Loop.h>
51 #include <GeomAPI_IPresentable.h>
53 #include <StdPrs_WFShape.hxx>
58 #include <TopoDS_Vertex.hxx>
59 #include <BRepBuilderAPI_MakeVertex.hxx>
60 #include <BRep_Builder.hxx>
61 #include <TopoDS_Compound.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);
90 bool PartSet_OperationPrs::hasShapes()
92 return !myShapeToPrsMap.IsEmpty();
95 void PartSet_OperationPrs::setShapeColor(const Quantity_Color& theColor)
97 myShapeColor = theColor;
100 void PartSet_OperationPrs::useAISWidth()
102 myUseAISWidth = true;
105 void PartSet_OperationPrs::Compute(
106 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
107 const Handle(Prs3d_Presentation)& thePresentation,
108 const Standard_Integer theMode)
110 #ifdef DEBUG_OPERATION_PRS
111 qDebug("PartSet_OperationPrs::Compute -- begin");
114 SetColor(myShapeColor);
115 thePresentation->Clear();
116 bool aReadyToDisplay = !myShapeToPrsMap.IsEmpty();
118 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(myWorkshop)->displayer();
119 Handle(Prs3d_Drawer) aDrawer = Attributes();
120 // create presentations on the base of the shapes
121 BRep_Builder aBuilder;
122 TopoDS_Compound aComp;
123 aBuilder.MakeCompound(aComp);
124 for(NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>::Iterator
125 anIter(myShapeToPrsMap); anIter.More(); anIter.Next()) {
126 const TopoDS_Shape& aShape = anIter.Key();
127 aBuilder.Add(aComp, aShape);
128 // change deviation coefficient to provide more precise circle
129 // as there is no result, the shape is processed to correct deviation. To be unified
130 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
133 Handle(AIS_InteractiveObject) anIO = anIter.Value();
134 if (!anIO.IsNull()) {
135 int aWidth = anIO->Width();
136 /// workaround for zero width. Else, there will be a crash
137 if (aWidth == 0) { // width returns of TSolid shape is zero
138 bool isDisplayed = !anIO->GetContext().IsNull();
139 aWidth = PartSet_Tools::getAISDefaultWidth();// default width value
141 setWidth(aDrawer, aWidth);
145 StdPrs_WFShape::Add(thePresentation, aShape, aDrawer);
148 Events_InfoMessage("PartSet_OperationPrs",
149 "An empty AIS presentation: PartSet_OperationPrs").send();
150 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
151 new Events_Message(Events_Loop::eventByName(EVENT_EMPTY_OPERATION_PRESENTATION)));
152 Events_Loop::loop()->send(aMsg);
157 if (!aReadyToDisplay) {
158 Events_InfoMessage("PartSet_OperationPrs",
159 "An empty AIS presentation: PartSet_OperationPrs").send();
160 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
161 new Events_Message(Events_Loop::eventByName(EVENT_EMPTY_OPERATION_PRESENTATION)));
162 Events_Loop::loop()->send(aMsg);
164 #ifdef DEBUG_OPERATION_PRS
165 qDebug("PartSet_OperationPrs::Compute -- end");
169 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
170 const Standard_Integer aMode)
172 // the presentation is not used in the selection
175 NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>& PartSet_OperationPrs::shapesMap()
177 return myShapeToPrsMap;
180 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
183 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
184 if (aComposite.get())
185 isSub = aComposite->isSub(theObject);
190 void PartSet_OperationPrs::addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
191 const FeaturePtr& theFeature, ModuleBase_IWorkshop* theWorkshop,
192 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
194 if (theObject.get()) {
195 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
197 ResultCompSolidPtr aCompsolidResult =
198 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
199 if (aCompsolidResult.get()) {
200 if (aCompsolidResult->numberOfSubs() > 0) {
201 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
202 ResultPtr aSubResult = aCompsolidResult->subResult(i);
203 if (aSubResult.get()) {
205 addValue(aSubResult, aShape, theFeature, theWorkshop, theObjectShapes);
211 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
213 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
214 if (aFeature.get()) {
215 AttributeBooleanPtr aCopyAttr =
216 aFeature->data()->boolean(SketchPlugin_SketchEntity::COPY_ID());
217 if (aCopyAttr.get()) {
218 bool isCopy = aCopyAttr->value();
227 GeomShapePtr aShape = theShape;
229 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
231 aShape = aResult->shape();
233 if (!isSubObject(theObject, theFeature))
234 appendShapeIfVisible(theWorkshop, theObject, aShape, theObjectShapes);
238 void PartSet_OperationPrs::appendShapeIfVisible(ModuleBase_IWorkshop* theWorkshop,
239 const ObjectPtr& theObject,
240 GeomShapePtr theGeomShape,
241 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
243 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
244 if (XGUI_Displayer::isVisible(aDisplayer, theObject)) {
245 if (theGeomShape.get()) {
246 if (theObjectShapes.contains(theObject))
247 theObjectShapes[theObject].append(theGeomShape);
249 QList<GeomShapePtr> aShapes;
250 aShapes.append(theGeomShape);
251 theObjectShapes[theObject] = aShapes;
254 #ifdef DEBUG_EMPTY_SHAPE
255 qDebug(QString("Empty shape in result, result: %1")
256 .arg(ModuleBase_Tools::objectInfo(theObject)).toStdString().c_str());
262 void PartSet_OperationPrs::getFeatureShapes(const FeaturePtr& theFeature,
263 ModuleBase_IWorkshop* theWorkshop,
264 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
266 theObjectShapes.clear();
267 if (!theFeature.get())
270 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
272 QList<GeomShapePtr> aShapes;
273 std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
274 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
275 for (; anIt != aLast; anIt++) {
276 AttributePtr anAttribute = *anIt;
277 if (!isSelectionAttribute(anAttribute))
280 if (!aValidators->isCase(theFeature, anAttribute->id()))
281 continue; // this attribute is not participated in the current case
283 std::string anAttrType = anAttribute->attributeType();
285 if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
286 std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
287 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
288 for(int i = 0; i < aCurSelList->size(); i++) {
289 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
290 ResultPtr aResult = aSelAttribute->context();
291 GeomShapePtr aShape = aSelAttribute->value();
292 addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
295 if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
296 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
297 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
298 for (int i = 0; i < aCurSelList->size(); i++) {
299 ObjectPtr anObject = aCurSelList->object(i);
300 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
301 // if a feature is stored in the attribute, we should obtain the feature results
302 // e.g. feature rectangle uses parameters feature lines in the attribute
303 if (aFeature.get()) {
304 getResultShapes(aFeature, theWorkshop, theObjectShapes, false);
307 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
309 GeomShapePtr aShape = aResult->shape();
311 addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
319 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
320 AttributeRefAttrPtr anAttr =
321 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
322 if (anAttr->isObject()) {
323 anObject = anAttr->object();
326 AttributePtr anAttribute = anAttr->attr();
327 aShape = PartSet_Tools::findShapeBy2DPoint(anAttribute, theWorkshop);
328 // the distance point is not found if the point is selected in the 2nd time
329 // TODO: after debug, this check can be removed
332 anObject = anAttr->attr()->owner();
335 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
336 AttributeSelectionPtr anAttr =
337 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
338 anObject = anAttr->context();
339 aShape = anAttr->value();
341 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
342 AttributeReferencePtr anAttr =
343 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
344 anObject = anAttr->value();
346 addValue(anObject, aShape, theFeature, theWorkshop, theObjectShapes);
351 void PartSet_OperationPrs::getResultShapes(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())
362 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
364 std::list<ResultPtr> aResults;
365 ModelAPI_Tools::allResults(theFeature, aResults);
366 std::list<ResultPtr>::const_iterator aRIt = aResults.begin(),
367 aRLast = aResults.end();
368 for (; aRIt != aRLast; aRIt++) {
369 ResultPtr aResult = *aRIt;
370 GeomShapePtr aGeomShape = aResult->shape();
371 appendShapeIfVisible(theWorkshop, aResult, aGeomShape, theObjectShapes);
375 void PartSet_OperationPrs::getPresentationShapes(const FeaturePtr& theFeature,
376 ModuleBase_IWorkshop* theWorkshop,
377 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes,
378 const bool theListShouldBeCleared)
380 if (theListShouldBeCleared)
381 theObjectShapes.clear();
383 if (!theFeature.get() || !theFeature->data()->isValid()) // if feature is already removed
386 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
388 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theFeature);
392 AISObjectPtr anAIS = aPrs->getAISObject(aDisplayer->getAISObject(theFeature));
396 Handle(AIS_InteractiveObject) anAISPrs = anAIS->impl<Handle(AIS_InteractiveObject)>();
397 if (!anAISPrs.IsNull()) {
398 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(anAISPrs);
399 if (!aShapePrs.IsNull()) {
400 TopoDS_Shape aShape = aShapePrs->Shape();
401 if (!aShape.IsNull()) {
402 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape());
403 aGeomShape->setImpl(new TopoDS_Shape(aShape));
404 appendShapeIfVisible(theWorkshop, theFeature, aGeomShape, theObjectShapes);
410 void PartSet_OperationPrs::getHighlightedShapes(ModuleBase_IWorkshop* theWorkshop,
412 QList<GeomShapePtr> >& theObjectShapes)
414 theObjectShapes.clear();
416 QList<ModuleBase_ViewerPrsPtr> aValues;
417 ModuleBase_ModelWidget* anActiveWidget = theWorkshop->module()->activeWidget();
419 anActiveWidget->getHighlighted(aValues);
421 QList<GeomShapePtr> aShapes;
422 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIIt = aValues.begin(),
423 aILast = aValues.end();
424 for (; anIIt != aILast; anIIt++) {
425 ModuleBase_ViewerPrsPtr aPrs = *anIIt;
426 ObjectPtr anObject = aPrs->object();
428 GeomShapePtr aGeomShape = aPrs->shape();
429 if (!aGeomShape.get() || aGeomShape->isNull()) {
430 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
432 aGeomShape = aResult->shape();
435 appendShapeIfVisible(theWorkshop, anObject, aGeomShape, theObjectShapes);
440 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
442 std::string anAttrType = theAttribute->attributeType();
444 return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
445 anAttrType == ModelAPI_AttributeRefList::typeId() ||
446 anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
447 anAttrType == ModelAPI_AttributeSelection::typeId() ||
448 anAttrType == ModelAPI_AttributeReference::typeId();
451 void PartSet_OperationPrs::fillShapeList(
452 const QMap<ObjectPtr, QList<GeomShapePtr> >& theFeatureShapes,
453 ModuleBase_IWorkshop* theWorkshop,
454 NCollection_DataMap<TopoDS_Shape,
455 Handle(AIS_InteractiveObject)>& theShapeToPrsMap)
457 theShapeToPrsMap.Clear();
459 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
461 // create presentations on the base of the shapes
462 QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = theFeatureShapes.begin(),
463 aLast = theFeatureShapes.end();
464 for (; anIt != aLast; anIt++) {
465 ObjectPtr anObject = anIt.key();
466 QList<GeomShapePtr> aShapes = anIt.value();
467 QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
468 for (; aShIt != aShLast; aShIt++) {
469 GeomShapePtr aGeomShape = *aShIt;
470 // the shape should not be checked here on empty value because it should be checked in
471 // appendShapeIfVisible() on the step of filling theFeatureShapes list
472 // the reason is to avoid empty AIS object visualized in the viewer
473 //if (!aGeomShape.get()) continue;
474 TopoDS_Shape aShape = aGeomShape.get() ? aGeomShape->impl<TopoDS_Shape>() : TopoDS_Shape();
478 // change deviation coefficient to provide more precise circle
479 Handle(AIS_InteractiveObject) anIO;
480 AISObjectPtr anAISPtr = aDisplayer->getAISObject(anObject);
482 anIO = anAISPtr->impl<Handle(AIS_InteractiveObject)>();
483 theShapeToPrsMap.Bind(aShape, anIO);