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>
20 #include <ModelAPI_Result.h>
21 #include <ModelAPI_Attribute.h>
22 #include <ModelAPI_AttributeRefAttr.h>
23 #include <ModelAPI_AttributeReference.h>
24 #include <ModelAPI_AttributeSelection.h>
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include <ModelAPI_AttributeRefList.h>
27 #include <ModelAPI_Validator.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_ResultCompSolid.h>
31 #include <Events_Error.h>
33 #include <GeomAPI_IPresentable.h>
35 #include <StdPrs_WFDeflectionShape.hxx>
39 static const int AIS_DEFAULT_WIDTH = 2;
41 //#define DEBUG_EMPTY_SHAPE
43 // multi-rotation/translation operation
44 //#define DEBUG_HIDE_COPY_ATTRIBUTE
45 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
46 #include <ModelAPI_AttributeBoolean.h>
47 #include <SketchPlugin_SketchEntity.h>
50 IMPLEMENT_STANDARD_HANDLE(PartSet_OperationPrs, ViewerData_AISShape);
51 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
53 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
54 : ViewerData_AISShape(TopoDS_Shape()), myWorkshop(theWorkshop), myUseAISWidth(false)
56 myShapeColor = Quantity_Color(1, 1, 1, Quantity_TOC_RGB);
59 bool PartSet_OperationPrs::hasShapes()
61 return !myFeatureShapes.empty();
64 void PartSet_OperationPrs::setShapeColor(const Quantity_Color& theColor)
66 myShapeColor = theColor;
69 void PartSet_OperationPrs::useAISWidth()
74 void PartSet_OperationPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
75 const Handle(Prs3d_Presentation)& thePresentation,
76 const Standard_Integer theMode)
78 SetColor(myShapeColor);
79 thePresentation->Clear();
81 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(myWorkshop)->displayer();
82 Handle(Prs3d_Drawer) aDrawer = Attributes();
84 // create presentations on the base of the shapes
85 bool anEmptyAIS = true;
86 QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = myFeatureShapes.begin(),
87 aLast = myFeatureShapes.end();
88 for (; anIt != aLast; anIt++) {
89 ObjectPtr anObject = anIt.key();
90 QList<GeomShapePtr> aShapes = anIt.value();
91 QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
92 for (; aShIt != aShLast; aShIt++) {
93 GeomShapePtr aGeomShape = *aShIt;
94 // the shape should not be checked here on empty value because it should be checked in
95 // appendShapeIfVisible() on the step of filling myFeatureShapes list
96 // the reason is to avoid empty AIS object visualized in the viewer
97 //if (!aGeomShape.get()) continue;
98 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
99 // change deviation coefficient to provide more precise circle
100 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
103 AISObjectPtr anAISPtr = aDisplayer->getAISObject(anObject);
104 if (anAISPtr.get()) {
105 Handle(AIS_InteractiveObject) anIO = anAISPtr->impl<Handle(AIS_InteractiveObject)>();
106 if (!anIO.IsNull()) {
107 int aWidth = anIO->Width();
108 /// workaround for zero width. Else, there will be a crash
109 if (aWidth == 0) { // width returns of TSolid shape is zero
110 bool isDisplayed = !anIO->GetContext().IsNull();
111 aWidth = AIS_DEFAULT_WIDTH;// default width value
113 setWidth(aDrawer, aWidth);
117 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
123 Events_Error::throwException("An empty AIS presentation: PartSet_OperationPrs");
126 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
127 const Standard_Integer aMode)
129 // the presentation is not used in the selection
132 bool PartSet_OperationPrs::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
134 bool aVisible = false;
135 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
136 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
137 if (aPrs.get() || aResult.get()) {
138 aVisible = theDisplayer->isVisible(theObject);
139 // compsolid is not visualized in the viewer, but should have presentation when all sub solids are
140 // visible. It is useful for highlight presentation where compsolid shape is selectable
141 if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultCompSolid::group()) {
142 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
143 if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
144 bool anAllSubsVisible = aCompsolidResult->numberOfSubs() > 0;
145 for(int i = 0; i < aCompsolidResult->numberOfSubs() && anAllSubsVisible; i++) {
146 anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
148 aVisible = anAllSubsVisible;
153 // check if all results of the feature are visible
154 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
155 std::list<ResultPtr> aResults = aFeature->results();
156 std::list<ResultPtr>::const_iterator aIt;
157 aVisible = !aResults.empty();
158 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
159 aVisible = aVisible && theDisplayer->isVisible(*aIt);
165 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
168 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
169 if (aComposite.get())
170 isSub = aComposite->isSub(theObject);
175 void PartSet_OperationPrs::addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
176 const FeaturePtr& theFeature, ModuleBase_IWorkshop* theWorkshop,
177 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
179 if (theObject.get()) {
180 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
182 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
183 if (aCompsolidResult.get()) {
184 if (aCompsolidResult->numberOfSubs() > 0) {
185 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
186 ResultPtr aSubResult = aCompsolidResult->subResult(i);
187 if (aSubResult.get()) {
189 addValue(aSubResult, aShape, theFeature, theWorkshop, theObjectShapes);
195 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
197 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
198 if (aFeature.get()) {
199 AttributeBooleanPtr aCopyAttr = aFeature->data()->boolean(SketchPlugin_SketchEntity::COPY_ID());
200 if (aCopyAttr.get()) {
201 bool isCopy = aCopyAttr->value();
210 GeomShapePtr aShape = theShape;
212 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
214 aShape = aResult->shape();
216 if (!isSubObject(theObject, theFeature))
217 appendShapeIfVisible(theWorkshop, theObject, aShape, theObjectShapes);
221 void PartSet_OperationPrs::appendShapeIfVisible(ModuleBase_IWorkshop* theWorkshop,
222 const ObjectPtr& theObject,
223 GeomShapePtr theGeomShape,
224 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
226 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
227 if (isVisible(aDisplayer, theObject)) {
228 if (theGeomShape.get()) {
229 if (theObjectShapes.contains(theObject))
230 theObjectShapes[theObject].append(theGeomShape);
232 QList<GeomShapePtr> aShapes;
233 aShapes.append(theGeomShape);
234 theObjectShapes[theObject] = aShapes;
237 #ifdef DEBUG_EMPTY_SHAPE
238 qDebug(QString("Empty shape in result, result: %1")
239 .arg(ModuleBase_Tools::objectInfo(theObject)).toStdString().c_str());
245 void PartSet_OperationPrs::getFeatureShapes(const FeaturePtr& theFeature,
246 ModuleBase_IWorkshop* theWorkshop,
247 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
249 theObjectShapes.clear();
250 if (!theFeature.get())
253 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
255 QList<GeomShapePtr> aShapes;
256 std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
257 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
258 for (; anIt != aLast; anIt++) {
259 AttributePtr anAttribute = *anIt;
260 if (!isSelectionAttribute(anAttribute))
263 if (!aValidators->isCase(theFeature, anAttribute->id()))
264 continue; // this attribute is not participated in the current case
266 std::string anAttrType = anAttribute->attributeType();
268 if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
269 std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
270 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
271 for(int i = 0; i < aCurSelList->size(); i++) {
272 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
273 ResultPtr aResult = aSelAttribute->context();
274 GeomShapePtr aShape = aSelAttribute->value();
275 addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
278 if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
279 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
280 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
281 for (int i = 0; i < aCurSelList->size(); i++) {
282 ObjectPtr anObject = aCurSelList->object(i);
283 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
284 // if a feature is stored in the attribute, we should obtain the feature results
285 // e.g. feature rectangle uses parameters feature lines in the attribute
286 if (aFeature.get()) {
287 getResultShapes(aFeature, theWorkshop, theObjectShapes, false);
290 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
292 GeomShapePtr aShape = aResult->shape();
294 addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
302 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
303 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
304 if (anAttr->isObject()) {
305 anObject = anAttr->object();
308 AttributePtr anAttribute = anAttr->attr();
309 aShape = PartSet_Tools::findShapeBy2DPoint(anAttribute, theWorkshop);
310 // the distance point is not found if the point is selected in the 2nd time
311 // TODO: after debug, this check can be removed
314 anObject = anAttr->attr()->owner();
317 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
318 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
319 anObject = anAttr->context();
320 aShape = anAttr->value();
322 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
323 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
324 anObject = anAttr->value();
326 addValue(anObject, aShape, theFeature, theWorkshop, theObjectShapes);
331 void PartSet_OperationPrs::getResultShapes(const FeaturePtr& theFeature,
332 ModuleBase_IWorkshop* theWorkshop,
333 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes,
334 const bool theListShouldBeCleared)
336 if (theListShouldBeCleared)
337 theObjectShapes.clear();
339 if (!theFeature.get())
342 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
344 std::list<ResultPtr> aFeatureResults = theFeature->results();
345 std::list<ResultPtr>::const_iterator aRIt = aFeatureResults.begin(),
346 aRLast = aFeatureResults.end();
347 for (; aRIt != aRLast; aRIt++) {
348 ResultPtr aResult = *aRIt;
349 GeomShapePtr aGeomShape = aResult->shape();
350 appendShapeIfVisible(theWorkshop, aResult, aGeomShape, theObjectShapes);
354 void PartSet_OperationPrs::getHighlightedShapes(ModuleBase_IWorkshop* theWorkshop,
355 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
357 theObjectShapes.clear();
359 QList<ModuleBase_ViewerPrs> aValues;
360 ModuleBase_IPropertyPanel* aPanel = theWorkshop->propertyPanel();
362 ModuleBase_ModelWidget* aWidget = aPanel->activeWidget();
364 aWidget->getHighlighted(aValues);
368 QList<GeomShapePtr> aShapes;
369 QList<ModuleBase_ViewerPrs>::const_iterator anIIt = aValues.begin(),
370 aILast = aValues.end();
371 for (; anIIt != aILast; anIIt++) {
372 ModuleBase_ViewerPrs aPrs = *anIIt;
373 ObjectPtr anObject = aPrs.object();
375 GeomShapePtr aGeomShape;
377 TopoDS_Shape aShape = aPrs.shape();
378 if (!aShape.IsNull()) {
379 aGeomShape = GeomShapePtr(new GeomAPI_Shape());
380 aGeomShape->setImpl(new TopoDS_Shape(aShape));
383 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
385 aGeomShape = aResult->shape();
388 appendShapeIfVisible(theWorkshop, anObject, aGeomShape, theObjectShapes);
393 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
395 std::string anAttrType = theAttribute->attributeType();
397 return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
398 anAttrType == ModelAPI_AttributeRefList::typeId() ||
399 anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
400 anAttrType == ModelAPI_AttributeSelection::typeId() ||
401 anAttrType == ModelAPI_AttributeReference::typeId();