.NET
cancel
Showing results forĀ 
ShowĀ Ā onlyĀ  | Search instead forĀ 
Did you mean:Ā 

Accessing AcadDocument and AcadApplication via .NET assembly

16 REPLIES 16
Reply
Message 1 of 17
matt_1ca
3722 Views, 16 Replies

Accessing AcadDocument and AcadApplication via .NET assembly

Wondering if someone could please tell me what is the equivalent code of my COM accessing code below in .NET assembly?

For many years the code below has given me the results I needed but now I was told that I have to use .NET AutoCAD assemblies
instead and rewrite the code shown below so that it would use all the rich features provided via AutoDesk.AutoCAD.ApplicationServices.

In a nutshell, my code below allows me to grab an existing instance of AutoCAD or create a new one if no instance of AutoCAD is open then grabs the active AutoCAD document for my use. Thank you so much for all the kind help you could give

Gratefully,
Matt

My code is shown below:

Public ThisDrawing as Autodesk.AutoCAD.InterOp.AcadDocument

Public Sub CreateOrGetAutoCADinstance

Dim acadApp as Autodesk.AutoCAD.InterOp.AcadApplication

On Error Resume Next

acadApp = GetObject(, "AutoCAD.Application")

If Err.Number Then
acadApp = CreateObject("AutoCAD.Application")
End If

acadApp.Visible = True

ThisDrawing = acadApp.ActiveDocument

End Sub
16 REPLIES 16
Message 2 of 17
NathTay
in reply to: matt_1ca

You can not use the AutoCAD .NET API from outside of AutoCAD. Therefore if you have an application that connects to an existing or creates a new instance of AutoCAD you will only be able to use the COM API.
Message 3 of 17
chiefbraincloud
in reply to: matt_1ca

if you are going to be writing in .NET with the managed API, then you never have to and would probably never want to create an instance of AutoCAD. Your code will need to be compiled into a .dll instead of an .exe and your .dll will be loaded into autocad, instead of starting AutoCAD externally.

Application level methods and properties are then always accessible through ApplicationServices.Application.

Documents can be accessed via: dim acdocs as ApplicationServices.DocumentCollection = ApplicationServices.Application.DocumentManager, dim ThisDrawing as Document = acdocs.MdiActiveDocument

You can not use the managed API's from an external application.
Dave O.                                                                  Sig-Logos32.png
Message 4 of 17
matt_1ca
in reply to: matt_1ca

Thank you so much for this enlightening answer ... now, I have more control of the tools I use knowing that both COM and
.NET managed API have their places.

Gratefully,
Matt
Message 5 of 17
matt_1ca
in reply to: matt_1ca

Thank you so much for this valuable answer... this gives me the nuts and bolts I need to
build my .NET managed API solution ... now I can proceed with more confidence
towards this new frontier.

Gratefully,
Matt
Message 6 of 17
mgorecki
in reply to: chiefbraincloud

Hello, I'm having a similar problem.  We have a web appliaction that will create a 2D array, then launch my .dll and pass it the array.  My code is supposed to launch AutoCad and then create a drawing using the info passed in the array.

I also want to check to see if AutoCad is running already.

Thanks in advance for your help

Mark

Message 7 of 17
arcticad
in reply to: matt_1ca

    Public Function isAutocadOpen() As Boolean

        Dim rtnValue As Boolean
        Dim ProcessList As System.Diagnostics.Process()
        Dim Proc As System.Diagnostics.Process

        ProcessList = System.Diagnostics.Process.GetProcesses()
        Try
            For Each Proc In ProcessList
                If UCase(CStr(Proc.ProcessName)) = UCase("acad") Then
                    MsgBox(Proc.Id.ToString)
                    rtnValue = True
                End If
            Next
        Catch ex As Exception
        End Try

        Return rtnValue
    End Function

 

---------------------------



(defun botsbuildbots() (botsbuildbots))
Message 8 of 17
mgorecki
in reply to: arcticad

Hi, thank you very much for that code.  I'll try it out.

 

Best regards,

Mark

Message 9 of 17
mgorecki
in reply to: arcticad

Again, thanks for the code.  I was also wondering if you could give me the command that will launch AutoCad if it's not currently running.  I don't normally ask for stuff like this, but I've looked all over and found many convoluted websites or sites that I needed to have a phd in programming to understand. 

Message 10 of 17
Balaji_Ram
in reply to: mgorecki

Hello,

 

Please look for the usage of "Activator.CreateInstance" as in this blog post :

http://through-the-interface.typepad.com/through_the_interface/2007/12/launching-autoc.html

 

In the sample code from the blog post, remember to change the progId based on the version of AutoCAD that you want to launch. For example : "AutoCAD.Application.19" for AutoCAD 2013.

 



Balaji
Developer Technical Services
Autodesk Developer Network

Message 11 of 17
mgorecki
in reply to: Balaji_Ram

Hello,

I've looked into that code, but I couldn't get it to convert to VB.net.  Do you have a VB version? 

 

Thanks,

Message 12 of 17


@mgorecki wrote:

Hello,

I've looked into that code, but I couldn't get it to convert to VB.net.  Do you have a VB version? 

 

Thanks,


What about using online C# <-> VB.NET converter?

Š’Ń–Š“ŠæŠ¾Š²Ń–Š“ь ŠŗŠ¾Ń€ŠøсŠ½Š°? ŠšŠ»Ń–ŠŗŠ½Ń–Ń‚ŃŒ Š½Š° "Š’ŠŸŠžŠ”ŠžŠ‘ŠŠ™ŠšŠ£" ціŠ¼ ŠæŠ¾Š²Ń–Š“Š¾Š¼Š»ŠµŠ½Š½ŃŠ¼! | Do you find the posts helpful? "LIKE" these posts!
ŠŠ°Ń…Š¾Š“ŠøтŠµ сŠ¾Š¾Š±Ń‰ŠµŠ½Šøя ŠæŠ¾Š»ŠµŠ·Š½Ń‹Š¼Šø? ŠŸŠ¾ŃŃ‚Š°Š²ŃŒŃ‚Šµ "ŠŠ ŠŠ’Š˜Š¢Š”ŠÆ" этŠøŠ¼ сŠ¾Š¾Š±Ń‰ŠµŠ½ŠøяŠ¼!
ŠŠ° Š²Š°ŃˆŠµ Š·Š°ŠæŠøтŠ°Š½Š½Ń Š²Ń–Š“ŠæŠ¾Š²Ń–Š»Šø? ŠŠ°Ń‚ŠøсŠ½Ń–Ń‚ŃŒ ŠŗŠ½Š¾ŠæŠŗу "ŠŸŠ Š˜Š™ŠŠÆŠ¢Š˜ Š Š†ŠØŠ•ŠŠŠÆ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
ŠŠ° Š²Š°Ńˆ Š²Š¾ŠæрŠ¾Ń усŠæŠµŃˆŠ½Š¾ Š¾Ń‚Š²ŠµŃ‚ŠøŠ»Šø? ŠŠ°Š¶Š¼ŠøтŠµ ŠŗŠ½Š¾ŠæŠŗу "Š£Š¢Š’Š•Š Š”Š˜Š¢Š¬ Š Š•ŠØŠ•ŠŠ˜Š•"


Alexander Rivilis / ŠŠ»ŠµŠŗсŠ°Š½Š“р Š ŠøŠ²ŠøŠ»Šøс / ŠžŠ»ŠµŠŗсŠ°Š½Š“р Š Ń–Š²Ń–Š»Ń–с
Programmer & Teacher & Helper / ŠŸŃ€Š¾Š³Ń€Š°Š¼Š¼Šøст - Š£Ń‡ŠøтŠµŠ»ŃŒ - ŠŸŠ¾Š¼Š¾Ń‰Š½ŠøŠŗ / ŠŸŃ€Š¾Š³Ń€Š°Š¼Ń–ст - Š²Ń‡ŠøтŠµŠ»ŃŒ - ŠæŠ¾Š¼Ń–чŠ½ŠøŠŗ
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 13 of 17

I've tried 2 different c# to VB converters and they both give me the same message, but for differing lines:

"An error occured converting your code, probably due to a syntax error:-- line 12 col 3: EOF expected"

I removed the extra spaces and changed the 17.1 to 18.0, but I still get the same error.

using System;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.Interop;

//This C# code can be added to an ā€œon-clickā€ event handler for a button (for example) or another function
//that makes sense in the context of your application.

  // "AutoCAD.Application.17" uses 2007 or 2008,
  //  whichever was most recently run
  // "AutoCAD.Application.17.1" uses 2008, specifically

  const string progID = "AutoCAD.Application.18.0";
  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 + "\"");
    }
  }
  if (acApp != null)
  {
    // By the time this is reached AutoCAD is fully
    // functional and can be interacted with through code

    acApp.Visible = true;
    acApp.ActiveDocument.SendCommand("_MYCOMMAND ");
  }

 

Message 14 of 17

Imports System
Imports System.Runtime.InteropServices
Imports Autodesk.AutoCAD.Interop

Namespace Test
    Public Class Test
        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.19"
            Dim acApp As AcadApplication = Nothing

            Try
                acApp = DirectCast(Marshal.GetActiveObject(progID), AcadApplication)
            Catch
                Try
                    Dim acType As Type = Type.GetTypeFromProgID(progID)
                    acApp = DirectCast(Activator.CreateInstance(acType, True), AcadApplication)
                Catch
                    MessageBox.Show("Cannot create object of type """ & progID & """")
                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("_MYCOMMAND ")
            End If
        End Sub
    End Class
End Namespace

Š’Ń–Š“ŠæŠ¾Š²Ń–Š“ь ŠŗŠ¾Ń€ŠøсŠ½Š°? ŠšŠ»Ń–ŠŗŠ½Ń–Ń‚ŃŒ Š½Š° "Š’ŠŸŠžŠ”ŠžŠ‘ŠŠ™ŠšŠ£" ціŠ¼ ŠæŠ¾Š²Ń–Š“Š¾Š¼Š»ŠµŠ½Š½ŃŠ¼! | Do you find the posts helpful? "LIKE" these posts!
ŠŠ°Ń…Š¾Š“ŠøтŠµ сŠ¾Š¾Š±Ń‰ŠµŠ½Šøя ŠæŠ¾Š»ŠµŠ·Š½Ń‹Š¼Šø? ŠŸŠ¾ŃŃ‚Š°Š²ŃŒŃ‚Šµ "ŠŠ ŠŠ’Š˜Š¢Š”ŠÆ" этŠøŠ¼ сŠ¾Š¾Š±Ń‰ŠµŠ½ŠøяŠ¼!
ŠŠ° Š²Š°ŃˆŠµ Š·Š°ŠæŠøтŠ°Š½Š½Ń Š²Ń–Š“ŠæŠ¾Š²Ń–Š»Šø? ŠŠ°Ń‚ŠøсŠ½Ń–Ń‚ŃŒ ŠŗŠ½Š¾ŠæŠŗу "ŠŸŠ Š˜Š™ŠŠÆŠ¢Š˜ Š Š†ŠØŠ•ŠŠŠÆ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
ŠŠ° Š²Š°Ńˆ Š²Š¾ŠæрŠ¾Ń усŠæŠµŃˆŠ½Š¾ Š¾Ń‚Š²ŠµŃ‚ŠøŠ»Šø? ŠŠ°Š¶Š¼ŠøтŠµ ŠŗŠ½Š¾ŠæŠŗу "Š£Š¢Š’Š•Š Š”Š˜Š¢Š¬ Š Š•ŠØŠ•ŠŠ˜Š•"


Alexander Rivilis / ŠŠ»ŠµŠŗсŠ°Š½Š“р Š ŠøŠ²ŠøŠ»Šøс / ŠžŠ»ŠµŠŗсŠ°Š½Š“р Š Ń–Š²Ń–Š»Ń–с
Programmer & Teacher & Helper / ŠŸŃ€Š¾Š³Ń€Š°Š¼Š¼Šøст - Š£Ń‡ŠøтŠµŠ»ŃŒ - ŠŸŠ¾Š¼Š¾Ń‰Š½ŠøŠŗ / ŠŸŃ€Š¾Š³Ń€Š°Š¼Ń–ст - Š²Ń‡ŠøтŠµŠ»ŃŒ - ŠæŠ¾Š¼Ń–чŠ½ŠøŠŗ
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 15 of 17

Hi Alexander,

This gets long, but I'm hoping you can help me out with this.

The scenario:

We have a web app that launches one program (just like the one below).  I add code to let the web app know AutoCad is running.

The web app then sends an array to another function in the same program.  The array sent has a handful of keys and variables.  My function reads the keys and variables to determine a second program to launch in Autocad (there are three potential programs to launch, the variables will let the code know which one to launch).

Once it knows, how does it go about doing the "netload", entering the path and .dll name of the program to launch, and then sending the command to launch the program (keeping in mind the program to launch will need to receive data, like passing the values of the variables)?

 

Thank you so much for helping me this far.

Mark

Message 16 of 17

Hello Alexander,

I think I found something that could work:

acApp.ActiveDocument.SendCommand("(command " & Chr(34) & "NETLOAD" & Chr(34) & " " & _

 Chr(34) & "C:\myFiles\myProgram.dll" & Chr(34) & ") ")

 

If that would work, and I have a sub called "POD" that's expecting an array, can I use acApp.ActiveDocument.SendCommand("(command "POD(arrayName)")")

 

 

Message 17 of 17

I think I began to understand what you want. But you can not pass array to command any way.

Š’Ń–Š“ŠæŠ¾Š²Ń–Š“ь ŠŗŠ¾Ń€ŠøсŠ½Š°? ŠšŠ»Ń–ŠŗŠ½Ń–Ń‚ŃŒ Š½Š° "Š’ŠŸŠžŠ”ŠžŠ‘ŠŠ™ŠšŠ£" ціŠ¼ ŠæŠ¾Š²Ń–Š“Š¾Š¼Š»ŠµŠ½Š½ŃŠ¼! | Do you find the posts helpful? "LIKE" these posts!
ŠŠ°Ń…Š¾Š“ŠøтŠµ сŠ¾Š¾Š±Ń‰ŠµŠ½Šøя ŠæŠ¾Š»ŠµŠ·Š½Ń‹Š¼Šø? ŠŸŠ¾ŃŃ‚Š°Š²ŃŒŃ‚Šµ "ŠŠ ŠŠ’Š˜Š¢Š”ŠÆ" этŠøŠ¼ сŠ¾Š¾Š±Ń‰ŠµŠ½ŠøяŠ¼!
ŠŠ° Š²Š°ŃˆŠµ Š·Š°ŠæŠøтŠ°Š½Š½Ń Š²Ń–Š“ŠæŠ¾Š²Ń–Š»Šø? ŠŠ°Ń‚ŠøсŠ½Ń–Ń‚ŃŒ ŠŗŠ½Š¾ŠæŠŗу "ŠŸŠ Š˜Š™ŠŠÆŠ¢Š˜ Š Š†ŠØŠ•ŠŠŠÆ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
ŠŠ° Š²Š°Ńˆ Š²Š¾ŠæрŠ¾Ń усŠæŠµŃˆŠ½Š¾ Š¾Ń‚Š²ŠµŃ‚ŠøŠ»Šø? ŠŠ°Š¶Š¼ŠøтŠµ ŠŗŠ½Š¾ŠæŠŗу "Š£Š¢Š’Š•Š Š”Š˜Š¢Š¬ Š Š•ŠØŠ•ŠŠ˜Š•"


Alexander Rivilis / ŠŠ»ŠµŠŗсŠ°Š½Š“р Š ŠøŠ²ŠøŠ»Šøс / ŠžŠ»ŠµŠŗсŠ°Š½Š“р Š Ń–Š²Ń–Š»Ń–с
Programmer & Teacher & Helper / ŠŸŃ€Š¾Š³Ń€Š°Š¼Š¼Šøст - Š£Ń‡ŠøтŠµŠ»ŃŒ - ŠŸŠ¾Š¼Š¾Ń‰Š½ŠøŠŗ / ŠŸŃ€Š¾Š³Ń€Š°Š¼Ń–ст - Š²Ń‡ŠøтŠµŠ»ŃŒ - ŠæŠ¾Š¼Ń–чŠ½ŠøŠŗ
Facebook | Twitter | LinkedIn
Expert Elite Member

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

ā€Boost