- 积分
- 24557
- 明经币
- 个
- 注册时间
- 2004-3-17
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
楼主 |
发表于 2015-4-5 18:10:21
|
显示全部楼层
本帖最后由 雪山飞狐_lzh 于 2015-4-5 21:17 编辑
可能的更好方法
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- using System.Reflection;
- using System.IO;
- using stdole;
- using System.Text.RegularExpressions;
- using System.Collections;
- namespace TlsCad.RunTime
- {
- public partial class Form1 : Form
- {
- [DllImport("user32.dll")]
- static extern IntPtr SetActiveWindow(IntPtr hWnd);
- [DllImport("user32.dll")]
- static extern bool SetForegroundWindow(IntPtr hWnd);
- public Form1()
- {
- InitializeComponent();
- AcadX.Connection();
- dynamic app = AcadX.Application;
- //获取当前文档
- dynamic doc = app.ActiveDocument;
- double[] p1 = { 0, 0, 0 };
- double[] p2 = { 0, 10, 0 };
- double[] p3 = { 10, 10, 0 };
- double[] p4 = { 10, 0, 0 };
- //Com对象数组必须封装为IDispatch数组
- //注意应引用stdole.dll
- IDispatch[] ents =
- {
- doc.ModelSpace.AddLine(p1, p2),
- doc.ModelSpace.AddLine(p2, p3),
- doc.ModelSpace.AddLine(p3, p4),
- doc.ModelSpace.AddLine(p4, p1)
- };
- //测试生成面域
- dynamic r = doc.ModelSpace.AddRegion(ents);
- double[] min, max;
- r[0].GetBoundingBox(out min, out max);
- app.ZoomWindow(min, max);
- //测试Utility,或者var util = new AcadUtility(doc);
- var util = new AcadX(".IAcadUtility", doc.Utility);
- object[] args = new object[]{ null, null, Type.Missing };
- if (util.Invoke("GetEntity", ref args))
- {
-
- dynamic obj = args[1];
- var pt = args[2] as double[];
- string info = string.Format("你选择了{0}实体", obj.ObjectName);
- info += string.Format("\n在{0},{1},{2}", pt.Cast<object>().ToArray());
- MessageBox.Show(info);
- }
- }
- }
- public class AcadX
- {
- public static dynamic Application
- { private set; get; }
- virtual protected string TypeName
- {
- get { return "Autodesk.AutoCAD.Interop"; }
- }
- protected static Assembly _assem
- { private set; get; }
- protected Type _type;
- protected object _obj;
- protected AcadX()
- {
- _type = _assem.GetType(TypeName);
- }
- public AcadX(string typeName, object obj)
- {
- _type = _assem.GetType(TypeName + typeName);
- _obj = obj;
- }
- public bool Invoke(string methodName, ref object[] args)
- {
-
- try
- {
- MethodInfo mi = _type.GetMethod(methodName);
- List<object> lst = new List<object> { mi.Invoke(_obj, args) };
- var pars = mi.GetParameters();
- for(int i = 0; i< pars.Count(); i++)
- {
- if (pars.IsOut)
- lst.Add(args);
- }
- args = lst.ToArray();
- return true;
- }
- catch
- {
- args = new object[0];
- return false;
- }
- }
- public static void Connection()
- {
- //获取当前AutoCad实例
- string pid = "AutoCad.Application";
- try
- {
- Application = Marshal.GetActiveObject(pid);
- }
- catch
- {
- //如果没有就创建之
- Type t = Type.GetTypeFromProgID(pid);
- Application = Activator.CreateInstance(t);
- Application.Visible = true;
- }
- GetComAssembly(Application);
- }
- private static void GetComAssembly(dynamic app)
- {
- string id = app.Version.Substring(0, 4);
- Dictionary<string, string> vers = new Dictionary<string, string>();
- string vername = null;
- DirectoryInfo di = new DirectoryInfo("C:\\windows\\assembly\\gac_msil\\Autodesk.AutoCAD.Interop");
- var sdis = di.GetDirectories();
- foreach (var sdi in sdis)
- {
- if (sdi.Name.Contains(id))
- {
- vername = sdi.Name;
- break;
- }
- }
- var verinfos = Regex.Split(vername, "__");
- _assem =
- Assembly.Load(
- string.Format(
- "Autodesk.AutoCAD.Interop, Version={0}, Culture=neutral, PublicKeyToken={1}",
- verinfos));
- }
- }
- public class AcadUtility : AcadX
- {
- protected override string TypeName
- {
- get { return base.TypeName + ".IAcadUtility"; }
- }
- public AcadUtility(dynamic doc)
- {
- _obj = doc.Utility;
- }
- public dynamic Document
- {
- set { _obj = value.Utility; }
- }
- }
- }
|
|