Hello,
I'm an absolute beginner at the Revit API and at this stage I am trying to do something very simple (no practical use, really, just experimenting with the API and how to return values to a winform):
- Create ribbon and buttons (works fine)
- Open a winform when clicking the button (works fine)
Problem area begins here:
- Trigger an external event when clicking a button on the winform
- After raising the event (_exEvent.Raise();) the winform closes itself (tried both this.Close(); and this.Dispose()) - this works fine, the form always closes. I close the winform so that Revit can pick up the external event and execute it.
- Have Revit execute this: MyGlobalVarible.myGlobalVariable = "Success!!!"; Here I use a public static string (see below why)
- At this point the External event reopens the winform and the winform displays MyGlobalVarible.myGlobalVariable (as the name suggests I make this variable accessible throughout the entire namespace so that I can pass values between classes freely).
All of the above works. However, it works inconsistently. Sometimes it does all it has to do 20 times in a row (winform closes, variable gets updated, winform reopens), and sometimes when I click the button on the form it just closes without reopening. Has someone else encountered the same behaviour? Maybe someone knows how to address this. Code snippets below.
Global variable declared in the plugin's namespace:
public class MyGlobalVarible
{
public static string myGlobalVariable;
}
Raising the external event by clicking a button on my winform:
public void button7_Click(object sender, EventArgs e)
{
ExternalEvent _exEvent = null;
EventRegisterHandler _exEventHandler = null;
_exEventHandler = new EventRegisterHandler();
_exEvent = ExternalEvent.Create(_exEventHandler);
_exEvent.Raise();
this.Close();
}
The external event implementation:
public class EventRegisterHandler : IExternalEventHandler
{
public void Execute(UIApplication app)
{
MyGlobalVarible.myGlobalVariable = "Success!!!";
var MainForm = new Form2();
MainForm.ShowDialog();
}
Solved! Go to Solution.
Hello,
I'm an absolute beginner at the Revit API and at this stage I am trying to do something very simple (no practical use, really, just experimenting with the API and how to return values to a winform):
- Create ribbon and buttons (works fine)
- Open a winform when clicking the button (works fine)
Problem area begins here:
- Trigger an external event when clicking a button on the winform
- After raising the event (_exEvent.Raise();) the winform closes itself (tried both this.Close(); and this.Dispose()) - this works fine, the form always closes. I close the winform so that Revit can pick up the external event and execute it.
- Have Revit execute this: MyGlobalVarible.myGlobalVariable = "Success!!!"; Here I use a public static string (see below why)
- At this point the External event reopens the winform and the winform displays MyGlobalVarible.myGlobalVariable (as the name suggests I make this variable accessible throughout the entire namespace so that I can pass values between classes freely).
All of the above works. However, it works inconsistently. Sometimes it does all it has to do 20 times in a row (winform closes, variable gets updated, winform reopens), and sometimes when I click the button on the form it just closes without reopening. Has someone else encountered the same behaviour? Maybe someone knows how to address this. Code snippets below.
Global variable declared in the plugin's namespace:
public class MyGlobalVarible
{
public static string myGlobalVariable;
}
Raising the external event by clicking a button on my winform:
public void button7_Click(object sender, EventArgs e)
{
ExternalEvent _exEvent = null;
EventRegisterHandler _exEventHandler = null;
_exEventHandler = new EventRegisterHandler();
_exEvent = ExternalEvent.Create(_exEventHandler);
_exEvent.Raise();
this.Close();
}
The external event implementation:
public class EventRegisterHandler : IExternalEventHandler
{
public void Execute(UIApplication app)
{
MyGlobalVarible.myGlobalVariable = "Success!!!";
var MainForm = new Form2();
MainForm.ShowDialog();
}
Solved! Go to Solution.
Solved by pavelkosykh88. Go to Solution.
Usually, ExternalEvents are used with the modeless windows. In your case (modal window), you can execute your logic without ExternalEvent and without closing window.
In order to try something with ExternalEvent, I suggest you to:
1) Make your window modeless (MainForm.Show())
2) Create ExternalEvent and IExternalEventHandler directly in IExternalCommand.Execute() and put them into your Window via constructor or property.
3) There is also not necessary to close your Window
Usually, ExternalEvents are used with the modeless windows. In your case (modal window), you can execute your logic without ExternalEvent and without closing window.
In order to try something with ExternalEvent, I suggest you to:
1) Make your window modeless (MainForm.Show())
2) Create ExternalEvent and IExternalEventHandler directly in IExternalCommand.Execute() and put them into your Window via constructor or property.
3) There is also not necessary to close your Window
Aaaaaaand it's working! 🙂 Thank you very much. As for model vs modeless windows, is there a best practice or preferred technique in the Revit API? For now I went with the external event solution using a constructor in my winform.
Aaaaaaand it's working! 🙂 Thank you very much. As for model vs modeless windows, is there a best practice or preferred technique in the Revit API? For now I went with the external event solution using a constructor in my winform.
I believe the Revit.Async library is designed to solve your problem.
With this library you can call Revit APIs directly in a modeless window.
I believe the Revit.Async library is designed to solve your problem.
With this library you can call Revit APIs directly in a modeless window.
Can't find what you're looking for? Ask the community or share your knowledge.