]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_Tools.cpp
Salome HOME
A correction to check modified value in extrusion cut/sketch/etc operations.
[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 bool isModelObject(FeaturePtr theFeature)
61 {
62   return theFeature && !theFeature->data();
63 }
64
65 //******************************************************************
66 std::string featureInfo(FeaturePtr theFeature)
67 {
68   std::ostringstream aStream;
69   if (theFeature)
70     aStream << theFeature.get() << " " << theFeature->getKind();
71   return QString(aStream.str().c_str()).toStdString();
72 }
73
74 //******************************************************************
75 /*FeaturePtr realFeature(const FeaturePtr theFeature)
76  {
77  if (theFeature->data()) {
78  return theFeature;
79  } else {
80  ObjectPtr aObject = std::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
81  return aObject->featureRef();
82  }
83  }*/
84
85 //******************************************************************
86 bool canRemoveOrRename(QWidget* theParent, const QObjectPtrList& theObjects)
87 {
88   bool aResult = true;
89   QString aNotActivatedNames;
90   if (!XGUI_Tools::allDocumentsActivated(aNotActivatedNames)) {
91     DocumentPtr aModuleDoc = ModelAPI_Session::get()->moduleDocument();
92     bool aFoundPartSetObject = false;
93     foreach (ObjectPtr aObj, theObjects) {
94       if (aObj->groupName() == ModelAPI_ResultPart::group())
95         continue;
96       aFoundPartSetObject = aObj->document() == aModuleDoc;
97     }
98     if (aFoundPartSetObject) {
99       QMessageBox::StandardButton aRes = QMessageBox::warning(theParent, QObject::tr("Warning"),
100                QObject::tr("Selected objects can be used in Part documents which are not loaded: \
101 %1. Whould you like to continue?").arg(aNotActivatedNames),
102                QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
103       aResult = aRes == QMessageBox::Yes;
104     }
105   }
106   return aResult;
107 }
108
109 //******************************************************************
110 bool canRename(const ObjectPtr& theObject, const QString& theName)
111 {
112   if (std::dynamic_pointer_cast<ModelAPI_ResultParameter>(theObject).get()) {
113     double aValue;
114     ResultParameterPtr aParam;
115     if (ModelAPI_Tools::findVariable(theObject->document(), qPrintable(theName), aValue, aParam)) {
116       QString aErrMsg(QObject::tr("Selected parameter can not be renamed to: %1. \
117  There is a parameter with the same name. Its value is: %2.").arg(qPrintable(theName)).arg(aValue));
118       // We can not use here a dialog box for message - it will crash editing process in ObjectBrowser
119       Events_Error::send(aErrMsg.toStdString());
120       return false;
121     }
122   }
123
124   return true;
125 }
126
127 //******************************************************************
128 bool allDocumentsActivated(QString& theNotActivatedNames)
129 {
130   bool anAllPartActivated = true;
131   QStringList aRefNames;
132
133   DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
134   int aSize = aRootDoc->size(ModelAPI_ResultPart::group());
135   for (int i = 0; i < aSize; i++) {
136     ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
137     ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
138     if (!aPart->isActivated()) {
139       anAllPartActivated = false;
140       aRefNames.append(aObject->data()->name().c_str());
141     }
142   }
143   theNotActivatedNames = aRefNames.join(", ");
144   return anAllPartActivated;
145 }
146
147 //**************************************************************
148 void refsToFeatureInFeatureDocument(const ObjectPtr& theObject, std::set<FeaturePtr>& theRefFeatures)
149 {
150   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
151   if (aFeature.get()) {
152     DocumentPtr aFeatureDoc = aFeature->document();
153     // 1. find references in the current document
154     aFeatureDoc->refsToFeature(aFeature, theRefFeatures, false);
155   }
156 }
157
158 //**************************************************************
159 bool isSubOfComposite(const ObjectPtr& theObject, const FeaturePtr& theFeature)
160 {
161   bool isSub = false;
162   CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
163   if (aComposite.get()) {
164     isSub = aComposite->isSub(theObject);
165     // the recursive is possible, the parameters are sketch circle and extrusion cut. They are
166     // separated by composite sketch feature
167     if (!isSub) {
168       int aNbSubs = aComposite->numberOfSubs();
169       for (int aSub = 0; aSub < aNbSubs && !isSub; aSub++) {
170         isSub = isSubOfComposite(theObject, aComposite->subFeature(aSub));
171       }
172     }
173   }
174   return isSub;
175 }
176
177 //**************************************************************
178 void refsToFeatureInAllDocuments(const ObjectPtr& theSourceObject, const ObjectPtr& theObject,
179                                  std::set<FeaturePtr>& theDirectRefFeatures, 
180                                  std::set<FeaturePtr>& theIndirectRefFeatures,
181                                  std::set<FeaturePtr>& theAlreadyProcessed)
182 {
183   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
184   if (!aFeature.get())
185     return;
186   if (theAlreadyProcessed.find(aFeature) != theAlreadyProcessed.end())
187     return;
188   theAlreadyProcessed.insert(aFeature);
189
190   // 1. find references in the current document
191   std::set<FeaturePtr> aRefFeatures;
192   refsToFeatureInFeatureDocument(theObject, aRefFeatures);
193   std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
194                                        aLast = aRefFeatures.end();
195   for (; anIt != aLast; anIt++) {
196     if (!isSubOfComposite(theSourceObject, *anIt))
197       theDirectRefFeatures.insert(*anIt);
198   }
199
200   // 2. find references in all documents if the document of the feature is
201   // "PartSet". Features of this document can be used in all other documents
202   DocumentPtr aFeatureDoc = aFeature->document();
203
204   SessionPtr aMgr = ModelAPI_Session::get();
205   DocumentPtr aModuleDoc = aMgr->moduleDocument();
206   if (aFeatureDoc == aModuleDoc) {
207     // the feature and results of the feature should be found in references
208     std::list<ObjectPtr> aObjects;
209     aObjects.push_back(aFeature);
210     typedef std::list<std::shared_ptr<ModelAPI_Result> > ResultsList;
211     const ResultsList& aResults = aFeature->results();
212     ResultsList::const_iterator aRIter = aResults.begin();
213     for (; aRIter != aResults.cend(); aRIter++) {
214       ResultPtr aRes = *aRIter;
215       if (aRes.get())
216         aObjects.push_back(aRes);
217     }
218     // get all opened documents; found features in the documents;
219     // get a list of objects where a feature refers;
220     // search in these objects the deleted objects.
221     SessionPtr aMgr = ModelAPI_Session::get();
222     std::list<DocumentPtr> anOpenedDocs = aMgr->allOpenedDocuments();
223     std::list<DocumentPtr>::const_iterator anIt = anOpenedDocs.begin(),
224                                             aLast = anOpenedDocs.end();
225     std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
226     for (; anIt != aLast; anIt++) {
227       DocumentPtr aDocument = *anIt;
228       if (aDocument == aFeatureDoc)
229         continue; // this document has been already processed in 1.1
230
231       int aFeaturesCount = aDocument->size(ModelAPI_Feature::group());
232       for (int aId = 0; aId < aFeaturesCount; aId++) {
233         ObjectPtr anObject = aDocument->object(ModelAPI_Feature::group(), aId);
234         FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
235         if (!aFeature.get())
236           continue;
237
238         aRefs.clear();
239         aFeature->data()->referencesToObjects(aRefs);
240         std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRef = aRefs.begin();
241         bool aHasReferenceToObject = false;
242         for(; aRef != aRefs.end() && !aHasReferenceToObject; aRef++) {
243           std::list<ObjectPtr>::iterator aRefObj = aRef->second.begin();
244           for(; aRefObj != aRef->second.end() && !aHasReferenceToObject; aRefObj++) {
245             std::list<ObjectPtr>::const_iterator aObjIt = aObjects.begin();
246             for(; aObjIt != aObjects.end() && !aHasReferenceToObject; aObjIt++) {
247               aHasReferenceToObject = *aObjIt == *aRefObj;
248             }
249           }
250         }
251         if (aHasReferenceToObject && !isSubOfComposite(theSourceObject, aFeature))
252           theDirectRefFeatures.insert(aFeature);
253       }
254     }
255   }
256
257   // Run recursion. It is possible recursive dependency, like the following: plane, extrusion uses plane,
258   // axis is built on extrusion. Delete of a plane should check the dependency from the axis also.
259   std::set<FeaturePtr>::const_iterator aFeatureIt = theDirectRefFeatures.begin();
260   for (; aFeatureIt != theDirectRefFeatures.end(); ++aFeatureIt) {
261     std::set<FeaturePtr> aRecursiveRefFeatures;
262     refsToFeatureInAllDocuments(theSourceObject, *aFeatureIt, 
263       aRecursiveRefFeatures, aRecursiveRefFeatures, theAlreadyProcessed);
264     theIndirectRefFeatures.insert(aRecursiveRefFeatures.begin(), aRecursiveRefFeatures.end());
265   }
266 }
267
268 }