Maxscript - доступ к переменной из функции, которая находится внутри структуры - PullRequest
0 голосов
/ 23 октября 2019

Я почти завершил написание этой структуры для копирования содержимого одного файла в новый файл.

Переменная numLines - это то, где у меня возникают трудности.

См. Строку 46 где есть тот, с которым у меня проблемы, и рабочий пример в строке 50 вместе с дополнительными комментариями.

Я пытался использовать this.numLines в качестве этих переменных ссылок внутри структуры.

А также присвоение локальной переменной внутри fn, но, похоже, ничего не работает.

(
  local readFilePath  = @"path to old file here.ms"
  local writeFilePath = @"path to new file here.ms"
  global _3db_FileOps
  local _3db_FileOps_Struct
  struct _3db_FileOps_Struct
  (
    readPath = readFilePath,
    writePath = writeFilePath,
    rp = dotnetobject "System.IO.Streamreader" readPath,
    wp = dotnetobject "System.IO.StreamWriter" writePath,
    currLine = 0,
    numLines = 0,
    fn CloseFiles =
    (
      try Close readPath catch() ; try Close writePath catch()
      ),
    fn CloseReaders =
    (
     try rp.Close() catch() ; try wp.Close() catch()
     ),
    fn CountLines =
    (
      while not this.rp.EndOfStream do
      (
        this.rp.ReadLine()
        numLines += 1
        if (mod numLines 500) == 0 do try windows.processPostedMessages() catch()
        )
      return numLines as integer
      close readPath
      rp.Close()
      ),
    fn CopyContents =
    (
      start = timeStamp()
      while not this.rp.EndOfStream do
      (
        arr = this.rp.ReadLine()
        this.wp.WriteLine(arr)


        --Here I want to progress the progressbar with the equation 100.*currLine/numLines which is not working.
        --The numLines variable is not working in this because if I replace that with the blocked
        --Not working because of  the variable numLines.
        --         progressTest.doit_prog.value = 100.*currLine/this.numLines


        --This works.
        progressTest.doit_prog.value = 100.*currLine/2000


        currLine += 1
        )
      rp.Close()
      wp.Close()
      end = timeStamp()
      format "Processing took % seconds\n" ((end - start) / 1000.0)
      )
    )
  _3db_FileOps = _3db_FileOps_Struct()
  )

try destroydialog progressTest catch()
rollout progressTest "Progress Test"
(
  button calculate "Scan file"
  label fileinfo "Lines in file" style_sunkenedge:true width:180 height:16
  button doit "Process file"
  progressbar doit_prog color:blue
  on calculate pressed do
  (
    start = timeStamp()
    result = _3db_FileOps.CountLines()
    _3db_FileOps.CloseFiles()
    fileinfo.text = result as string
    end = timeStamp()
    format "Processing took % milliseconds\n" ((end - start) / 1000.0)
    )
  on doit pressed do ( _3db_FileOps.CopyContents() ; _3db_FileOps.CloseFiles() )
  )
createDialog progressTest 200 120
...