Salome HOME
bf1eeb9b2b1180a5e3b099518b3c9a6fc5fb38c6
[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
12 #include <QString>
13
14 PartSet_ExternalObjectsMgr::PartSet_ExternalObjectsMgr(const std::string& theExternal, const bool theDefaultValue)
15 : myUseExternal(theDefaultValue)
16 {
17   QString aIsExternal(theExternal.c_str());
18   if (!aIsExternal.isEmpty()) {
19     QString aStr = aIsExternal.toUpper();
20     myUseExternal = (aStr == "TRUE") || (aStr == "YES"); 
21   }
22 }
23
24 ObjectPtr PartSet_ExternalObjectsMgr::externalObject(const ObjectPtr& theSelectedObject,
25                                                      const GeomShapePtr& theShape,
26                                                      const CompositeFeaturePtr& theSketch)
27 {
28   ObjectPtr aSelectedObject = PartSet_Tools::findFixedObjectByExternal(theShape->impl<TopoDS_Shape>(),
29                                                              theSelectedObject, theSketch);
30   if (!aSelectedObject.get()) {
31     // Processing of external (non-sketch) object
32     aSelectedObject = PartSet_Tools::createFixedObjectByExternal(theShape->impl<TopoDS_Shape>(),
33                                                                  theSelectedObject, theSketch);
34     if (aSelectedObject.get())
35       myExternalObjects.append(aSelectedObject);
36   }
37   return aSelectedObject;
38 }
39
40 //********************************************************************
41 ObjectPtr PartSet_ExternalObjectsMgr::externalObjectValidated(const ObjectPtr& theSelectedObject,
42                                                      const GeomShapePtr& theShape,
43                                                      const CompositeFeaturePtr& theSketch)
44 {
45   // TODO(nds): unite with externalObject()
46   ObjectPtr aSelectedObject = PartSet_Tools::findFixedObjectByExternal(theShape->impl<TopoDS_Shape>(),
47                                                              theSelectedObject, theSketch);
48   if (!aSelectedObject.get()) {
49     // Processing of external (non-sketch) object
50     aSelectedObject = PartSet_Tools::createFixedObjectByExternal(theShape->impl<TopoDS_Shape>(),
51                                                                  theSelectedObject, theSketch);
52     if (aSelectedObject.get())
53       myExternalObjectValidated = aSelectedObject;
54   }
55   return aSelectedObject;
56 }
57
58 //********************************************************************
59 void PartSet_ExternalObjectsMgr::removeExternal(const CompositeFeaturePtr& theSketch,
60                                                 const FeaturePtr& theFeature)
61 {
62   QObjectPtrList::const_iterator anIt = myExternalObjects.begin(), aLast = myExternalObjects.end();
63   for (; anIt != aLast; anIt++) {
64     ObjectPtr anObject = *anIt;
65     if (anObject.get()) {
66       DocumentPtr aDoc = anObject->document();
67       FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
68       if (aFeature.get() != NULL) {
69         QObjectPtrList anObjects;
70         anObjects.append(aFeature);
71         // the external feature should be removed with all references, sketch feature should be ignored
72         std::set<FeaturePtr> anIgnoredFeatures;
73         anIgnoredFeatures.insert(theSketch);
74         // the current feature should be ignored, because it can use the external feature in the
75         // attributes and, therefore have a references to it. So, the delete functionality tries
76         // to delete this feature. Test case is creation of a constraint on external point,
77         // use in this control after an external point, the point of the sketch.
78         anIgnoredFeatures.insert(theFeature);
79         XGUI_Workshop::deleteFeatures(anObjects, anIgnoredFeatures);
80       }
81     }
82     //removeExternalObject(anObject, theSketch, theFeature);
83   }
84   myExternalObjects.clear();
85 }
86
87 //********************************************************************
88 void PartSet_ExternalObjectsMgr::removeUnusedExternalObjects(const QObjectPtrList& theIgnoreObjects,
89                                                              const CompositeFeaturePtr& theSketch,
90                                                              const FeaturePtr& theFeature)
91 {
92   /*
93   // TODO(nds): unite with removeExternal(), remove parameters
94   QObjectPtrList aUsedExternalObjects;
95
96   QObjectPtrList::const_iterator anIt = myExternalObjects.begin(), aLast = myExternalObjects.end();
97   for (; anIt != aLast; anIt++) {
98     ObjectPtr anObject = *anIt;
99     if (theIgnoreObjects.contains(anObject))
100       aUsedExternalObjects.append(anObject);
101     else
102       removeExternalObject(anObject, theSketch, theFeature);
103   }*/
104   myExternalObjects.clear();
105   //if (!aUsedExternalObjects.empty())
106   //  myExternalObjects = aUsedExternalObjects;
107 }
108
109 //********************************************************************
110 void PartSet_ExternalObjectsMgr::removeExternalValidated(const CompositeFeaturePtr& theSketch,
111                                                          const FeaturePtr& theFeature)
112 {
113   // TODO(nds): unite with removeExternal(), remove parameters
114   removeExternalObject(myExternalObjectValidated, theSketch, theFeature);
115   myExternalObjectValidated = ObjectPtr();
116 }
117
118 void PartSet_ExternalObjectsMgr::removeExternalObject(const ObjectPtr& theObject,
119                                                       const CompositeFeaturePtr& theSketch,
120                                                       const FeaturePtr& theFeature)
121 {
122   if (theObject.get()) {
123     DocumentPtr aDoc = theObject->document();
124     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
125     if (aFeature.get() != NULL) {
126       QObjectPtrList anObjects;
127       anObjects.append(aFeature);
128       // the external feature should be removed with all references, sketch feature should be ignored
129       std::set<FeaturePtr> anIgnoredFeatures;
130       anIgnoredFeatures.insert(theSketch);
131       // the current feature should be ignored, because it can use the external feature in the
132       // attributes and, therefore have a references to it. So, the delete functionality tries
133       // to delete this feature. Test case is creation of a constraint on external point,
134       // use in this control after an external point, the point of the sketch.
135       anIgnoredFeatures.insert(theFeature);
136       XGUI_Workshop::deleteFeatures(anObjects, anIgnoredFeatures);
137     }
138   }
139 }