Imports System Imports System.Runtime.InteropServices Imports Autodesk.AutoCAD.Interop Public Interface IMessageFilter _ Function HandleInComingCall(ByVal dwCallType As Integer, ByVal hTaskCaller As IntPtr, ByVal dwTickCount As Integer, ByVal lpInterfaceInfo As IntPtr) As Integer _ Function RetryRejectedCall(ByVal hTaskCallee As IntPtr, ByVal dwTickCount As Integer, ByVal dwRejectType As Integer) As Integer _ Function MessagePending(ByVal hTaskCallee As IntPtr, ByVal dwTickCount As Integer, ByVal dwPendingType As Integer) As Integer End Interface Public Class Form1 Implements IMessageFilter Private Declare Function CoRegisterMessageFilter Lib "ole32.dll" (ByVal lpMessageFilter As IMessageFilter, ByRef lplpMessageFilter As IMessageFilter) As Integer Function IMessageFilter_HandleInComingCall(ByVal dwCallType As Integer, ByVal hTaskCaller As IntPtr, ByVal dwTickCount As Integer, ByVal lpInterfaceInfo As IntPtr) As Integer Implements IMessageFilter.HandleInComingCall ListBox1.Items.Add("Handle") ListBox1.Update() Return 0 ' SERVERCALL_ISHANDLED End Function Function IMessageFilter_RetryRejectedCall(ByVal hTaskCallee As IntPtr, ByVal dwTickCount As Integer, ByVal dwRejectType As Integer) As Integer Implements IMessageFilter.RetryRejectedCall ListBox1.Items.Add("Retry") ListBox1.Update() Return 1000 ' Retry in a second End Function Function IMessageFilter_MessagePending(ByVal hTaskCallee As IntPtr, ByVal dwTickCount As Integer, ByVal dwPendingType As Integer) As Integer Implements IMessageFilter.MessagePending ListBox1.Items.Add("Pending:") ListBox1.Update() Return 1 ' PENDINGMSG_WAITNOPROCESS End Function Public Sub New() ' This call is required by the Windows Form Designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. Dim oldFilter As IMessageFilter CoRegisterMessageFilter(Me, oldFilter) End Sub Public acApp As AcadApplication = Nothing Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click acApp = Nothing Const progID As String = "AutoCAD.Application.18" Try acApp = DirectCast(Marshal.GetActiveObject(progID), AcadApplication) Catch ex1 As Exception Try Dim acType As Type = Type.GetTypeFromProgID(progID) acApp = DirectCast(Activator.CreateInstance(acType, True), AcadApplication) Catch ex2 As Exception MessageBox.Show("Cannot create object of type """ & progID & """") End Try End Try If acApp IsNot Nothing Then ListBox1.Items.Add("AutoCAD Started:" & acApp.Application.Version) ListBox1.Update() Try Dim doc As AcadDocument = acApp.ActiveDocument Dim pt1(2) As Double pt1(0) = 4 pt1(1) = 0 pt1(2) = 0 doc.ModelSpace.AddCircle(pt1, 4) ListBox1.Items.Add("Circle Drawn") ListBox1.Update() Dim pt2(2) As Double pt2(0) = 7 pt2(1) = 8 pt2(2) = 0 doc.ModelSpace.AddLine(pt1, pt2) ListBox1.Items.Add("Line Drawn") ListBox1.Update() Dim SaveType As Common.AcSaveAsType = Common.AcSaveAsType.ac2010_dwg doc.SaveAs("C:\temp\Test.dwg", SaveType, ) doc.Close() ListBox1.Items.Add("Doc Closed") ListBox1.Update() Catch ex1 As Exception MsgBox(ex1.Message) Finally Try acApp.Application.Quit() ListBox1.Items.Add("Cad Closed") ListBox1.Update() Catch ex2 As Exception MsgBox(ex2.Message) End Try End Try End If End Sub End Class