C#创建Autocad实例
C#創(chuàng)建AcadApplication實(shí)例時(shí),有時(shí)候會(huì)創(chuàng)建實(shí)例失敗,找了好久,參考以下鏈接整理了一下代碼,經(jīng)過測(cè)試,運(yùn)行ok。
ref: http://through-the-interface.typepad.com/through_the_interface/2010/02/handling-com-calls-rejected-by-autocad-from-an-external-net-application.html
?
ref: http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/1dab0874-bba5-481a-9986-a11a66efa0a0
?
ref: http://msdn.microsoft.com/en-us/library/ms693740(VS.85).aspx
?
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Autodesk.AutoCAD.Interop; using System.Runtime.InteropServices; using System.Diagnostics; using System.Threading; using Microsoft.Win32;namespace AcadUtils {public enum AcadVersion{Acad2009,Acad2010,Unknow}[ComImport,InterfaceType(ComInterfaceType.InterfaceIsIUnknown),Guid("00000016-0000-0000-C000-000000000046")]public interface IMessageFilter{[PreserveSig]int HandleInComingCall(int dwCallType, IntPtr hTaskCaller,int dwTickCount, IntPtr lpInterfaceInfo);[PreserveSig]int RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType);[PreserveSig]int MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType);}public class AcadMgr:IMessageFilter{static string progID;IMessageFilter oldFilter;AcadApplication acapp = null;[DllImport("ole32.dll")]static extern int CoRegisterMessageFilter(IMessageFilter lpMessageFilter,out IMessageFilter lplpMessageFilter);static AcadMgr(){if (AcadVer == AcadVersion.Acad2009)progID = "AutoCAD.Application.17";else if (AcadVer == AcadVersion.Acad2010)progID = "AutoCAD.Application.18";elseprogID = "Unknown";}public AcadMgr(){ CoRegisterMessageFilter(this, out oldFilter);try{acapp = (AcadApplication)Marshal.GetActiveObject(progID);}catch{try{Type acType = Type.GetTypeFromProgID(progID);acapp = (AcadApplication)Activator.CreateInstance(acType, true);}catch{System.Windows.Forms.MessageBox.Show("cannot create object type " + progID);}}if (acapp != null){acapp.Visible = true; }}public AcadApplication Application{get{return acapp;}}public static AcadVersion AcadVer{get{try{RegistryKey acadKey = Registry.CurrentUser.OpenSubKey("Software//Autodesk//AutoCAD");string acadVerNum = acadKey.GetValue("CurVer").ToString();if (acadVerNum == "R18.0")return AcadVersion.Acad2010;else if (acadVerNum == "R17.0")return AcadVersion.Acad2009;elsereturn AcadVersion.Unknow;}catch{return AcadVersion.Unknow;}}}int IMessageFilter.HandleInComingCall(int dwCallType, IntPtr hTaskCaller,int dwTickCount, IntPtr lpInterfaceInfo){return 0;}int IMessageFilter.RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType){return 1000;}int IMessageFilter.MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType){return 1;}} }
總結(jié)
以上是生活随笔為你收集整理的C#创建Autocad实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MLDN魔乐科技 Oracle学习笔记
- 下一篇: (转)无法打开C盘,提示本次操作由于这台