Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

External Events

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
stephen_harrison
931 Views, 4 Replies

External Events

stephen_harrison
Advocate
Advocate

Hope someone can help with what is probably a naive question from someone just starting out on the path of programming.

I have been working on a small app for Revit 2016 and have managed to retrieve the required data from the database and display this in a ListView. Clicking a form button then creates a list of the selected items and then invokes a method (located on the form) in which the Transaction is started. It is intended that this would then action the list of selected items. The programme compiles without error or Warnings but during debugging Revit closes upon reaching the transaction with an “Access Violation” I have also noticed that the doc reference within the Transaction reference within comes up “null” during debugging.

 

Am I missing something with the above or is the problem as I suspect that I have left the API call and therefore need to look at External Events? For clarity the Form is Model not Modeless.

 

In preparation I have been exploring the code within the SDK samples on ModelessForms as well as created a simple app utilising the External Events code within the Revit 2016 Help and unfortunately I seem to run into the same problem when running them and other sample external event code I have downloaded.

 

Revit Error Dialog displays stating “Object reference not set to an instance of an object”

 

I have not amended the sample code in any way and keep getting the same error no matter which sample External events programme I try. Please enlighten me as the only way I find I really learn how to programme is through exploring working sample code.

0 Likes

External Events

Hope someone can help with what is probably a naive question from someone just starting out on the path of programming.

I have been working on a small app for Revit 2016 and have managed to retrieve the required data from the database and display this in a ListView. Clicking a form button then creates a list of the selected items and then invokes a method (located on the form) in which the Transaction is started. It is intended that this would then action the list of selected items. The programme compiles without error or Warnings but during debugging Revit closes upon reaching the transaction with an “Access Violation” I have also noticed that the doc reference within the Transaction reference within comes up “null” during debugging.

 

Am I missing something with the above or is the problem as I suspect that I have left the API call and therefore need to look at External Events? For clarity the Form is Model not Modeless.

 

In preparation I have been exploring the code within the SDK samples on ModelessForms as well as created a simple app utilising the External Events code within the Revit 2016 Help and unfortunately I seem to run into the same problem when running them and other sample external event code I have downloaded.

 

Revit Error Dialog displays stating “Object reference not set to an instance of an object”

 

I have not amended the sample code in any way and keep getting the same error no matter which sample External events programme I try. Please enlighten me as the only way I find I really learn how to programme is through exploring working sample code.

4 REPLIES 4
Message 2 of 5

arnostlobel
Alumni
Alumni

Stephen:

 

Please let me understand the problem more. Do you claim that if you use one of the Modeless Dialog samples in as provided in the SDK without any further changes that it crashes when used in Revit? If so, can yuo please tell me at what line exactly does it crash?

 

Thank you

Arnošt Löbel
0 Likes

Stephen:

 

Please let me understand the problem more. Do you claim that if you use one of the Modeless Dialog samples in as provided in the SDK without any further changes that it crashes when used in Revit? If so, can yuo please tell me at what line exactly does it crash?

 

Thank you

Arnošt Löbel
Message 3 of 5

stephen_harrison
Advocate
Advocate

To clarify my Question was in two parts

1) I have created a windows Model form that populates a List view with data from the open Revit document. I wish to then select items from this list and then through clicking a button on the form action the selected items. Unfortunately everything I seem to try doesn’t work. Is this because I have to utilise External Events in order invoke a transaction either within the Button Click or from a method resultant from the Button Click?

2) I have encountered “Object reference not set to an instance of an object” error when utilising the Revit 2016 SDK samples on ModelessForms. Happily I have identified my error and resolved this question.

After re-coding my original project in anticipation that Event Handlers are the solution to Question 1 this unfortunately has resulted in additional Questions.

The form and Event Handlers appear to work but unfortunately the code to populate the listView currently resides on the form and I have been unable to access the Method from outside the Form class. I have tried relocating the code to the Command class but unfortunately these attempts have raised the following questions:

  1. i) How to reference the form?
  2. ii) How to reference the ListView on the form?

iii) How to reference a Method on the form?

For clarity the code I have utilised to create the form and EventHandler classes is primarily that shown within the Revit 2016 Help.

Apologies for what probably is a basic coding question but this is my first time utilising event handlers which from the examples I have seen result in the form being created and shown from the Application class called from the Command class (Revit 2016 Help EventHandler code) and my usual techniques for referencing the form don’t appear to work.

 

Any help and examples of code would be much appreciated.

0 Likes

To clarify my Question was in two parts

1) I have created a windows Model form that populates a List view with data from the open Revit document. I wish to then select items from this list and then through clicking a button on the form action the selected items. Unfortunately everything I seem to try doesn’t work. Is this because I have to utilise External Events in order invoke a transaction either within the Button Click or from a method resultant from the Button Click?

2) I have encountered “Object reference not set to an instance of an object” error when utilising the Revit 2016 SDK samples on ModelessForms. Happily I have identified my error and resolved this question.

After re-coding my original project in anticipation that Event Handlers are the solution to Question 1 this unfortunately has resulted in additional Questions.

The form and Event Handlers appear to work but unfortunately the code to populate the listView currently resides on the form and I have been unable to access the Method from outside the Form class. I have tried relocating the code to the Command class but unfortunately these attempts have raised the following questions:

  1. i) How to reference the form?
  2. ii) How to reference the ListView on the form?

iii) How to reference a Method on the form?

For clarity the code I have utilised to create the form and EventHandler classes is primarily that shown within the Revit 2016 Help.

Apologies for what probably is a basic coding question but this is my first time utilising event handlers which from the examples I have seen result in the form being created and shown from the Application class called from the Command class (Revit 2016 Help EventHandler code) and my usual techniques for referencing the form don’t appear to work.

 

Any help and examples of code would be much appreciated.

Message 4 of 5

arnostlobel
Alumni
Alumni
Accepted solution

Hi Stephen!

 

I am afraid it'll be rather hard helping you with specifics without seeing at least fragments of your code. However, to save you some time at least with the basic design, here're some notes:

 

  • If your dialog is indeed modal (i.e. not modeless), you do not need External Events to call into the Revit API. In fact, you would never receive a call back from Revit while you stay in the dialog, meaning you would lock yourself out.
  • You can use transactions and other API classes and methods directly from your modal dialog, providing the dialog was instantiated and invoked from your external command.
  • Since you have to invoke your dialog from an external command, you should pass the current document as an argument of your modal dialog's constructor. In the dialog class you'll store the the document in some sort of a class variable and use it to perform actions (while still in the dialog)

That's pretty much it. I suggest you start with some very simple version of your dialog. Do not worry first about what you will have in the dialog eventually. Create a simplistic dialog with just two buttons - OK and Cancel. On the OK event do start a transaction, make some trivial change in the document, and close the transaction. That's it. Once you have that figured out, than you'll start adding more controls and actions to the dialog, There ought to be plenty of examples of modal dialogs in the samples provided in the SDK. (Not the two samples under the Modeless Dialog folder, though).

 

Good luck

Arnošt Löbel
0 Likes

Hi Stephen!

 

I am afraid it'll be rather hard helping you with specifics without seeing at least fragments of your code. However, to save you some time at least with the basic design, here're some notes:

 

  • If your dialog is indeed modal (i.e. not modeless), you do not need External Events to call into the Revit API. In fact, you would never receive a call back from Revit while you stay in the dialog, meaning you would lock yourself out.
  • You can use transactions and other API classes and methods directly from your modal dialog, providing the dialog was instantiated and invoked from your external command.
  • Since you have to invoke your dialog from an external command, you should pass the current document as an argument of your modal dialog's constructor. In the dialog class you'll store the the document in some sort of a class variable and use it to perform actions (while still in the dialog)

That's pretty much it. I suggest you start with some very simple version of your dialog. Do not worry first about what you will have in the dialog eventually. Create a simplistic dialog with just two buttons - OK and Cancel. On the OK event do start a transaction, make some trivial change in the document, and close the transaction. That's it. Once you have that figured out, than you'll start adding more controls and actions to the dialog, There ought to be plenty of examples of modal dialogs in the samples provided in the SDK. (Not the two samples under the Modeless Dialog folder, though).

 

Good luck

Arnošt Löbel
Message 5 of 5

stephen_harrison
Advocate
Advocate

Thanks you for the guidance. It helped confirm that I was on the right track with my initial programme (i.e. without Event Handlers). So much so that the programme was working within a couple of minutes.

0 Likes

Thanks you for the guidance. It helped confirm that I was on the right track with my initial programme (i.e. without Event Handlers). So much so that the programme was working within a couple of minutes.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report