def Output(m="", msgId=""): timestamp = time_util.GetDateTimeNow().ToString("HH:mm:ss") message = timestamp + " : " + ( ("[" + str(msgId) + "]" + " ") if msgId != "" else "") + m + "\n" if SHOW_OUTPUT: ORIGINAL_STDOUT.write(message) if logging_util.LOG_FILE[0] is not None: logging_util.LOG_FILE[0].WriteMessage({"msgId": msgId, "message": m}) return
def MonitorProcess(process, monitoringAction, monitorIntervalInSeconds, unresponsiveThreshholdInSeconds, onBeginUnresponsive, onEndUnresponsive): wasResponding = True isResponding = True unresponsiveStartTime = None haveNotifiedBeginUnresponsive = False process.Refresh() while not process.HasExited: wasResponding = isResponding isResponding = IsProcessResponding(process) if wasResponding and not isResponding: # responsive -> unresponsive unresponsiveStartTime = time_util.GetDateTimeNow() haveNotifiedBeginUnresponsive = False elif isResponding and not wasResponding: # unresponsive -> responsive if haveNotifiedBeginUnresponsive: # notify end of unresponsiveness onEndUnresponsive( time_util.GetSecondsElapsedSince(unresponsiveStartTime)) haveNotifiedBeginUnresponsive = False elif not isResponding and not wasResponding: # continuing unresponsiveness if not haveNotifiedBeginUnresponsive: # notify unresponsiveness beyond threshold if time_util.GetSecondsElapsedSince( unresponsiveStartTime ) >= unresponsiveThreshholdInSeconds: onBeginUnresponsive() haveNotifiedBeginUnresponsive = True thread_util.SleepForSeconds(monitorIntervalInSeconds) WinForms.Application.DoEvents() monitoringAction() process.Refresh() # Was notified of beginning of unresponsiveness, therefore need to notify end of unresponsiveness. if haveNotifiedBeginUnresponsive: onEndUnresponsive( time_util.GetSecondsElapsedSince(unresponsiveStartTime)) haveNotifiedBeginUnresponsive = False return
raise finally: if openCreateNewLocal and deleteLocalAfter: try: if File.Exists(localFilePath): output() output("Deleting local file...") File.Delete(localFilePath) output() output("Local file deleted.") except Exception, e: output() output("WARNING: failed to delete the local file!") if enableDataExport: snapshotEndTime = time_util.GetDateTimeNow() snapshotData = snapshot_data_exporter.ExportSnapshotData( sessionId, centralFilePath, snapshotStartTime, snapshotEndTime, dataExportFolderPath, revitJournalFilePath, snapshotError) snapshot_data_util.ConsolidateSnapshotData( dataExportFolderPath, output) # Ensure aborted message is shown in the event of an exception. if aborted: output() output("Operation aborted.") if aborted: output() output("Operation aborted.")
def RunBatchTaskScript(scriptFilePath): aborted = False uiapp = revit_session.GetSessionUIApplication() sessionId = revit_script_util.GetSessionId() centralFilePath = revit_script_util.GetRevitFilePath() openInUI = revit_script_util.GetOpenInUI() enableDataExport = revit_script_util.GetEnableDataExport() dataExportFolderPath = revit_script_util.GetDataExportFolderPath() showMessageBoxOnTaskError = revit_script_util.GetShowMessageBoxOnTaskError( ) centralFileOpenOption = revit_script_util.GetCentralFileOpenOption() deleteLocalAfter = revit_script_util.GetDeleteLocalAfter() discardWorksetsOnDetach = revit_script_util.GetDiscardWorksetsOnDetach() progressNumber = revit_script_util.GetProgressNumber() progressMax = revit_script_util.GetProgressMax() output = revit_script_util.Output if enableDataExport and not path_util.DirectoryExists( dataExportFolderPath): output() output("ERROR: data export folder does not exist!") if not str.IsNullOrWhiteSpace(dataExportFolderPath): output() output("\t" + dataExportFolderPath) aborted = True elif not path_util.FileExists(centralFilePath): output() output("ERROR: Revit project file does not exist!") if not str.IsNullOrWhiteSpace(centralFilePath): output() output("\t" + centralFilePath) aborted = True else: if enableDataExport: snapshotStartTime = time_util.GetDateTimeNow() snapshotError = None snapshotEndTime = None revitJournalFilePath = uiapp.Application.RecordingJournalFilename snapshotData = snapshot_data_exporter.ExportTemporarySnapshotData( sessionId, centralFilePath, snapshotStartTime, snapshotEndTime, dataExportFolderPath, revitJournalFilePath, snapshotError) localFilePath = None openCreateNewLocal = False # default is False because the file may not be a workshared Central file. isCentralModel = False isLocalModel = False try: output() output("Processing file (" + str(progressNumber) + " of " + str(progressMax) + "): " + centralFilePath) if revit_file_util.IsWorkshared(centralFilePath): if revit_file_util.IsLocalModel(centralFilePath): output() output( "WARNING: the file being processed appears to be a Workshared Local file!" ) isLocalModel = True if revit_file_util.IsCentralModel(centralFilePath): output() output("The file is a Central Model file.") isCentralModel = True if centralFileOpenOption == BatchRvt.CentralFileOpenOption.CreateNewLocal: openCreateNewLocal = True elif path_util.HasFileExtension(centralFilePath, ".rfa"): output() output("The file is a Family file.") else: output() output("The file is a Non-workshared file.") if enableDataExport: output() output("Export folder is: " + dataExportFolderPath) def processDocument(doc): revit_script_util.SetScriptDocument(doc) def executeTaskScript(): success = False output() output("Task script operation started.") if path_util.HasFileExtension( scriptFilePath, script_util.DYNAMO_SCRIPT_FILE_EXTENSION): if revit_dynamo.IsDynamoRevitModuleLoaded(): revit_dynamo.ExecuteDynamoScript(uiapp, scriptFilePath, showUI=False) success = True else: success = False output() output(revit_dynamo_error. DYNAMO_REVIT_MODULE_NOT_FOUND_ERROR_MESSAGE) else: script_util.ExecuteScript(scriptFilePath) success = True if success: output() output("Task script operation completed.") else: output() output( "ERROR: An error occurred while executing the task script! Operation aborted." ) return result = script_host_error.WithErrorHandling( executeTaskScript, "ERROR: An error occurred while executing the task script! Operation aborted.", output, showMessageBoxOnTaskError) return result result = None activeDoc = None #revit_script_util.GetActiveDocument(uiapp) if activeDoc is not None: result = processDocument(activeDoc) else: if openCreateNewLocal: revitVersion = RevitVersion.GetSupportedRevitVersion( revit_session.GetSessionRevitVersionNumber()) localFilePath = RevitVersion.GetRevitLocalFilePath( revitVersion, centralFilePath) try: if File.Exists(localFilePath): output() output("Deleting existing local file...") File.Delete(localFilePath) output() output("Local file deleted.") except Exception, e: output() output("WARNING: failed to delete the local file!") path_util.CreateDirectoryForFilePath(localFilePath) result = revit_script_util.RunNewLocalDocumentAction( uiapp, openInUI, centralFilePath, localFilePath, processDocument, output) elif isCentralModel or isLocalModel: result = revit_script_util.RunDetachedDocumentAction( uiapp, openInUI, centralFilePath, discardWorksetsOnDetach, processDocument, output) else:
session_data_exporter.ExportSessionData( batchRvtConfig.SessionId, batchRvtConfig.SessionStartTime, None, batchRvtConfig.SessionDataFolderPath, None) sessionError = None try: if batchRvtConfig.RevitProcessingOption == BatchRvt.RevitProcessingOption.BatchRevitFileProcessing: aborted = RunBatchRevitTasks(batchRvtConfig) else: aborted = RunSingleRevitTask(batchRvtConfig) except Exception, e: sessionError = exception_util.GetExceptionDetails(e) raise finally: if batchRvtConfig.EnableDataExport: sessionEndTime = time_util.GetDateTimeNow() session_data_exporter.ExportSessionData( batchRvtConfig.SessionId, batchRvtConfig.SessionStartTime, sessionEndTime, batchRvtConfig.SessionDataFolderPath, sessionError) Output() if aborted: Output("Operation aborted.") else: Output("Operation completed.") Output() return