/// <summary>
        /// Reads the version selection data for specific revit version from
        /// registry and returns a new VersionSelectorData.
        /// </summary>
        /// <param name="revitVersion">Revit version</param>
        /// <returns>VersionSelectorData</returns>
        public static VersionSelectorData ReadFromRegistry(string revitVersion)
        {
            var data = new VersionSelectorData()
            {
                RevitVersion = revitVersion
            };

            data.SelectedVersion = data.ThisDynamoVersion; //Default initialization

            try
            {
                var regKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
                regKey = regKey.OpenSubKey(RegKey64);

                var key = string.Format(@"Dynamo {0}.{1}", data.ThisDynamoVersion.Major, data.ThisDynamoVersion.Minor);
                regKey = regKey.OpenSubKey(key);
                if (regKey == null)
                {
                    return(data);
                }

                var version = regKey.GetValue(data.RevitVersion);

                Version selectedVersion = null;
                if (Version.TryParse(version as string, out selectedVersion))
                {
                    data.SelectedVersion = selectedVersion;
                }
            }
            catch (SystemException) { }

            return(data);
        }
示例#2
0
        public static bool LaunchDynamoCommand(int index, ExternalCommandData commandData)
        {
            if (index >= Products.Count)
            {
                return(false); //Index out of range
            }
            try
            {
                var revitVersion = commandData.Application.Application.VersionNumber;
                var p            = Products[index];
                var path         = GetDynamoRevitPath(p, revitVersion);
                var data         = new VersionSelectorData()
                {
                    RevitVersion = revitVersion, SelectedVersion = p.VersionInfo
                };
                data.WriteToRegistry();

                splitButton.Enabled = false; //Disable the split button, no more needed.
                splitButton.Visible = false; //Hide it from the UI
                //For older than 0.8.2, hide the Dynamo Versions ribbon panel
                //Otherwise rename it to "Visual Programming", so that DynamoRevit
                //will add it's button to this panel.
                if (p.VersionInfo < new Version(0, 8, 2))
                {
                    ribbonPanel.Visible = false; //Hide the panel
                }
                else
                {
                    ribbonPanel.Name  = Resources.VisualProgramming;//Same as used in App Description for DynamoRevitApp
                    ribbonPanel.Title = Resources.VisualProgramming;
                }

                //Initialize application
                var ass      = Assembly.LoadFrom(path);
                var revitApp = ass.CreateInstance("Dynamo.Applications.DynamoRevitApp");
                revitApp.GetType()
                .GetMethod("OnStartup")
                .Invoke(revitApp, new object[] { uiApplication });

                //Invoke command
                string message  = string.Empty;
                var    revitCmd = ass.CreateInstance("Dynamo.Applications.DynamoRevit");
                revitCmd.GetType()
                .GetMethod("Execute")
                .Invoke(revitCmd, new object[] { commandData, message, null });

                uiApplication = null; //release application, no more needed.
                return(true);
            }
            catch (Exception)
            {
                splitButton.Enabled = true; //Ensure that the split button is enabled.
                splitButton.Visible = true; //Also the split button is visible
                ribbonPanel.Visible = true; //As well as the panel is visible
                throw;
            }
        }
        /// <summary>
        /// Prompts for version selection task dialog
        /// </summary>
        /// <param name="availableProducts">Choice list of Dynamo Products available for selection</param>
        /// <returns>DynamoProduct to launch or null</returns>
        private DynamoProduct?PromptVersionSelectorDialog(List <DynamoProduct> availableProducts)
        {
            // Creates a Revit task dialog to communicate information to the user.
            TaskDialog mainDialog = new TaskDialog(Resources.DynamoVersions);

            mainDialog.MainInstruction = Resources.DynamoVersionSelection;
            mainDialog.MainContent     = Resources.VersionSelectionContent;

            // Add commmandLink options to task dialog
            int id           = (int)TaskDialogCommandLinkId.CommandLink1;
            var revitVersion = uiControlledApplication.ControlledApplication.VersionNumber;
            //Get the default selection data
            var selectorData               = VersionSelectorData.ReadFromRegistry(revitVersion);
            var selectedVersion            = selectorData.SelectedVersion.ToString(2);
            TaskDialogResult defaultResult = TaskDialogResult.CommandLink1;

            foreach (var item in availableProducts)
            {
                var versionText = String.Format(Resources.DynamoVersionText, item.VersionInfo.ToString(3));
                mainDialog.AddCommandLink((TaskDialogCommandLinkId)id, versionText, item.InstallLocation);
                if (item.VersionInfo.ToString(2) == selectedVersion)
                {
                    defaultResult = (TaskDialogResult)id;
                }

                id++;
            }

            // Set common buttons and default button. If no CommonButton or CommandLink is added,
            // task dialog will show a Close button by default
            mainDialog.CommonButtons = TaskDialogCommonButtons.Close;
            mainDialog.DefaultButton = defaultResult;

            TaskDialogResult tResult = mainDialog.Show();

            if (tResult == TaskDialogResult.Close)
            {
                return(null);
            }

            var index = (int)tResult - (int)TaskDialogCommandLinkId.CommandLink1;

            return(availableProducts[index]);
        }
        /// <summary>
        /// Launches specific version of Dynamo command
        /// </summary>
        /// <param name="product">DynamoProduct to launch</param>
        /// <param name="e">Command executed event argument</param>
        /// <param name="playlist">Launch the Playlist app or just Dynamo</param>
        /// <returns>true for success</returns>
        private bool LaunchDynamoCommand(DynamoProduct product, ExecutedEventArgs e, bool playlist = false)
        {
            if (uiControlledApplication == null)
            {
                throw new InvalidOperationException();
            }

            var revitVersion = uiControlledApplication.ControlledApplication.VersionNumber;
            var path         = GetDynamoRevitPath(product, revitVersion);
            var data         = new VersionSelectorData()
            {
                RevitVersion = revitVersion, SelectedVersion = product.VersionInfo
            };

            data.WriteToRegistry();

            //Initialize application
            var ass      = Assembly.LoadFrom(path);
            var revitApp = ass.CreateInstance("Dynamo.Applications.DynamoRevitApp");

            if (null == revitApp)
            {
                return(false);
            }

            //Remove the command binding, because now DynamoRevitApp will
            //do the command binding for DynamoRevit command.
            RemoveCommandBinding();

            var type   = revitApp.GetType();
            var result = type.GetMethod("OnStartup").Invoke(revitApp, new object[] { uiControlledApplication });

            if ((Result)result != Result.Succeeded)
            {
                return(false);
            }

            UIApplication uiApp = new UIApplication(e.ActiveDocument.Application);

            if (!playlist)
            {
                //Invoke command
                string message = string.Empty;
                result = type.GetMethod("ExecuteDynamoCommand").Invoke(revitApp, new object[] { e.GetJournalData(), uiApp });
                if ((Result)result != Result.Succeeded)
                {
                    return(false);
                }
            }
            else
            {
                //Dependent components have done a re-binding of Playlist command in order to be executed in their context.
                //In order to lunch the Playlist we just need to post the command to the Revit application.
                var dynamoPlayerCmdId = RevitCommandId.LookupCommandId("ID_PLAYLIST_DYNAMO");
                if (dynamoPlayerCmdId != null)
                {
                    uiApp.PostCommand(dynamoPlayerCmdId);
                }
            }

            uiControlledApplication = null; //release application, no more needed.
            return(true);
        }
示例#5
0
        public Result OnStartup(UIControlledApplication application)
        {
            uiApplication = application;
            var revitVersion = application.ControlledApplication.VersionNumber;
            //Get the default selection data
            var selectorData = VersionSelectorData.ReadFromRegistry(revitVersion);

            var revitFolder =
                new DirectoryInfo(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));

            var debugPath      = revitFolder.Parent.FullName;
            var dynamoProducts = FindDynamoRevitInstallations(debugPath, revitVersion);

            Products = new List <DynamoProduct>();
            int index           = -1;
            var selectedVersion = selectorData.SelectedVersion.ToString(2);

            foreach (var p in dynamoProducts)
            {
                var path = VersionLoader.GetDynamoRevitPath(p, revitVersion);
                if (!File.Exists(path))
                {
                    continue;
                }

                if (p.VersionInfo.ToString(2) == selectedVersion)
                {
                    index = Products.Count;
                }

                Products.Add(p);
            }

            if (index == -1)
            {
                index = Products.Count - 1;
            }

            // If there are multiple versions installed, then create
            // a couple of push buttons in a panel to allow selection of a version.
            // If only one version is installed, no multi-selection is required.
            if (Products.Count > 1)
            {
                ribbonPanel = application.CreateRibbonPanel(Resources.DynamoVersions);

                splitButton = AddSplitButtonGroup(ribbonPanel);

                if (index != -1)
                {
                    splitButton.CurrentButton = splitButton.GetItems().ElementAt(index);
                }
            }

            string loadPath = GetDynamoRevitPath(Products.ElementAt(index), revitVersion);

            if (String.IsNullOrEmpty(loadPath))
            {
                return(Result.Failed);
            }

            if (Products.Count == 1) //If only one product is installed load the Revit App directly
            {
                var ass      = Assembly.LoadFrom(loadPath);
                var revitApp = ass.CreateInstance("Dynamo.Applications.DynamoRevitApp");
                revitApp.GetType().GetMethod("OnStartup").Invoke(revitApp, new object[] { application });
            }

            return(Result.Succeeded);
        }