Salome HOME
ModuleBase_WidgetShapeSelector correction to process AttrRefAtt like MultiSelector...
[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 void PartSet_WidgetShapeSelector::getGeomSelection(const ModuleBase_ViewerPrs& thePrs,
57                                                    ObjectPtr& theObject,
58                                                    GeomShapePtr& theShape)
59 {
60   ModuleBase_WidgetShapeSelector::getGeomSelection(thePrs, theObject, theShape);
61
62   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theObject);
63   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
64           std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
65   // there is no a sketch feature is selected, but the shape exists, try to create an exernal object
66   // TODO: unite with the same functionality in PartSet_WidgetShapeSelector
67   if (aSPFeature.get() == NULL && myExternalObjectMgr->useExternal()) {
68     GeomShapePtr aShape = theShape;
69     if (!aShape.get()) {
70       ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
71       if (aResult.get())
72         aShape = aResult->shape();
73     }
74     if (aShape.get() != NULL && !aShape->isNull())
75       theObject = myExternalObjectMgr->externalObject(theObject, aShape, sketch(), myIsInValidate);
76   }
77 }
78
79 //********************************************************************
80 GeomShapePtr PartSet_WidgetShapeSelector::getShape() const
81 {
82   // an empty shape by default
83   DataPtr aData = myFeature->data();
84   AttributePtr anAttribute = aData->attribute(attributeID());
85   GeomShapePtr aShape = PartSet_Tools::findShapeBy2DPoint(anAttribute, myWorkshop);
86
87   if (!aShape.get())
88     aShape = ModuleBase_WidgetShapeSelector::getShape();
89   return aShape;
90 }
91
92 //********************************************************************
93 void PartSet_WidgetShapeSelector::restoreAttributeValue(const bool theValid)
94 {
95   ModuleBase_WidgetShapeSelector::restoreAttributeValue(theValid);
96   myExternalObjectMgr->removeExternal(sketch(), myFeature, myWorkshop, true);
97 }
98