.NET

Reply
Distinguished Contributor
Posts: 374
Registered: ‎09-29-2000
Message 1 of 17 (602 Views)

Accessing AcadDocument and AcadApplication via .NET assembly

602 Views, 16 Replies
05-12-2009 03:27 PM
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
Distinguished Contributor
Posts: 1,691
Registered: ‎12-15-2003
Message 2 of 17 (602 Views)

Re: Accessing AcadDocument and AcadApplication via .NET assembly

05-12-2009 04:22 PM 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.
*Expert Elite*
chiefbraincloud
Posts: 750
Registered: ‎02-13-2008
Message 3 of 17 (602 Views)

Re: Accessing AcadDocument and AcadApplication via .NET assembly

05-12-2009 04:41 PM 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
Distinguished Contributor
Posts: 374
Registered: ‎09-29-2000
Message 4 of 17 (602 Views)

Re: Accessing AcadDocument and AcadApplication via .NET assembly

05-13-2009 12:45 AM 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
Distinguished Contributor
Posts: 374
Registered: ‎09-29-2000
Message 5 of 17 (602 Views)

Re: Accessing AcadDocument and AcadApplication via .NET assembly

05-13-2009 12:48 AM 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
Mentor
Posts: 384
Registered: ‎09-23-2004
Message 6 of 17 (290 Views)

Re: Accessing AcadDocument and AcadApplication via .NET assembly

01-30-2013 12:46 PM 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

*Expert Elite*
arcticad
Posts: 1,285
Registered: ‎06-21-2004
Message 7 of 17 (284 Views)

Re: Accessing AcadDocument and AcadApplication via .NET assembly

01-30-2013 01:13 PM 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))
Mentor
Posts: 384
Registered: ‎09-23-2004
Message 8 of 17 (280 Views)

Re: Accessing AcadDocument and AcadApplication via .NET assembly

01-30-2013 01:26 PM in reply to: arcticad

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

 

Best regards,

Mark

Mentor
Posts: 384
Registered: ‎09-23-2004
Message 9 of 17 (262 Views)

Re: Accessing AcadDocument and AcadApplication via .NET assembly

01-31-2013 08:29 AM 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. 

ADN Support Specialist
Balaji_Ram
Posts: 609
Registered: ‎03-21-2011
Message 10 of 17 (246 Views)

Re: Accessing AcadDocument and AcadApplication via .NET assembly

02-01-2013 09:14 AM 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

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Welcome to the new Autodesk Community!
If this is your first visit, click here to get started and make the most of the Community. Let us know what you think of the new experience in the Community Feedback Forum.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community