Salome HOME
Fixed crash when attribute External in feature Projection changed.
[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 #include "PartSet_Module.h"
9 #include "PartSet_SketcherMgr.h"
10
11 #include <ModelAPI_AttributeRefAttr.h>
12 #include <ModelAPI_Session.h>
13 #include <ModelAPI_Validator.h>
14
15 #include <ModuleBase_Definitions.h>
16 #include <ModuleBase_ViewerPrs.h>
17
18 #include <Config_WidgetAPI.h>
19
20 #include <PartSet_ExternalObjectsMgr.h>
21 #include <SketchPlugin_Feature.h>
22
23 #include <XGUI_Workshop.h>
24
25 #include <XGUI_ModuleConnector.h>
26 #include <XGUI_Displayer.h>
27 #include <XGUI_SelectionMgr.h>
28 #include <XGUI_Selection.h>
29 #include <XGUI_Tools.h>
30
31 PartSet_WidgetShapeSelector::PartSet_WidgetShapeSelector(QWidget* theParent,
32                                                          ModuleBase_IWorkshop* theWorkshop,
33                                                          const Config_WidgetAPI* theData)
34 : ModuleBase_WidgetShapeSelector(theParent, theWorkshop, theData)
35 {
36   myUseSketchPlane = theData->getBooleanAttribute("use_sketch_plane", true);
37   myExternalObjectMgr = new PartSet_ExternalObjectsMgr(theData->getProperty("use_external"),
38                                          theData->getProperty("can_create_external"), true);
39 }
40
41 PartSet_WidgetShapeSelector::~PartSet_WidgetShapeSelector()
42 {
43   delete myExternalObjectMgr;
44 }
45
46 //********************************************************************
47 bool PartSet_WidgetShapeSelector::activateSelectionAndFilters(bool toActivate)
48 {
49   bool aHasSelectionFilter = ModuleBase_WidgetShapeSelector::activateSelectionAndFilters
50                                                                            (toActivate);
51   if (!myUseSketchPlane) {
52     XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
53     PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(aWorkshop->module());
54     bool isUsePlaneFilterOnly = !toActivate;
55     aModule->sketchMgr()->activatePlaneFilter(isUsePlaneFilterOnly);
56   }
57   return aHasSelectionFilter;
58 }
59
60 //********************************************************************
61 bool PartSet_WidgetShapeSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
62 {
63   bool aValid = ModuleBase_WidgetShapeSelector::isValidSelectionCustom(thePrs);
64   if (aValid) {
65     ObjectPtr anObject = myWorkshop->selection()->getResult(thePrs);
66     aValid = myExternalObjectMgr->isValidObject(anObject);
67   }
68   return aValid;
69 }
70
71 void PartSet_WidgetShapeSelector::getGeomSelection(const ModuleBase_ViewerPrsPtr& thePrs,
72                                                    ObjectPtr& theObject,
73                                                    GeomShapePtr& theShape)
74 {
75   ModuleBase_WidgetShapeSelector::getGeomSelection(thePrs, theObject, theShape);
76
77   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theObject);
78   std::shared_ptr<SketchPlugin_Feature> aSPFeature =
79           std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
80   // there is no a sketch feature is selected, but the shape exists,
81   // try to create an exernal object
82   // TODO: unite with the same functionality in PartSet_WidgetShapeSelector
83   if (aSPFeature.get() == NULL) {
84     ObjectPtr anExternalObject = ObjectPtr();
85     GeomShapePtr anExternalShape = GeomShapePtr();
86     if (myExternalObjectMgr->useExternal()) {
87       if (myExternalObjectMgr->canCreateExternal()) {
88         GeomShapePtr aShape = theShape;
89         if (!aShape.get()) {
90           ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
91           if (aResult.get())
92             aShape = aResult->shape();
93         }
94         if (aShape.get() != NULL && !aShape->isNull())
95           anExternalObject =
96             myExternalObjectMgr->externalObject(theObject, aShape, sketch(), myIsInValidate);
97       }
98       else { /// use objects of found selection
99         anExternalObject = theObject;
100         anExternalShape = theShape;
101       }
102     }
103     /// the object is null if the selected feature is "external"(not sketch entity feature of the
104     /// current sketch) and it is not created by object manager
105     theObject = anExternalObject;
106     theShape = anExternalShape;
107   }
108 }
109
110 //********************************************************************
111 void PartSet_WidgetShapeSelector::restoreAttributeValue(const AttributePtr& theAttribute,
112                                                         const bool theValid)
113 {
114   ModuleBase_WidgetShapeSelector::restoreAttributeValue(theAttribute, theValid);
115   myExternalObjectMgr->removeExternal(sketch(), myFeature, myWorkshop, true);
116 }
117