internal static string GetDynamoRevitPath(DynamoProduct product, string revitVersion)
 {
     if (product.VersionInfo < new Version(0, 7))
     {
         return(string.Empty); //0.6.3 and older version not supported for Revit2015 onwards
     }
     return(Path.Combine(product.InstallLocation, string.Format("Revit_{0}", revitVersion), "DynamoRevitDS.dll"));
 }
        private Result loadProduct(DynamoProduct product, string revitVersion)
        {
            string loadPath = GetDynamoRevitPath(product, revitVersion);

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

            var ass      = Assembly.LoadFrom(loadPath);
            var revitApp = ass.CreateInstance("Dynamo.Applications.DynamoRevitApp");

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

            return(Result.Succeeded);
        }
        private void executedPlaylist(object sender, ExecutedEventArgs e)
        {
            if (PlaylistProducts.Count == 0)
            {
                return;
            }

            DynamoProduct productToLaunch = PlaylistProducts.First();

            if (PlaylistProducts.Count > 1)
            {
                var product = PromptVersionSelectorDialog(PlaylistProducts);
                if (product.HasValue)
                {
                    productToLaunch = product.Value;
                }
            }

            LaunchDynamoCommand(productToLaunch, e, true);
        }
        /// <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);
        }