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