Salome HOME
Issue #672 - Crash when distance constraint
[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 //********************************************************************
46 bool PartSet_WidgetShapeSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
47 {
48   bool aValid = ModuleBase_WidgetShapeSelector::isValidSelectionCustom(thePrs);
49   if (aValid) {
50     ObjectPtr anObject = myWorkshop->selection()->getResult(thePrs);
51     aValid = myExternalObjectMgr->isValidObject(anObject);
52   }
53   return aValid;
54 }
55
56 //********************************************************************
57 void PartSet_WidgetShapeSelector::setObject(ObjectPtr theSelectedObject, GeomShapePtr theShape)
58 {
59   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theSelectedObject);
60   // Do check using of external feature
61   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
62           std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
63   // Processing of sketch object
64   if (aSPFeature.get() != NULL && theShape.get())
65     setPointAttribute(theSelectedObject, theShape);
66   else
67     ModuleBase_WidgetShapeSelector::setObject(theSelectedObject, theShape);
68 }
69
70 void PartSet_WidgetShapeSelector::getGeomSelection(const ModuleBase_ViewerPrs& thePrs,
71                                                    ObjectPtr& theObject,
72                                                    GeomShapePtr& theShape)
73 {
74   ModuleBase_WidgetShapeSelector::getGeomSelection(thePrs, theObject, theShape);
75
76   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theObject);
77   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
78           std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
79   // there is no a sketch feature is selected, but the shape exists, try to create an exernal object
80   // TODO: unite with the same functionality in PartSet_WidgetShapeSelector
81   if (aSPFeature.get() == NULL && myExternalObjectMgr->useExternal()) {
82     GeomShapePtr aShape = theShape;
83     if (!aShape.get()) {
84       ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
85       if (aResult.get())
86         aShape = aResult->shape();
87     }
88     if (aShape.get() != NULL && !aShape->isNull())
89       theObject = myExternalObjectMgr->externalObject(theObject, aShape, sketch());
90   }
91 }
92
93 //********************************************************************
94 GeomShapePtr PartSet_WidgetShapeSelector::getShape() const
95 {
96   // an empty shape by default
97   GeomShapePtr aShape;
98
99   // 1. find an attribute value in attribute reference attribute value
100   DataPtr aData = myFeature->data();
101   AttributePtr aAttr = aData->attribute(attributeID());
102   AttributeRefAttrPtr aRefAttr = 
103     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
104   if (aRefAttr) {
105     if (!aRefAttr->isObject()) {
106       AttributePtr anAttribute = aRefAttr->attr();
107       if (anAttribute.get()) {
108         XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
109         XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
110
111         // 2. find visualized vertices of the attribute and if the attribute of the vertex is
112         // the same, return it
113         FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(anAttribute->owner());
114         // 2.1 get visualized results of the feature
115         const std::list<ResultPtr>& aResList = anAttributeFeature->results();
116         std::list<ResultPtr>::const_iterator anIt = aResList.begin(), aLast = aResList.end();
117         for (; anIt != aLast; anIt++) {
118           AISObjectPtr aAISObj = aDisplayer->getAISObject(*anIt);
119           if (aAISObj.get() != NULL) {
120             Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
121             // 2.2 find selected owners of a visualizedd object
122             SelectMgr_IndexedMapOfOwner aSelectedOwners;  
123             aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
124             for  (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
125               Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
126               if (!anOwner.IsNull()) {
127                 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
128                 if (!aBRepOwner.IsNull() && aBRepOwner->HasShape()) {
129                   const TopoDS_Shape& aBRepShape = aBRepOwner->Shape();
130                   if (aBRepShape.ShapeType() == TopAbs_VERTEX) {
131                     // 2.3 if the owner is vertex and an attribute of the vertex is equal to the initial
132                     // attribute, returns the shape
133                     AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(anAttributeFeature,
134                                                                                   aBRepShape, sketch());
135                     if (aPntAttr.get() != NULL && aPntAttr == anAttribute) {
136                       aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
137                       aShape->setImpl(new TopoDS_Shape(aBRepShape));
138                       break;
139                     }
140                   }
141                 }
142               }
143             }
144           }
145         }
146       }
147     }
148   }
149   if (!aShape.get())
150     aShape = ModuleBase_WidgetShapeSelector::getShape();
151   return aShape;
152 }
153
154 //********************************************************************
155 void PartSet_WidgetShapeSelector::restoreAttributeValue(const bool theValid)
156 {
157   ModuleBase_WidgetShapeSelector::restoreAttributeValue(theValid);
158   myExternalObjectMgr->removeExternal(sketch(), myFeature);
159 }
160
161 //********************************************************************
162 void PartSet_WidgetShapeSelector::setPointAttribute(ObjectPtr theSelectedObject, GeomShapePtr theShape)
163 {
164   DataPtr aData = myFeature->data();
165   AttributePtr aAttr = aData->attribute(attributeID());
166   AttributeRefAttrPtr aRefAttr = 
167     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
168   if (aRefAttr) {
169     // it is possible that the point feature is selected. It should be used itself
170     // instead of searching an attribute for the shape
171     bool aShapeIsResult = false;
172     /*ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
173     if (aResult.get() != NULL) {
174       GeomShapePtr aShapePtr = aResult->shape();
175       // it is important to call isEqual of the shape of result.
176       // It is a GeomAPI_Vertex shape for the point. The shape of the parameter is 
177       // GeomAPI_Shape. It is important to use the realization of the isEqual method from
178       // GeomAPI_Vertex class
179       aShapeIsResult = aShapePtr.get() != NULL && aShapePtr->isEqual(theShape);
180     }*/
181
182     AttributePtr aPntAttr;
183     if (!aShapeIsResult) {
184       TopoDS_Shape aTDSShape = theShape->impl<TopoDS_Shape>();
185       aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theSelectedObject, aTDSShape, mySketch);
186     }
187     // this is an alternative, whether the attribute should be set or object in the attribute
188     // the first check is the attribute because the object already exist
189     // the object is set only if there is no selected attribute
190     // test case is - preselection for distance operation, which contains two points selected on lines
191     if (aPntAttr)
192       aRefAttr->setAttr(aPntAttr);
193     else
194       aRefAttr->setObject(theSelectedObject);
195   }
196 }