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