1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 #include "XGUI_Tools.h"
5 #include "XGUI_ModuleConnector.h"
6 #include "XGUI_Workshop.h"
8 #include "ModuleBase_IWorkshop.h"
10 #include <TopoDS_Shape.hxx>
11 #include <ModelAPI_Object.h>
12 #include <ModelAPI_Result.h>
13 #include <ModelAPI_ResultParameter.h>
14 #include <ModelAPI_Feature.h>
15 #include <ModelAPI_Session.h>
16 #include <ModelAPI_Document.h>
17 #include <ModelAPI_ResultPart.h>
18 #include <ModelAPI_CompositeFeature.h>
19 #include <ModelAPI_Tools.h>
20 #include <Events_Error.h>
22 #include <GeomAPI_Shape.h>
25 #include <QMessageBox>
30 namespace XGUI_Tools {
31 //******************************************************************
32 QString dir(const QString& path, bool isAbs)
34 QDir aDir = QFileInfo(path).dir();
35 QString dirPath = isAbs ? aDir.absolutePath() : aDir.path();
36 if (dirPath == QString("."))
41 //******************************************************************
42 QString file(const QString& path, bool withExt)
45 while (!fPath.isEmpty() && (fPath[fPath.length() - 1] == '\\' || fPath[fPath.length() - 1] == '/'))
46 fPath.remove(fPath.length() - 1, 1);
49 return QFileInfo(fPath).fileName();
51 return QFileInfo(fPath).completeBaseName();
54 //******************************************************************
55 QString addSlash(const QString& path)
58 if (!res.isEmpty() && res.at(res.length() - 1) != QChar('/')
59 && res.at(res.length() - 1) != QChar('\\'))
60 res += QDir::separator();
64 //******************************************************************
65 QString unionOfObjectNames(const QObjectPtrList& theObjects, const QString& theSeparator)
67 QStringList aObjectNames;
68 foreach (ObjectPtr aObj, theObjects) {
69 if (!aObj->data()->isValid())
71 aObjectNames << QString::fromStdString(aObj->data()->name());
73 return aObjectNames.join(", ");
76 //******************************************************************
77 bool isModelObject(FeaturePtr theFeature)
79 return theFeature && !theFeature->data();
82 //******************************************************************
83 std::string featureInfo(FeaturePtr theFeature)
85 std::ostringstream aStream;
87 aStream << theFeature.get() << " " << theFeature->getKind();
88 return QString(aStream.str().c_str()).toStdString();
91 //******************************************************************
92 /*FeaturePtr realFeature(const FeaturePtr theFeature)
94 if (theFeature->data()) {
97 ObjectPtr aObject = std::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
98 return aObject->featureRef();
102 //******************************************************************
103 bool canRemoveOrRename(QWidget* theParent, const QObjectPtrList& theObjects)
106 QString aNotActivatedNames;
107 if (!XGUI_Tools::allDocumentsActivated(aNotActivatedNames)) {
108 DocumentPtr aModuleDoc = ModelAPI_Session::get()->moduleDocument();
109 bool aFoundPartSetObject = false;
110 foreach (ObjectPtr aObj, theObjects) {
111 if (aObj->groupName() == ModelAPI_ResultPart::group())
113 aFoundPartSetObject = aObj->document() == aModuleDoc;
115 if (aFoundPartSetObject) {
116 QMessageBox::StandardButton aRes = QMessageBox::warning(theParent, QObject::tr("Warning"),
117 QObject::tr("Selected objects can be used in Part documents which are not loaded: \
118 %1. Whould you like to continue?").arg(aNotActivatedNames),
119 QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
120 aResult = aRes == QMessageBox::Yes;
126 //******************************************************************
127 bool canRename(const ObjectPtr& theObject, const QString& theName)
129 if (std::dynamic_pointer_cast<ModelAPI_ResultParameter>(theObject).get()) {
131 ResultParameterPtr aParam;
132 if (ModelAPI_Tools::findVariable(theObject->document(), qPrintable(theName), aValue, aParam)) {
133 QString aErrMsg(QObject::tr("Selected parameter can not be renamed to: %1. \
134 There is a parameter with the same name. Its value is: %2.").arg(qPrintable(theName)).arg(aValue));
135 // We can not use here a dialog box for message - it will crash editing process in ObjectBrowser
136 Events_Error::send(aErrMsg.toStdString());
144 //******************************************************************
145 bool allDocumentsActivated(QString& theNotActivatedNames)
147 bool anAllPartActivated = true;
148 QStringList aRefNames;
150 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
151 int aSize = aRootDoc->size(ModelAPI_ResultPart::group());
152 for (int i = 0; i < aSize; i++) {
153 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
154 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
155 if (!aPart->isActivated()) {
156 anAllPartActivated = false;
157 aRefNames.append(aObject->data()->name().c_str());
160 theNotActivatedNames = aRefNames.join(", ");
161 return anAllPartActivated;
164 //**************************************************************
165 void refsToFeatureInFeatureDocument(const ObjectPtr& theObject, std::set<FeaturePtr>& theRefFeatures)
167 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
168 if (aFeature.get()) {
169 DocumentPtr aFeatureDoc = aFeature->document();
170 // 1. find references in the current document
171 aFeatureDoc->refsToFeature(aFeature, theRefFeatures, false);
175 //**************************************************************
176 bool isSubOfComposite(const ObjectPtr& theObject, const FeaturePtr& theFeature)
179 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
180 if (aComposite.get()) {
181 isSub = aComposite->isSub(theObject);
182 // the recursive is possible, the parameters are sketch circle and extrusion cut. They are
183 // separated by composite sketch feature
185 int aNbSubs = aComposite->numberOfSubs();
186 for (int aSub = 0; aSub < aNbSubs && !isSub; aSub++) {
187 isSub = isSubOfComposite(theObject, aComposite->subFeature(aSub));
194 //**************************************************************
195 bool isSubOfComposite(const ObjectPtr& theObject)
198 std::set<FeaturePtr> aRefFeatures;
199 refsToFeatureInFeatureDocument(theObject, aRefFeatures);
200 std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
201 aLast = aRefFeatures.end();
202 for (; anIt != aLast && !isSub; anIt++) {
203 isSub = isSubOfComposite(theObject, *anIt);
208 //**************************************************************
209 void refsToFeatureInAllDocuments(const ObjectPtr& theSourceObject, const ObjectPtr& theObject,
210 const QObjectPtrList& theIgnoreList,
211 std::set<FeaturePtr>& theDirectRefFeatures,
212 std::set<FeaturePtr>& theIndirectRefFeatures,
213 std::set<FeaturePtr>& theAlreadyProcessed)
215 refsDirectToFeatureInAllDocuments(theSourceObject, theObject, theIgnoreList, theDirectRefFeatures,
216 theAlreadyProcessed);
218 // Run recursion. It is possible recursive dependency, like the following: plane, extrusion uses plane,
219 // axis is built on extrusion. Delete of a plane should check the dependency from the axis also.
220 std::set<FeaturePtr>::const_iterator aFeatureIt = theDirectRefFeatures.begin();
221 for (; aFeatureIt != theDirectRefFeatures.end(); ++aFeatureIt) {
222 std::set<FeaturePtr> aRecursiveRefFeatures;
223 refsToFeatureInAllDocuments(theSourceObject, *aFeatureIt, theIgnoreList,
224 aRecursiveRefFeatures, aRecursiveRefFeatures, theAlreadyProcessed);
225 theIndirectRefFeatures.insert(aRecursiveRefFeatures.begin(), aRecursiveRefFeatures.end());
230 //**************************************************************
231 void refsDirectToFeatureInAllDocuments(const ObjectPtr& theSourceObject, const ObjectPtr& theObject,
232 const QObjectPtrList& theIgnoreList,
233 std::set<FeaturePtr>& theDirectRefFeatures,
234 std::set<FeaturePtr>& theAlreadyProcessed)
236 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
239 if (theAlreadyProcessed.find(aFeature) != theAlreadyProcessed.end())
241 theAlreadyProcessed.insert(aFeature);
243 //convert ignore object list to containt sub-features if the composite feature is in the list
244 QObjectPtrList aFullIgnoreList;
245 QObjectPtrList::const_iterator anIIt = theIgnoreList.begin(), anILast = theIgnoreList.end();
246 for (; anIIt != anILast; anIIt++) {
247 aFullIgnoreList.append(*anIIt);
248 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIIt);
249 // if the current feature is aborted, the composite is removed and has invalid data
250 if (aComposite.get() && aComposite->data()->isValid()) {
251 int aNbSubs = aComposite->numberOfSubs();
252 for (int aSub = 0; aSub < aNbSubs; aSub++) {
253 aFullIgnoreList.append(aComposite->subFeature(aSub));
258 // 1. find references in the current document
259 std::set<FeaturePtr> aRefFeatures;
260 refsToFeatureInFeatureDocument(theObject, aRefFeatures);
261 std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
262 aLast = aRefFeatures.end();
263 for (; anIt != aLast; anIt++) {
264 // composite feature should not be deleted when the sub feature is to be deleted
265 if (!isSubOfComposite(theSourceObject, *anIt) && !aFullIgnoreList.contains(*anIt))
266 theDirectRefFeatures.insert(*anIt);
269 // 2. find references in all documents if the document of the feature is
270 // "PartSet". Features of this document can be used in all other documents
271 DocumentPtr aFeatureDoc = aFeature->document();
273 SessionPtr aMgr = ModelAPI_Session::get();
274 DocumentPtr aModuleDoc = aMgr->moduleDocument();
275 if (aFeatureDoc == aModuleDoc) {
276 // the feature and results of the feature should be found in references
277 std::list<ObjectPtr> aObjects;
278 aObjects.push_back(aFeature);
279 typedef std::list<std::shared_ptr<ModelAPI_Result> > ResultsList;
280 const ResultsList& aResults = aFeature->results();
281 ResultsList::const_iterator aRIter = aResults.begin();
282 for (; aRIter != aResults.cend(); aRIter++) {
283 ResultPtr aRes = *aRIter;
285 aObjects.push_back(aRes);
287 // get all opened documents; found features in the documents;
288 // get a list of objects where a feature refers;
289 // search in these objects the deleted objects.
290 SessionPtr aMgr = ModelAPI_Session::get();
291 std::list<DocumentPtr> anOpenedDocs = aMgr->allOpenedDocuments();
292 std::list<DocumentPtr>::const_iterator anIt = anOpenedDocs.begin(),
293 aLast = anOpenedDocs.end();
294 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
295 for (; anIt != aLast; anIt++) {
296 DocumentPtr aDocument = *anIt;
297 if (aDocument == aFeatureDoc)
298 continue; // this document has been already processed in 1.1
300 int aFeaturesCount = aDocument->size(ModelAPI_Feature::group());
301 for (int aId = 0; aId < aFeaturesCount; aId++) {
302 ObjectPtr anObject = aDocument->object(ModelAPI_Feature::group(), aId);
303 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
308 aFeature->data()->referencesToObjects(aRefs);
309 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRef = aRefs.begin();
310 bool aHasReferenceToObject = false;
311 for(; aRef != aRefs.end() && !aHasReferenceToObject; aRef++) {
312 std::list<ObjectPtr>::iterator aRefObj = aRef->second.begin();
313 for(; aRefObj != aRef->second.end() && !aHasReferenceToObject; aRefObj++) {
314 std::list<ObjectPtr>::const_iterator aObjIt = aObjects.begin();
315 for(; aObjIt != aObjects.end() && !aHasReferenceToObject; aObjIt++) {
316 aHasReferenceToObject = *aObjIt == *aRefObj;
320 if (aHasReferenceToObject && !isSubOfComposite(theSourceObject, aFeature) &&
321 !theIgnoreList.contains(aFeature))
322 theDirectRefFeatures.insert(aFeature);
328 XGUI_Workshop* workshop(ModuleBase_IWorkshop* theWorkshop)
330 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
331 return aConnector->workshop();