WPF button execute external command

WPF button execute external command

Anonymous
Not applicable
4,724 Views
6 Replies
Message 1 of 7

WPF button execute external command

Anonymous
Not applicable

Hello i am having trouble with connecting WPF buttons to execute method from some another class.

 

I have class called TEST and it looks like this:

namespace IgorAddin
{
    [Transaction(TransactionMode.Manual)]
    public class TestClass : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument uidoc = uiapp.ActiveUIDocument;
            Application app = uiapp.Application;
            Document doc = uidoc.Document;
           

            TaskDialog.Show("TEST", "TEST");

            return Result.Succeeded;
        }
    }

}

 

 and have WPF button like this: 

 

 

<Window x:Class="IgorAddin.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  
    Title="Window1" Height="400" Width="300" ResizeMode="NoResize">


    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="89*"/>
            <ColumnDefinition Width="206*"/>
        </Grid.ColumnDefinitions>
        <Button x:Name="button" Content="TestButton" Margin="0,0,205,342" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="75" Click="button_Click" Grid.ColumnSpan="2" />
            </Grid>


</Window>

 

How can i make this button execute TestClass?

 

 

It's probably easy but i am little lost here.

 

Thanks in advance,

0 Likes
4,725 Views
6 Replies
Replies (6)
Message 2 of 7

rosalesduquej
Alumni
Alumni

Hi Igor,

 

This Forum is dedicated to the use of Revit API functionality, What you are asking here is a Programming question. I will still answer it but keep that in mind for the future. Your button has an OnClick action where you will need to pass the command in order to access it, I don't see that anywhere. You have created the command successfuly which if you pass the addin file to reference to that command, from Revit you will be able to launch it. But what I understand that you are trying to do is, to have a windows form inside of Revit and have a button there that launches the Command. Right? 

 

I will invite you to read a bit more how the Buttons Class work in C#, It will save you more time in the future and the best part will be you will do it yourself. https://msdn.microsoft.com/en-us/library/system.windows.forms.button(v=vs.90).aspx

 

You can double click your button in the WPF form and you will see that the method button_Click gets created where you can reference the command.

 

https://msdn.microsoft.com/en-us/library/hkkb40tf(v=vs.90).aspx

 

Cheers,



Jaime Rosales D.
Sr. Developer Consultant
Twitter | AEC ADN DevBlog
0 Likes
Message 3 of 7

Anonymous
Not applicable

First of all i would like to thank you for responding on my non Revit API related question. I'm an architect by profession and learning about Revit API (motivated on RTC Budapest) so i didn't notice difference in topics. I'll have that in minde for future questions.

 

About technical question. I set up one push button in Revit that lounches this WPF window. On that WPF window i have this button which is designed for some renaming purposes but for simlicity i put only regular TaskDialog. I know about XAML.CS file wich have button_click action but problem is that i don't know how to connect my Class TestClass with that button because it have execute method. If i instanciate class in my button_click i need to fill in (ExternalCommandData commandData, ref string message, ElementSet elements) arguments.

Code looks like this:

 private void button_Click(object sender, RoutedEventArgs e)
        {
            TestClass tc = new TestClass();
            tc.Execute()
        

        }

But i don't know what should i fill in in for asked arguments. Correct me if i am wrong or there is other way to execute this class. Maby this is stupid question for people who understand programming( much better then me) but for me is obsitcle in progress.

 

 

Thank you for understanding!

0 Likes
Message 4 of 7

GonçaloFeio1321
Enthusiast
Enthusiast

If you are using WPF, that button should ideally bind teh Command property to something that implements ICommand.

Google for implementations like DelegateCommand.

Subscribing to the clicked event is the winforms way of doing it, not wpf (it is considered a code smell).

 

0 Likes
Message 5 of 7

Anonymous
Not applicable

I am also trying to run a code in some other class that has an Revit external command. Have you found a solution to call that class from the button?

 

 

0 Likes
Message 6 of 7

Anonymous
Not applicable

Massive post edit after re-educating myself on the results I found researching all this. Also keep in mind, I've been doing this for less than half of a year. I'm liable to be thoroughly corrected by the veterans around here.

 

How are you implementing your interface that the button is on?

 

I made this work on normal WPF XAML window that opens with a ribbon button click. When the window instance opens, the addin passes the needed arguments into the window's opening sequence and then uses an external event. That way, elements in the window can use those arguments too.

 

However, I still struggle on this subject because I have a dockable dialog as well. I'm not sure how to pass those arguments in this manner because the dockable dialog steals focus from Revit, and it isn't as convenient to pass the arguments in a show and hide sequence.

 

 

Very useful article from the Knowledge Network. If you are trying to achieve what I did with a standard WPF window, you will want to read this closely ( do not skim )

 

https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2016/ENU/Revit-API/files/GUID-...

 

 Helpful post from Jeremy

http://thebuildingcoder.typepad.com/blog/2013/12/replacing-an-idling-event-handler-by-an-external-ev...

 

All things considered, I tossed this to the side since it started to look problematic to not only get working, but maintain with dockable windows.

 

 

Edit: Here is where I threw my hands up and said never mind. Per TBC article above:

 

"I also noticed that Revit needs focus to actually forward the external event Raise invocation to call its Execute method. I am still hoping to find an easy clean way to trigger that programmatically from outside..."

 

0 Likes
Message 7 of 7

MERLET_J
Participant
Participant

hy your link is dead sorry

0 Likes