newer 发表于 2021-1-26 16:29:12

内存中进行批处理文件

问题:

Batch process in-memory


解答:

When we try batch process files in memory, we must take special attention to which transaction is in use, but the real trick is how to save the file: AutoCAD will lock the file in use and therefore the SaveAs method is not available for the current file, so the trick could be save as a temporary file and then replace it. This approach can be used to batch process and is faster then open drawings “on scree” (visible). The trade-off is that some features may not be available, such as Layout Manager.

   Public Sub batchProcess()
   ' get all DWG files from a specific folder
   Dim directory As New System.IO.DirectoryInfo("C:\temp")
   Dim files As System.IO.FileInfo() = _
       directory.GetFiles("*.dwg")

   For Each file As System.IO.FileInfo In files
       ' generate a temp file location
       Dim tempFileName As String = _
         System.IO.Path.GetTempFileName()

       Using db As New Database(False, True)
         ' open the current file
         db.ReadDwgFile(file.FullName, _
                        System.IO.FileShare.ReadWrite, _
                        True, String.Empty)

         Using trans As Transaction = _
         db.TransactionManager.StartTransaction()

         ' ToDo:
         ' Do your processing here


         ' commit changes
         trans.Commit()
         End Using

         ' save as temp file
         db.SaveAs(tempFileName, DwgVersion.Current)

       End Using ' dispose the database

       ' now replace
       System.IO.File.Copy(tempFileName, file.FullName, True)
       ' and erase the temp file
       System.IO.File.Delete(tempFileName)
   Next
   End Sub


页: [1]
查看完整版本: 内存中进行批处理文件