def LoadScriptDatas(): scriptDataFilePath = GetScriptDataFilePath() if scriptDataFilePath is None: raise Exception("ERROR: could not retrieve script data file path from host.") scriptDatas = ScriptDataUtil.LoadManyFromFile(scriptDataFilePath) if scriptDatas is None: raise Exception("ERROR: could not load script data file.") return scriptDatas
def RunSingleRevitTask(batchRvtConfig): aborted = False revitVersion = batchRvtConfig.SingleRevitTaskRevitVersion Output() Output("Revit Version:") Output() Output("\t" + RevitVersion.GetRevitVersionText(revitVersion)) if revitVersion not in RevitVersion.GetInstalledRevitVersions(): Output() Output( "ERROR: The specified Revit version is not installed or the addin is not installed for it." ) aborted = True if not aborted: if batchRvtConfig.ExecutePreProcessingScript: aborted = ExecutePreProcessingScript(batchRvtConfig, Output) if not aborted: Output() Output("Starting single task operation...") Output() Output("Starting Revit " + RevitVersion.GetRevitVersionText(revitVersion) + " session...") scriptData = ScriptDataUtil.ScriptData() scriptData.SessionId.SetValue(batchRvtConfig.SessionId) scriptData.TaskScriptFilePath.SetValue(batchRvtConfig.ScriptFilePath) scriptData.TaskData.SetValue(batchRvtConfig.TaskData) scriptData.EnableDataExport.SetValue(batchRvtConfig.EnableDataExport) scriptData.SessionDataFolderPath.SetValue( batchRvtConfig.SessionDataFolderPath) scriptData.ShowMessageBoxOnTaskScriptError.SetValue( batchRvtConfig.ShowMessageBoxOnTaskError) scriptData.RevitProcessingOption.SetValue( batchRvtConfig.RevitProcessingOption) scriptData.ProgressNumber.SetValue(1) scriptData.ProgressMax.SetValue(1) batchRvtScriptsFolderPath = BatchRvt.GetBatchRvtScriptsFolderPath() batch_rvt_monitor_util.RunScriptedRevitSession( revitVersion, batchRvtScriptsFolderPath, batchRvtConfig.ScriptFilePath, [scriptData], 1, batchRvtConfig.ProcessingTimeOutInMinutes, batchRvtConfig.ShowRevitProcessErrorMessages, batchRvtConfig.TestModeFolderPath, Output) if not aborted: if batchRvtConfig.ExecutePostProcessingScript: aborted = ExecutePostProcessingScript(batchRvtConfig, Output) return aborted
def monitoringAction(): pendingProcessOutputReadLineTask[0] = ShowRevitProcessOutput( hostRevitProcess.StandardOutput, output, pendingProcessOutputReadLineTask[0]) pendingProcessErrorReadLineTask[0] = ShowRevitProcessError( hostRevitProcess.StandardError, showRevitProcessErrorMessages, output, pendingProcessErrorReadLineTask[0]) pendingReadLineTask[0] = ShowRevitScriptOutput( scriptOutputStreamReader, output, pendingReadLineTask[0]) if time_util.GetSecondsElapsedSinceUtc( progressRecordCheckTimeUtc[0] ) > REVIT_PROGRESS_CHECK_INTERVAL_IN_SECONDS: progressRecordCheckTimeUtc[ 0] = time_util.GetDateTimeUtcNow() progressRecordNumber = ScriptDataUtil.GetProgressNumber( progressRecordFilePath) if progressRecordNumber is not None: if currentProgressRecordNumber[ 0] != progressRecordNumber: # Progress update detected. currentProgressRecordNumber[ 0] = progressRecordNumber progressRecordChangedTimeUtc[ 0] = time_util.GetDateTimeUtcNow() if processingTimeOutInMinutes > 0: if currentProgressRecordNumber[0] != 0: if time_util.GetSecondsElapsedSinceUtc( progressRecordChangedTimeUtc[0]) > ( processingTimeOutInMinutes * SECONDS_PER_MINUTE): output() output( "WARNING: Timed-out waiting for Revit task / file to be processed. Forcibly terminating the Revit process..." ) TerminateHostRevitProcess(hostRevitProcess, output) if currentProgressRecordNumber[0] == 0: if time_util.GetSecondsElapsedSinceUtc( progressRecordChangedTimeUtc[0] ) > REVIT_PROCESS_BEGIN_PROCESSING_TIMEOUT_IN_SECONDS: output() output( "WARNING: Timed-out waiting for Revit script host to begin task / file processing. Forcibly terminating the Revit process..." ) TerminateHostRevitProcess(hostRevitProcess, output) if snapshotDataFilesExistTimestamp[0] is not None: if time_util.GetSecondsElapsedSinceUtc( snapshotDataFilesExistTimestamp[0] ) > REVIT_PROCESS_EXIT_TIMEOUT_IN_SECONDS: output() output( "WARNING: Timed-out waiting for the Revit process to exit. Forcibly terminating the Revit process..." ) TerminateHostRevitProcess(hostRevitProcess, output) elif snapshotDataFilePaths.All( lambda snapshotDataFilePath: File.Exists( snapshotDataFilePath)): output() output( "Detected snapshot data files. Waiting for Revit process to exit..." ) snapshotDataFilesExistTimestamp[ 0] = time_util.GetDateTimeUtcNow() try: revit_dialog_detection.DismissCheekyRevitDialogBoxes( hostRevitProcessId, output) except Exception, e: output() output( "WARNING: an error occurred in the cheeky Revit dialog box dismisser!" ) exception_util.LogOutputErrorDetails(e, output)
def RunScriptedRevitSession(revitVersion, batchRvtScriptsFolderPath, scriptFilePath, scriptDatas, progressNumber, processingTimeOutInMinutes, showRevitProcessErrorMessages, testModeFolderPath, output): scriptDataFilePath = ScriptDataUtil.GetUniqueScriptDataFilePath() ScriptDataUtil.SaveManyToFile(scriptDataFilePath, scriptDatas) progressRecordFilePath = ScriptDataUtil.GetProgressRecordFilePath( scriptDataFilePath) serverStream = server_util.CreateAnonymousPipeServer( server_util.IN, server_util.HandleInheritability.Inheritable) def serverStreamAction(): scriptOutputStreamReader = stream_io_util.GetStreamReader(serverStream) def streamReaderAction(): scriptOutputPipeHandleString = serverStream.GetClientHandleAsString( ) def clientHandleAction(): hostRevitProcess = revit_process_host.StartHostRevitProcess( revitVersion, batchRvtScriptsFolderPath, scriptFilePath, scriptDataFilePath, progressNumber, scriptOutputPipeHandleString, testModeFolderPath) return hostRevitProcess hostRevitProcess = UsingClientHandle(serverStream, clientHandleAction) hostRevitProcessId = hostRevitProcess.Id global_test_mode.ExportRevitProcessId(hostRevitProcessId) snapshotDataFilePaths = [ snapshot_data_util.GetSnapshotDataFilePath( scriptData.DataExportFolderPath.GetValue()) for scriptData in scriptDatas ] pendingReadLineTask = [ None ] # Needs to be a list so it can be captured by reference in closures. pendingProcessOutputReadLineTask = [None] # As above. pendingProcessErrorReadLineTask = [None] # As above. snapshotDataFilesExistTimestamp = [ None ] # Needs to be a list so it can be captured by reference in closures. currentProgressRecordNumber = [ 0 ] # Needs to be a list so it can be captured by reference in closures. progressRecordCheckTimeUtc = [time_util.GetDateTimeUtcNow() ] # As above. progressRecordChangedTimeUtc = [time_util.GetDateTimeUtcNow() ] # As above. def monitoringAction(): pendingProcessOutputReadLineTask[0] = ShowRevitProcessOutput( hostRevitProcess.StandardOutput, output, pendingProcessOutputReadLineTask[0]) pendingProcessErrorReadLineTask[0] = ShowRevitProcessError( hostRevitProcess.StandardError, showRevitProcessErrorMessages, output, pendingProcessErrorReadLineTask[0]) pendingReadLineTask[0] = ShowRevitScriptOutput( scriptOutputStreamReader, output, pendingReadLineTask[0]) if time_util.GetSecondsElapsedSinceUtc( progressRecordCheckTimeUtc[0] ) > REVIT_PROGRESS_CHECK_INTERVAL_IN_SECONDS: progressRecordCheckTimeUtc[ 0] = time_util.GetDateTimeUtcNow() progressRecordNumber = ScriptDataUtil.GetProgressNumber( progressRecordFilePath) if progressRecordNumber is not None: if currentProgressRecordNumber[ 0] != progressRecordNumber: # Progress update detected. currentProgressRecordNumber[ 0] = progressRecordNumber progressRecordChangedTimeUtc[ 0] = time_util.GetDateTimeUtcNow() if processingTimeOutInMinutes > 0: if currentProgressRecordNumber[0] != 0: if time_util.GetSecondsElapsedSinceUtc( progressRecordChangedTimeUtc[0]) > ( processingTimeOutInMinutes * SECONDS_PER_MINUTE): output() output( "WARNING: Timed-out waiting for Revit task / file to be processed. Forcibly terminating the Revit process..." ) TerminateHostRevitProcess(hostRevitProcess, output) if currentProgressRecordNumber[0] == 0: if time_util.GetSecondsElapsedSinceUtc( progressRecordChangedTimeUtc[0] ) > REVIT_PROCESS_BEGIN_PROCESSING_TIMEOUT_IN_SECONDS: output() output( "WARNING: Timed-out waiting for Revit script host to begin task / file processing. Forcibly terminating the Revit process..." ) TerminateHostRevitProcess(hostRevitProcess, output) if snapshotDataFilesExistTimestamp[0] is not None: if time_util.GetSecondsElapsedSinceUtc( snapshotDataFilesExistTimestamp[0] ) > REVIT_PROCESS_EXIT_TIMEOUT_IN_SECONDS: output() output( "WARNING: Timed-out waiting for the Revit process to exit. Forcibly terminating the Revit process..." ) TerminateHostRevitProcess(hostRevitProcess, output) elif snapshotDataFilePaths.All( lambda snapshotDataFilePath: File.Exists( snapshotDataFilePath)): output() output( "Detected snapshot data files. Waiting for Revit process to exit..." ) snapshotDataFilesExistTimestamp[ 0] = time_util.GetDateTimeUtcNow() try: revit_dialog_detection.DismissCheekyRevitDialogBoxes( hostRevitProcessId, output) except Exception, e: output() output( "WARNING: an error occurred in the cheeky Revit dialog box dismisser!" ) exception_util.LogOutputErrorDetails(e, output) return monitor_revit_process.MonitorHostRevitProcess( hostRevitProcess, monitoringAction, output) return stream_io_util.UsingStream(scriptOutputStreamReader, streamReaderAction) return
0] = time_util.GetDateTimeUtcNow() try: revit_dialog_detection.DismissCheekyRevitDialogBoxes( hostRevitProcessId, output) except Exception, e: output() output( "WARNING: an error occurred in the cheeky Revit dialog box dismisser!" ) exception_util.LogOutputErrorDetails(e, output) return monitor_revit_process.MonitorHostRevitProcess( hostRevitProcess, monitoringAction, output) return stream_io_util.UsingStream(scriptOutputStreamReader, streamReaderAction) return stream_io_util.UsingStream(serverStream, serverStreamAction) lastProgressNumber = ScriptDataUtil.GetProgressNumber( progressRecordFilePath) nextProgressNumber = (lastProgressNumber + 1) if lastProgressNumber is not None else None return nextProgressNumber
def ProcessRevitFiles(batchRvtConfig, supportedRevitFileList): aborted = False totalFilesCount = len(supportedRevitFileList) progressNumber = 1 for revitVersion, supportedRevitFiles in GroupByRevitVersion( batchRvtConfig, supportedRevitFileList): sessionRevitFiles = [] queuedRevitFiles = list(supportedRevitFiles) while queuedRevitFiles.Any(): scriptDatas = [] snapshotDataExportFolderPaths = [] if batchRvtConfig.RevitSessionOption == BatchRvt.RevitSessionOption.UseSameSessionForFilesOfSameVersion: sessionRevitFiles = queuedRevitFiles queuedRevitFiles = [] else: sessionRevitFiles = [queuedRevitFiles[0]] queuedRevitFiles = queuedRevitFiles[1:] sessionFilesCount = len(sessionRevitFiles) if len(sessionRevitFiles) == 1: Output() Output("Processing Revit file (" + str(progressNumber) + " of " + str(totalFilesCount) + ")" + " in Revit " + RevitVersion.GetRevitVersionText(revitVersion) + " session.") else: Output() Output("Processing Revit files (" + str(progressNumber) + " to " + str(progressNumber + sessionFilesCount - 1) + " of " + str(totalFilesCount) + ")" + " in Revit " + RevitVersion.GetRevitVersionText(revitVersion) + " session.") for supportedRevitFileInfo in sessionRevitFiles: batch_rvt_monitor_util.ShowSupportedRevitFileInfo( supportedRevitFileInfo, Output) Output() Output("Starting Revit " + RevitVersion.GetRevitVersionText(revitVersion) + " session...") for index, supportedRevitFileInfo in enumerate(sessionRevitFiles): snapshotDataExportFolderPath = str.Empty revitFilePath = supportedRevitFileInfo.GetRevitFileInfo( ).GetFullPath() if batchRvtConfig.EnableDataExport: snapshotDataExportFolderPath = snapshot_data_util.GetSnapshotFolderPath( batchRvtConfig.DataExportFolderPath, revitFilePath, batchRvtConfig.SessionStartTime) path_util.CreateDirectory(snapshotDataExportFolderPath) snapshotDataExportFolderPaths.append( snapshotDataExportFolderPath) revitFilePath = supportedRevitFileInfo.GetRevitFileInfo( ).GetFullPath() scriptData = ScriptDataUtil.ScriptData() scriptData.SessionId.SetValue(batchRvtConfig.SessionId) scriptData.TaskScriptFilePath.SetValue( batchRvtConfig.ScriptFilePath) scriptData.RevitFilePath.SetValue(revitFilePath) scriptData.TaskData.SetValue(batchRvtConfig.TaskData) scriptData.OpenInUI.SetValue(batchRvtConfig.OpenInUI) scriptData.EnableDataExport.SetValue( batchRvtConfig.EnableDataExport) scriptData.SessionDataFolderPath.SetValue( batchRvtConfig.SessionDataFolderPath) scriptData.DataExportFolderPath.SetValue( snapshotDataExportFolderPath) scriptData.ShowMessageBoxOnTaskScriptError.SetValue( batchRvtConfig.ShowMessageBoxOnTaskError) scriptData.RevitProcessingOption.SetValue( batchRvtConfig.RevitProcessingOption) scriptData.CentralFileOpenOption.SetValue( batchRvtConfig.CentralFileOpenOption) scriptData.DeleteLocalAfter.SetValue( batchRvtConfig.DeleteLocalAfter) scriptData.DiscardWorksetsOnDetach.SetValue( batchRvtConfig.DiscardWorksetsOnDetach) scriptData.ProgressNumber.SetValue(progressNumber + index) scriptData.ProgressMax.SetValue(totalFilesCount) scriptDatas.append(scriptData) batchRvtScriptsFolderPath = BatchRvt.GetBatchRvtScriptsFolderPath() while scriptDatas.Any(): nextProgressNumber = batch_rvt_monitor_util.RunScriptedRevitSession( revitVersion, batchRvtScriptsFolderPath, batchRvtConfig.ScriptFilePath, scriptDatas, progressNumber, batchRvtConfig.ProcessingTimeOutInMinutes, batchRvtConfig.TestModeFolderPath, Output) if nextProgressNumber is None: Output() Output( "WARNING: The Revit session failed to initialize properly! No Revit files were processed in this session!" ) progressNumber += len(scriptDatas) break else: progressNumber = nextProgressNumber scriptDatas = (scriptDatas.Where( lambda scriptData: scriptData.ProgressNumber.GetValue( ) >= progressNumber).ToList()) if batchRvtConfig.EnableDataExport: Output() Output("Consolidating snapshots data.") for snapshotDataExportFolderPath in snapshotDataExportFolderPaths: snapshot_data_util.ConsolidateSnapshotData( snapshotDataExportFolderPath, Output) # NOTE: Have disabled copying of journal files for now because if many files were processed # in the same Revit session, too many copies of a potentially large journal file # will be made. Consider modifying the logic so that the journal file is copied only # once per Revit seesion. Perhaps copy it to the BatchRvt session folder. if False: try: snapshot_data_util.CopySnapshotRevitJournalFile( snapshotDataExportFolderPath, Output) except Exception, e: Output() Output( "WARNING: failed to copy the Revit session's journal file to snapshot data folder:" ) Output() Output("\t" + snapshotDataExportFolderPath) exception_util.LogOutputErrorDetails(e, Output)