Salome HOME
Copyright update 2021
[modules/shaper.git] / src / XGUI / XGUI_Tools.cpp
1 // Copyright (C) 2014-2021  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "XGUI_Tools.h"
21
22 #include "XGUI_ModuleConnector.h"
23 #include "XGUI_Workshop.h"
24
25 #include "ModuleBase_IWorkshop.h"
26 #include "ModuleBase_Tools.h"
27
28 #include <TopoDS_Shape.hxx>
29 #include <ModelAPI_Object.h>
30 #include <ModelAPI_Result.h>
31 #include <ModelAPI_ResultParameter.h>
32 #include <ModelAPI_Feature.h>
33 #include <ModelAPI_Session.h>
34 #include <ModelAPI_Document.h>
35 #include <ModelAPI_ResultPart.h>
36 #include <ModelAPI_CompositeFeature.h>
37 #include <ModelAPI_Tools.h>
38 #include <ModelAPI_ResultConstruction.h>
39 #include <ModelAPI_ResultBody.h>
40 #include <Events_InfoMessage.h>
41
42 #include <GeomAPI_Shape.h>
43 #include <GeomAlgoAPI_CompoundBuilder.h>
44
45 #include <TopoDS_Shape.hxx>
46
47 #include <QDir>
48 #include <QMessageBox>
49
50 #include <iostream>
51 #include <sstream>
52 #include <string>
53
54 #ifndef WIN32
55 # include <sys/stat.h>
56 # include <dirent.h>
57 # include <unistd.h>
58 # define _separator_ '/'
59 #else
60 #include <io.h>
61 #define F_OK 0
62 #define access _access
63 # include <windows.h>
64 # define _separator_ '\\'
65 #endif
66
67 namespace XGUI_Tools {
68 //******************************************************************
69 QString dir(const QString& path, bool isAbs)
70 {
71   QDir aDir = QFileInfo(path).dir();
72   QString dirPath = isAbs ? aDir.absolutePath() : aDir.path();
73   if (dirPath == QString("."))
74     dirPath = QString();
75   return dirPath;
76 }
77
78 //******************************************************************
79 QString file(const QString& path, bool withExt)
80 {
81   QString fPath = path;
82   while (!fPath.isEmpty() && (fPath[fPath.length() - 1] == '\\' ||
83           fPath[fPath.length() - 1] == '/'))
84     fPath.remove(fPath.length() - 1, 1);
85
86   if (withExt)
87     return QFileInfo(fPath).fileName();
88   else
89     return QFileInfo(fPath).completeBaseName();
90 }
91
92 //******************************************************************
93 QString addSlash(const QString& path)
94 {
95   QString res = path;
96   if (!res.isEmpty() && res.at(res.length() - 1) != QChar('/')
97       && res.at(res.length() - 1) != QChar('\\'))
98     res += QDir::separator();
99   return res;
100 }
101
102 //******************************************************************
103 QString unionOfObjectNames(const QObjectPtrList& theObjects, const QString& theSeparator)
104 {
105   QStringList aObjectNames;
106   foreach (ObjectPtr aObj, theObjects) {
107     if (aObj->data()->isValid())
108       aObjectNames << QString::fromStdWString(aObj->data()->name());
109   }
110   if (aObjectNames.count() == 0)
111     return QString();
112   if (aObjectNames.count() == 1)
113     return aObjectNames.first();
114   return aObjectNames.join(theSeparator);
115 }
116
117 //******************************************************************
118 bool isModelObject(FeaturePtr theFeature)
119 {
120   return theFeature && !theFeature->data();
121 }
122
123 //******************************************************************
124 std::string featureInfo(FeaturePtr theFeature)
125 {
126   std::ostringstream aStream;
127   if (theFeature)
128     aStream << theFeature.get() << " " << theFeature->getKind();
129   return QString(aStream.str().c_str()).toStdString();
130 }
131
132 //******************************************************************
133 /*FeaturePtr realFeature(const FeaturePtr theFeature)
134  {
135  if (theFeature->data()) {
136  return theFeature;
137  } else {
138  ObjectPtr aObject = std::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
139  return aObject->featureRef();
140  }
141  }*/
142
143
144 //******************************************************************
145 bool canRemoveOrRename(QWidget* theParent, const std::set<FeaturePtr>& theFeatures)
146 {
147   bool aResult = true;
148   std::wstring aNotActivatedNames;
149   if (!ModelAPI_Tools::allDocumentsActivated(aNotActivatedNames)) {
150     bool aFoundPartSetObject = ModuleBase_Tools::hasModuleDocumentFeature(theFeatures);
151     if (aFoundPartSetObject) {
152       const char* aKeyStr = "Selected objects can be used in Part documents which are not loaded: "
153                             "%1. Whould you like to continue?";
154       QMessageBox::StandardButton aRes = QMessageBox::warning(theParent, QObject::tr("Warning"),
155                QObject::tr(aKeyStr).arg(QString::fromStdWString(aNotActivatedNames)),
156                QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
157       aResult = aRes == QMessageBox::Yes;
158     }
159   }
160   return aResult;
161 }
162
163 //******************************************************************
164 bool isAscii(const QString& theStr)
165 {
166   char aCh;
167   for (int i = 0; i < theStr.size(); i++) {
168     aCh = theStr[i].toLatin1();
169     if (aCh == 0)
170       return false;
171     if ((aCh >= 0x30) && (aCh <= 0x39))
172       continue;
173     else if ((aCh >= 0x41) && (aCh <= 0x5A))
174       continue;
175     else if ((aCh >= 0x61) && (aCh <= 0x7A))
176       continue;
177     else if (aCh == 0x5f)
178       continue;
179     else
180       return false;
181   }
182   return true;
183 }
184
185 //******************************************************************
186 bool isValidName(const QString& theName)
187 {
188   QChar aChar;
189   for (int i = 0; i < theName.size(); i++) {
190     aChar = theName[i];
191     if (!aChar.isLetterOrNumber()) {
192       if ((aChar != "_") && (!aChar.isSpace()))
193         return false;
194     }
195   }
196   return true;
197 }
198
199 //******************************************************************
200 bool canRename(const ObjectPtr& theObject, const QString& theName)
201 {
202   std::string aType = theObject->groupName();
203   if (aType == ModelAPI_ResultParameter::group()) {
204     // For parameters names only ASCII symbols have to be used
205     if (!isAscii(theName))
206       return false;
207
208     double aValue;
209     ResultParameterPtr aParam;
210     if (ModelAPI_Tools::findVariable(theObject->document(),
211           FeaturePtr(), theName.toStdWString(), aValue, aParam)) {
212       const char* aKeyStr = "Selected parameter can not be renamed to: %1. "
213                             "There is a parameter with the same name. Its value is: %2.";
214       QString aErrMsg(QObject::tr(aKeyStr).arg(theName).arg(aValue));
215       // We can not use here a dialog box for message -
216       // it will crash editing process in ObjectBrowser
217       Events_InfoMessage("XGUI_Tools", aErrMsg.toStdString()).send();
218       return false;
219     }
220   }
221   else {
222     if (!isValidName(theName))
223       return false;
224
225     DocumentPtr aDoc = theObject->document();
226     ObjectPtr aObj =
227       aDoc->objectByName(aType, theName.toStdWString());
228
229     if (aObj.get() && theObject != aObj) {
230       QString aErrMsg(QObject::tr("Name %2 already exists in %1.").
231         arg(aType.c_str()).arg(theName));
232       // We can not use here a dialog box for message -
233       // it will crash editing process in ObjectBrowser
234       Events_InfoMessage("XGUI_Tools", aErrMsg.toStdString()).send();
235       return false;
236     }
237   }
238
239   return true;
240 }
241
242 //**************************************************************
243
244 XGUI_Workshop* workshop(ModuleBase_IWorkshop* theWorkshop)
245 {
246   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
247   return aConnector ? aConnector->workshop() : 0;
248 }
249
250
251 //********************************************************************
252 QString generateName(const ModuleBase_ViewerPrsPtr& thePrs)
253 {
254   if (!thePrs.get() || !thePrs->object().get())
255     return "Undefined";
256
257   GeomShapePtr aContext;
258   ObjectPtr anObject = thePrs->object();
259   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
260   if (aResult.get())
261     aContext = aResult->shape();
262   else {
263     // TODO if there is this case
264   }
265
266   QString aName = QString::fromStdWString(anObject->data()->name());
267   if (aContext.get()) {
268     GeomShapePtr aSubShape(new GeomAPI_Shape());
269     TopoDS_Shape aShape = ModuleBase_Tools::getSelectedShape(thePrs);
270     if (!aShape.IsNull()) {
271       aSubShape->setImpl(new TopoDS_Shape(aShape));
272       if (!aSubShape->isEqual(aContext)) {
273         QString aTypeName;
274         switch (aShape.ShapeType()) {
275         case TopAbs_COMPOUND:
276           aTypeName = "compound";
277           break;
278         case TopAbs_COMPSOLID:
279           aTypeName = "compsolid";
280           break;
281         case TopAbs_SOLID:
282           aTypeName = "solid";
283           break;
284         case TopAbs_SHELL:
285           aTypeName = "shell";
286           break;
287         case TopAbs_FACE:
288           aTypeName = "face";
289           break;
290         case TopAbs_WIRE:
291           aTypeName = "wire";
292           break;
293         case TopAbs_EDGE:
294           aTypeName = "edge";
295           break;
296         case TopAbs_VERTEX:
297           aTypeName = "vertex";
298           break;
299         case TopAbs_SHAPE:
300           aTypeName = "shape";
301           break;
302         }
303         int aId = GeomAlgoAPI_CompoundBuilder::id(aContext, aSubShape);
304         aName += QString("/%1_%2").arg(aTypeName).arg(aId);
305       }
306     }
307   }
308   return aName;
309 }
310
311 std::wstring strToWStr(const std::string& theStr) {
312   size_t aLen = theStr.size();
313   std::wstring aResult(aLen, L'#');
314   mbstowcs(&aResult[0], theStr.c_str(), aLen);
315   return aResult;
316 }
317
318 std::string getTmpDirByPath( const std::string& theTmpPath)
319 {
320   std::string aTmpDir = theTmpPath;
321   if (aTmpDir == "" || access(aTmpDir.c_str() , F_OK) != 0) {
322 #ifdef WIN32
323     char *Tmp_dir = getenv("TEMP");
324     if (Tmp_dir == NULL) {
325       Tmp_dir = getenv("TMP");
326       if (Tmp_dir == NULL)
327         aTmpDir = "C:\\";
328       else
329         aTmpDir = Tmp_dir;
330     }
331     else
332       aTmpDir = Tmp_dir;
333 #else
334     aTmpDir = "/tmp/";
335 #endif
336   }
337
338   if (aTmpDir.back() != _separator_)
339     aTmpDir += _separator_;
340
341   srand((unsigned int)time( NULL ));
342   //Get a random number to present a name of a sub directory
343   int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0));
344   char buffer[127];
345   sprintf(buffer, "%d", aRND);
346   std::string aSubDir(buffer);
347   if (aSubDir.size() <= 1)
348     aSubDir = "123049876";
349
350   aTmpDir += aSubDir; //Get RND sub directory
351
352   std::string aDir = aTmpDir;
353
354   for(aRND = 0; access(aDir.c_str() , F_OK) == 0; aRND++) {
355     sprintf( buffer, "%d", aRND );
356     aDir = aTmpDir + buffer;  //Build a unique directory name
357   }
358   if (aDir.back() != _separator_)
359     aDir += _separator_;
360
361 #ifdef WIN32
362   CreateDirectory(strToWStr(aDir).c_str(), NULL);
363 #else
364   mkdir( aDir.c_str(), 0x1ff );
365 #endif
366
367   return aDir;
368 }
369
370 std::string getTmpDirByEnv( const char* thePathEnv)
371 {
372   char* aVal = thePathEnv[0] == 0 ? 0 : getenv(thePathEnv);
373   std::string dir = aVal ? aVal : "";
374   return getTmpDirByPath(dir).c_str();
375 }
376
377 void removeTemporaryFiles(const std::string& theDirectory,
378   const std::list<std::string>& theFiles)
379 {
380   std::string aDirName = theDirectory;
381
382   std::list<std::string>::const_iterator aFilesIter = theFiles.cbegin();
383   for(; aFilesIter != theFiles.cend(); aFilesIter++) {
384     const std::string& aFile = *aFilesIter;
385     if(access(aFile.c_str() , F_OK) != 0)
386       continue;
387
388 #ifdef WIN32
389     DeleteFile(strToWStr(aFile).c_str());
390 #else
391     unlink(aFile.c_str());
392 #endif
393   }
394
395   if(access(aDirName.c_str() , F_OK) == 0) {
396 #ifdef WIN32
397     RemoveDirectory(strToWStr(aDirName).c_str());
398 #else
399     rmdir(aDirName.c_str());
400 #endif
401   }
402 }
403
404 }