Example #1
0
        ListAvailableCommands()
        {
            foreach (PyRevitClone clone in PyRevitClones.GetRegisteredClones())
            {
                PyRevitCLIAppCmds.PrintHeader($"Commands in Clone \"{clone.Name}\"");
                foreach (PyRevitExtension ext in clone.GetExtensions())
                {
                    if (ext.Type == PyRevitExtensionTypes.UIExtension)
                    {
                        foreach (PyRevitRunnerCommand cmd in ext.GetCommands())
                        {
                            Console.WriteLine(cmd);
                        }
                    }
                }
            }

            foreach (PyRevitExtension ext in PyRevitExtensions.GetInstalledExtensions())
            {
                if (ext.Type == PyRevitExtensionTypes.UIExtension)
                {
                    PyRevitCLIAppCmds.PrintHeader($"Commands in Extension \"{ext.Name}\"");
                    foreach (PyRevitRunnerCommand cmd in ext.GetCommands())
                    {
                        Console.WriteLine(cmd);
                    }
                }
            }
        }
Example #2
0
 PrintExtensionDefinitions(string searchPattern, string headerPrefix = "Registered")
 {
     PyRevitCLIAppCmds.PrintHeader(string.Format("{0} Extensions", headerPrefix));
     foreach (PyRevitExtensionDefinition ext in PyRevitExtensions.LookupRegisteredExtensions(searchPattern))
     {
         Console.WriteLine(ext);
     }
 }
Example #3
0
 private static void PrintBuildInfo()
 {
     PyRevitCLIAppCmds.PrintHeader("Supported Revits");
     foreach (var revit in RevitProduct.ListSupportedProducts().OrderByDescending(x => x.Version))
     {
         Console.WriteLine(string.Format("{0} | Version: {1} | Build: {2}({3})", revit.Name, revit.Version, revit.BuildNumber, revit.BuildTarget));
     }
 }
Example #4
0
 PrintExtensionLookupSources()
 {
     PyRevitCLIAppCmds.PrintHeader("Extension Sources - Default");
     Console.WriteLine(PyRevitExtensions.GetDefaultExtensionLookupSource());
     PyRevitCLIAppCmds.PrintHeader("Extension Sources - Additional");
     foreach (var extLookupSrc in PyRevitExtensions.GetRegisteredExtensionLookupSources())
     {
         Console.WriteLine(extLookupSrc);
     }
 }
Example #5
0
 PrintExtensionSearchPaths()
 {
     PyRevitCLIAppCmds.PrintHeader("Default Extension Search Path");
     Console.WriteLine(PyRevitConsts.DefaultExtensionsPath);
     PyRevitCLIAppCmds.PrintHeader("Extension Search Paths");
     foreach (var searchPath in PyRevitExtensions.GetRegisteredExtensionSearchPaths())
     {
         Console.WriteLine(searchPath);
     }
 }
 PrintCloneEngines(string cloneName)
 {
     if (cloneName != null)
     {
         var clone = PyRevitClones.GetRegisteredClone(cloneName);
         PyRevitCLIAppCmds.PrintHeader(string.Format("Deployments for \"{0}\"", clone.Name));
         foreach (var engine in clone.GetConfiguredEngines())
         {
             Console.WriteLine(engine);
         }
     }
 }
Example #7
0
        PrintExtensions(IEnumerable <PyRevitExtension> extList = null, string headerPrefix = "Installed")
        {
            if (extList is null)
            {
                extList = PyRevitExtensions.GetInstalledExtensions();
            }

            PyRevitCLIAppCmds.PrintHeader(string.Format("{0} Extensions", headerPrefix));
            foreach (PyRevitExtension ext in extList.OrderBy(x => x.Name))
            {
                Console.WriteLine(ext);
            }
        }
 PrintAttachments(int revitYear = 0)
 {
     PyRevitCLIAppCmds.PrintHeader("Attachments");
     foreach (var attachment in PyRevitAttachments.GetAttachments().OrderByDescending(x => x.Product.Version))
     {
         if (revitYear == 0)
         {
             Console.WriteLine(attachment);
         }
         else if (revitYear == attachment.Product.ProductYear)
         {
             Console.WriteLine(attachment);
         }
     }
 }
        PrintClones()
        {
            PyRevitCLIAppCmds.PrintHeader("Registered Clones (full git repos)");
            var clones = PyRevitClones.GetRegisteredClones().OrderBy(x => x.Name);

            foreach (var clone in clones.Where(c => c.IsRepoDeploy))
            {
                Console.WriteLine(clone);
            }

            PyRevitCLIAppCmds.PrintHeader("Registered Clones (deployed from archive/image)");
            foreach (var clone in clones.Where(c => !c.IsRepoDeploy))
            {
                Console.WriteLine(clone);
            }
        }
 PrintCloneDeployments(string cloneName)
 {
     if (cloneName != null)
     {
         var clone = PyRevitClones.GetRegisteredClone(cloneName);
         PyRevitCLIAppCmds.PrintHeader(string.Format("Deployments for \"{0}\"", clone.Name));
         foreach (var dep in clone.GetConfiguredDeployments())
         {
             Console.WriteLine(string.Format("\"{0}\" deploys:", dep.Name));
             foreach (var path in dep.Paths)
             {
                 Console.WriteLine("    " + path);
             }
             Console.WriteLine();
         }
     }
 }
Example #11
0
 PrintLocalRevits(bool running = false)
 {
     if (running)
     {
         PyRevitCLIAppCmds.PrintHeader("Running Revit Instances");
         foreach (var revit in RevitController.ListRunningRevits().OrderByDescending(x => x.RevitProduct.Version))
         {
             Console.WriteLine(revit);
         }
     }
     else
     {
         PyRevitCLIAppCmds.PrintHeader("Installed Revits");
         foreach (var revit in RevitProduct.ListInstalledProducts().OrderByDescending(x => x.Version))
         {
             Console.WriteLine(revit);
         }
     }
 }
        PrintReleases(string searchPattern, bool latest = false, bool printReleaseNotes = false, bool listPreReleases = false)
        {
            PyRevitCLIAppCmds.PrintHeader("Releases");
            List <PyRevitRelease> releasesToList = new List <PyRevitRelease>();

            // determine latest release
            if (latest)
            {
                var latestRelease = PyRevitReleases.GetLatestRelease(includePreRelease: listPreReleases);

                if (latestRelease is null)
                {
                    throw new PyRevitException("Can not determine latest release.");
                }

                releasesToList.Add(latestRelease);
            }
            else
            {
                if (searchPattern != null)
                {
                    releasesToList = PyRevitReleases.FindReleases(searchPattern, includePreRelease: listPreReleases);
                }
                else
                {
                    releasesToList = PyRevitReleases.GetReleases().Where(r => r.IsPyRevitRelease).ToList();
                }
            }

            foreach (var prelease in releasesToList)
            {
                Console.WriteLine(prelease);
                if (printReleaseNotes)
                {
                    Console.WriteLine(prelease.ReleaseNotes.Indent(1));
                }
            }
        }
 PrintCloneInfo(string cloneName)
 {
     PyRevitCLIAppCmds.PrintHeader("Clone info");
     Console.WriteLine(PyRevitClones.GetRegisteredClone(cloneName));
 }
Example #14
0
        RunExtensionCommand(string commandName, string targetFile, string revitYear, PyRevitRunnerOptions runOptions, bool targetIsFileList = false)
        {
            // verify command
            if (commandName is null || commandName == string.Empty)
            {
                throw new Exception("Command name must be provided.");
            }

            // try target revit year
            int revitYearNumber = 0;

            int.TryParse(revitYear, out revitYearNumber);


            // setup a list of models
            var modelFiles = new List <string>();

            // if target file is a list of model paths
            if (targetIsFileList)
            {
                // attempt at reading the list file and grab the model files only
                foreach (var modelPath in File.ReadAllLines(targetFile))
                {
                    modelFiles.Add(modelPath);
                }
            }
            // otherwise just work on this model
            else
            {
                modelFiles.Add(targetFile);
            }


            // verify all models are accessible
            foreach (string modelFile in modelFiles)
            {
                if (!CommonUtils.VerifyFile(modelFile))
                {
                    throw new Exception($"Model does not exist at \"{modelFile}\"");
                }
            }


            // determine which Revit version to launch
            if (revitYearNumber != 0)
            {
                foreach (string modelFile in modelFiles)
                {
                    var modelInfo = new RevitModelFile(modelFile);

                    // if specific revit version is provided, make sure model is not newer
                    if (modelInfo.RevitProduct != null)
                    {
                        if (modelInfo.RevitProduct.ProductYear > revitYearNumber)
                        {
                            throw new Exception($"Model at \"{modelFile}\" is newer than the specified version: {revitYearNumber}");
                        }
                    }
                    else
                    {
                        throw new Exception($"Can not detect the Revit version of model at \"{modelFile}\". Model might be newer than specified version {revitYearNumber}.");
                    }
                }
            }
            else
            {
                // determine revit model version from given files
                foreach (string modelFile in modelFiles)
                {
                    var modelInfo = new RevitModelFile(modelFile);
                    if (modelInfo.RevitProduct != null)
                    {
                        if (revitYearNumber == 0)
                        {
                            revitYearNumber = modelInfo.RevitProduct.ProductYear;
                        }
                        else if (modelInfo.RevitProduct.ProductYear > revitYearNumber)
                        {
                            revitYearNumber = modelInfo.RevitProduct.ProductYear;
                        }
                    }
                }

                // if could not determine revit version from given files,
                // use latest version
                if (revitYearNumber == 0)
                {
                    revitYearNumber = RevitProduct.ListInstalledProducts().Max(r => r.ProductYear);
                }
            }

            // now run
            if (revitYearNumber != 0)
            {
                // determine attached clone
                var attachment = PyRevitAttachments.GetAttached(revitYearNumber);
                if (attachment is null)
                {
                    logger.Error($"pyRevit is not attached to Revit \"{revitYearNumber}\". " +
                                 "Runner needs to use the attached clone and engine to execute the script.");
                }
                else
                {
                    // determine script to run
                    string commandScriptPath = null;

                    if (!CommonUtils.VerifyPythonScript(commandName))
                    {
                        logger.Debug("Input is not a script file \"{0}\"", commandName);
                        logger.Debug("Attempting to find run command matching \"{0}\"", commandName);

                        // 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(PyRevitExtensions.GetInstalledExtensions());

                        foreach (PyRevitExtension ext in targetExtensions)
                        {
                            logger.Debug("Searching for run command in: \"{0}\"", ext.ToString());
                            if (ext.Type == PyRevitExtensionTypes.UIExtension)
                            {
                                try {
                                    var cmdScript = ext.GetCommand(commandName);
                                    if (cmdScript != null)
                                    {
                                        logger.Debug("Run command matching \"{0}\" found: \"{1}\"",
                                                     commandName, cmdScript);
                                        commandScriptPath = cmdScript.Path;
                                        break;
                                    }
                                }
                                catch {
                                    // does not include command
                                    continue;
                                }
                            }
                        }
                    }
                    else
                    {
                        commandScriptPath = commandName;
                    }

                    // if command is not found, stop
                    if (commandScriptPath is null)
                    {
                        throw new PyRevitException(
                                  string.Format("Run command not found: \"{0}\"", commandName)
                                  );
                    }

                    // 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));
                    }
                }
            }
        }
Example #15
0
        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 is 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 = PyRevitAttachments.GetAttached(revitYearNumber);
                    if (attachment is 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(PyRevitExtensions.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 is 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));
                        }
                    }
                }
            }
        }