- 积分
- 24557
- 明经币
- 个
- 注册时间
- 2004-3-17
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
楼主 |
发表于 2009-12-13 23:39:00
|
显示全部楼层
本帖最后由 作者 于 2009-12-14 0:06:50 编辑
代码更改了一下,这样可以了
结论是:
1、不要在NetApi中使用DBObject.FromAcadObject函数
2、注意当前文档加锁的方法
3、C#做Com反射真的好累!
C#代码:VBA调用的代码- Sub tt()
- Dim tc As Object
- Set tc = Application.GetInterfaceObject("TlsCad.Common.Curve")
-
- Dim obj As Object, pnts(1)
- ThisDrawing.Utility.GetEntity obj, pnts(0)
- tc.Set obj.ObjectID
-
- pnts(0) = tc.GetClosestPointTo(pnts(0), False)
- pnts(1) = ThisDrawing.Utility.GetPoint()
- pnts(1) = tc.GetClosestPointTo(pnts(1), False)
-
- tc.SplitByPoints (pnts)
-
- End Sub
VB调用的代码- Private Sub Command1_Click()
- Dim app As Object
- Set app = GetObject(, "AutoCad.Application")
- Dim tc As Object
- Set tc = app.GetInterfaceObject("TlsCad.Common.Curve")
- Dim obj As Object, pnts(1)
- app.ActiveDocument.Utility.GetEntity obj, pnts(0)
- tc.Set obj.ObjectID
-
- pnts(0) = tc.GetClosestPointTo(pnts(0), False)
- pnts(1) = app.ActiveDocument.Utility.GetPoint()
- pnts(1) = tc.GetClosestPointTo(pnts(1), False)
-
- tc.SplitByPoints (pnts)
- End Sub
C#调用的代码- 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 Autodesk.AutoCAD.Interop;
- using Autodesk.AutoCAD.Interop.Common;
- using System.Runtime.InteropServices;
- using System.Reflection;
- namespace ComTest
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- AcadApplication acApp = GetAcApp();
- object tc = acApp.GetInterfaceObject("TlsCad.Common.Curve");
- object acCurve;
- object[] pnts = new object[2];
- AcadDocument acDoc = acApp.ActiveDocument;
- acDoc.Utility.GetEntity(out acCurve, out pnts[0], "");
- InvokeMethod(tc, "Set", ((AcadEntity)acCurve).ObjectID);
- pnts[0] = InvokeMethod(tc, "GetClosestPointTo", pnts[0], false);
- pnts[1] = acDoc.Utility.GetPoint(pnts[0], "");
- pnts[1] = InvokeMethod(tc, "GetClosestPointTo", pnts[1], false);
- InvokeMethod(tc, "SplitByPoints", new object[1]{pnts});
- }
- public object InvokeMethod(object obj, string methodName, params object[] args)
- {
- Type t = obj.GetType();
- object res =
- t.InvokeMember(
- methodName,
- BindingFlags.Public | BindingFlags.InvokeMethod,
- null,
- obj,
- args);
- return res;
- }
- public AcadApplication GetAcApp()
- {
- const string progID = "AutoCAD.Application.18";
- AcadApplication acApp = null;
- try
- {
- acApp =
- (AcadApplication)Marshal.GetActiveObject(progID);
- }
- catch
- {
- try
- {
- Type acType =
- Type.GetTypeFromProgID(progID);
- acApp =
- (AcadApplication)Activator.CreateInstance(
- acType,
- true
- );
- }
- catch
- {
- MessageBox.Show(
- "Cannot create object of type "" +
- progID + """
- );
- }
- }
- return acApp;
- }
- }
- }
|
|