1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PartSet_ExternalObjectsMgr.cpp
4 // Created: 15 Apr 2015
5 // Author: Natalia Ermolaeva
7 #include "PartSet_ExternalObjectsMgr.h"
8 #include "PartSet_Tools.h"
10 #include <XGUI_Workshop.h>
11 #include <XGUI_ModuleConnector.h>
13 #include <SketchPlugin_Feature.h>
17 PartSet_ExternalObjectsMgr::PartSet_ExternalObjectsMgr(const std::string& theExternal, const bool theDefaultValue)
18 : myUseExternal(theDefaultValue)
20 QString aIsExternal(theExternal.c_str());
21 if (!aIsExternal.isEmpty()) {
22 QString aStr = aIsExternal.toUpper();
23 myUseExternal = (aStr == "TRUE") || (aStr == "YES");
27 bool PartSet_ExternalObjectsMgr::isValidObject(const ObjectPtr& theObject)
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);
35 // Do check that we can use external feature
36 if (aSPFeature.get() != NULL && aSPFeature->isExternal() && !useExternal()) {
43 ObjectPtr PartSet_ExternalObjectsMgr::externalObject(const ObjectPtr& theSelectedObject,
44 const GeomShapePtr& theShape,
45 const CompositeFeaturePtr& theSketch)
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);
56 return aSelectedObject;
59 //********************************************************************
60 ObjectPtr PartSet_ExternalObjectsMgr::externalObjectValidated(const ObjectPtr& theSelectedObject,
61 const GeomShapePtr& theShape,
62 const CompositeFeaturePtr& theSketch)
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;
74 return aSelectedObject;
77 //********************************************************************
78 void PartSet_ExternalObjectsMgr::removeExternal(const CompositeFeaturePtr& theSketch,
79 const FeaturePtr& theFeature,
80 ModuleBase_IWorkshop* theWorkshop)
82 QObjectPtrList::const_iterator anIt = myExternalObjects.begin(), aLast = myExternalObjects.end();
83 for (; anIt != aLast; anIt++) {
84 ObjectPtr anObject = *anIt;
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);
102 //removeExternalObject(anObject, theSketch, theFeature);
104 myExternalObjects.clear();
107 //********************************************************************
108 void PartSet_ExternalObjectsMgr::removeUnusedExternalObjects(const QObjectPtrList& theIgnoreObjects,
109 const CompositeFeaturePtr& theSketch,
110 const FeaturePtr& theFeature)
113 // TODO(nds): unite with removeExternal(), remove parameters
114 QObjectPtrList aUsedExternalObjects;
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);
122 removeExternalObject(anObject, theSketch, theFeature);
124 myExternalObjects.clear();
125 //if (!aUsedExternalObjects.empty())
126 // myExternalObjects = aUsedExternalObjects;
129 //********************************************************************
130 void PartSet_ExternalObjectsMgr::removeExternalValidated(const CompositeFeaturePtr& theSketch,
131 const FeaturePtr& theFeature,
132 ModuleBase_IWorkshop* theWorkshop)
134 // TODO(nds): unite with removeExternal(), remove parameters
135 removeExternalObject(myExternalObjectValidated, theSketch, theFeature, theWorkshop);
136 myExternalObjectValidated = ObjectPtr();
139 void PartSet_ExternalObjectsMgr::removeExternalObject(const ObjectPtr& theObject,
140 const CompositeFeaturePtr& theSketch,
141 const FeaturePtr& theFeature,
142 ModuleBase_IWorkshop* theWorkshop)
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);
163 XGUI_Workshop* PartSet_ExternalObjectsMgr::workshop(ModuleBase_IWorkshop* theWorkshop)
165 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
166 return aConnector->workshop();