Is it possible to run the .cs files I have created in my Revit API project by clicking the buttons in a form?

Is it possible to run the .cs files I have created in my Revit API project by clicking the buttons in a form?

aliriza.yilmaz
Participant Participant
1,334 Views
6 Replies
Message 1 of 7

Is it possible to run the .cs files I have created in my Revit API project by clicking the buttons in a form?

aliriza.yilmaz
Participant
Participant

Is it possible to activate the operation of an External Command file for drawing Duct with the button clicked after filtering a form?

 

issue.jpg

0 Likes
1,335 Views
6 Replies
Replies (6)
Message 2 of 7

bhprest
Advocate
Advocate

I think the most likely answer you're going to get is to trigger an external event from the form. In the Revit SDK examples, there is an example titled "ModelessForm_ExternalEvent" that shows the components necessary for handling the process.

 

However, that example uses an enum to determine which action happens after the External Event is recognized and executed by the application, and I've had success instead using the Interlocked object to pass references to the classes that actually hold the method to be executed. This allows you to simplify the Execute function in the request handler significantly, and removes the need to maintain an exhaustive list of operations in an enum.

0 Likes
Message 3 of 7

RPTHOMAS108
Mentor
Mentor

You would have to use CodeDOM to compile the code into an assembly, load it with reflection and then execute it from an ExternalEvent by passing the objects you get from that context through to your loaded assembly function. That's the workflow I have successfully followed. My main experience is of using it to compile VB code but there is a C# compiler I've not used.

 

The most tricky part of CodeDOM is adding all the references that get done automatically with VS. Things that you take for granted for features such as Linq etc. 

Message 4 of 7

joshua.lumley
Advocate
Advocate

Check out the link, I have a modeless form  demo project with 19 buttons, 18 of them run external events

 

https://www.youtube.com/watch?v=j-Fv086zLnk 

0 Likes
Message 5 of 7

aliriza.yilmaz
Participant
Participant

I examined the video and plugin you have published, but I could not fully understand how you can run the codes you wrote from another window. Can you send a small sample of this?

0 Likes
Message 6 of 7

joshua.lumley
Advocate
Advocate
0 Likes
Message 7 of 7

mhannonQ65N2
Collaborator
Collaborator

It looks like the form and the command you want to run are part of the same project. You can simply move the code that executes your command to a static method, make your command call that static method, and then call that static method in your form (if it is modal) or in an external event (if the form is modeless).

For example 

public class MyCommand : IExternalCommand
    public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
    UIApplication uiApplication = commandData.Application;
    View view = commandData.View;
    Document document = view.Document;
    DoStuff(uiApplication, view, document);
}

public static Result DoStuff(UIApplication uiApplication, View view, Document document)
{
    // Do Stuff
}

 

 

Also, you should sanitize your SQL!!!

0 Likes