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 <GeomAPI_IPresentable.h>
30 #include <StdPrs_WFDeflectionShape.hxx>
34 IMPLEMENT_STANDARD_HANDLE(PartSet_OperationPrs, ViewerData_AISShape);
35 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
37 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
38 : ViewerData_AISShape(TopoDS_Shape()), myFeature(FeaturePtr()), myWorkshop(theWorkshop)
40 myShapeColor = ModuleBase_Tools::color("Visualization", "construction_plane_color", "1,1,0");
41 myResultColor = ModuleBase_Tools::color("Visualization", "construction_plane_color", "0,1,0");
44 bool PartSet_OperationPrs::canActivate(const FeaturePtr& theFeature)
46 bool aHasSelectionAttribute = false;
48 std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
49 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
50 for (; anIt != aLast && !aHasSelectionAttribute; anIt++)
51 aHasSelectionAttribute = isSelectionAttribute(*anIt);
53 return aHasSelectionAttribute;
56 void PartSet_OperationPrs::setFeature(const FeaturePtr& theFeature)
58 myFeature = theFeature;
62 /*bool PartSet_OperationPrs::dependOn(const ObjectPtr& theResult)
64 return myFeatureShapes.contains(theResult);
67 void PartSet_OperationPrs::updateShapes()
69 myFeatureShapes.clear();
70 getFeatureShapes(myFeatureShapes);
72 myFeatureResults.clear();
74 myFeatureResults = myFeature->results();
77 bool PartSet_OperationPrs::hasShapes()
79 return !myFeatureShapes.empty() || !myFeatureResults.empty();
82 void PartSet_OperationPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
83 const Handle(Prs3d_Presentation)& thePresentation,
84 const Standard_Integer theMode)
88 // when the feature can not be visualized in the module, the operation preview should not
90 if (!myWorkshop->module()->canDisplayObject(myFeature))
93 Quantity_Color aColor(1., 1., 0., Quantity_TOC_RGB); // yellow
96 thePresentation->Clear();
97 XGUI_Displayer* aDisplayer = workshop()->displayer();
99 // create presentations on the base of the shapes
100 Handle(Prs3d_Drawer) aDrawer = Attributes();
101 QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = myFeatureShapes.begin(),
102 aLast = myFeatureShapes.end();
103 for (; anIt != aLast; anIt++) {
104 ObjectPtr anObject = anIt.key();
105 if (!isVisible(aDisplayer, anObject))
107 QList<GeomShapePtr> aShapes = anIt.value();
108 QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
109 for (; aShIt != aShLast; aShIt++) {
110 GeomShapePtr aGeomShape = *aShIt;
111 if (!aGeomShape.get())
113 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
114 // change deviation coefficient to provide more precise circle
115 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
116 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
120 aColor = Quantity_Color(0., 1., 0., Quantity_TOC_RGB); // green
123 std::list<ResultPtr>::const_iterator aRIt = myFeatureResults.begin(),
124 aRLast = myFeatureResults.end();
125 for (; aRIt != aRLast; aRIt++) {
126 ResultPtr aResult = *aRIt;
127 if (!isVisible(aDisplayer, aResult))
129 GeomShapePtr aGeomShape = aResult->shape();
130 if (!aGeomShape.get())
132 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
133 // change deviation coefficient to provide more precise circle
134 ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
135 StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
139 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
140 const Standard_Integer aMode)
142 // the presentation is not used in the selection
145 bool PartSet_OperationPrs::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
147 bool aVisible = false;
148 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
149 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
150 if (aPrs.get() || aResult.get())
151 aVisible = theDisplayer->isVisible(theObject);
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 addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
176 const FeaturePtr& theFeature,
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 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
185 ResultPtr aSubResult = aCompsolidResult->subResult(i);
186 if (aSubResult.get()) {
188 addValue(aSubResult, aShape, theFeature, theObjectShapes);
196 GeomShapePtr aShape = theShape;
198 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
200 aShape = aResult->shape();
202 if (!isSubObject(theObject, theFeature)) {
203 if (theObjectShapes.contains(theObject))
204 theObjectShapes[theObject].append(aShape);
206 QList<GeomShapePtr> aShapes;
207 aShapes.append(aShape);
208 theObjectShapes[theObject] = aShapes;
214 void PartSet_OperationPrs::getFeatureShapes(QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
216 if (!myFeature.get())
219 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
221 QList<GeomShapePtr> aShapes;
222 std::list<AttributePtr> anAttributes = myFeature->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(myFeature, 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, myFeature, 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++) {
249 addValue(aCurSelList->object(i), aShape, myFeature, theObjectShapes);
255 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
256 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
257 if (anAttr->isObject()) {
258 anObject = anAttr->object();
261 aShape = PartSet_Tools::findShapeBy2DPoint(anAttr, myWorkshop);
262 // the distance point is not found if the point is selected in the 2nd time
263 // TODO: after debug, this check can be removed
266 anObject = anAttr->attr()->owner();
269 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
270 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
271 anObject = anAttr->context();
272 aShape = anAttr->value();
274 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
275 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
276 anObject = anAttr->value();
278 addValue(anObject, aShape, myFeature, theObjectShapes);
283 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
285 std::string anAttrType = theAttribute->attributeType();
287 return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
288 anAttrType == ModelAPI_AttributeRefList::typeId() ||
289 anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
290 anAttrType == ModelAPI_AttributeSelection::typeId() ||
291 anAttrType == ModelAPI_AttributeReference::typeId();
294 XGUI_Workshop* PartSet_OperationPrs::workshop() const
296 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
297 return aConnector->workshop();