Salome HOME
df63169bcca030b776758ccd2dfbd51b25876f9a
[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   if (!aSelectedObject.get()) {
51     // Processing of external (non-sketch) object
52     aSelectedObject = PartSet_Tools::createFixedObjectByExternal(theShape->impl<TopoDS_Shape>(),
53                                                     theSelectedObject, theSketch, theTemporary);
54     if (aSelectedObject.get()) {
55       if (theTemporary)
56         myExternalObjectValidated = aSelectedObject;
57       else
58         myExternalObjects.append(aSelectedObject);
59     }
60   }
61   return aSelectedObject;
62 }
63
64 //********************************************************************
65 /*ObjectPtr PartSet_ExternalObjectsMgr::externalObjectValidated(const ObjectPtr& theSelectedObject,
66                                                      const GeomShapePtr& theShape,
67                                                      const CompositeFeaturePtr& theSketch)
68 {
69   // TODO(nds): unite with externalObject()
70   ObjectPtr aSelectedObject = PartSet_Tools::findFixedObjectByExternal(theShape->impl<TopoDS_Shape>(),
71                                                              theSelectedObject, theSketch);
72   if (!aSelectedObject.get()) {
73     // Processing of external (non-sketch) object
74     aSelectedObject = PartSet_Tools::createFixedObjectByExternal(theShape->impl<TopoDS_Shape>(),
75                                                                  theSelectedObject, theSketch);
76     if (aSelectedObject.get())
77       myExternalObjectValidated = aSelectedObject;
78   }
79   return aSelectedObject;
80 }*/
81
82 //********************************************************************
83 void PartSet_ExternalObjectsMgr::removeExternal(const CompositeFeaturePtr& theSketch,
84                                                 const FeaturePtr& theFeature,
85                                                 ModuleBase_IWorkshop* theWorkshop,
86                                                 const bool theTemporary)
87 {
88   if (theTemporary)
89     removeExternalObject(myExternalObjectValidated, theSketch, theFeature, theWorkshop);
90   else{
91     QObjectPtrList::const_iterator anIt = myExternalObjects.begin(), aLast = myExternalObjects.end();
92     for (; anIt != aLast; anIt++) {
93       ObjectPtr anObject = *anIt;
94       removeExternalObject(anObject, theSketch, theFeature, theWorkshop);
95     }
96     myExternalObjects.clear();
97   }
98 }
99
100 //********************************************************************
101 void PartSet_ExternalObjectsMgr::removeUnusedExternalObjects(const QObjectPtrList& theIgnoreObjects,
102                                                              const CompositeFeaturePtr& theSketch,
103                                                              const FeaturePtr& theFeature)
104 {
105   /*
106   // TODO(nds): unite with removeExternal(), remove parameters
107   QObjectPtrList aUsedExternalObjects;
108
109   QObjectPtrList::const_iterator anIt = myExternalObjects.begin(), aLast = myExternalObjects.end();
110   for (; anIt != aLast; anIt++) {
111     ObjectPtr anObject = *anIt;
112     if (theIgnoreObjects.contains(anObject))
113       aUsedExternalObjects.append(anObject);
114     else
115       removeExternalObject(anObject, theSketch, theFeature);
116   }*/
117   myExternalObjects.clear();
118   //if (!aUsedExternalObjects.empty())
119   //  myExternalObjects = aUsedExternalObjects;
120 }
121
122 //********************************************************************
123 /*void PartSet_ExternalObjectsMgr::removeExternalValidated(const CompositeFeaturePtr& theSketch,
124                                                          const FeaturePtr& theFeature,
125                                                          ModuleBase_IWorkshop* theWorkshop)
126 {
127   // TODO(nds): unite with removeExternal(), remove parameters
128   removeExternalObject(myExternalObjectValidated, theSketch, theFeature, theWorkshop);
129   myExternalObjectValidated = ObjectPtr();
130 }*/
131
132 void PartSet_ExternalObjectsMgr::removeExternalObject(const ObjectPtr& theObject,
133                                                       const CompositeFeaturePtr& theSketch,
134                                                       const FeaturePtr& theFeature,
135                                                       ModuleBase_IWorkshop* theWorkshop)
136 {
137   if (theObject.get()) {
138     DocumentPtr aDoc = theObject->document();
139     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
140     if (aFeature.get() != NULL) {
141       QObjectPtrList anObjects;
142       anObjects.append(aFeature);
143       // the external feature should be removed with all references, sketch feature should be ignored
144       std::set<FeaturePtr> anIgnoredFeatures;
145       // the current feature should be ignored, because it can use the external feature in the
146       // attributes and, therefore have a references to it. So, the delete functionality tries
147       // to delete this feature. Test case is creation of a constraint on external point,
148       // use in this control after an external point, the point of the sketch.
149       anIgnoredFeatures.insert(theFeature);
150       workshop(theWorkshop)->deleteFeatures(anObjects, anIgnoredFeatures);
151     }
152   }
153 }
154
155 XGUI_Workshop* PartSet_ExternalObjectsMgr::workshop(ModuleBase_IWorkshop* theWorkshop)
156 {
157   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
158   return aConnector->workshop();
159 }