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.get() ? aGeomShape->impl<TopoDS_Shape>() : 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 isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
135 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
136 if (aComposite.get())
137 isSub = aComposite->isSub(theObject);
142 void PartSet_OperationPrs::addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
143 const FeaturePtr& theFeature, ModuleBase_IWorkshop* theWorkshop,
144 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
146 if (theObject.get()) {
147 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
149 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
150 if (aCompsolidResult.get()) {
151 if (aCompsolidResult->numberOfSubs() > 0) {
152 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
153 ResultPtr aSubResult = aCompsolidResult->subResult(i);
154 if (aSubResult.get()) {
156 addValue(aSubResult, aShape, theFeature, theWorkshop, theObjectShapes);
162 #ifdef DEBUG_HIDE_COPY_ATTRIBUTE
164 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
165 if (aFeature.get()) {
166 AttributeBooleanPtr aCopyAttr = aFeature->data()->boolean(SketchPlugin_SketchEntity::COPY_ID());
167 if (aCopyAttr.get()) {
168 bool isCopy = aCopyAttr->value();
177 GeomShapePtr aShape = theShape;
179 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
181 aShape = aResult->shape();
183 if (!isSubObject(theObject, theFeature))
184 appendShapeIfVisible(theWorkshop, theObject, aShape, theObjectShapes);
188 void PartSet_OperationPrs::appendShapeIfVisible(ModuleBase_IWorkshop* theWorkshop,
189 const ObjectPtr& theObject,
190 GeomShapePtr theGeomShape,
191 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
193 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
194 if (XGUI_Displayer::isVisible(aDisplayer, theObject)) {
195 if (theGeomShape.get()) {
196 if (theObjectShapes.contains(theObject))
197 theObjectShapes[theObject].append(theGeomShape);
199 QList<GeomShapePtr> aShapes;
200 aShapes.append(theGeomShape);
201 theObjectShapes[theObject] = aShapes;
204 #ifdef DEBUG_EMPTY_SHAPE
205 qDebug(QString("Empty shape in result, result: %1")
206 .arg(ModuleBase_Tools::objectInfo(theObject)).toStdString().c_str());
212 void PartSet_OperationPrs::getFeatureShapes(const FeaturePtr& theFeature,
213 ModuleBase_IWorkshop* theWorkshop,
214 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
216 theObjectShapes.clear();
217 if (!theFeature.get())
220 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
222 QList<GeomShapePtr> aShapes;
223 std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
224 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
225 for (; anIt != aLast; anIt++) {
226 AttributePtr anAttribute = *anIt;
227 if (!isSelectionAttribute(anAttribute))
230 if (!aValidators->isCase(theFeature, anAttribute->id()))
231 continue; // this attribute is not participated in the current case
233 std::string anAttrType = anAttribute->attributeType();
235 if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
236 std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
237 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
238 for(int i = 0; i < aCurSelList->size(); i++) {
239 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
240 ResultPtr aResult = aSelAttribute->context();
241 GeomShapePtr aShape = aSelAttribute->value();
242 addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
245 if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
246 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
247 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
248 for (int i = 0; i < aCurSelList->size(); i++) {
249 ObjectPtr anObject = aCurSelList->object(i);
250 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
251 // if a feature is stored in the attribute, we should obtain the feature results
252 // e.g. feature rectangle uses parameters feature lines in the attribute
253 if (aFeature.get()) {
254 getResultShapes(aFeature, theWorkshop, theObjectShapes, false);
257 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
259 GeomShapePtr aShape = aResult->shape();
261 addValue(aResult, aShape, theFeature, theWorkshop, theObjectShapes);
269 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
270 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
271 if (anAttr->isObject()) {
272 anObject = anAttr->object();
275 AttributePtr anAttribute = anAttr->attr();
276 aShape = PartSet_Tools::findShapeBy2DPoint(anAttribute, theWorkshop);
277 // the distance point is not found if the point is selected in the 2nd time
278 // TODO: after debug, this check can be removed
281 anObject = anAttr->attr()->owner();
284 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
285 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
286 anObject = anAttr->context();
287 aShape = anAttr->value();
289 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
290 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
291 anObject = anAttr->value();
293 addValue(anObject, aShape, theFeature, theWorkshop, theObjectShapes);
298 void PartSet_OperationPrs::getResultShapes(const FeaturePtr& theFeature,
299 ModuleBase_IWorkshop* theWorkshop,
300 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes,
301 const bool theListShouldBeCleared)
303 if (theListShouldBeCleared)
304 theObjectShapes.clear();
306 if (!theFeature.get())
309 XGUI_Displayer* aDisplayer = XGUI_Tools::workshop(theWorkshop)->displayer();
311 std::list<ResultPtr> aFeatureResults = theFeature->results();
312 std::list<ResultPtr>::const_iterator aRIt = aFeatureResults.begin(),
313 aRLast = aFeatureResults.end();
314 for (; aRIt != aRLast; aRIt++) {
315 ResultPtr aResult = *aRIt;
316 GeomShapePtr aGeomShape = aResult->shape();
317 appendShapeIfVisible(theWorkshop, aResult, aGeomShape, theObjectShapes);
321 void PartSet_OperationPrs::getHighlightedShapes(ModuleBase_IWorkshop* theWorkshop,
322 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
324 theObjectShapes.clear();
326 QList<ModuleBase_ViewerPrs> aValues;
327 ModuleBase_IPropertyPanel* aPanel = theWorkshop->propertyPanel();
329 ModuleBase_ModelWidget* aWidget = aPanel->activeWidget();
331 aWidget->getHighlighted(aValues);
335 QList<GeomShapePtr> aShapes;
336 QList<ModuleBase_ViewerPrs>::const_iterator anIIt = aValues.begin(),
337 aILast = aValues.end();
338 for (; anIIt != aILast; anIIt++) {
339 ModuleBase_ViewerPrs aPrs = *anIIt;
340 ObjectPtr anObject = aPrs.object();
342 GeomShapePtr aGeomShape = aPrs.shape();
343 if (!aGeomShape.get() || aGeomShape->isNull()) {
344 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
346 aGeomShape = aResult->shape();
349 appendShapeIfVisible(theWorkshop, anObject, aGeomShape, theObjectShapes);
354 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
356 std::string anAttrType = theAttribute->attributeType();
358 return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
359 anAttrType == ModelAPI_AttributeRefList::typeId() ||
360 anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
361 anAttrType == ModelAPI_AttributeSelection::typeId() ||
362 anAttrType == ModelAPI_AttributeReference::typeId();