Salome HOME
"2.11 Constraint with a point from the intersection between an outer edge and plane...
[modules/shaper.git] / src / XGUI / XGUI_Tools.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 #include "XGUI_Tools.h"
4
5 #include "XGUI_ModuleConnector.h"
6 #include "XGUI_Workshop.h"
7
8 #include "ModuleBase_IWorkshop.h"
9
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>
21
22 #include <GeomAPI_Shape.h>
23
24 #include <QDir>
25 #include <QMessageBox>
26
27 #include <iostream>
28 #include <sstream>
29
30 namespace XGUI_Tools {
31 //******************************************************************
32 QString dir(const QString& path, bool isAbs)
33 {
34   QDir aDir = QFileInfo(path).dir();
35   QString dirPath = isAbs ? aDir.absolutePath() : aDir.path();
36   if (dirPath == QString("."))
37     dirPath = QString();
38   return dirPath;
39 }
40
41 //******************************************************************
42 QString file(const QString& path, bool withExt)
43 {
44   QString fPath = path;
45   while (!fPath.isEmpty() && (fPath[fPath.length() - 1] == '\\' || fPath[fPath.length() - 1] == '/'))
46     fPath.remove(fPath.length() - 1, 1);
47
48   if (withExt)
49     return QFileInfo(fPath).fileName();
50   else
51     return QFileInfo(fPath).completeBaseName();
52 }
53
54 //******************************************************************
55 QString addSlash(const QString& path)
56 {
57   QString res = path;
58   if (!res.isEmpty() && res.at(res.length() - 1) != QChar('/')
59       && res.at(res.length() - 1) != QChar('\\'))
60     res += QDir::separator();
61   return res;
62 }
63
64 //******************************************************************
65 QString unionOfObjectNames(const QObjectPtrList& theObjects, const QString& theSeparator)
66 {
67   QStringList aObjectNames;
68   foreach (ObjectPtr aObj, theObjects) {
69     if (!aObj->data()->isValid())
70       continue;
71     aObjectNames << QString::fromStdString(aObj->data()->name());
72   }
73   return aObjectNames.join(", ");
74 }
75
76 //******************************************************************
77 bool isModelObject(FeaturePtr theFeature)
78 {
79   return theFeature && !theFeature->data();
80 }
81
82 //******************************************************************
83 std::string featureInfo(FeaturePtr theFeature)
84 {
85   std::ostringstream aStream;
86   if (theFeature)
87     aStream << theFeature.get() << " " << theFeature->getKind();
88   return QString(aStream.str().c_str()).toStdString();
89 }
90
91 //******************************************************************
92 /*FeaturePtr realFeature(const FeaturePtr theFeature)
93  {
94  if (theFeature->data()) {
95  return theFeature;
96  } else {
97  ObjectPtr aObject = std::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
98  return aObject->featureRef();
99  }
100  }*/
101
102 //******************************************************************
103 bool canRemoveOrRename(QWidget* theParent, const QObjectPtrList& theObjects)
104 {
105   bool aResult = true;
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())
112         continue;
113       aFoundPartSetObject = aObj->document() == aModuleDoc;
114     }
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;
121     }
122   }
123   return aResult;
124 }
125
126 //******************************************************************
127 bool canRename(const ObjectPtr& theObject, const QString& theName)
128 {
129   if (std::dynamic_pointer_cast<ModelAPI_ResultParameter>(theObject).get()) {
130     double aValue;
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());
137       return false;
138     }
139   }
140
141   return true;
142 }
143
144 //******************************************************************
145 bool allDocumentsActivated(QString& theNotActivatedNames)
146 {
147   bool anAllPartActivated = true;
148   QStringList aRefNames;
149
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());
158     }
159   }
160   theNotActivatedNames = aRefNames.join(", ");
161   return anAllPartActivated;
162 }
163
164 //**************************************************************
165 void refsToFeatureInFeatureDocument(const ObjectPtr& theObject, std::set<FeaturePtr>& theRefFeatures)
166 {
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);
172   }
173 }
174
175 //**************************************************************
176 bool isSubOfComposite(const ObjectPtr& theObject, const FeaturePtr& theFeature)
177 {
178   bool isSub = false;
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
184     if (!isSub) {
185       int aNbSubs = aComposite->numberOfSubs();
186       for (int aSub = 0; aSub < aNbSubs && !isSub; aSub++) {
187         isSub = isSubOfComposite(theObject, aComposite->subFeature(aSub));
188       }
189     }
190   }
191   return isSub;
192 }
193
194 //**************************************************************
195 bool isSubOfComposite(const ObjectPtr& theObject)
196 {
197   bool isSub = false;
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);
204   }
205   return isSub;
206 }
207
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)
214 {
215   refsDirectToFeatureInAllDocuments(theSourceObject, theObject, theIgnoreList, theDirectRefFeatures, 
216                                     theAlreadyProcessed);
217
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());
226   }
227
228 }
229
230 //**************************************************************
231 void refsDirectToFeatureInAllDocuments(const ObjectPtr& theSourceObject, const ObjectPtr& theObject,
232                                        const QObjectPtrList& theIgnoreList,
233                                        std::set<FeaturePtr>& theDirectRefFeatures, 
234                                        std::set<FeaturePtr>& theAlreadyProcessed)
235 {
236   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
237   if (!aFeature.get())
238     return;
239   if (theAlreadyProcessed.find(aFeature) != theAlreadyProcessed.end())
240     return;
241   theAlreadyProcessed.insert(aFeature);
242
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));
254       }
255     }
256   }
257
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);
267   }
268
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();
272
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;
284       if (aRes.get())
285         aObjects.push_back(aRes);
286     }
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
299
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);
304         if (!aFeature.get())
305           continue;
306
307         aRefs.clear();
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;
317             }
318           }
319         }
320         if (aHasReferenceToObject && !isSubOfComposite(theSourceObject, aFeature) &&
321             !theIgnoreList.contains(aFeature))
322           theDirectRefFeatures.insert(aFeature);
323       }
324     }
325   }
326 }
327
328 XGUI_Workshop* workshop(ModuleBase_IWorkshop* theWorkshop)
329 {
330   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
331   return aConnector->workshop();
332 }
333
334 }