Salome HOME
Issue #1618 naming in sketch is broken: external object creation should use the shape...
[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"), true);
38 }
39
40 PartSet_WidgetShapeSelector::~PartSet_WidgetShapeSelector()
41 {
42   delete myExternalObjectMgr;
43 }
44
45 //********************************************************************
46 bool PartSet_WidgetShapeSelector::activateSelectionAndFilters(bool toActivate)
47 {
48   bool aHasSelectionFilter = ModuleBase_WidgetShapeSelector::activateSelectionAndFilters
49                                                                            (toActivate);
50   if (!myUseSketchPlane) {
51     XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
52     PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(aWorkshop->module());
53     bool isUsePlaneFilterOnly = !toActivate;
54     aModule->sketchMgr()->activatePlaneFilter(isUsePlaneFilterOnly);
55   }
56   return aHasSelectionFilter;
57 }
58
59 //********************************************************************
60 bool PartSet_WidgetShapeSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
61 {
62   bool aValid = ModuleBase_WidgetShapeSelector::isValidSelectionCustom(thePrs);
63   if (aValid) {
64     ObjectPtr anObject = myWorkshop->selection()->getResult(thePrs);
65     aValid = myExternalObjectMgr->isValidObject(anObject);
66   }
67   return aValid;
68 }
69
70 void PartSet_WidgetShapeSelector::getGeomSelection(const ModuleBase_ViewerPrsPtr& 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) {
82     ObjectPtr anExternalObject = ObjectPtr();
83     if (myExternalObjectMgr->useExternal()) {
84       GeomShapePtr aShape = theShape;
85       if (!aShape.get()) {
86         ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
87         if (aResult.get())
88           aShape = aResult->shape();
89       }
90       if (aShape.get() != NULL && !aShape->isNull())
91         anExternalObject = myExternalObjectMgr->externalObject(theObject, aShape, sketch(), myIsInValidate);
92     }
93     /// the object is null if the selected feature is "external"(not sketch entity feature of the
94     /// current sketch) and it is not created by object manager
95     theObject = anExternalObject;
96   }
97 }
98
99 //********************************************************************
100 void PartSet_WidgetShapeSelector::restoreAttributeValue(const AttributePtr& theAttribute,
101                                                         const bool theValid)
102 {
103   ModuleBase_WidgetShapeSelector::restoreAttributeValue(theAttribute, theValid);
104   myExternalObjectMgr->removeExternal(sketch(), myFeature, myWorkshop, true);
105 }
106