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