Salome HOME
1. Projection Sketch feature: regression correction that objects are not selected...
[modules/shaper.git] / src / PartSet / PartSet_ExternalObjectsMgr.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_ExternalObjectsMgr.cpp
4 // Created:     15 Apr 2015
5 // Author:      Natalia Ermolaeva
6
7 #include "PartSet_ExternalObjectsMgr.h"
8 #include "PartSet_Tools.h"
9
10 #include <XGUI_Workshop.h>
11 #include <XGUI_ModuleConnector.h>
12
13 #include <SketchPlugin_Feature.h>
14
15 #include <QString>
16
17 PartSet_ExternalObjectsMgr::PartSet_ExternalObjectsMgr(const std::string& theExternal,
18                                                        const std::string& theCanCreateExternal,
19                                                        const bool theDefaultValue)
20 : myUseExternal(theDefaultValue), myCanCreateExternal(true)
21 {
22   QString aIsExternal(theExternal.c_str());
23   if (!aIsExternal.isEmpty()) {
24     QString aStr = aIsExternal.toUpper();
25     myUseExternal = (aStr == "TRUE") || (aStr == "YES"); 
26   }
27
28   QString aCanCreateExternal(theCanCreateExternal.c_str());
29   if (!aCanCreateExternal.isEmpty()) {
30     QString aStr = aCanCreateExternal.toUpper();
31     myCanCreateExternal = (aStr == "TRUE") || (aStr == "YES"); 
32   }
33 }
34
35 bool PartSet_ExternalObjectsMgr::isValidObject(const ObjectPtr& theObject)
36 {
37   bool aValid = true;
38   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
39   // Do check using of external feature
40   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
41           std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
42
43   // Do check that we can use external feature
44   if (aSPFeature.get() != NULL && aSPFeature->isExternal() && !useExternal()) {
45     aValid = false;
46   }
47
48   return aValid;
49 }
50
51 ObjectPtr PartSet_ExternalObjectsMgr::externalObject(const ObjectPtr& theSelectedObject,
52                                                      const GeomShapePtr& theShape,
53                                                      const CompositeFeaturePtr& theSketch,
54                                                      const bool theTemporary)
55 {
56   ObjectPtr aSelectedObject = PartSet_Tools::findFixedObjectByExternal(
57                                   theShape->impl<TopoDS_Shape>(), theSelectedObject, theSketch);
58   FeaturePtr aFeature = ModelAPI_Feature::feature(aSelectedObject);
59   if (aFeature.get()) {
60     std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
61                             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
62     /// some sketch entities should be never shown, e.g. projection feature
63     /// such external features should not be used in selection
64     if (aSketchFeature.get() && !aSketchFeature->canBeDisplayed())
65       return ObjectPtr();
66   }
67
68   if (!aSelectedObject.get()) {
69     // Processing of external (non-sketch) object
70     aSelectedObject = PartSet_Tools::createFixedObjectByExternal(theShape->impl<TopoDS_Shape>(),
71                                                     theSelectedObject, theSketch, theTemporary);
72     if (aSelectedObject.get() && theTemporary)
73         myExternalObjectValidated = aSelectedObject;
74   }
75   return aSelectedObject;
76 }
77
78 //********************************************************************
79 void PartSet_ExternalObjectsMgr::removeExternal(const CompositeFeaturePtr& theSketch,
80                                                 const FeaturePtr& theFeature,
81                                                 ModuleBase_IWorkshop* theWorkshop,
82                                                 const bool theTemporary)
83 {
84   if (theTemporary)
85     removeExternalObject(myExternalObjectValidated, theSketch, theFeature, theWorkshop);
86 }
87
88 void PartSet_ExternalObjectsMgr::removeExternalObject(const ObjectPtr& theObject,
89                                                       const CompositeFeaturePtr& /*theSketch*/,
90                                                       const FeaturePtr& theFeature,
91                                                       ModuleBase_IWorkshop* theWorkshop)
92 {
93   if (theObject.get()) {
94     DocumentPtr aDoc = theObject->document();
95     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
96     if (aFeature.get() != NULL) {
97       QObjectPtrList anObjects;
98       anObjects.append(aFeature);
99       // the external feature should be removed with all references, composite sketch feature will be ignored
100       workshop(theWorkshop)->deleteFeatures(anObjects);
101     }
102   }
103 }
104
105 XGUI_Workshop* PartSet_ExternalObjectsMgr::workshop(ModuleBase_IWorkshop* theWorkshop)
106 {
107   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
108   return aConnector->workshop();
109 }