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 void PartSet_OperationPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
80 const Handle(Prs3d_Presentation)& thePresentation,
81 const Standard_Integer theMode)
83 // when the feature can not be visualized in the module, the operation preview should not
85 if (!myWorkshop->module()->canDisplayObject(myFeature))
88 Quantity_Color aColor(1., 1., 0., Quantity_TOC_RGB); // yellow
91 thePresentation->Clear();
92 XGUI_Displayer* aDisplayer = workshop()->displayer();
94 // create presentations on the base of the shapes
95 Handle(Prs3d_Drawer) aDrawer = Attributes();
96 QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = myFeatureShapes.begin(),
97 aLast = myFeatureShapes.end();
98 for (; anIt != aLast; anIt++) {
99 ObjectPtr anObject = anIt.key();
100 if (!isVisible(aDisplayer, anObject))
102 QList<GeomShapePtr> aShapes = anIt.value();
103 QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
104 for (; aShIt != aShLast; aShIt++) {
105 GeomShapePtr aGeomShape = *aShIt;
106 if (!aGeomShape.get())
108 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
109 // change deviation coefficient to provide more precise circle
110 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
111 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
115 aColor = Quantity_Color(0., 1., 0., Quantity_TOC_RGB); // green
118 std::list<ResultPtr>::const_iterator aRIt = myFeatureResults.begin(),
119 aRLast = myFeatureResults.end();
120 for (; aRIt != aRLast; aRIt++) {
121 ResultPtr aResult = *aRIt;
122 if (!isVisible(aDisplayer, aResult))
124 GeomShapePtr aGeomShape = aResult->shape();
125 if (!aGeomShape.get())
127 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
128 // change deviation coefficient to provide more precise circle
129 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
130 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
134 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
135 const Standard_Integer aMode)
137 // the presentation is not used in the selection
140 bool PartSet_OperationPrs::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
142 bool aVisible = false;
143 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
144 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
145 if (aPrs.get() || aResult.get())
146 aVisible = theDisplayer->isVisible(theObject);
148 // check if all results of the feature are visible
149 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
150 std::list<ResultPtr> aResults = aFeature->results();
151 std::list<ResultPtr>::const_iterator aIt;
152 aVisible = !aResults.empty();
153 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
154 aVisible = aVisible && theDisplayer->isVisible(*aIt);
160 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
163 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
164 if (aComposite.get())
165 isSub = aComposite->isSub(theObject);
170 void addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
171 const FeaturePtr& theFeature,
172 QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
174 if (theObject.get()) {
175 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
177 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
178 if (aCompsolidResult.get()) {
179 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
180 ResultPtr aSubResult = aCompsolidResult->subResult(i);
181 if (aSubResult.get()) {
183 addValue(aSubResult, aShape, theFeature, theObjectShapes);
191 GeomShapePtr aShape = theShape;
193 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
195 aShape = aResult->shape();
197 if (!isSubObject(theObject, theFeature)) {
198 if (theObjectShapes.contains(theObject))
199 theObjectShapes[theObject].append(aShape);
201 QList<GeomShapePtr> aShapes;
202 aShapes.append(aShape);
203 theObjectShapes[theObject] = aShapes;
209 void PartSet_OperationPrs::getFeatureShapes(QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
211 if (!myFeature.get())
214 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
216 QList<GeomShapePtr> aShapes;
217 std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
218 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
219 for (; anIt != aLast; anIt++) {
220 AttributePtr anAttribute = *anIt;
221 if (!isSelectionAttribute(anAttribute))
224 if (!aValidators->isCase(myFeature, anAttribute->id()))
225 continue; // this attribute is not participated in the current case
227 std::string anAttrType = anAttribute->attributeType();
229 if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
230 std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
231 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
232 for(int i = 0; i < aCurSelList->size(); i++) {
233 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
234 ResultPtr aResult = aSelAttribute->context();
235 GeomShapePtr aShape = aSelAttribute->value();
236 addValue(aResult, aShape, myFeature, theObjectShapes);
239 if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
240 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
241 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
242 for (int i = 0; i < aCurSelList->size(); i++) {
244 addValue(aCurSelList->object(i), aShape, myFeature, theObjectShapes);
250 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
251 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
252 if (anAttr->isObject()) {
253 anObject = anAttr->object();
256 aShape = PartSet_Tools::findShapeBy2DPoint(anAttr, myWorkshop);
257 // the distance point is not found if the point is selected in the 2nd time
258 // TODO: after debug, this check can be removed
261 anObject = anAttr->attr()->owner();
264 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
265 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
266 anObject = anAttr->context();
267 aShape = anAttr->value();
269 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
270 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
271 anObject = anAttr->value();
273 addValue(anObject, aShape, myFeature, theObjectShapes);
278 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
280 std::string anAttrType = theAttribute->attributeType();
282 return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
283 anAttrType == ModelAPI_AttributeRefList::typeId() ||
284 anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
285 anAttrType == ModelAPI_AttributeSelection::typeId() ||
286 anAttrType == ModelAPI_AttributeReference::typeId();
289 XGUI_Workshop* PartSet_OperationPrs::workshop() const
291 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
292 return aConnector->workshop();