SwitchAttachment(string cloneName, string revitYear) { var clone = PyRevit.GetRegisteredClone(cloneName); if (revitYear != null) { int revitYearNumber = 0; if (int.TryParse(revitYear, out revitYearNumber)) { var attachment = PyRevit.GetAttached(revitYearNumber); if (attachment != null) { if (attachment.Engine != null) { PyRevit.Attach(attachment.Product.ProductYear, clone, engineVer: attachment.Engine.Version, allUsers: attachment.AllUsers); } else { throw new pyRevitException( string.Format("Can not determine attachment engine for Revit \"{0}\"", revitYear) ); } } else { throw new pyRevitException( string.Format("Can not determine existing attachment for Revit \"{0}\"", revitYear) ); } } else { throw new pyRevitException(string.Format("Invalid Revit year \"{0}\"", revitYear)); } } else { // read current attachments and reattach using the same config with the new clone foreach (var attachment in PyRevit.GetAttachments()) { if (attachment.Engine != null) { PyRevit.Attach( attachment.Product.ProductYear, clone, engineVer: attachment.Engine.Version, allUsers: attachment.AllUsers); } else { throw new pyRevitException("Can not determine attachment engine."); } } } }
RunPythonCommand(string inputCommand, string targetFile, string revitYear, PyRevitRunnerOptions runOptions) { // determine if script or command var modelFiles = new List <string>(); // make sure file exists if (targetFile != null) { CommonUtils.VerifyFile(targetFile); } if (inputCommand != null) { // determine target revit year int revitYearNumber = 0; // if revit year is not specified try to get from model file if (revitYear == null) { if (targetFile != null) { try { revitYearNumber = new RevitModelFile(targetFile).RevitProduct.ProductYear; // collect model names also modelFiles.Add(targetFile); } catch (Exception ex) { logger.Error( "Revit version must be explicitly specifies if using a model list file. | {0}", ex.Message ); } } // if no revit year and no model, run with latest revit else { revitYearNumber = RevitProduct.ListInstalledProducts().Max(r => r.ProductYear); } } // otherwise, grab the year from argument else { revitYearNumber = int.Parse(revitYear); // prepare model list of provided if (targetFile != null) { try { var modelVer = new RevitModelFile(targetFile).RevitProduct.ProductYear; if (revitYearNumber < modelVer) { logger.Warn("Model is newer than the target Revit version."); } else { modelFiles.Add(targetFile); } } catch { // attempt at reading the list file and grab the model files only foreach (var modelPath in File.ReadAllLines(targetFile)) { if (CommonUtils.VerifyFile(modelPath)) { try { var modelVer = new RevitModelFile(modelPath).RevitProduct.ProductYear; if (revitYearNumber < modelVer) { logger.Warn("Model is newer than the target Revit version."); } else { modelFiles.Add(modelPath); } } catch { logger.Error("File is not a valid Revit file: \"{0}\"", modelPath); } } else { logger.Error("File does not exist: \"{0}\"", modelPath); } } } } } // now run if (revitYearNumber != 0) { // determine attached clone var attachment = PyRevit.GetAttached(revitYearNumber); if (attachment == null) { logger.Error("pyRevit is not attached to Revit \"{0}\". " + "Runner needs to use the attached clone and engine to execute the script.", revitYear); } else { // determine script to run string commandScriptPath = null; if (!CommonUtils.VerifyPythonScript(inputCommand)) { logger.Debug("Input is not a script file \"{0}\"", inputCommand); logger.Debug("Attempting to find run command matching \"{0}\"", inputCommand); // try to find run command in attached clone being used for execution // if not found, try to get run command from all other installed extensions var targetExtensions = new List <PyRevitExtension>(); if (attachment.Clone != null) { targetExtensions.AddRange(attachment.Clone.GetExtensions()); } targetExtensions.AddRange(PyRevit.GetInstalledExtensions()); foreach (PyRevitExtension ext in targetExtensions) { logger.Debug("Searching for run command in: \"{0}\"", ext.ToString()); if (ext.Type == PyRevitExtensionTypes.RunnerExtension) { try { var cmdScript = ext.GetRunCommand(inputCommand); if (cmdScript != null) { logger.Debug("Run command matching \"{0}\" found: \"{1}\"", inputCommand, cmdScript); commandScriptPath = cmdScript; break; } } catch { // does not include command continue; } } } } else { commandScriptPath = inputCommand; } // if command is not found, stop if (commandScriptPath == null) { throw new pyRevitException( string.Format("Run command not found: \"{0}\"", inputCommand) ); } // RUN! var execEnv = PyRevitRunner.Run( attachment, commandScriptPath, modelFiles, runOptions ); // print results (exec env) PyRevitCLIAppCmds.PrintHeader("Execution Environment"); Console.WriteLine(string.Format("Execution Id: \"{0}\"", execEnv.ExecutionId)); Console.WriteLine(string.Format("Product: {0}", execEnv.Revit)); Console.WriteLine(string.Format("Clone: {0}", execEnv.Clone)); Console.WriteLine(string.Format("Engine: {0}", execEnv.Engine)); Console.WriteLine(string.Format("Script: \"{0}\"", execEnv.Script)); Console.WriteLine(string.Format("Working Directory: \"{0}\"", execEnv.WorkingDirectory)); Console.WriteLine(string.Format("Journal File: \"{0}\"", execEnv.JournalFile)); Console.WriteLine(string.Format("Manifest File: \"{0}\"", execEnv.PyRevitRunnerManifestFile)); Console.WriteLine(string.Format("Log File: \"{0}\"", execEnv.LogFile)); // report whether the env was purge or not if (execEnv.Purged) { Console.WriteLine("Execution env is successfully purged."); } // print target models if (execEnv.ModelPaths.Count() > 0) { PyRevitCLIAppCmds.PrintHeader("Target Models"); foreach (var modelPath in execEnv.ModelPaths) { Console.WriteLine(modelPath); } } // print log file contents if exists if (File.Exists(execEnv.LogFile)) { PyRevitCLIAppCmds.PrintHeader("Execution Log"); Console.WriteLine(File.ReadAllText(execEnv.LogFile)); } } } } }