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_Result.h>
22 #include <ModelAPI_Attribute.h>
23 #include <ModelAPI_AttributeRefAttr.h>
24 #include <ModelAPI_AttributeReference.h>
25 #include <ModelAPI_AttributeSelection.h>
26 #include <ModelAPI_AttributeSelectionList.h>
27 #include <ModelAPI_AttributeRefList.h>
28 #include <ModelAPI_Validator.h>
29 #include <ModelAPI_Session.h>
30 #include <ModelAPI_ResultCompSolid.h>
32 #include <Events_Error.h>
34 #include <GeomAPI_IPresentable.h>
36 #include <StdPrs_WFDeflectionShape.hxx>
40 //#define DEBUG_EMPTY_SHAPE
42 // multi-rotation/translation operation
43 //#define DEBUG_HIDE_COPY_ATTRIBUTE
44 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
45 #include <ModelAPI_AttributeBoolean.h>
46 #include <SketchPlugin_SketchEntity.h>
49 IMPLEMENT_STANDARD_HANDLE(PartSet_OperationPrs, ViewerData_AISShape);
50 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
52 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
53 : ViewerData_AISShape(TopoDS_Shape()), myWorkshop(theWorkshop), myUseAISWidth(false)
55 myShapeColor = Quantity_Color(1, 1, 1, Quantity_TOC_RGB);
58 bool PartSet_OperationPrs::hasShapes()
60 return !myFeatureShapes.empty();
63 void PartSet_OperationPrs::setShapeColor(const Quantity_Color& theColor)
65 myShapeColor = theColor;
68 void PartSet_OperationPrs::useAISWidth()
73 void PartSet_OperationPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
74 const Handle(Prs3d_Presentation)& thePresentation,
75 const Standard_Integer theMode)
77 SetColor(myShapeColor);
78 thePresentation->Clear();
80 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(myWorkshop)->displayer();
81 Handle(Prs3d_Drawer) aDrawer = Attributes();
83 // create presentations on the base of the shapes
84 bool anEmptyAIS = true;
85 QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = myFeatureShapes.begin(),
86 aLast = myFeatureShapes.end();
87 for (; anIt != aLast; anIt++) {
88 ObjectPtr anObject = anIt.key();
89 QList<GeomShapePtr> aShapes = anIt.value();
90 QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
91 for (; aShIt != aShLast; aShIt++) {
92 GeomShapePtr aGeomShape = *aShIt;
93 // the shape should not be checked here on empty value because it should be checked in
94 // appendShapeIfVisible() on the step of filling myFeatureShapes list
95 // the reason is to avoid empty AIS object visualized in the viewer
96 //if (!aGeomShape.get()) continue;
97 TopoDS_Shape aShape = aGeomShape.get() ? aGeomShape->impl<TopoDS_Shape>() : TopoDS_Shape();
98 // change deviation coefficient to provide more precise circle
99 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
102 AISObjectPtr anAISPtr = aDisplayer->getAISObject(anObject);
103 if (anAISPtr.get()) {
104 Handle(AIS_InteractiveObject) anIO = anAISPtr->impl<Handle(AIS_InteractiveObject)>();
105 if (!anIO.IsNull()) {
106 int aWidth = anIO->Width();
107 /// workaround for zero width. Else, there will be a crash
108 if (aWidth == 0) { // width returns of TSolid shape is zero
109 bool isDisplayed = !anIO->GetContext().IsNull();
110 aWidth = PartSet_Tools::getAISDefaultWidth();// default width value
112 setWidth(aDrawer, aWidth);
116 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
122 Events_Error::throwException("An empty AIS presentation: PartSet_OperationPrs");
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 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
134 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
135 if (aComposite.get())
136 isSub = aComposite->isSub(theObject);
141 void PartSet_OperationPrs::addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
142 const FeaturePtr& theFeature, ModuleBase_IWorkshop* theWorkshop,
143 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
145 if (theObject.get()) {
146 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
148 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
149 if (aCompsolidResult.get()) {
150 if (aCompsolidResult->numberOfSubs() > 0) {
151 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
152 ResultPtr aSubResult = aCompsolidResult->subResult(i);
153 if (aSubResult.get()) {
155 addValue(aSubResult, aShape, theFeature, theWorkshop, theObjectShapes);
161 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
163 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
164 if (aFeature.get()) {
165 AttributeBooleanPtr aCopyAttr = aFeature->data()->boolean(SketchPlugin_SketchEntity::COPY_ID());
166 if (aCopyAttr.get()) {
167 bool isCopy = aCopyAttr->value();
176 GeomShapePtr aShape = theShape;
178 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
180 aShape = aResult->shape();
182 if (!isSubObject(theObject, theFeature))
183 appendShapeIfVisible(theWorkshop, theObject, aShape, theObjectShapes);
187 void PartSet_OperationPrs::appendShapeIfVisible(ModuleBase_IWorkshop* theWorkshop,
188 const ObjectPtr& theObject,
189 GeomShapePtr theGeomShape,
190 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
192 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
193 if (XGUI_Displayer::isVisible(aDisplayer, theObject)) {
194 if (theGeomShape.get()) {
195 if (theObjectShapes.contains(theObject))
196 theObjectShapes[theObject].append(theGeomShape);
198 QList<GeomShapePtr> aShapes;
199 aShapes.append(theGeomShape);
200 theObjectShapes[theObject] = aShapes;
203 #ifdef DEBUG_EMPTY_SHAPE
204 qDebug(QString("Empty shape in result, result: %1")
205 .arg(ModuleBase_Tools::objectInfo(theObject)).toStdString().c_str());
211 void PartSet_OperationPrs::getFeatureShapes(const FeaturePtr& theFeature,
212 ModuleBase_IWorkshop* theWorkshop,
213 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
215 theObjectShapes.clear();
216 if (!theFeature.get())
219 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
221 QList<GeomShapePtr> aShapes;
222 std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
223 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
224 for (; anIt != aLast; anIt++) {
225 AttributePtr anAttribute = *anIt;
226 if (!isSelectionAttribute(anAttribute))
229 if (!aValidators->isCase(theFeature, anAttribute->id()))
230 continue; // this attribute is not participated in the current case
232 std::string anAttrType = anAttribute->attributeType();
234 if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
235 std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
236 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
237 for(int i = 0; i < aCurSelList->size(); i++) {
238 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
239 ResultPtr aResult = aSelAttribute->context();
240 GeomShapePtr aShape = aSelAttribute->value();
241 addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
244 if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
245 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
246 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
247 for (int i = 0; i < aCurSelList->size(); i++) {
248 ObjectPtr anObject = aCurSelList->object(i);
249 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
250 // if a feature is stored in the attribute, we should obtain the feature results
251 // e.g. feature rectangle uses parameters feature lines in the attribute
252 if (aFeature.get()) {
253 getResultShapes(aFeature, theWorkshop, theObjectShapes, false);
256 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
258 GeomShapePtr aShape = aResult->shape();
260 addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
268 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
269 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
270 if (anAttr->isObject()) {
271 anObject = anAttr->object();
274 AttributePtr anAttribute = anAttr->attr();
275 aShape = PartSet_Tools::findShapeBy2DPoint(anAttribute, theWorkshop);
276 // the distance point is not found if the point is selected in the 2nd time
277 // TODO: after debug, this check can be removed
280 anObject = anAttr->attr()->owner();
283 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
284 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
285 anObject = anAttr->context();
286 aShape = anAttr->value();
288 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
289 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
290 anObject = anAttr->value();
292 addValue(anObject, aShape, theFeature, theWorkshop, theObjectShapes);
297 void PartSet_OperationPrs::getResultShapes(const FeaturePtr& theFeature,
298 ModuleBase_IWorkshop* theWorkshop,
299 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes,
300 const bool theListShouldBeCleared)
302 if (theListShouldBeCleared)
303 theObjectShapes.clear();
305 if (!theFeature.get())
308 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
310 std::list<ResultPtr> aFeatureResults = theFeature->results();
311 std::list<ResultPtr>::const_iterator aRIt = aFeatureResults.begin(),
312 aRLast = aFeatureResults.end();
313 for (; aRIt != aRLast; aRIt++) {
314 ResultPtr aResult = *aRIt;
315 GeomShapePtr aGeomShape = aResult->shape();
316 appendShapeIfVisible(theWorkshop, aResult, aGeomShape, theObjectShapes);
320 void PartSet_OperationPrs::getHighlightedShapes(ModuleBase_IWorkshop* theWorkshop,
321 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
323 theObjectShapes.clear();
325 QList<ModuleBase_ViewerPrsPtr> aValues;
326 ModuleBase_IPropertyPanel* aPanel = theWorkshop->propertyPanel();
328 ModuleBase_ModelWidget* aWidget = aPanel->activeWidget();
330 aWidget->getHighlighted(aValues);
334 QList<GeomShapePtr> aShapes;
335 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIIt = aValues.begin(),
336 aILast = aValues.end();
337 for (; anIIt != aILast; anIIt++) {
338 ModuleBase_ViewerPrsPtr aPrs = *anIIt;
339 ObjectPtr anObject = aPrs->object();
341 GeomShapePtr aGeomShape = aPrs->shape();
342 if (!aGeomShape.get() || aGeomShape->isNull()) {
343 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
345 aGeomShape = aResult->shape();
348 appendShapeIfVisible(theWorkshop, anObject, aGeomShape, theObjectShapes);
353 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
355 std::string anAttrType = theAttribute->attributeType();
357 return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
358 anAttrType == ModelAPI_AttributeRefList::typeId() ||
359 anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
360 anAttrType == ModelAPI_AttributeSelection::typeId() ||
361 anAttrType == ModelAPI_AttributeReference::typeId();