.NET

Reply
Mentor
mgorecki
Posts: 414
Registered: ‎09-23-2004
Message 1 of 34 (660 Views)

Launching AutoCad Issue

660 Views, 33 Replies
02-11-2013 03:03 PM

I have another program that will launch this code below (admittedly, I got it from "Through the Interface")

Imports Autodesk.AutoCAD
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.ApplicationServices.Application
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
'Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Windows
Imports System
Imports Autodesk.AutoCAD.Interop
Imports System.Runtime.InteropServices
Imports Autodesk.AutoCAD.Internal.Forms
Imports System.Windows.Forms

Public Class launchAutoCad
    Public Shared Sub StartAutoCAD()
        ' "AutoCAD.Application.17" uses 2007, 2008, 2009
        '  whichever was most recently run
        ' "AutoCAD.Application.17.1" uses 2008, specifically
        ' "AutoCAD.Application.18" uses 2010, 2011, 2012
        ' "AutoCAD.Application.18.1" uses 2011, specifically
        ' "AutoCAD.Application.19" uses 2013,...

        Const progID As String = "AutoCAD.Application.18"
        Dim acApp As AcadApplication = Nothing

        Try
            acApp = DirectCast(Marshal.GetActiveObject(progID), AcadApplication)
        Catch ex2 As Exception
            Try
                Dim acType As Type = Type.GetTypeFromProgID(progID)
                acApp = DirectCast(Activator.CreateInstance(acType, True), AcadApplication)

            Catch ex As Exception
                MessageBox.Show("Cannot create object of type """ & progID & """")
            End Try
        End Try
        acApp = DirectCast(Marshal.GetActiveObject(progID), AcadApplication)

        If acApp IsNot Nothing Then
            ' By the time this is reached AutoCAD is fully
            ' functional and can be interacted with through code
            acApp.Visible = True
            acApp.ActiveDocument.SendCommand("(command " & Chr(34) & "NETLOAD" & Chr(34) & " " & _ Chr(34) & "C:\\mystuff\\myprog.dll" & Chr(34) & ") ")
        End If
    End Sub
End Class

 

At the first "Try" it will throw an exception because AutoCad is not running.

In the second "Try" we have:

acApp = DirectCast(Activator.CreateInstance(acType, True), AcadApplication)

Which launched AutoCad (it shows up in the task manager).

 

But still, it then goes into the next "Catch" and the message box says is can't create that ype of object.  But I can see that it launched AutoCad.

So I copied the line:

acApp = DirectCast(Marshal.GetActiveObject(progID), AcadApplication)

and forced it.  It worked.

Why would it throw an error if it did what it was supposed to do?  I see so many people using this same code on many different forums, but I can't understand why it doesn't work for me.

Thanks, in advance.

 

ADN Support Specialist
fenton.webb
Posts: 352
Registered: ‎07-24-2007
Message 2 of 34 (644 Views)

Re: Launching AutoCad Issue

02-11-2013 05:19 PM in reply to: mgorecki

It's really difficult to say what the problem might be. Usually, when you have issues with COM not working as expected it's because the installation is corrupted somehow...

 

Here's what I would advise...

 

1) Uninstall all Autodesk products

2) reinstall in chronological order.





Fenton Webb

Developer Technical Services

Autodesk Developer Network


Moderator
Alexander.Rivilis
Posts: 1,432
Registered: ‎04-09-2008
Message 3 of 34 (633 Views)

Re: Launching AutoCad Issue

02-11-2013 11:05 PM in reply to: mgorecki

Try this solution: http://through-the-interface.typepad.com/through_the_interface/2010/02/handling-com-calls-rejected-b...

 

P.S.: Are you trying to use AutoCAD .NET API from external exe-file? It is impossible.

E.g. those namespaces can be using only from  dll-file netloading into AutoCAD:

Imports Autodesk.AutoCAD
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.ApplicationServices.Application
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Windows

 

 


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

ADN Support Specialist
fenton.webb
Posts: 352
Registered: ‎07-24-2007
Message 4 of 34 (618 Views)

Re: Launching AutoCad Issue

02-12-2013 10:12 AM in reply to: Alexander.Rivilis

"those namespaces can be using only from  dll-file netloading into AutoCAD"

 

Not necessarily - you can import .NET DLLs that are dependent on another EXE as a reference to its types only (the dependencies will be ignored). This is particularly useful when using reflection.





Fenton Webb

Developer Technical Services

Autodesk Developer Network


Mentor
mgorecki
Posts: 414
Registered: ‎09-23-2004
Message 5 of 34 (615 Views)

Re: Launching AutoCad Issue

02-12-2013 10:28 AM in reply to: fenton.webb

I've gotten around the thrown exception by changing the code as shown:

Public Class launchAutoCad
    Public Shared Sub StartAutoCAD(ByVal rad As String)
        ' "AutoCAD.Application.17" uses 2007, 2008, 2009
        '  whichever was most recently run
        ' "AutoCAD.Application.17.1" uses 2008, specifically
        ' "AutoCAD.Application.18" uses 2010, 2011, 2012
        ' "AutoCAD.Application.18.1" uses 2011, specifically
        ' "AutoCAD.Application.19" uses 2013,...

        Const progID As String = "AutoCAD.Application.18"
        Dim acApp As AcadApplication = Nothing

        Try
            ' Get a running instance of AutoCad
            acApp = DirectCast(Marshal.GetActiveObject(progID), AcadApplication)
        Catch 'ex As Exception
            Try
                Dim acType As Type = Type.GetTypeFromProgID(progID)
                acApp = DirectCast(Activator.CreateInstance(acType, True), AcadApplication)

            Catch 'ex2 As Exception
                If IsProcessRunning("acad") = False Then
                    MessageBox.Show("Cannot create object of type """ & progID & """")
                Else
                    acApp = DirectCast(Marshal.GetActiveObject(progID), AcadApplication)
                End If
            End Try
        End Try

        If acApp IsNot Nothing Then
            ' By the time this is reached AutoCAD is fully
            ' functional and can be interacted with through code
            acApp.Visible = True
            'acApp.ActiveDocument.SendCommand("(command " & Chr(34) & "NETLOAD" & Chr(34) & " " & _
            '                    Chr(34) & "C:\\mystuff\\myprog.dll" & Chr(34) & ") ")


            Dim AcadDocs As Object = acApp.GetType().InvokeMember("Documents", Reflection.BindingFlags.GetProperty, Nothing, acApp, Nothing)
            ' Create array of parameters  
            Dim filename As String = "temp.dwg"
            Dim args() As Object = New Object(1) {}

            args(0) = filename
            args(1) = False 'read-only=false  

            ' open a drawing  
            Dim AcDoc As Object = AcadDocs.GetType.InvokeMember("Open", Reflection.BindingFlags.InvokeMethod, Nothing, AcadDocs, args, Nothing)
            Dim Util As Object = New Object

            ' Get reference on active document  
            AcDoc = acApp.GetType.InvokeMember("ActiveDocument", Reflection.BindingFlags.GetProperty, Nothing, acApp, Nothing, Nothing)

            ' Get reference of  AcadUtility  
            Util = AcDoc.GetType().InvokeMember("Utility", Reflection.BindingFlags.GetProperty, Nothing, AcDoc, Nothing)

            ' Get reference on ModelSpace  
            Dim oSpace As Object = AcDoc.GetType.InvokeMember("ModelSpace", Reflection.BindingFlags.GetProperty, Nothing, AcDoc, Nothing)

            ''********************************* draw circle ************************** 
            Dim center As Point3d = New Geometry.Point3d(0, 0, 0)
            Dim radius As Double
            radius = (CDbl(Val(rad)))
            DrawCircle(center, radius)

        End If
    End Sub

 

 I have a function defined (IsProcessRunning) that works for this.  It actually checks to see if the process "acad.exe" is actually running.

 

I am launching the dll from an exe currently (for testing) eventually it will be a web app.

I do have another issue though, I was just trying to pass the dll a string (just to see if I could)

Public Shared Sub StartAutoCAD(ByVal rad As String)

 

In the calling .exe:

Module Module1
    Sub Main()
        Dim rad As String = "12.5"
        launchAutoCad.launchAutoCad.StartAutoCAD(rad)
    End Sub
End Module

 I get the following error:

Could not load file or assembly 'acdbmgd, Version=18.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

 

Any help clearing that one up would be greatly appreciated.

ADN Support Specialist
fenton.webb
Posts: 352
Registered: ‎07-24-2007
Message 6 of 34 (611 Views)

Re: Launching AutoCad Issue

02-12-2013 11:14 AM in reply to: mgorecki

By the way, to refect the types without resolving the dependencies... Do this:

 

Assembly.ReflectionOnlyLoadFrom()




Fenton Webb

Developer Technical Services

Autodesk Developer Network


Moderator
Alexander.Rivilis
Posts: 1,432
Registered: ‎04-09-2008
Message 7 of 34 (609 Views)

Re: Launching AutoCad Issue

02-12-2013 12:07 PM in reply to: fenton.webb

fenton.webb wrote:

"those namespaces can be using only from  dll-file netloading into AutoCAD"

 

Not necessarily - you can import .NET DLLs that are dependent on another EXE as a reference to its types only (the dependencies will be ignored). This is particularly useful when using reflection.


Hi, Fenton!

Are you saying that I can use the classes / methods of acdbmgd.dll, acmgd.dll, accoremgd.dll from external exe-file??? You're kidding me? ;-)

Otherwise I do not see any reason to load these assemblies in the exe-file if I can not use the dll-files (ie to execute code from them)


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Moderator
Alexander.Rivilis
Posts: 1,432
Registered: ‎04-09-2008
Message 8 of 34 (607 Views)

Re: Launching AutoCad Issue

02-12-2013 12:11 PM in reply to: mgorecki

mgorecki wrote:

... I get the following error:

Could not load file or assembly 'acdbmgd, Version=18.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified...

 

Any help clearing that one up would be greatly appreciated.


That's because you're trying to use the classes / methods from acmgd.dll from external exe-file instead from dll-file loaded into AutoCAD. Point3d is a class defined in acdbmgd.dll


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Mentor
mgorecki
Posts: 414
Registered: ‎09-23-2004
Message 9 of 34 (600 Views)

Re: Launching AutoCad Issue

02-12-2013 12:44 PM in reply to: Alexander.Rivilis

Hi Alexander, can you tell me how to resolve this?  I have been writing VB.Net code, but always ran the code in an existing AutoCad session, never like this.  This is all new to me.

 

Thanks

Mentor
mgorecki
Posts: 414
Registered: ‎09-23-2004
Message 10 of 34 (597 Views)

Re: Launching AutoCad Issue

02-12-2013 12:48 PM in reply to: fenton.webb

Hello Fenton,

You wrote:

 

By the way, to refect the types without resolving the dependencies... Do this:

 

Assembly.ReflectionOnlyLoadFrom()

 

 

This is going to sound stupid, but, what do I do with that?

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

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