]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
Generate temporary file names by model.checkPythonDump()
authorazv <azv@opencascade.com>
Tue, 10 Mar 2020 13:54:28 +0000 (16:54 +0300)
committerazv <azv@opencascade.com>
Tue, 10 Mar 2020 13:54:28 +0000 (16:54 +0300)
src/ConnectorPlugin/ConnectorPlugin_ExportFeature.py
src/ModelHighAPI/ModelHighAPI_Tools.cpp
src/ModelHighAPI/ModelHighAPI_Tools.h
src/PythonAPI/model/services/__init__.py

index 925b78c723fafbe5857941345f6b0fdc50a9014c..34bde32ecff116ad966fafd2f65583cb6210cd32 100644 (file)
@@ -31,15 +31,6 @@ from salome.shaper import model
 
 import os
 
-def getTmpFileName(ext):
-    import tempfile
-    tempdir = tempfile.gettempdir()
-    tmp_file = tempfile.NamedTemporaryFile(suffix=".%s"%ext, prefix='shaper_', dir=tempdir, delete=False)
-    tmp_filename = tmp_file.name
-    if os.name == "nt":
-        tmp_filename.replace("\\", "/")
-    return tmp_filename
-
 ## @ingroup Plugins
 #  Feature to export all shapes and groups into the GEOM module
 class ExportFeature(ModelAPI.ModelAPI_Feature):
@@ -87,7 +78,7 @@ class ExportFeature(ModelAPI.ModelAPI_Feature):
 
               if  not aResult.shape() or aResult.shape().isNull():
                 continue
-              tmpXAOFile = getTmpFileName("xao")
+              tmpXAOFile = model.getTmpFileName("shaper_", ".xao")
               self.tmpXAOFile = tmpXAOFile
               #print "Export to %s"%tmpXAOFile
               exportXAO = ExchangeAPI.exportToXAO(self.Part, tmpXAOFile, model.selection(aResult), "automatic_shaper_export_to_XAO")
index e2b0e3cbd5d5e86213f1d00eeddc6fdd8ef4e282..882c94f405d739e4c7f2242bd9446cbf378e48e1 100644 (file)
@@ -489,7 +489,7 @@ std::string storeFeatures(const std::string& theDocName, DocumentPtr theDoc,
 typedef std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> > Storage;
 
 static bool dumpToPython(SessionPtr theSession,
-                         const char* theFilename,
+                         const std::string& theFilename,
                          const checkDumpType theSelectionType,
                          const std::string& theErrorMsgContext)
 {
@@ -516,7 +516,7 @@ static bool dumpToPython(SessionPtr theSession,
 }
 
 static bool checkDump(SessionPtr theSession,
-                      char* theFilename,
+                      const char* theFilename,
                       Storage& theStorage,
                       const std::string& theErrorMsgContext)
 {
@@ -544,20 +544,29 @@ static bool checkDump(SessionPtr theSession,
   return true;
 }
 
-bool checkPythonDump(const checkDumpType theCheckType)
+bool checkPyDump(const std::string& theFilenameNaming,
+                 const std::string& theFilenameGeo,
+                 const std::string& theFilenameWeak,
+                 const checkDumpType theCheckType)
 {
   static const std::string anErrorByNaming("checkPythonDump by naming");
   static const std::string anErrorByGeometry("checkPythonDump by geometry");
   static const std::string anErrorByWeak("checkPythonDump by weak naming");
 
-  static char aFileForNamingDump[] = "./check_dump.py";
-  static char aFileForGeometryDump[] = "./check_dump_geo.py";
-  static char aFileForWeakDump[] = "./check_dump_weak.py";
+#ifdef _DEBUG
+  std::string aFileForNamingDump("./check_dump.py");
+  std::string aFileForGeometryDump("./check_dump_geo.py");
+  std::string aFileForWeakDump("./check_dump_weak.py");
+#else
+  std::string aFileForNamingDump = theFilenameNaming;
+  std::string aFileForGeometryDump = theFilenameGeo;
+  std::string aFileForWeakDump = theFilenameWeak;
+#endif
 
   SessionPtr aSession = ModelAPI_Session::get();
   // dump with the specified types
-  char* aFileName = theCheckType == CHECK_GEOMETRICAL ? aFileForGeometryDump :
-                   (theCheckType == CHECK_WEAK ? aFileForWeakDump : aFileForNamingDump);
+  std::string aFileName = theCheckType == CHECK_GEOMETRICAL ? aFileForGeometryDump :
+                          (theCheckType == CHECK_WEAK ? aFileForWeakDump : aFileForNamingDump);
   if (!dumpToPython(aSession, aFileName, theCheckType, anErrorByNaming))
     return false;
 
@@ -574,14 +583,14 @@ bool checkPythonDump(const checkDumpType theCheckType)
   bool isOk = true;
   if (theCheckType & CHECK_NAMING) {
     // check dump with the selection by names
-    isOk = checkDump(aSession, aFileForNamingDump, aStore, anErrorByNaming);
+    isOk = checkDump(aSession, aFileForNamingDump.c_str(), aStore, anErrorByNaming);
   }
   if (theCheckType & CHECK_GEOMETRICAL) {
     // check dump with the selection by geometry
-    isOk = isOk && checkDump(aSession, aFileForGeometryDump, aStore, anErrorByGeometry);
+    isOk = isOk && checkDump(aSession, aFileForGeometryDump.c_str(), aStore, anErrorByGeometry);
   }
   if (theCheckType & CHECK_WEAK) {
-    isOk = isOk && checkDump(aSession, aFileForWeakDump, aStore, anErrorByWeak);
+    isOk = isOk && checkDump(aSession, aFileForWeakDump.c_str(), aStore, anErrorByWeak);
   }
 
   return isOk;
index e46596c3e1a0ea7191348fe68a073c08803d4bac..2b0b12c6c1fd234b85da5a17f95d914d17c8e544 100644 (file)
@@ -189,7 +189,10 @@ enum checkDumpType {
 /// model must be recreated fully, with all attributes
 /// \returns true if check is well done
 MODELHIGHAPI_EXPORT
-bool checkPythonDump(const checkDumpType theCheckType = CHECK_NAMING_AND_GEOMETRICAL);
+bool checkPyDump(const std::string& theFilenameNaming,
+                 const std::string& theFilenameGeo,
+                 const std::string& theFilenameWeak,
+                 const checkDumpType theCheckType = CHECK_NAMING_AND_GEOMETRICAL);
 
 //--------------------------------------------------------------------------------------
 //--------------------------------------------------------------------------------------
index a8bd057618b390629cf6d69983c5173f11af4493..ce63d93a2c13510c743094ea9088f588d91026e8 100644 (file)
@@ -28,7 +28,6 @@ from ModelHighAPI import undo, redo
 from ModelHighAPI import reset
 from ModelHighAPI import addFolder, removeFolder
 from ModelHighAPI import ModelHighAPI_Selection as selection
-from ModelHighAPI import checkPythonDump as checkPythonDump
 from ModelAPI import findPartFeature
 
 # a method used for the python dump of the SHAPER STUDY
@@ -44,3 +43,34 @@ def featureStringId(theFeature):
   if aRoot and aCurrent:
     return str(findPartFeature(aRoot, aCurrent).data().featureId()) + ":" + str(theFeature.feature().data().featureId())
   return ""
+
+
+import os
+
+def getTmpFileName(thePrefix, theSuffix):
+  import tempfile
+  tempdir = tempfile.gettempdir()
+  tmp_file = tempfile.NamedTemporaryFile(suffix=theSuffix, prefix=thePrefix, dir=tempdir, delete=False)
+  tmp_filename = tmp_file.name
+  if os.name == "nt":
+    tmp_filename.replace("\\", "/")
+  tmp_file.close()
+  return tmp_filename
+
+def removeTmpFile(theFilename):
+  try: os.remove(theFilename)
+  except OSError: pass
+
+# Verify the Python dump with generating the auxiliary filenames
+from ModelHighAPI import CHECK_NAMING_AND_GEOMETRICAL
+from ModelHighAPI import checkPyDump
+def checkPythonDump(theDumpMode = CHECK_NAMING_AND_GEOMETRICAL):
+  aPrefix = getTmpFileName("check_dump_", '')
+  aNaming = aPrefix + ".py"
+  aGeo  = aPrefix + "_geo.py"
+  aWeak = aPrefix + "_weak.py"
+  isOk = checkPyDump(aNaming, aGeo, aWeak, theDumpMode)
+  removeTmpFile(aNaming)
+  removeTmpFile(aGeo)
+  removeTmpFile(aWeak)
+  return isOk