DavidMena
DavidMena

Reputation: 29

Initializing a Win. Form from Revit Push Buttons and Executing commands

Can anybody tell me how (if it is possible) to implement a class that I've already written pushing a button in a form that is showed when clicking a button in a Revit Addin Ribbon? Attached is the link to the image of what I've achieved so far.

I'm using C# language inside Visual Studio implementing ExternalApplication class.

Thanks

            namespace PEASA_TOOLS_2021
            {
                public class PEASA_TOOLS_2021 : IExternalApplication
                {
                    static void AddRibbonPanel(UIControlledApplication application)
                        {
                          //Create panel
                          RibbonPanel ribbonPannel4 = application.CreateRibbonPanel(tabName, "Pruebas");
                          //Button for execute Form.
                          PushButtonData pbFormdata = new 
                          PushButtonData("cmdFormData","Extracción de Data", thisAssemblyPath, "PEASA_TOOLS_2021.DATA_EXTRACTION.DataExtractionButton");
                          PushButton pbForm = ribbonPannel4.AddItem(pbFormdata) as PushButton;
                          BitmapImage pruebasImage = new BitmapImage(new Uri("pack://application:,,,/PEASA_TOOLS_2021;component/RECURSOS/PRUEBAS.png"));
                          pbForm.LargeImage = pruebasImage;
                         }
                 }
             }

1

Upvotes: 2

Views: 840

Answers (1)

Mostafa Tarek Yassien
Mostafa Tarek Yassien

Reputation: 532

I have been there, and I know your struggle. here is what I have got to so far and it is working for me, it may not be the best solution, but it is the only one I have.

First, you need to add these Variables to your Form's partial class

[Transaction(TransactionMode.Manual)] 
[Regeneration(RegenerationOption.Manual)]
public partial class Form1 : System.Windows.Forms.Form
{
    private UIApplication uiapp;
    private UIDocument uidoc;
    private Document doc;
    private ExternalCommandData commandData;
    private string message;
    private ElementSet elements;

Then, you will have to edit your form constructor to be like this:

public Form1(ExternalCommandData commandData,ref string message, ElementSet elements)
    {
        InitializeComponent();
        this.commandData = commandData;
        this.elements = elements;
        uiapp = commandData.Application;
        uidoc = uiapp.ActiveUIDocument;
        doc = uidoc.Document;
    }

Then you will create a function under the Form partial class, this function is the one you write your command in:

and it will be like this:

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
     write your code here
     return Result.Succeeded;
     }

then you just simply call the Execute Function under the Button1_Click function

protected internal  void Button1_Click(object sender, EventArgs e)
    {
        Execute(commandData, ref message, elements);       
    }

Now for the class that calls the Form after pushing the PushButton:

Class DataExtractionButton : 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;
        ExternalCommandData revit = commandData;
var window = new Form1(commandData, ref message, elements);
window.ShowDialog();
return Result.Succeeded;
    }
}

that should be the way to do it.

I hope this can help you, it worked with my case.

Upvotes: 1

Related Questions