]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_ExternalObjectsMgr.cpp
Salome HOME
2cdcd24bf149c737ff892b8532d21d23684088df
[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         // the current feature should be ignored, because it can use the external feature in the
94         // attributes and, therefore have a references to it. So, the delete functionality tries
95         // to delete this feature. Test case is creation of a constraint on external point,
96         // use in this control after an external point, the point of the sketch.
97         anIgnoredFeatures.insert(theFeature);
98         workshop(theWorkshop)->deleteFeatures(anObjects, anIgnoredFeatures);
99       }
100     }
101     //removeExternalObject(anObject, theSketch, theFeature);
102   }
103   myExternalObjects.clear();
104 }
105
106 //********************************************************************
107 void PartSet_ExternalObjectsMgr::removeUnusedExternalObjects(const QObjectPtrList& theIgnoreObjects,
108                                                              const CompositeFeaturePtr& theSketch,
109                                                              const FeaturePtr& theFeature)
110 {
111   /*
112   // TODO(nds): unite with removeExternal(), remove parameters
113   QObjectPtrList aUsedExternalObjects;
114
115   QObjectPtrList::const_iterator anIt = myExternalObjects.begin(), aLast = myExternalObjects.end();
116   for (; anIt != aLast; anIt++) {
117     ObjectPtr anObject = *anIt;
118     if (theIgnoreObjects.contains(anObject))
119       aUsedExternalObjects.append(anObject);
120     else
121       removeExternalObject(anObject, theSketch, theFeature);
122   }*/
123   myExternalObjects.clear();
124   //if (!aUsedExternalObjects.empty())
125   //  myExternalObjects = aUsedExternalObjects;
126 }
127
128 //********************************************************************
129 void PartSet_ExternalObjectsMgr::removeExternalValidated(const CompositeFeaturePtr& theSketch,
130                                                          const FeaturePtr& theFeature,
131                                                          ModuleBase_IWorkshop* theWorkshop)
132 {
133   // TODO(nds): unite with removeExternal(), remove parameters
134   removeExternalObject(myExternalObjectValidated, theSketch, theFeature, theWorkshop);
135   myExternalObjectValidated = ObjectPtr();
136 }
137
138 void PartSet_ExternalObjectsMgr::removeExternalObject(const ObjectPtr& theObject,
139                                                       const CompositeFeaturePtr& theSketch,
140                                                       const FeaturePtr& theFeature,
141                                                       ModuleBase_IWorkshop* theWorkshop)
142 {
143   if (theObject.get()) {
144     DocumentPtr aDoc = theObject->document();
145     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
146     if (aFeature.get() != NULL) {
147       QObjectPtrList anObjects;
148       anObjects.append(aFeature);
149       // the external feature should be removed with all references, sketch feature should be ignored
150       std::set<FeaturePtr> anIgnoredFeatures;
151       // the current feature should be ignored, because it can use the external feature in the
152       // attributes and, therefore have a references to it. So, the delete functionality tries
153       // to delete this feature. Test case is creation of a constraint on external point,
154       // use in this control after an external point, the point of the sketch.
155       anIgnoredFeatures.insert(theFeature);
156       workshop(theWorkshop)->deleteFeatures(anObjects, anIgnoredFeatures);
157     }
158   }
159 }
160
161 XGUI_Workshop* PartSet_ExternalObjectsMgr::workshop(ModuleBase_IWorkshop* theWorkshop)
162 {
163   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
164   return aConnector->workshop();
165 }