using System.Windows.Forms;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
namespace HelloWorld
{
public partial class Form1 : System.Windows.Forms.Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.dll", EntryPoint = "FindWindow")]
internal static extern IntPtr FindWindow(string ClassName, string WindowNamw);
[DllImport("user32.dll")]
internal static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, int lParam);
internal const int WM_CLICK = 0x00f5;
[DllImport("user32")]
internal static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr lparam);
internal delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);//回调函数
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
internal static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll")]
internal static extern bool SetForegroundWindow(IntPtr hWnd); //设置窗体获得焦点
internal static IntPtr finish;
ExecuteEvent Exc = null;
ExternalEvent eventHandler = null;
private void Form1_Load(object sender, EventArgs e)
{
Exc = new ExecuteEvent();
eventHandler = ExternalEvent.Create(Exc);
}
public static void SetOk()
{
IntPtr Revit = Autodesk.Windows.ComponentManager.ApplicationWindow;
if (Revit != IntPtr.Zero)
{
FindChildClassHwnd(Revit, IntPtr.Zero);
SendMessage(finish, WM_CLICK, IntPtr.Zero, 0);
}
}
internal static bool FindChildClassHwnd(IntPtr hwndParent, IntPtr lParam)
{
EnumWindowProc childProc = new EnumWindowProc(FindChildClassHwnd);
IntPtr hwnd = FindWindowEx(hwndParent, IntPtr.Zero, "Button", "完成");
if (hwnd != IntPtr.Zero)
{
finish = hwnd;
return false;
}
EnumChildWindows(hwndParent, childProc, IntPtr.Zero);
return true;
}
private void button1_Click(object sender, EventArgs e)
{
SetOk();
this.Close();
}
private void button3_Click(object sender, EventArgs e)
{
eventHandler.Raise();
}
}
public class ExecuteEvent : IExternalEventHandler
{
public void Execute(UIApplication app)
{
Document doc = app.ActiveUIDocument.Document;
UIDocument uidoc = app.ActiveUIDocument;
//框选目标构件群
IList list = uidoc.Selection.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Element, "");
foreach (Reference refer in list)
{
//逻辑代码
}
}
public string GetName()
{
return "this is a Test";
}
}
}
转自:http://blog.sina.com.cn/s/blog_16796559b0102y8fs.html