Esempio n. 1
0
 static DynamoRevit()
 {
     idleActions       = new List <Action>();
     extCommandData    = null;
     dynamoViewModel   = null;
     revitDynamoModel  = null;
     handledCrash      = false;
     ModelState        = RevitDynamoModelState.NotStarted;
     preLoadExceptions = new List <Exception>();
 }
Esempio n. 2
0
        public Result ExecuteCommand(DynamoRevitCommandData commandData)
        {
            var startupTimer = Stopwatch.StartNew();

            if (ModelState == RevitDynamoModelState.StartedUIless)
            {
                if (CheckJournalForKey(commandData, JournalKeys.ShowUiKey, true) ||
                    CheckJournalForKey(commandData, JournalKeys.ModelShutDownKey))
                {
                    //When we move from UIless to UI we prefer to start with a fresh revitDynamoModel
                    //in order to benefit from a startup sequence similar to Dynamo Revit UI launch.
                    //Also there might be other situations which demand a new revitDynamoModel.
                    //In order to be able to address them we process ModelShutDownKey.
                    //An example of this situation is when you have a revitDynamoModel already started and you switch
                    //the document in Revit. Since revitDynamoModel is well connected to the previous document we need to
                    //shut it down and start a new one in order to able to run a graph in the new document.
                    revitDynamoModel.ShutDown(false);
                    ModelState = RevitDynamoModelState.NotStarted;
                }
                else
                {
                    TryOpenAndExecuteWorkspaceInCommandData(commandData);
                    return(Result.Succeeded);
                }
            }

            if (ModelState == RevitDynamoModelState.StartedUI)
            {
                TryOpenAndExecuteWorkspaceInCommandData(commandData);
                return(Result.Succeeded);
            }

            HandleDebug(commandData);

            InitializeCore(commandData);
            //subscribe to the assembly load
            AppDomain.CurrentDomain.AssemblyLoad += AssemblyLoad;

            try
            {
                extCommandData = commandData;

                UpdateSystemPathForProcess();

                // create core data models
                revitDynamoModel = InitializeCoreModel(extCommandData);
                revitDynamoModel.UpdateManager.RegisterExternalApplicationProcessId(Process.GetCurrentProcess().Id);
                revitDynamoModel.Logger.Log("SYSTEM", string.Format("Environment Path:{0}", Environment.GetEnvironmentVariable("PATH")));

                // handle initialization steps after RevitDynamoModel is created.
                revitDynamoModel.HandlePostInitialization();
                ModelState = RevitDynamoModelState.StartedUIless;

                // show the window
                if (CheckJournalForKey(extCommandData, JournalKeys.ShowUiKey, true))
                {
                    dynamoViewModel = InitializeCoreViewModel(revitDynamoModel);

                    // Let the host (e.g. Revit) control the rendering mode
                    var save = RenderOptions.ProcessRenderMode;
                    InitializeCoreView().Show();
                    RenderOptions.ProcessRenderMode = save;
                    revitDynamoModel.Logger.Log(Dynamo.Applications.Properties.Resources.WPFRenderMode + RenderOptions.ProcessRenderMode.ToString());

                    ModelState = RevitDynamoModelState.StartedUI;
                    // Disable the Dynamo button to prevent a re-run
                    DynamoRevitApp.DynamoButtonEnabled = false;
                }

                //foreach preloaded exception send a notification to the Dynamo Logger
                //these are messages we want the user to notice.
                preLoadExceptions.ForEach(x => revitDynamoModel.Logger.LogNotification
                                              (revitDynamoModel.GetType().ToString(),
                                              x.GetType().ToString(),
                                              DynamoApplications.Properties.Resources.MismatchedAssemblyVersionShortMessage,
                                              x.Message));

                TryOpenAndExecuteWorkspaceInCommandData(extCommandData);

                //unsubscribe to the assembly load
                AppDomain.CurrentDomain.AssemblyLoad -= AssemblyLoad;
                Analytics.TrackStartupTime("DynamoRevit", startupTimer.Elapsed, ModelState.ToString());
            }
            catch (Exception ex)
            {
                // notify instrumentation
                Analytics.TrackException(ex, true);

                MessageBox.Show(ex.ToString());

                DynamoRevitApp.DynamoButtonEnabled = true;

                //If for some reason Dynamo has crashed while startup make sure the Dynamo Model is properly shutdown.
                if (revitDynamoModel != null)
                {
                    revitDynamoModel.ShutDown(false);
                    revitDynamoModel = null;
                }

                return(Result.Failed);
            }

            return(Result.Succeeded);
        }