.NET

Reply
Distinguished Contributor
alex_b
Posts: 396
Registered: ‎08-15-2003
Message 1 of 3 (247 Views)

Launching AutoCAD from a .NET application

247 Views, 2 Replies
04-10-2014 01:09 AM

Hello,

 

I'm trying to launch AutoCAD 2012 from an out-of-process .NET application, based on Kean Walmsley's code at http://through-the-interface.typepad.com/through_the_interface/2007/12/launching-autoc.html#comment-...

While this works OK for V2004 and V2008, it crashes A2012. The crash is erraticall, but usually occurs when launching a UI-based command (Layer manager, Open dialog, etc.)

It seems somehow to be related to UI-thread blocking. (the app launches AutoCAD, may communicate with it, and stays there waiting for it to exit).

Anybody experienced this?

 

Thanks,

 

alex

Valued Contributor
ProfWolfMan
Posts: 86
Registered: ‎04-21-2010
Message 2 of 3 (211 Views)

Re: Launching AutoCAD from a .NET application

04-10-2014 10:56 PM in reply to: alex_b

Hi Alex,

 

I do not know exact solution of your problem.

Meanwhile, you can have a look on the following code, which is used by myself since AutoCAD2008 wihtout any issues.

 

 Dim AcadApp As Object
        Try
            AcadApp = GetACAD()
            If IsNothing(AcadApp) Then
                MsgBox("Cannot get AutoCAD application.Open AutoCAD and try again.", MsgBoxStyle.Information)
                End
            End If

 

Public Function GetACAD() As Object

        Dim regs As Microsoft.Win32.RegistryKey = Nothing
        Dim AcadObj As Object = Nothing

       
        Try
            regs = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\Autodesk\AutoCAD\R19.1")
            If regs IsNot Nothing Then AcadObj = GetAcadOpp("19")
            If IsNothing(AcadObj) = False Then Return AcadObj

            regs = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\Autodesk\AutoCAD\R18.2")
            If regs IsNot Nothing Then AcadObj = GetAcadOpp("18")
            If IsNothing(AcadObj) = False Then Return AcadObj

            regs = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\Autodesk\AutoCAD\R18.1")
            If regs IsNot Nothing Then AcadObj = GetAcadOpp("18")
            If IsNothing(AcadObj) = False Then Return AcadObj

            regs = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\Autodesk\AutoCAD\R18.0")
            If regs IsNot Nothing Then AcadObj = GetAcadOpp("18")
            If IsNothing(AcadObj) = False Then Return AcadObj

            regs = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\Autodesk\AutoCAD\R17.2")
            If regs IsNot Nothing Then AcadObj = GetAcadOpp("17")
            If IsNothing(AcadObj) = False Then Return AcadObj

            regs = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\Autodesk\AutoCAD\R17.1")
            If regs IsNot Nothing Then AcadObj = GetAcadOpp("17")
            If IsNothing(AcadObj) = False Then Return AcadObj

        Catch ex As Exception
            MsgBox("Error Getting AutoCAD Info " & ex.Message)
            Return Nothing
        End Try

        Return Nothing

    End Function

    Private Function GetAcadOpp(ByVal AcadString As String) As Object
        Dim AcadApp As Object = Nothing

        Try
            Select Case (AcadString)
                Case "19"
                    AcadApp = GetObject(, "AutoCAD.Application.19")
                Case "18"
                    AcadApp = GetObject(, "AutoCAD.Application.18")
                Case "17"
                    AcadApp = GetObject(, "AutoCAD.Application.17")
            End Select
            If Not AcadApp Is Nothing Then Return AcadApp
        Catch ex As Exception
            AcadApp = Nothing
        Finally

        End Try

        If IsNothing(AcadApp) Then
            Try
                Select Case (AcadString)
                    Case "19"
                        AcadApp = CreateObject("AutoCAD.Application.19")
                    Case "18"
                        AcadApp = CreateObject("AutoCAD.Application.18")
                    Case "17"
                        AcadApp = CreateObject("AutoCAD.Application.17")
                End Select
                If Not AcadApp Is Nothing Then Return AcadApp
            Catch ex As Exception
                AcadApp = Nothing
            Finally

            End Try
        End If

        Return AcadApp

    End Function

 

Thanks & Regards,
G
Distinguished Contributor
alex_b
Posts: 396
Registered: ‎08-15-2003
Message 3 of 3 (142 Views)

Re: Launching AutoCAD from a .NET application

04-16-2014 10:05 PM in reply to: ProfWolfMan

Hi ProfWolfMan,

 

My code (in C#) is essentially the same as yours.

To explain a bit more: the code launches Autocad and can communicate with and drive it; the problem is if the user tries to use it in the normal manner, then it crashes. It happens only in V2012.

Do you use console application or GUI?

 

Thanks,

 

alex

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.