Salome HOME
Issue #1550 Correction for external objects, case: Create extrusion(on circle), start...
[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, const bool theDefaultValue)
18 : myUseExternal(theDefaultValue)
19 {
20   QString aIsExternal(theExternal.c_str());
21   if (!aIsExternal.isEmpty()) {
22     QString aStr = aIsExternal.toUpper();
23     myUseExternal = (aStr == "TRUE") || (aStr == "YES"); 
24   }
25 }
26
27 bool PartSet_ExternalObjectsMgr::isValidObject(const ObjectPtr& theObject)
28 {
29   bool aValid = true;
30   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
31   // Do check using of external feature
32   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
33           std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
34
35   // Do check that we can use external feature
36   if (aSPFeature.get() != NULL && aSPFeature->isExternal() && !useExternal()) {
37     aValid = false;
38   }
39
40   return aValid;
41 }
42
43 ObjectPtr PartSet_ExternalObjectsMgr::externalObject(const ObjectPtr& theSelectedObject,
44                                                      const GeomShapePtr& theShape,
45                                                      const CompositeFeaturePtr& theSketch,
46                                                      const bool theTemporary)
47 {
48   ObjectPtr aSelectedObject = PartSet_Tools::findFixedObjectByExternal(
49                                   theShape->impl<TopoDS_Shape>(), theSelectedObject, theSketch);
50   FeaturePtr aFeature = ModelAPI_Feature::feature(aSelectedObject);
51   if (aFeature.get()) {
52     std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
53                             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
54     /// some sketch entities should be never shown, e.g. projection feature
55     /// such external features should not be used in selection
56     if (aSketchFeature.get() && !aSketchFeature->canBeDisplayed())
57       return ObjectPtr();
58   }
59
60   if (!aSelectedObject.get()) {
61     // Processing of external (non-sketch) object
62     aSelectedObject = PartSet_Tools::createFixedObjectByExternal(theShape->impl<TopoDS_Shape>(),
63                                                     theSelectedObject, theSketch, theTemporary);
64     if (aSelectedObject.get() && theTemporary)
65         myExternalObjectValidated = aSelectedObject;
66   }
67   return aSelectedObject;
68 }
69
70 //********************************************************************
71 void PartSet_ExternalObjectsMgr::removeExternal(const CompositeFeaturePtr& theSketch,
72                                                 const FeaturePtr& theFeature,
73                                                 ModuleBase_IWorkshop* theWorkshop,
74                                                 const bool theTemporary)
75 {
76   if (theTemporary)
77     removeExternalObject(myExternalObjectValidated, theSketch, theFeature, theWorkshop);
78 }
79
80 void PartSet_ExternalObjectsMgr::removeExternalObject(const ObjectPtr& theObject,
81                                                       const CompositeFeaturePtr& /*theSketch*/,
82                                                       const FeaturePtr& theFeature,
83                                                       ModuleBase_IWorkshop* theWorkshop)
84 {
85   if (theObject.get()) {
86     DocumentPtr aDoc = theObject->document();
87     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
88     if (aFeature.get() != NULL) {
89       QObjectPtrList anObjects;
90       anObjects.append(aFeature);
91       // the external feature should be removed with all references, composite sketch feature will be ignored
92       workshop(theWorkshop)->deleteFeatures(anObjects);
93     }
94   }
95 }
96
97 XGUI_Workshop* PartSet_ExternalObjectsMgr::workshop(ModuleBase_IWorkshop* theWorkshop)
98 {
99   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
100   return aConnector->workshop();
101 }