Accessing an External Command when a form's button is triggered

Accessing an External Command when a form's button is triggered

Anonymous
Not applicable
1,426 Views
4 Replies
Message 1 of 5

Accessing an External Command when a form's button is triggered

Anonymous
Not applicable

Hi !

 

I'm new to the Revit API and pretty excited of the possibilities. But heh, I do have some problems ! 

 

I have an External Command say A which opens a .Net form say B.

 

This form does some calculations and I would like to paste the result in an object's property of Revit, like a wall. To do that, a transaction must be open, I get that. So everything must be done from the Execute method of the External Command.

 

I can manage to catch the events of the form from the external command. But the methods of the form don't have access to the revit API.

 

 

I could export the result of the form as a property in the external command but the Execute method should "pause" and wait that the calculations in the form are done (said otherwise that the user has pushed the button).

 

I hope I am clear enough... Have you guys any ideas ?

 

I just want to paste a data of a .Net form (called from an external command) into a Revit's object. I saw on blogs and forums that many people try to do that but no solutions. 

 

Thx !

 

JB

0 Likes
Accepted solutions (1)
1,427 Views
4 Replies
Replies (4)
Message 2 of 5

Anonymous
Not applicable

I managed to do it but i think it is not a regular method... Here is the code (sorry not very clean cause i'm testing things...)

public class fonction1 : IExternalCommand
{
     public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
        //Appel du formulaire 1
        ClassLibrary1.Form1 frm = new ClassLibrary1.Form1();
        frm.Show();
        UIApplication uiApp = commandData.Application;
        Document doc = uiApp.ActiveUIDocument.Document;
        Reference pickedRef = null;
        Selection sel = uiApp.ActiveUIDocument.Selection;
        frm.button2.Click += delegate
        {
            //A filtrer pour ne pouvoir sélectionner qu'une poutre
            pickedRef = sel.PickObject(ObjectType.Element, "Sélectionner une pouliche");
            Element elem = doc.GetElement(pickedRef);
            Wall aWall = (Wall)elem;
            aWall.get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS).Set(frm.label5.Text);
            frm.Close();
        };
        return Result.Succeeded;
    }
}  

 

I don't remember where i saw the syntax with the " delegate " keyword. I know eventhandler are delegates but... maybe somebody could explain it to me...

 

And when I run the script one time, pressing the enter key in Revit start the script again... Strange.

 

Best

jb

Message 3 of 5

Anonymous
Not applicable

I used the power of anonymous delegates... Wouh

0 Likes
Message 4 of 5

Joe.Ye
Alumni
Alumni
Accepted solution

 

In the modeless dialog, we can assign the calculation result done in a event handler of a button to element in the model.

However we cannot assign the value directly to the target element. As you said, the value assignment should be done within a transaction. 

 

There are two ways we can use to do that.

1. External Event. In the event handler of the ExternalEvent, Revit automatically starts a transaction. So you can change the model/assign value to element in the event handler of the ExternalEvent.  ExternalEvent was introduced in Revit 2013. So this can only be used in Revit 2013.

 

2. Idling Event.  After the calcualation is done, you can assign the value to element/change the model in the Idling event handler.  

 

For the usage of the two ways, please get more information from the Revit help documentation.

In the Revit SDK, there are two samples showing the two ways.

They are locacated in the subfolder of  Revit2013SDK\samples\ModelessDialog.

The two samples show how to change the door's flip/hinge direction in the modeless dialog.

 

As the sample name indicates, ModelessForm_ExternalEvent shows the usage of external event. This should be easier to use.

ModelessForm_IdlingEvent shows the usage of the Idling event.

 



Joe Ye
Contractor
Developer Technical Services
Autodesk Developer Network
Message 5 of 5

Anonymous
Not applicable

Thank you it will certainly do the job better than the way I found !

0 Likes