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 email : webmaster.salome@opencascade.com<mailto: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_ResultCompSolid.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>
62 //#define DEBUG_EMPTY_SHAPE
63 //#define DEBUG_OPERATION_PRS
65 // multi-rotation/translation operation
66 //#define DEBUG_HIDE_COPY_ATTRIBUTE
67 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
68 #include <ModelAPI_AttributeBoolean.h>
69 #include <SketchPlugin_SketchEntity.h>
72 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
74 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
75 : ViewerData_AISShape(TopoDS_Shape()), myWorkshop(theWorkshop), myUseAISWidth(false)
77 #ifdef DEBUG_OPERATION_PRS
78 qDebug("PartSet_OperationPrs::PartSet_OperationPrs");
80 myShapeColor = Quantity_Color(1, 1, 1, Quantity_TOC_RGB);
82 // first presentation for having correct Compute until presentation with shapes are set
83 gp_Pnt aPnt(0.0, 0.0, 0.0);
84 BRepBuilderAPI_MakeVertex aMaker(aPnt);
85 TopoDS_Vertex aVertex = aMaker.Vertex();
86 myShapeToPrsMap.Bind(aVertex, NULL);
89 bool PartSet_OperationPrs::hasShapes()
91 return !myShapeToPrsMap.IsEmpty();
94 void PartSet_OperationPrs::setShapeColor(const Quantity_Color& theColor)
96 myShapeColor = theColor;
99 void PartSet_OperationPrs::useAISWidth()
101 myUseAISWidth = true;
104 void PartSet_OperationPrs::Compute(
105 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
106 const Handle(Prs3d_Presentation)& thePresentation,
107 const Standard_Integer theMode)
109 #ifdef DEBUG_OPERATION_PRS
110 qDebug("PartSet_OperationPrs::Compute -- begin");
113 SetColor(myShapeColor);
114 thePresentation->Clear();
115 bool aReadyToDisplay = !myShapeToPrsMap.IsEmpty();
117 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(myWorkshop)->displayer();
118 Handle(Prs3d_Drawer) aDrawer = Attributes();
119 // create presentations on the base of the shapes
120 BRep_Builder aBuilder;
121 TopoDS_Compound aComp;
122 aBuilder.MakeCompound(aComp);
123 for(NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>::Iterator
124 anIter(myShapeToPrsMap); anIter.More(); anIter.Next()) {
125 const TopoDS_Shape& aShape = anIter.Key();
126 aBuilder.Add(aComp, aShape);
127 // change deviation coefficient to provide more precise circle
128 // as there is no result, the shape is processed to correct deviation. To be unified
129 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
132 Handle(AIS_InteractiveObject) anIO = anIter.Value();
133 if (!anIO.IsNull()) {
134 int aWidth = anIO->Width();
135 /// workaround for zero width. Else, there will be a crash
136 if (aWidth == 0) { // width returns of TSolid shape is zero
137 bool isDisplayed = !anIO->GetContext().IsNull();
138 aWidth = PartSet_Tools::getAISDefaultWidth();// default width value
140 setWidth(aDrawer, aWidth);
143 StdPrs_WFShape::Add(thePresentation, aShape, aDrawer);
146 if (!aReadyToDisplay) {
147 Events_InfoMessage("PartSet_OperationPrs",
148 "An empty AIS presentation: PartSet_OperationPrs").send();
149 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
150 new Events_Message(Events_Loop::eventByName(EVENT_EMPTY_OPERATION_PRESENTATION)));
151 Events_Loop::loop()->send(aMsg);
153 #ifdef DEBUG_OPERATION_PRS
154 qDebug("PartSet_OperationPrs::Compute -- end");
158 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
159 const Standard_Integer aMode)
161 // the presentation is not used in the selection
164 NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>& PartSet_OperationPrs::shapesMap()
166 return myShapeToPrsMap;
169 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
172 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
173 if (aComposite.get())
174 isSub = aComposite->isSub(theObject);
179 void PartSet_OperationPrs::addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
180 const FeaturePtr& theFeature, ModuleBase_IWorkshop* theWorkshop,
181 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
183 if (theObject.get()) {
184 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
186 ResultCompSolidPtr aCompsolidResult =
187 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
188 if (aCompsolidResult.get()) {
189 if (aCompsolidResult->numberOfSubs() > 0) {
190 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
191 ResultPtr aSubResult = aCompsolidResult->subResult(i);
192 if (aSubResult.get()) {
194 addValue(aSubResult, aShape, theFeature, theWorkshop, theObjectShapes);
200 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
202 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
203 if (aFeature.get()) {
204 AttributeBooleanPtr aCopyAttr =
205 aFeature->data()->boolean(SketchPlugin_SketchEntity::COPY_ID());
206 if (aCopyAttr.get()) {
207 bool isCopy = aCopyAttr->value();
216 GeomShapePtr aShape = theShape;
218 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
220 aShape = aResult->shape();
222 if (!isSubObject(theObject, theFeature))
223 appendShapeIfVisible(theWorkshop, theObject, aShape, theObjectShapes);
227 void PartSet_OperationPrs::appendShapeIfVisible(ModuleBase_IWorkshop* theWorkshop,
228 const ObjectPtr& theObject,
229 GeomShapePtr theGeomShape,
230 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
232 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
233 if (XGUI_Displayer::isVisible(aDisplayer, theObject)) {
234 if (theGeomShape.get()) {
235 if (theObjectShapes.contains(theObject))
236 theObjectShapes[theObject].append(theGeomShape);
238 QList<GeomShapePtr> aShapes;
239 aShapes.append(theGeomShape);
240 theObjectShapes[theObject] = aShapes;
243 #ifdef DEBUG_EMPTY_SHAPE
244 qDebug(QString("Empty shape in result, result: %1")
245 .arg(ModuleBase_Tools::objectInfo(theObject)).toStdString().c_str());
251 void PartSet_OperationPrs::getFeatureShapes(const FeaturePtr& theFeature,
252 ModuleBase_IWorkshop* theWorkshop,
253 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
255 theObjectShapes.clear();
256 if (!theFeature.get())
259 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
261 QList<GeomShapePtr> aShapes;
262 std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
263 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
264 for (; anIt != aLast; anIt++) {
265 AttributePtr anAttribute = *anIt;
266 if (!isSelectionAttribute(anAttribute))
269 if (!aValidators->isCase(theFeature, anAttribute->id()))
270 continue; // this attribute is not participated in the current case
272 std::string anAttrType = anAttribute->attributeType();
274 if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
275 std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
276 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
277 for(int i = 0; i < aCurSelList->size(); i++) {
278 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
279 ResultPtr aResult = aSelAttribute->context();
280 GeomShapePtr aShape = aSelAttribute->value();
281 addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
284 if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
285 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
286 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
287 for (int i = 0; i < aCurSelList->size(); i++) {
288 ObjectPtr anObject = aCurSelList->object(i);
289 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
290 // if a feature is stored in the attribute, we should obtain the feature results
291 // e.g. feature rectangle uses parameters feature lines in the attribute
292 if (aFeature.get()) {
293 getResultShapes(aFeature, theWorkshop, theObjectShapes, false);
296 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
298 GeomShapePtr aShape = aResult->shape();
300 addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
308 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
309 AttributeRefAttrPtr anAttr =
310 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
311 if (anAttr->isObject()) {
312 anObject = anAttr->object();
315 AttributePtr anAttribute = anAttr->attr();
316 aShape = PartSet_Tools::findShapeBy2DPoint(anAttribute, theWorkshop);
317 // the distance point is not found if the point is selected in the 2nd time
318 // TODO: after debug, this check can be removed
321 anObject = anAttr->attr()->owner();
324 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
325 AttributeSelectionPtr anAttr =
326 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
327 anObject = anAttr->context();
328 aShape = anAttr->value();
330 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
331 AttributeReferencePtr anAttr =
332 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
333 anObject = anAttr->value();
335 addValue(anObject, aShape, theFeature, theWorkshop, theObjectShapes);
340 void PartSet_OperationPrs::getResultShapes(const FeaturePtr& theFeature,
341 ModuleBase_IWorkshop* theWorkshop,
342 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes,
343 const bool theListShouldBeCleared)
345 if (theListShouldBeCleared)
346 theObjectShapes.clear();
348 if (!theFeature.get())
351 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
353 std::list<ResultPtr> aResults;
354 ModelAPI_Tools::allResults(theFeature, aResults);
355 std::list<ResultPtr>::const_iterator aRIt = aResults.begin(),
356 aRLast = aResults.end();
357 for (; aRIt != aRLast; aRIt++) {
358 ResultPtr aResult = *aRIt;
359 GeomShapePtr aGeomShape = aResult->shape();
360 appendShapeIfVisible(theWorkshop, aResult, aGeomShape, theObjectShapes);
364 void PartSet_OperationPrs::getPresentationShapes(const FeaturePtr& theFeature,
365 ModuleBase_IWorkshop* theWorkshop,
366 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes,
367 const bool theListShouldBeCleared)
369 if (theListShouldBeCleared)
370 theObjectShapes.clear();
372 if (!theFeature.get() || !theFeature->data()->isValid()) // if feature is already removed
375 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
377 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theFeature);
381 AISObjectPtr anAIS = aPrs->getAISObject(aDisplayer->getAISObject(theFeature));
385 Handle(AIS_InteractiveObject) anAISPrs = anAIS->impl<Handle(AIS_InteractiveObject)>();
386 if (!anAISPrs.IsNull()) {
387 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(anAISPrs);
388 if (!aShapePrs.IsNull()) {
389 TopoDS_Shape aShape = aShapePrs->Shape();
390 if (!aShape.IsNull()) {
391 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape());
392 aGeomShape->setImpl(new TopoDS_Shape(aShape));
393 appendShapeIfVisible(theWorkshop, theFeature, aGeomShape, theObjectShapes);
399 void PartSet_OperationPrs::getHighlightedShapes(ModuleBase_IWorkshop* theWorkshop,
401 QList<GeomShapePtr> >& theObjectShapes)
403 theObjectShapes.clear();
405 QList<ModuleBase_ViewerPrsPtr> aValues;
406 ModuleBase_ModelWidget* anActiveWidget = theWorkshop->module()->activeWidget();
408 anActiveWidget->getHighlighted(aValues);
410 QList<GeomShapePtr> aShapes;
411 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIIt = aValues.begin(),
412 aILast = aValues.end();
413 for (; anIIt != aILast; anIIt++) {
414 ModuleBase_ViewerPrsPtr aPrs = *anIIt;
415 ObjectPtr anObject = aPrs->object();
417 GeomShapePtr aGeomShape = aPrs->shape();
418 if (!aGeomShape.get() || aGeomShape->isNull()) {
419 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
421 aGeomShape = aResult->shape();
424 appendShapeIfVisible(theWorkshop, anObject, aGeomShape, theObjectShapes);
429 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
431 std::string anAttrType = theAttribute->attributeType();
433 return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
434 anAttrType == ModelAPI_AttributeRefList::typeId() ||
435 anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
436 anAttrType == ModelAPI_AttributeSelection::typeId() ||
437 anAttrType == ModelAPI_AttributeReference::typeId();
440 void PartSet_OperationPrs::fillShapeList(
441 const QMap<ObjectPtr, QList<GeomShapePtr> >& theFeatureShapes,
442 ModuleBase_IWorkshop* theWorkshop,
443 NCollection_DataMap<TopoDS_Shape,
444 Handle(AIS_InteractiveObject)>& theShapeToPrsMap)
446 theShapeToPrsMap.Clear();
448 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
450 // create presentations on the base of the shapes
451 QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = theFeatureShapes.begin(),
452 aLast = theFeatureShapes.end();
453 for (; anIt != aLast; anIt++) {
454 ObjectPtr anObject = anIt.key();
455 QList<GeomShapePtr> aShapes = anIt.value();
456 QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
457 for (; aShIt != aShLast; aShIt++) {
458 GeomShapePtr aGeomShape = *aShIt;
459 // the shape should not be checked here on empty value because it should be checked in
460 // appendShapeIfVisible() on the step of filling theFeatureShapes list
461 // the reason is to avoid empty AIS object visualized in the viewer
462 //if (!aGeomShape.get()) continue;
463 TopoDS_Shape aShape = aGeomShape.get() ? aGeomShape->impl<TopoDS_Shape>() : TopoDS_Shape();
467 // change deviation coefficient to provide more precise circle
468 Handle(AIS_InteractiveObject) anIO;
469 AISObjectPtr anAISPtr = aDisplayer->getAISObject(anObject);
471 anIO = anAISPtr->impl<Handle(AIS_InteractiveObject)>();
472 theShapeToPrsMap.Bind(aShape, anIO);