AddInCommandBinding crash revit

AddInCommandBinding crash revit

minet.axel
Enthusiast Enthusiast
445 Views
0 Replies
Message 1 of 1

AddInCommandBinding crash revit

minet.axel
Enthusiast
Enthusiast

Hello,

I use AddInCommandBinding to find the "Model Line" command. To do this from the revit application:

- Link the command "ID_OBJECTS_PROJECT_CURVE".
- Subscribe to the BeforeExecuted event.
- Creating an ExternalEvent to use a modelessDialog (FormNoElementHost)
- button2_Click launches externalEvent
- ExternalEvent (ExternalEventCreateCodeLine) launches the modelLine command

 

My problem is that revives crash at the end of the event "modelLineCommand_BeforeExecuted". This only happens when I debug the application. Indeed I noticed that if I run Revit then run the command without going through visual studio, my revit does not crash

 

 class AppRevit : IExternalApplication
    {
        public static AppRevit thisApp = null;
        public static String s_commandToModelLine = "ID_OBJECTS_PROJECT_CURVE"; // ModelLine
        public static RevitCommandId s_commandId;

        public Result OnShutdown(UIControlledApplication application)
        {
            if (s_commandId.HasBinding)
                application.RemoveAddInCommandBinding(s_commandId);

            return Result.Succeeded;
        }

        public Result OnStartup(UIControlledApplication application)
        {
            thisApp = this;

            //binding commands
            s_commandId = RevitCommandId.LookupCommandId(s_commandToModelLine);
            if (!s_commandId.CanHaveBinding)
            {
                ShowDialog("Error", "The target command" + s_commandToModelLine +
                    " selected for disabling cannot be overridden");
                return Result.Failed;
            }

            try
            {
                AddInCommandBinding modelLineCommand = application.CreateAddInCommandBinding(s_commandId);
                modelLineCommand.BeforeExecuted += modelLineCommand_BeforeExecuted;
            }
            catch (Exception)
            {

                ShowDialog("Error", "This add-in is unable to disable the target command " + s_commandToModelLine +
                    "; most likly another add-in has overridden this command.");
            }

            return Result.Succeeded;
        }


        void modelLineCommand_BeforeExecuted(object sender, Autodesk.Revit.UI.Events.BeforeExecutedEventArgs e)
        {
            ShowDialog("BeforeExecuted", "modelLineCommand");
        }

        private static void ShowDialog(string title, string message)
        {
            TaskDialog td = new TaskDialog(title)
            {
                MainInstruction = message,
                TitleAutoPrefix = false
            };
            td.Show();
        }
    }

 

public partial class FormNoElementHost : Forms.Form
    {
        ExternalCommandData _commandData;

        public ExternalEvent ExEvent { get; set; }

        public FormNoElementHost(ExternalCommandData commandData)
            : this()
        {
            _commandData = commandData;
        }

        public FormNoElementHost()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Family family;
            string message;
            UtilsFamily.LoadFamily(_commandData.Application.ActiveUIDocument.Document, out family, out message);
        }

        private void button2_Click(object sender, EventArgs e)
        {
          //  System.Windows.Forms.MessageBox.Show("Other");

          //  _commandData.Application.PostCommand(AppRevit.s_commandId);

            ExEvent.Raise();
        }
    }

 

 public class ExternalEventLigneModeleCréer : IExternalEventHandler
    {
        public void Execute(UIApplication app)
        {
            //AppRevit.thisApp.CréationSPE.CréerLigneModele();

            System.Windows.Forms.MessageBox.Show("Other");

            app.PostCommand(AppRevit.s_commandId);
        }

        public string GetName()
        {
            return "External Event Créer ligne modele";
        }
    }

 

 

 [Transaction(TransactionMode.Manual)]
    public class FormNoElementHostCommand : IExternalCommand
    {
        public Autodesk.Revit.ApplicationServices.Application RevitApp;
        ExternalEvent _exEvent;

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            ExternalEventLigneModeleCréer handlerLigneModeleCréer = new ExternalEventLigneModeleCréer();
            _exEvent = ExternalEvent.Create(handlerLigneModeleCréer);

            FormNoElementHost form = new FormNoElementHost(commandData);
            form.ExEvent = _exEvent;
            form.Show();

            return Result.Succeeded;
        }
    }

 

 

 

0 Likes
446 Views
0 Replies
Replies (0)