1 // Copyright (C) 2014-2024 CEA, EDF
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "XGUI_Tools.h"
22 #include "XGUI_ModuleConnector.h"
23 #include "XGUI_Workshop.h"
25 #include "ModuleBase_IWorkshop.h"
26 #include "ModuleBase_Tools.h"
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>
42 #include <GeomAPI_Shape.h>
43 #include <GeomAPI_IndexedMapOfShape.h>
45 #include <TopoDS_Shape.hxx>
48 #include <QMessageBox>
55 # include <sys/stat.h>
58 # define _separator_ '/'
62 #define access _access
64 # define _separator_ '\\'
67 namespace XGUI_Tools {
68 //******************************************************************
69 QString dir(const QString& path, bool isAbs)
71 QDir aDir = QFileInfo(path).dir();
72 QString dirPath = isAbs ? aDir.absolutePath() : aDir.path();
73 if (dirPath == QString("."))
78 //******************************************************************
79 QString file(const QString& path, bool withExt)
82 while (!fPath.isEmpty() && (fPath[fPath.length() - 1] == '\\' ||
83 fPath[fPath.length() - 1] == '/'))
84 fPath.remove(fPath.length() - 1, 1);
87 return QFileInfo(fPath).fileName();
89 return QFileInfo(fPath).completeBaseName();
92 //******************************************************************
93 QString addSlash(const QString& path)
96 if (!res.isEmpty() && res.at(res.length() - 1) != QChar('/')
97 && res.at(res.length() - 1) != QChar('\\'))
98 res += QDir::separator();
102 //******************************************************************
103 QString unionOfObjectNames(const QObjectPtrList& theObjects, const QString& theSeparator)
105 QStringList aObjectNames;
106 foreach (ObjectPtr aObj, theObjects) {
107 if (aObj->data()->isValid())
108 aObjectNames << QString::fromStdWString(aObj->data()->name());
110 if (aObjectNames.count() == 0)
112 if (aObjectNames.count() == 1)
113 return aObjectNames.first();
114 return aObjectNames.join(theSeparator);
117 //******************************************************************
118 bool isModelObject(FeaturePtr theFeature)
120 return theFeature && !theFeature->data();
123 //******************************************************************
124 std::string featureInfo(FeaturePtr theFeature)
126 std::ostringstream aStream;
128 aStream << theFeature.get() << " " << theFeature->getKind();
129 return QString(aStream.str().c_str()).toStdString();
132 //******************************************************************
133 /*FeaturePtr realFeature(const FeaturePtr theFeature)
135 if (theFeature->data()) {
138 ObjectPtr aObject = std::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
139 return aObject->featureRef();
144 //******************************************************************
145 bool canRemoveOrRename(QWidget* theParent, const std::set<FeaturePtr>& theFeatures)
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;
163 //******************************************************************
164 bool isAscii(const QString& theStr)
167 for (int i = 0; i < theStr.size(); i++) {
168 aCh = theStr[i].toLatin1();
171 if ((aCh >= 0x30) && (aCh <= 0x39))
173 else if ((aCh >= 0x41) && (aCh <= 0x5A))
175 else if ((aCh >= 0x61) && (aCh <= 0x7A))
177 else if (aCh == 0x5f)
185 //******************************************************************
186 bool isValidName(const QString& theName)
189 for (int i = 0; i < theName.size(); i++) {
191 if (!aChar.isLetterOrNumber()) {
192 if ((aChar != "_") && (!aChar.isSpace()))
199 //******************************************************************
200 bool canRename(const ObjectPtr& theObject, const QString& theName)
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))
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();
222 if (!isValidName(theName))
225 DocumentPtr aDoc = theObject->document();
227 aDoc->objectByName(aType, theName.toStdWString());
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();
242 //**************************************************************
244 XGUI_Workshop* workshop(ModuleBase_IWorkshop* theWorkshop)
246 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
247 return aConnector ? aConnector->workshop() : 0;
251 //********************************************************************
252 QString generateName(const ModuleBase_ViewerPrsPtr& thePrs)
254 if (!thePrs.get() || !thePrs->object().get())
257 GeomShapePtr aContext;
258 ObjectPtr anObject = thePrs->object();
259 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
261 aContext = aResult->shape();
263 // TODO if there is this case
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)) {
274 switch (aShape.ShapeType()) {
275 case TopAbs_COMPOUND:
276 aTypeName = "compound";
278 case TopAbs_COMPSOLID:
279 aTypeName = "compsolid";
297 aTypeName = "vertex";
303 GeomAPI_IndexedMapOfShape aSubShapesMap (aContext);
304 int aId = aSubShapesMap.FindIndexEqualLocations(aSubShape);
305 aName += QString("/%1_%2").arg(aTypeName).arg(aId);
312 std::wstring strToWStr(const std::string& theStr) {
313 size_t aLen = theStr.size();
314 std::wstring aResult(aLen, L'#');
315 mbstowcs(&aResult[0], theStr.c_str(), aLen);
319 std::string getTmpDirByPath( const std::string& theTmpPath)
321 std::string aTmpDir = theTmpPath;
322 if (aTmpDir == "" || access(aTmpDir.c_str() , F_OK) != 0) {
324 char *Tmp_dir = getenv("TEMP");
325 if (Tmp_dir == NULL) {
326 Tmp_dir = getenv("TMP");
339 if (aTmpDir.back() != _separator_)
340 aTmpDir += _separator_;
342 srand((unsigned int)time( NULL ));
343 //Get a random number to present a name of a sub directory
344 int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0));
346 sprintf(buffer, "%d", aRND);
347 std::string aSubDir(buffer);
348 if (aSubDir.size() <= 1)
349 aSubDir = "123049876";
351 aTmpDir += aSubDir; //Get RND sub directory
353 std::string aDir = aTmpDir;
355 for(aRND = 0; access(aDir.c_str() , F_OK) == 0; aRND++) {
356 sprintf( buffer, "%d", aRND );
357 aDir = aTmpDir + buffer; //Build a unique directory name
359 if (aDir.back() != _separator_)
363 CreateDirectory(strToWStr(aDir).c_str(), NULL);
365 mkdir( aDir.c_str(), 0x1ff );
371 std::string getTmpDirByEnv( const char* thePathEnv)
373 char* aVal = thePathEnv[0] == 0 ? 0 : getenv(thePathEnv);
374 std::string dir = aVal ? aVal : "";
375 return getTmpDirByPath(dir).c_str();
378 void removeTemporaryFiles(const std::string& theDirectory,
379 const std::list<std::string>& theFiles)
381 std::string aDirName = theDirectory;
383 std::list<std::string>::const_iterator aFilesIter = theFiles.cbegin();
384 for(; aFilesIter != theFiles.cend(); aFilesIter++) {
385 const std::string& aFile = *aFilesIter;
386 if(access(aFile.c_str() , F_OK) != 0)
390 DeleteFile(strToWStr(aFile).c_str());
392 unlink(aFile.c_str());
396 if(access(aDirName.c_str() , F_OK) == 0) {
398 RemoveDirectory(strToWStr(aDirName).c_str());
400 rmdir(aDirName.c_str());