SwitchAttachment(string cloneName, string revitYear)
        {
            var clone = PyRevitClones.GetRegisteredClone(cloneName);

            if (revitYear != null)
            {
                int revitYearNumber = 0;
                if (int.TryParse(revitYear, out revitYearNumber))
                {
                    var attachment = PyRevitAttachments.GetAttached(revitYearNumber);
                    if (attachment != null)
                    {
                        if (attachment.Engine != null)
                        {
                            PyRevitAttachments.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 PyRevitAttachments.GetAttachments())
                {
                    if (attachment.Engine != null)
                    {
                        PyRevitAttachments.Attach(
                            attachment.Product.ProductYear,
                            clone,
                            engineVer: attachment.Engine.Version,
                            allUsers: attachment.AllUsers);
                    }
                    else
                    {
                        throw new PyRevitException("Can not determine attachment engine.");
                    }
                }
            }
        }
        AttachClone(PyRevitClone clone, PyRevitEngineVersion engineVersion,
                    string revitYear, bool installed, bool attached,
                    bool allUsers)
        {
            // decide targets revits to attach to
            int revitYearNumber = 0;

            if (installed)
            {
                foreach (var revit in RevitProduct.ListInstalledProducts())
                {
                    PyRevitAttachments.Attach(revit.ProductYear, clone, engineVer: engineVersion, allUsers: allUsers);
                }
            }
            else if (attached)
            {
                foreach (var attachment in PyRevitAttachments.GetAttachments())
                {
                    PyRevitAttachments.Attach(attachment.Product.ProductYear, clone, engineVer: engineVersion, allUsers: allUsers);
                }
            }
            else if (int.TryParse(revitYear, out revitYearNumber))
            {
                PyRevitAttachments.Attach(revitYearNumber, clone, engineVer: engineVersion, allUsers: allUsers);
            }
        }
Esempio n. 3
0
        CreateEnvJson()
        {
            // collecet search paths
            var searchPaths = new List <string>()
            {
                PyRevitConsts.DefaultExtensionsPath
            };

            searchPaths.AddRange(PyRevitExtensions.GetRegisteredExtensionSearchPaths());

            // collect list of lookup sources
            var lookupSrc = new List <string>()
            {
                PyRevitExtensions.GetDefaultExtensionLookupSource()
            };

            lookupSrc.AddRange(PyRevitExtensions.GetRegisteredExtensionLookupSources());

            // create json data object
            var jsonData = new Dictionary <string, object>()
            {
                { "meta", new Dictionary <string, object>()
                  {
                      { "version", "0.1.0" }
                  } },
                { "clones", PyRevitClones.GetRegisteredClones() },
                { "attachments", PyRevitAttachments.GetAttachments() },
                { "extensions", PyRevitExtensions.GetInstalledExtensions() },
                { "searchPaths", searchPaths },
                { "lookupSources", lookupSrc },
                { "installed", RevitProduct.ListInstalledProducts() },
                { "running", RevitController.ListRunningRevits() },
                { "pyrevitDataDir", PyRevitLabsConsts.PyRevitPath },
                { "userEnv", new Dictionary <string, object>()
                  {
                      { "osVersion", UserEnv.GetWindowsVersion() },
                      { "execUser", string.Format("{0}\\{1}", Environment.UserDomainName, Environment.UserName) },
                      { "activeUser", UserEnv.GetLoggedInUserName() },
                      { "isAdmin", UserEnv.IsRunAsAdmin() },
                      { "userAppdata", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) },
                      { "latestFramework", UserEnv.GetInstalledDotNetVersion() },
                      { "targetPacks", UserEnv.GetInstalledDotnetTargetPacks() },
                      { "targetPacksCore", UserEnv.GetInstalledDotnetCoreTargetPacks() },
                      { "cliVersion", PyRevitCLI.CLIVersion },
                  } },
            };

            var jsonExportCfg = new JsonSerializerSettings {
                Error = delegate(object sender, pyRevitLabs.Json.Serialization.ErrorEventArgs args) {
                    args.ErrorContext.Handled = true;
                },
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            };

            jsonExportCfg.Converters.Add(new JsonVersionConverter());

            return(JsonConvert.SerializeObject(jsonData, jsonExportCfg));
        }
Esempio n. 4
0
        AttachClone(string cloneName,
                    bool latest, bool dynamoSafe, string engineVersion,
                    string revitYear, bool installed, bool attached,
                    bool allUsers)
        {
            var clone = PyRevitClones.GetRegisteredClone(cloneName);

            // grab engine version
            int engineVer = 0;

            int.TryParse(engineVersion, out engineVer);

            if (latest)
            {
                logger.Debug("Attaching on latest engine...");
                var latestCloneEngine =
                    clone.GetEngines().Where(x => x.Runtime).OrderByDescending(x => x.Version).First();
                logger.Debug(string.Format("Latest engine: {0}", latestCloneEngine));
                if (latestCloneEngine != null)
                {
                    engineVer = latestCloneEngine.Version;
                }
                else
                {
                    throw new PyRevitException("Can not determine latest runtime engine for this clone.");
                }
            }
            else if (dynamoSafe)
            {
                logger.Debug("Attaching on dynamo-safe engine");
                engineVer = PyRevitConsts.ConfigsDynamoCompatibleEnginerVer;
            }

            // decide targets revits to attach to
            int revitYearNumber = 0;

            if (installed)
            {
                foreach (var revit in RevitProduct.ListInstalledProducts())
                {
                    PyRevitAttachments.Attach(revit.ProductYear, clone, engineVer: engineVer, allUsers: allUsers);
                }
            }
            else if (attached)
            {
                foreach (var attachment in PyRevitAttachments.GetAttachments())
                {
                    PyRevitAttachments.Attach(attachment.Product.ProductYear, clone, engineVer: engineVer, allUsers: allUsers);
                }
            }
            else if (int.TryParse(revitYear, out revitYearNumber))
            {
                PyRevitAttachments.Attach(revitYearNumber, clone, engineVer: engineVer, allUsers: allUsers);
            }
        }
 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);
         }
     }
 }
        DeleteClone(bool allClones, string cloneName, bool clearConfigs)
        {
            if (allClones)
            {
                PyRevitClones.DeleteAllClones(clearConfigs: clearConfigs);
            }
            else if (cloneName != null)
            {
                // lets detach the clone if there is any attachments to it
                var clone = PyRevitClones.GetRegisteredClone(cloneName);
                foreach (var attachment in PyRevitAttachments.GetAttachments())
                {
                    if (attachment.Clone.Equals(clone))
                    {
                        logger.Debug($"Detaching existing attachment: {attachment}");
                        PyRevitAttachments.Detach(attachment.Product.ProductYear, attachment.AllUsers);
                    }
                }

                PyRevitClones.Delete(clone, clearConfigs: clearConfigs);
            }
        }