Problems with External Event in modeless form

Problems with External Event in modeless form

Anonymous
Not applicable
1,794 Views
2 Replies
Message 1 of 3

Problems with External Event in modeless form

Anonymous
Not applicable

I have a code for a modeless form and I keep getting an error on starting a transaction. If I use ShowDialog it works fine. I can't seem to figure how to create a working modeless dialog fo

[Transaction(TransactionMode.Manual)]
    class FormShow : IExternalCommand
    {
        public Result Execute(ExternalCommandData c, ref string m, ElementSet e)
        {
            ExternalEvent x = ExternalEvent.Create(new ExHandler());
            UIApplication u = c.Application;
            new Form1(x, u).Show();
            return Result.Succeeded;
        }
        class ExHandler : IExternalEventHandler
        {
            public void Execute(UIApplication ap) { }
            public string GetName() { "form1" }
        }
    }

    class Form1 : Form
    {
        ExternalEvent x;
        UIApplication u;
        Document d;
        public Form1(ExternalEvent e, UIApplication a)
        {
            InitializeComponent();
            x = e;
            u = a;
            d = u.ActiveUIDocument.Document;
        }
        private void Button1_Click(object sender, EventArgs e)
        {
            x.Raise();
            using (Transaction t = new Transaction(d, "command"))
            {
                t.Start();
                //do something
                t.Commit();
            }
            this.Close();
        }
    }

r Revit.

0 Likes
Accepted solutions (2)
1,795 Views
2 Replies
Replies (2)
Message 2 of 3

jeremytammik
Autodesk
Autodesk
Accepted solution

Please first take a look at the Revit SDK sample ModelessDialog/ModelessForm_ExternalEvent.

 

That is the official documentation showing how to set things up.

 

Several other samples and discussions are shared by The Building Coder in the topic group on external events for modeless access and driving Revit from outside:

 

https://thebuildingcoder.typepad.com/blog/about-the-author.html#5.28

  



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 3 of 3

SamBerk
Advocate
Advocate
Accepted solution

Hi @Anonymous 

 

The problem with your code is that you are starting the transaction inside the button click handler.

You have to do it in the IExternalEventHandler.Execute method, and it will get executed when you call x.Raise();

like this:   

[Transaction(TransactionMode.Manual)]
    class FormShow : IExternalCommand
    {
        public Result Execute(ExternalCommandData c, ref string m, ElementSet e)
        {
            ExternalEvent x = ExternalEvent.Create(new ExHandler());
            UIApplication u = c.Application;
            new Form1(x, u).Show();
            return Result.Succeeded;
        }
        class ExHandler : IExternalEventHandler
        {
            public void Execute(UIApplication ap) 
{
using (Transaction t = new Transaction(ap.ActiveUIDocument.Document, "command"))
{
t.Start();
//do something
t.Commit();
}
} public string GetName() { "form1" } } } class Form1 : Form { ExternalEvent x; UIApplication u; Document d; public Form1(ExternalEvent e, UIApplication a) { InitializeComponent(); x = e; u = a; d = u.ActiveUIDocument.Document; } private void Button1_Click(object sender, EventArgs e) { x.Raise(); this.Close(); } }

 

0 Likes