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"
14 #include "ModuleBase_Tools.h"
15 #include "ModuleBase_IModule.h"
17 #include <ModelAPI_Result.h>
18 #include <ModelAPI_Attribute.h>
19 #include <ModelAPI_AttributeRefAttr.h>
20 #include <ModelAPI_AttributeReference.h>
21 #include <ModelAPI_AttributeSelection.h>
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_AttributeRefList.h>
24 #include <ModelAPI_Validator.h>
25 #include <ModelAPI_Session.h>
26 #include <ModelAPI_ResultCompSolid.h>
28 #include <GeomValidators_Tools.h>
30 #include <GeomAPI_IPresentable.h>
32 #include <StdPrs_WFDeflectionShape.hxx>
36 IMPLEMENT_STANDARD_HANDLE(PartSet_OperationPrs, ViewerData_AISShape);
37 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
39 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
40 : ViewerData_AISShape(TopoDS_Shape()), myFeature(FeaturePtr()), myWorkshop(theWorkshop)
42 myShapeColor = ModuleBase_Tools::color("Visualization", "construction_plane_color", "1,1,0");
43 myResultColor = ModuleBase_Tools::color("Visualization", "construction_plane_color", "0,1,0");
46 bool PartSet_OperationPrs::canActivate(const FeaturePtr& theFeature)
48 bool aHasSelectionAttribute = false;
50 std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
51 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
52 for (; anIt != aLast && !aHasSelectionAttribute; anIt++)
53 aHasSelectionAttribute = isSelectionAttribute(*anIt);
55 return aHasSelectionAttribute;
58 void PartSet_OperationPrs::setFeature(const FeaturePtr& theFeature)
60 myFeature = theFeature;
64 /*bool PartSet_OperationPrs::dependOn(const ObjectPtr& theResult)
66 return myFeatureShapes.contains(theResult);
69 void PartSet_OperationPrs::updateShapes()
71 myFeatureShapes.clear();
72 getFeatureShapes(myFeatureShapes);
74 myFeatureResults.clear();
76 myFeatureResults = myFeature->results();
79 bool PartSet_OperationPrs::hasShapes()
81 return !myFeatureShapes.empty() || !myFeatureResults.empty();
84 void PartSet_OperationPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
85 const Handle(Prs3d_Presentation)& thePresentation,
86 const Standard_Integer theMode)
90 // when the feature can not be visualized in the module, the operation preview should not
92 if (!myWorkshop->module()->canDisplayObject(myFeature))
95 Quantity_Color aColor(1., 1., 0., Quantity_TOC_RGB); // yellow
98 thePresentation->Clear();
99 XGUI_Displayer* aDisplayer = workshop()->displayer();
101 // create presentations on the base of the shapes
102 Handle(Prs3d_Drawer) aDrawer = Attributes();
103 QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = myFeatureShapes.begin(),
104 aLast = myFeatureShapes.end();
105 for (; anIt != aLast; anIt++) {
106 ObjectPtr anObject = anIt.key();
107 if (!isVisible(aDisplayer, anObject))
109 QList<GeomShapePtr> aShapes = anIt.value();
110 QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
111 for (; aShIt != aShLast; aShIt++) {
112 GeomShapePtr aGeomShape = *aShIt;
113 if (!aGeomShape.get())
115 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
116 // change deviation coefficient to provide more precise circle
117 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
118 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
122 aColor = Quantity_Color(0., 1., 0., Quantity_TOC_RGB); // green
125 std::list<ResultPtr>::const_iterator aRIt = myFeatureResults.begin(),
126 aRLast = myFeatureResults.end();
127 for (; aRIt != aRLast; aRIt++) {
128 ResultPtr aResult = *aRIt;
129 if (!isVisible(aDisplayer, aResult))
131 GeomShapePtr aGeomShape = aResult->shape();
132 if (!aGeomShape.get())
134 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
135 // change deviation coefficient to provide more precise circle
136 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
137 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
141 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
142 const Standard_Integer aMode)
144 // the presentation is not used in the selection
147 bool PartSet_OperationPrs::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
149 bool aVisible = false;
150 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
151 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
152 if (aPrs.get() || aResult.get())
153 aVisible = theDisplayer->isVisible(theObject);
155 // check if all results of the feature are visible
156 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
157 std::list<ResultPtr> aResults = aFeature->results();
158 std::list<ResultPtr>::const_iterator aIt;
159 aVisible = !aResults.empty();
160 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
161 aVisible = aVisible && theDisplayer->isVisible(*aIt);
167 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
170 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
171 if (aComposite.get())
172 isSub = aComposite->isSub(theObject);
177 void addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
178 const FeaturePtr& theFeature,
179 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
181 if (theObject.get()) {
182 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
184 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
185 if (aCompsolidResult.get()) {
186 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
187 ResultPtr aSubResult = aCompsolidResult->subResult(i);
188 if (aSubResult.get()) {
190 addValue(aSubResult, aShape, theFeature, theObjectShapes);
198 GeomShapePtr aShape = theShape;
200 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
202 aShape = aResult->shape();
204 if (!isSubObject(theObject, theFeature)) {
205 if (theObjectShapes.contains(theObject))
206 theObjectShapes[theObject].append(aShape);
208 QList<GeomShapePtr> aShapes;
209 aShapes.append(aShape);
210 theObjectShapes[theObject] = aShapes;
216 void PartSet_OperationPrs::getFeatureShapes(QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
218 if (!myFeature.get())
221 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
223 QList<GeomShapePtr> aShapes;
224 std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
225 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
226 for (; anIt != aLast; anIt++) {
227 AttributePtr anAttribute = *anIt;
228 if (!isSelectionAttribute(anAttribute))
231 if (!aValidators->isCase(myFeature, anAttribute->id()))
232 continue; // this attribute is not participated in the current case
234 std::string anAttrType = anAttribute->attributeType();
236 if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
237 std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
238 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
239 for(int i = 0; i < aCurSelList->size(); i++) {
240 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
241 ResultPtr aResult = aSelAttribute->context();
242 GeomShapePtr aShape = aSelAttribute->value();
243 addValue(aResult, aShape, myFeature, theObjectShapes);
246 if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
247 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
248 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
249 for (int i = 0; i < aCurSelList->size(); i++) {
251 addValue(aCurSelList->object(i), aShape, myFeature, theObjectShapes);
257 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
258 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
259 if (anAttr->isObject()) {
260 anObject = anAttr->object();
263 aShape = PartSet_Tools::findShapeBy2DPoint(anAttr, myWorkshop);
264 // the distance point is not found if the point is selected in the 2nd time
265 // TODO: after debug, this check can be removed
268 anObject = anAttr->attr()->owner();
271 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
272 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
273 anObject = anAttr->context();
274 aShape = anAttr->value();
276 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
277 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
278 anObject = anAttr->value();
280 addValue(anObject, aShape, myFeature, theObjectShapes);
285 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
287 std::string anAttrType = theAttribute->attributeType();
289 return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
290 anAttrType == ModelAPI_AttributeRefList::typeId() ||
291 anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
292 anAttrType == ModelAPI_AttributeSelection::typeId() ||
293 anAttrType == ModelAPI_AttributeReference::typeId();
296 XGUI_Workshop* PartSet_OperationPrs::workshop() const
298 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
299 return aConnector->workshop();