diff --git a/src/IFoxCAD.Cad/Runtime/Reflection.cs b/src/IFoxCAD.Cad/Runtime/Reflection.cs new file mode 100644 index 0000000000000000000000000000000000000000..af8e2e73bab4aa4ca7d77fdaa5e8ecafbc8a7607 --- /dev/null +++ b/src/IFoxCAD.Cad/Runtime/Reflection.cs @@ -0,0 +1,63 @@ +namespace IFoxCAD.Cad; + +public static class Com +{ + #region 测试 + //[CommandMethod("TestCom")] + //public static void TestCom() + //{ + // //测试1 zoom E 成功 + // Acap.AcadApplication.InvokeMethod("ZoomExtents"); + // using var tr = new DBTrans(); + // var r1 = tr.Editor.GetEntity("\n选择闭合多段线"); + // var ent = tr.GetObject(r1.ObjectId); + // if (ent is Polyline) + // { + // var acadent = ent.AcadObject; + // //测试2 将闭合多段线改为不闭合 成功 + // acadent.SetProperty("closed", 0); + // //测试3 偏移闭合多段线 成功 + // acadent.InvokeMethod("offset", 100); + // //测试4,多段线增加一个点 成功 + // acadent.InvokeMethod("Addvertex", 0, Point2d.Origin.ToArray()); + // //测试5 拿到多段线图层 成功 + // var layer = acadent.GetProperty("Layer") as string; + // MessageBox.Show(layer); + // } + //} + #endregion + /// + /// com接口获取对象属性值,类似VisualLisp的vlax-get-property函数 + /// + /// 对象 + /// 属性名称 + /// 属性值 + public static object GetProperty(this object obj, string key) + { + var comtype = Type.GetTypeFromHandle(Type.GetTypeHandle(obj)); + return comtype.InvokeMember(key, BindingFlags.GetProperty, null, obj, null); + } + /// + /// com接口设置对象属性值,类似VisualLisp的vlax-put-property函数 + /// + /// 对象 + /// 属性名称 + /// 属性值 + public static void SetProperty(this object obj, string key, object value) + { + var comtype = Type.GetTypeFromHandle(Type.GetTypeHandle(obj)); + comtype.InvokeMember(key, BindingFlags.SetProperty, null, obj, new object[] { value }); + } + /// + /// com接口使用com方法,类似VisualLisp的vlax-invoke-method函数 + /// + /// 对象 + /// 方法名 + /// 方法需要的参数 + /// 方法的返回值 + public static object InvokeMethod(this object obj, string method, params object[] objArray) + { + var comtype = Type.GetTypeFromHandle(Type.GetTypeHandle(obj)); + return comtype.InvokeMember(method, BindingFlags.InvokeMethod, null, obj, objArray); + } +}