I'm trying to use the app from the Execute method, into a button that resides in a Windows Forms.
Bellow is the short version of the code with the location of the error: "The name 'app' does not exist in the current context"
#region Namespacesusing System;using System.IO;using System.Collections.Generic;using System.Diagnostics;using Autodesk.Revit.ApplicationServices;using Autodesk.Revit.Attributes;using Autodesk.Revit.DB;using Autodesk.Revit.UI;using Autodesk.Revit.UI.Selection;using System.Text;using System.Drawing;using System.Resources;//using System.Windows.Forms;using System.Collections;using System.Xml;using Autodesk.Revit;using System.ComponentModel;using System.Linq;using System.Windows.Forms;using Form = System.Windows.Forms.Form;using System.Threading.Tasks;#endregionnamespace NameSpace1
{
[TransactionAttribute(TransactionMode.Manual)]publicclass Class1 : IExternalCommand
{//public static Autodesk.Revit.ApplicationServices.Application app;
//public UIApplication uiapp;
//public UIDocument uidoc;
//public Document doc;
public Result Execute(ExternalCommandData commandData, refstring message, ElementSet elements)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Autodesk.Revit.ApplicationServices.Application app = uiapp.Application;
Document doc = uidoc.Document;
MyForm form = new MyForm();
form.Show();return Result.Succeeded;
}
}publicpartialclass MyForm : System.Windows.Forms.Form
{public MyForm() : base()
{this.BackColor = System.Drawing.Color.AliceBlue;this.Size = new Size(1200, 900);this.Text = " MyForm Name";this.Dock = System.Windows.Forms.DockStyle.Fill;this.AutoScroll = true;this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;this.TopMost = true;// Add My Button
System.Windows.Forms.Button myButton = new System.Windows.Forms.Button();
myButton.Font = new System.Drawing.Font("Segoe UI", 9.75F);
myButton.Location = new System.Drawing.Point(850, 400);
myButton.Size = new System.Drawing.Size(290, 50);
myButton.BackColor = System.Drawing.Color.Crimson;
myButton.ForeColor = System.Drawing.Color.White;
myButton.Text = " My Test Button ";
myButton.Click += new System.EventHandler(myButton_Click);this.Controls.Add(myButton);void myButton_Click(object sender, EventArgs e)
{var myFam = app.OpenDocumentFile(@"C:\myRevitFamily.rfa"); // error "The name 'app' does not exist in the current context"
// Process the Opened Revit Family
myFam.Close();
}
}
}
}
Thank you for your help!