Salome HOME
A fix for the next case:
[modules/shaper.git] / src / PartSet / PartSet_WidgetShapeSelector.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_WidgetShapeSelector.cpp
4 // Created:     27 Nov 2014
5 // Author:      Vitaly Smetannikov
6
7 #include "PartSet_WidgetShapeSelector.h"
8
9 #include <ModelAPI_AttributeRefAttr.h>
10 #include <ModelAPI_Session.h>
11 #include <ModelAPI_Validator.h>
12
13 #include <ModuleBase_Definitions.h>
14 #include <Config_WidgetAPI.h>
15
16 #include <PartSet_Tools.h>
17 #include <PartSet_ExternalObjectsMgr.h>
18 #include <SketchPlugin_Feature.h>
19
20 #include <SketchPlugin_ConstraintRigid.h>
21
22 #include <XGUI_Workshop.h>
23
24 #include <XGUI_ModuleConnector.h>
25 #include <XGUI_Displayer.h>
26 #include <XGUI_SelectionMgr.h>
27 #include <XGUI_Selection.h>
28 #include <SelectMgr_IndexedMapOfOwner.hxx>
29 #include <StdSelect_BRepOwner.hxx>
30
31 PartSet_WidgetShapeSelector::PartSet_WidgetShapeSelector(QWidget* theParent,
32                                                          ModuleBase_IWorkshop* theWorkshop,
33                                                          const Config_WidgetAPI* theData,
34                                                          const std::string& theParentId)
35 : ModuleBase_WidgetShapeSelector(theParent, theWorkshop, theData, theParentId)
36 {
37   myExternalObjectMgr = new PartSet_ExternalObjectsMgr(theData->getProperty("use_external"), true);
38 }
39
40 PartSet_WidgetShapeSelector::~PartSet_WidgetShapeSelector()
41 {
42   delete myExternalObjectMgr;
43 }
44
45 bool PartSet_WidgetShapeSelector::setObject(ObjectPtr theSelectedObject, GeomShapePtr theShape)
46 {
47   ObjectPtr aSelectedObject = theSelectedObject;
48   //GeomShapePtr aShape = theShape;
49
50   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aSelectedObject);
51   if (aSelectedFeature == myFeature)  // In order to avoid selection of the same object
52     return false;
53   // Do check using of external feature
54   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
55           std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
56
57   // Do check that we can use external feature
58   if ((aSPFeature.get() != NULL) && aSPFeature->isExternal() && (!myExternalObjectMgr->useExternal()))
59     return false;
60
61   if (aSPFeature.get() == NULL && theShape.get() != NULL && !theShape->isNull() && myExternalObjectMgr->useExternal()) {
62     aSelectedObject = myExternalObjectMgr->externalObject(theSelectedObject, theShape, sketch());
63   } else {
64     // Processing of sketch object
65     if (theShape.get()) {
66       setPointAttribute(theSelectedObject, theShape);
67       return true;
68     }
69   }
70   return ModuleBase_WidgetShapeSelector::setObject(aSelectedObject, theShape);
71 }
72
73 //********************************************************************
74 GeomShapePtr PartSet_WidgetShapeSelector::getShape() const
75 {
76   // an empty shape by default
77   GeomShapePtr aShape;
78
79   // 1. find an attribute value in attribute reference attribute value
80   DataPtr aData = myFeature->data();
81   AttributePtr aAttr = aData->attribute(attributeID());
82   AttributeRefAttrPtr aRefAttr = 
83     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
84   if (aRefAttr) {
85     if (!aRefAttr->isObject()) {
86       AttributePtr anAttribute = aRefAttr->attr();
87       if (anAttribute.get()) {
88         XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
89         XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
90
91         // 2. find visualized vertices of the attribute and if the attribute of the vertex is
92         // the same, return it
93         FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(anAttribute->owner());
94         // 2.1 get visualized results of the feature
95         const std::list<ResultPtr>& aResList = anAttributeFeature->results();
96         std::list<ResultPtr>::const_iterator anIt = aResList.begin(), aLast = aResList.end();
97         for (; anIt != aLast; anIt++) {
98           AISObjectPtr aAISObj = aDisplayer->getAISObject(*anIt);
99           if (aAISObj.get() != NULL) {
100             Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
101             // 2.2 find selected owners of a visualizedd object
102             SelectMgr_IndexedMapOfOwner aSelectedOwners;  
103             aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
104             for  (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
105               Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
106               if (!anOwner.IsNull()) {
107                 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
108                 if (!aBRepOwner.IsNull() && aBRepOwner->HasShape()) {
109                   const TopoDS_Shape& aBRepShape = aBRepOwner->Shape();
110                   if (aBRepShape.ShapeType() == TopAbs_VERTEX) {
111                     // 2.3 if the owner is vertex and an attribute of the vertex is equal to the initial
112                     // attribute, returns the shape
113                     AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(anAttributeFeature,
114                                                                                   aBRepShape, sketch());
115                     if (aPntAttr.get() != NULL && aPntAttr == anAttribute) {
116                       aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
117                       aShape->setImpl(new TopoDS_Shape(aBRepShape));
118                       break;
119                     }
120                   }
121                 }
122               }
123             }
124           }
125         }
126       }
127     }
128   }
129   if (!aShape.get())
130     aShape = ModuleBase_WidgetShapeSelector::getShape();
131   return aShape;
132 }
133
134 //********************************************************************
135 void PartSet_WidgetShapeSelector::restoreAttributeValue(const bool theValid)
136 {
137   ModuleBase_WidgetShapeSelector::restoreAttributeValue(theValid);
138   myExternalObjectMgr->removeExternal(sketch(), myFeature);
139 }
140
141 //********************************************************************
142 void PartSet_WidgetShapeSelector::setPointAttribute(ObjectPtr theSelectedObject, GeomShapePtr theShape)
143 {
144   DataPtr aData = myFeature->data();
145   AttributePtr aAttr = aData->attribute(attributeID());
146   AttributeRefAttrPtr aRefAttr = 
147     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
148   if (aRefAttr) {
149     // it is possible that the point feature is selected. It should be used itself
150     // instead of searching an attribute for the shape
151     bool aShapeIsResult = false;
152     /*ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
153     if (aResult.get() != NULL) {
154       GeomShapePtr aShapePtr = aResult->shape();
155       // it is important to call isEqual of the shape of result.
156       // It is a GeomAPI_Vertex shape for the point. The shape of the parameter is 
157       // GeomAPI_Shape. It is important to use the realization of the isEqual method from
158       // GeomAPI_Vertex class
159       aShapeIsResult = aShapePtr.get() != NULL && aShapePtr->isEqual(theShape);
160     }*/
161
162     AttributePtr aPntAttr;
163     if (!aShapeIsResult) {
164       TopoDS_Shape aTDSShape = theShape->impl<TopoDS_Shape>();
165       aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theSelectedObject, aTDSShape, mySketch);
166     }
167     // this is an alternative, whether the attribute should be set or object in the attribute
168     // the first check is the attribute because the object already exist
169     // the object is set only if there is no selected attribute
170     // test case is - preselection for distance operation, which contains two points selected on lines
171     if (aPntAttr)
172       aRefAttr->setAttr(aPntAttr);
173     else
174       aRefAttr->setObject(theSelectedObject);
175   }
176 }