]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_Tools.cpp
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 <TopoDS_Shape.hxx>
6 #include <ModelAPI_Object.h>
7 #include <ModelAPI_Result.h>
8 #include <ModelAPI_ResultParameter.h>
9 #include <ModelAPI_Feature.h>
10 #include <ModelAPI_Session.h>
11 #include <ModelAPI_Document.h>
12 #include <ModelAPI_ResultPart.h>
13 #include <ModelAPI_CompositeFeature.h>
14 #include <ModelAPI_Tools.h>
15 #include <Events_Error.h>
16
17 #include <GeomAPI_Shape.h>
18
19 #include <QDir>
20 #include <QMessageBox>
21
22 #include <iostream>
23 #include <sstream>
24
25 namespace XGUI_Tools {
26 //******************************************************************
27 QString dir(const QString& path, bool isAbs)
28 {
29   QDir aDir = QFileInfo(path).dir();
30   QString dirPath = isAbs ? aDir.absolutePath() : aDir.path();
31   if (dirPath == QString("."))
32     dirPath = QString();
33   return dirPath;
34 }
35
36 //******************************************************************
37 QString file(const QString& path, bool withExt)
38 {
39   QString fPath = path;
40   while (!fPath.isEmpty() && (fPath[fPath.length() - 1] == '\\' || fPath[fPath.length() - 1] == '/'))
41     fPath.remove(fPath.length() - 1, 1);
42
43   if (withExt)
44     return QFileInfo(fPath).fileName();
45   else
46     return QFileInfo(fPath).completeBaseName();
47 }
48
49 //******************************************************************
50 QString addSlash(const QString& path)
51 {
52   QString res = path;
53   if (!res.isEmpty() && res.at(res.length() - 1) != QChar('/')
54       && res.at(res.length() - 1) != QChar('\\'))
55     res += QDir::separator();
56   return res;
57 }
58
59 //******************************************************************
60 QString unionOfObjectNames(const QObjectPtrList& theObjects, const QString& theSeparator)
61 {
62   QStringList aObjectNames;
63   foreach (ObjectPtr aObj, theObjects) {
64     if (!aObj->data()->isValid())
65       continue;
66     aObjectNames << QString::fromStdString(aObj->data()->name());
67   }
68   return aObjectNames.join(", ");
69 }
70
71 //******************************************************************
72 bool isModelObject(FeaturePtr theFeature)
73 {
74   return theFeature && !theFeature->data();
75 }
76
77 //******************************************************************
78 std::string featureInfo(FeaturePtr theFeature)
79 {
80   std::ostringstream aStream;
81   if (theFeature)
82     aStream << theFeature.get() << " " << theFeature->getKind();
83   return QString(aStream.str().c_str()).toStdString();
84 }
85
86 //******************************************************************
87 /*FeaturePtr realFeature(const FeaturePtr theFeature)
88  {
89  if (theFeature->data()) {
90  return theFeature;
91  } else {
92  ObjectPtr aObject = std::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
93  return aObject->featureRef();
94  }
95  }*/
96
97 //******************************************************************
98 bool canRemoveOrRename(QWidget* theParent, const QObjectPtrList& theObjects)
99 {
100   bool aResult = true;
101   QString aNotActivatedNames;
102   if (!XGUI_Tools::allDocumentsActivated(aNotActivatedNames)) {
103     DocumentPtr aModuleDoc = ModelAPI_Session::get()->moduleDocument();
104     bool aFoundPartSetObject = false;
105     foreach (ObjectPtr aObj, theObjects) {
106       if (aObj->groupName() == ModelAPI_ResultPart::group())
107         continue;
108       aFoundPartSetObject = aObj->document() == aModuleDoc;
109     }
110     if (aFoundPartSetObject) {
111       QMessageBox::StandardButton aRes = QMessageBox::warning(theParent, QObject::tr("Warning"),
112                QObject::tr("Selected objects can be used in Part documents which are not loaded: \
113 %1. Whould you like to continue?").arg(aNotActivatedNames),
114                QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
115       aResult = aRes == QMessageBox::Yes;
116     }
117   }
118   return aResult;
119 }
120
121 //******************************************************************
122 bool canRename(const ObjectPtr& theObject, const QString& theName)
123 {
124   if (std::dynamic_pointer_cast<ModelAPI_ResultParameter>(theObject).get()) {
125     double aValue;
126     ResultParameterPtr aParam;
127     if (ModelAPI_Tools::findVariable(theObject->document(), qPrintable(theName), aValue, aParam)) {
128       QString aErrMsg(QObject::tr("Selected parameter can not be renamed to: %1. \
129  There is a parameter with the same name. Its value is: %2.").arg(qPrintable(theName)).arg(aValue));
130       // We can not use here a dialog box for message - it will crash editing process in ObjectBrowser
131       Events_Error::send(aErrMsg.toStdString());
132       return false;
133     }
134   }
135
136   return true;
137 }
138
139 //******************************************************************
140 bool allDocumentsActivated(QString& theNotActivatedNames)
141 {
142   bool anAllPartActivated = true;
143   QStringList aRefNames;
144
145   DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
146   int aSize = aRootDoc->size(ModelAPI_ResultPart::group());
147   for (int i = 0; i < aSize; i++) {
148     ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
149     ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
150     if (!aPart->isActivated()) {
151       anAllPartActivated = false;
152       aRefNames.append(aObject->data()->name().c_str());
153     }
154   }
155   theNotActivatedNames = aRefNames.join(", ");
156   return anAllPartActivated;
157 }
158
159 //**************************************************************
160 void refsToFeatureInFeatureDocument(const ObjectPtr& theObject, std::set<FeaturePtr>& theRefFeatures)
161 {
162   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
163   if (aFeature.get()) {
164     DocumentPtr aFeatureDoc = aFeature->document();
165     // 1. find references in the current document
166     aFeatureDoc->refsToFeature(aFeature, theRefFeatures, false);
167   }
168 }
169
170 //**************************************************************
171 bool isSubOfComposite(const ObjectPtr& theObject, const FeaturePtr& theFeature)
172 {
173   bool isSub = false;
174   CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
175   if (aComposite.get()) {
176     isSub = aComposite->isSub(theObject);
177     // the recursive is possible, the parameters are sketch circle and extrusion cut. They are
178     // separated by composite sketch feature
179     if (!isSub) {
180       int aNbSubs = aComposite->numberOfSubs();
181       for (int aSub = 0; aSub < aNbSubs && !isSub; aSub++) {
182         isSub = isSubOfComposite(theObject, aComposite->subFeature(aSub));
183       }
184     }
185   }
186   return isSub;
187 }
188
189 //**************************************************************
190 bool isSubOfComposite(const ObjectPtr& theObject)
191 {
192   bool isSub = false;
193   std::set<FeaturePtr> aRefFeatures;
194   refsToFeatureInFeatureDocument(theObject, aRefFeatures);
195   std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
196                                        aLast = aRefFeatures.end();
197   for (; anIt != aLast && !isSub; anIt++) {
198     isSub = isSubOfComposite(theObject, *anIt);
199   }
200   return isSub;
201 }
202
203 //**************************************************************
204 void refsToFeatureInAllDocuments(const ObjectPtr& theSourceObject, const ObjectPtr& theObject,
205                                  const QObjectPtrList& theIgnoreList,
206                                  std::set<FeaturePtr>& theDirectRefFeatures, 
207                                  std::set<FeaturePtr>& theIndirectRefFeatures,
208                                  std::set<FeaturePtr>& theAlreadyProcessed)
209 {
210   refsDirectToFeatureInAllDocuments(theSourceObject, theObject, theIgnoreList, theDirectRefFeatures, 
211                                     theAlreadyProcessed);
212
213   // Run recursion. It is possible recursive dependency, like the following: plane, extrusion uses plane,
214   // axis is built on extrusion. Delete of a plane should check the dependency from the axis also.
215   std::set<FeaturePtr>::const_iterator aFeatureIt = theDirectRefFeatures.begin();
216   for (; aFeatureIt != theDirectRefFeatures.end(); ++aFeatureIt) {
217     std::set<FeaturePtr> aRecursiveRefFeatures;
218     refsToFeatureInAllDocuments(theSourceObject, *aFeatureIt, theIgnoreList,
219       aRecursiveRefFeatures, aRecursiveRefFeatures, theAlreadyProcessed);
220     theIndirectRefFeatures.insert(aRecursiveRefFeatures.begin(), aRecursiveRefFeatures.end());
221   }
222
223 }
224
225 //**************************************************************
226 void refsDirectToFeatureInAllDocuments(const ObjectPtr& theSourceObject, const ObjectPtr& theObject,
227                                        const QObjectPtrList& theIgnoreList,
228                                        std::set<FeaturePtr>& theDirectRefFeatures, 
229                                        std::set<FeaturePtr>& theAlreadyProcessed)
230 {
231   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
232   if (!aFeature.get())
233     return;
234   if (theAlreadyProcessed.find(aFeature) != theAlreadyProcessed.end())
235     return;
236   theAlreadyProcessed.insert(aFeature);
237
238   //convert ignore object list to containt sub-features if the composite feature is in the list
239   QObjectPtrList aFullIgnoreList;
240   QObjectPtrList::const_iterator anIIt = theIgnoreList.begin(), anILast = theIgnoreList.end();
241   for (; anIIt != anILast; anIIt++) {
242     aFullIgnoreList.append(*anIIt);
243     CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIIt);
244     // if the current feature is aborted, the composite is removed and has invalid data
245     if (aComposite.get() && aComposite->data()->isValid()) {
246       int aNbSubs = aComposite->numberOfSubs();
247       for (int aSub = 0; aSub < aNbSubs; aSub++) {
248         aFullIgnoreList.append(aComposite->subFeature(aSub));
249       }
250     }
251   }
252
253   // 1. find references in the current document
254   std::set<FeaturePtr> aRefFeatures;
255   refsToFeatureInFeatureDocument(theObject, aRefFeatures);
256   std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
257                                        aLast = aRefFeatures.end();
258   for (; anIt != aLast; anIt++) {
259     // composite feature should not be deleted when the sub feature is to be deleted
260     if (!isSubOfComposite(theSourceObject, *anIt) && !aFullIgnoreList.contains(*anIt))
261       theDirectRefFeatures.insert(*anIt);
262   }
263
264   // 2. find references in all documents if the document of the feature is
265   // "PartSet". Features of this document can be used in all other documents
266   DocumentPtr aFeatureDoc = aFeature->document();
267
268   SessionPtr aMgr = ModelAPI_Session::get();
269   DocumentPtr aModuleDoc = aMgr->moduleDocument();
270   if (aFeatureDoc == aModuleDoc) {
271     // the feature and results of the feature should be found in references
272     std::list<ObjectPtr> aObjects;
273     aObjects.push_back(aFeature);
274     typedef std::list<std::shared_ptr<ModelAPI_Result> > ResultsList;
275     const ResultsList& aResults = aFeature->results();
276     ResultsList::const_iterator aRIter = aResults.begin();
277     for (; aRIter != aResults.cend(); aRIter++) {
278       ResultPtr aRes = *aRIter;
279       if (aRes.get())
280         aObjects.push_back(aRes);
281     }
282     // get all opened documents; found features in the documents;
283     // get a list of objects where a feature refers;
284     // search in these objects the deleted objects.
285     SessionPtr aMgr = ModelAPI_Session::get();
286     std::list<DocumentPtr> anOpenedDocs = aMgr->allOpenedDocuments();
287     std::list<DocumentPtr>::const_iterator anIt = anOpenedDocs.begin(),
288                                             aLast = anOpenedDocs.end();
289     std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
290     for (; anIt != aLast; anIt++) {
291       DocumentPtr aDocument = *anIt;
292       if (aDocument == aFeatureDoc)
293         continue; // this document has been already processed in 1.1
294
295       int aFeaturesCount = aDocument->size(ModelAPI_Feature::group());
296       for (int aId = 0; aId < aFeaturesCount; aId++) {
297         ObjectPtr anObject = aDocument->object(ModelAPI_Feature::group(), aId);
298         FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
299         if (!aFeature.get())
300           continue;
301
302         aRefs.clear();
303         aFeature->data()->referencesToObjects(aRefs);
304         std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRef = aRefs.begin();
305         bool aHasReferenceToObject = false;
306         for(; aRef != aRefs.end() && !aHasReferenceToObject; aRef++) {
307           std::list<ObjectPtr>::iterator aRefObj = aRef->second.begin();
308           for(; aRefObj != aRef->second.end() && !aHasReferenceToObject; aRefObj++) {
309             std::list<ObjectPtr>::const_iterator aObjIt = aObjects.begin();
310             for(; aObjIt != aObjects.end() && !aHasReferenceToObject; aObjIt++) {
311               aHasReferenceToObject = *aObjIt == *aRefObj;
312             }
313           }
314         }
315         if (aHasReferenceToObject && !isSubOfComposite(theSourceObject, aFeature) &&
316             !theIgnoreList.contains(aFeature))
317           theDirectRefFeatures.insert(aFeature);
318       }
319     }
320   }
321 }
322
323 }