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

Launching AutoCad Issue

33 REPLIES 33
Reply
Message 1 of 34
mgorecki
2178 Views, 33 Replies

Launching AutoCad Issue

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.

 

33 REPLIES 33
Message 21 of 34

You confused me so much that I can not understand what you need.
I see what you would do:
1. exe-file has to run AutoCAD and load the dll-file in the AutoCAD command NETLOAD
2. dll-to interact with the exe-file and receive data from it. How you do it - I do not know.

Problem of the interaction of two processes unrelated to programming in AutoCAD .NET API

For example: http://www.codeproject.com/Articles/138290/Programming-Memory-Mapped-Files-with-the-NET-Frame
3. exe-file does not need to refer to dll-file.


That's all.

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | 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 22 of 34

Ok I just want to know it this is possible:

1.  I have a .dll that gets called by an exe, that will launch AutoCad ( that part works)

2.  I want it to accept an array argument so that it can create some graphics in the AutoCad session that is now open.

 

Can an exe file pass a dll an argument?  If so, how?

 

The exe is a standalone, and the dll is basically an AutoCad dll.

Message 23 of 34

>>Ok I just want to know it this is possible:
>>1.  I have a .dll that gets called by an exe, that will launch AutoCad ( that part works)

Why do you need dll-file, if exe-file itself can run AutoCAD? You put me in a deadlock.

>>2.  I want it to accept an array argument so that it can create some graphics in the AutoCad session that is now open.

 The easiest way:

1) write down your’s array to a “file with a specific name” from exe-file.

2) register with dll-file new command in AutoCAD (for example, with name “StartTransArray”)

3) start command “StartTransArray” with exe-file

4) function which registered as command handler read “file with a specific name” and draw graphics to AutoCAD based on this file content.

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | 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 24 of 34
norman.yuan
in reply to: mgorecki

Although Alex has replyed...

 

Yes, you can have a DLL, that is called from EXE, to start AutoCAD (but why, I'd ask as Alex asked? Is launching AutoCAD via COM that complicated to deserve a separate DLL project?). In this ccase, the DLL IS PART of the process of the EXE. Thus, you CANNOT use AutoCAD ObjectARX .NET assemblies (acdbmgd/acmgd.dll).

 

 

Message 25 of 34
mgorecki
in reply to: norman.yuan

Here's the thing; we are going to use a web application that will launch a dll and pass it an array.

That dll will launch AutoCad and run the program using the array as the information it will use to set the variables in the program.

It was not my call to do it this way.

 

The web app developer that is creating the web app is out right now, so I don't have his web app to use to test my code.  So, I was just trying to create a simple exe file to use in it's place until they came back.  They will be gone for 3 weeks and the project needs to be finished in about 4 weeks.  Yay for me 😞

 

That's why I needed the exe to launch the dll which in turn would launch AutoCad and run using the variables passed to it.

Message 26 of 34
jeff
in reply to: mgorecki

Not sure how you 'launch' a dll, but I am not familiar how but there might be a way to create an entry point for a dll.

 

 

If you have to ship it very soon and just need to get it going the first hack that comes to mind is to serialize the data to the same location as the dll, and have a static initializer  in the class of the first method called that will deserialize the data.

 

Sounds and feels pretty hackish, but maybe searching for "C# dll main" or "Class Library entry point" might turn up some results.

You can also find your answers @ TheSwamp
Message 27 of 34
jeff
in reply to: jeff

Sorry,

My last post does not make sense and was confrused.

You can also find your answers @ TheSwamp
Message 28 of 34
alex_b
in reply to: mgorecki

Just a thought:

Are you trying to NETLOAD a plugin (dll) into Acad and pass to it an array to use inside Acad?

Message 29 of 34
mgorecki
in reply to: alex_b

No, I don't think it's possible to pass an array to a program that is loaded using Netload.

I just want to be able to pass an array argument to the .dll.

Message 30 of 34
mgorecki
in reply to: norman.yuan

Hi Norman,

Thank you for your help (on this and stuff in the past).  Ok we've decided to go another route.  We're going to stick to the text file that the dll will read and get all it's info.  So I've updated the code and all is wonderful, but now they also want me to save and exit AutoCad when the drawing is complete.

 

I have the save part down, but I cannot quit AutoCad as the program was "netloaded".

 

Can you please tell me how to launch AutoCad, create graphics, save and then close AutoCad?  I've read a bunch of things and I know you can launch AutoCad from a dll, but the problem happens when you try to create graphic objects because the launching program doesn't like having a reference to "acdbmgd".

 

This is some of the code I'm using to launch:

<CommandMethod("POD")> _
Public Sub POD()
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 MsgBox("Cannot create object of type """ & progID & """") Else acApp = DirectCast(Marshal.GetActiveObject(progID), AcadApplication) End If End Try End Try ' AutoCad should be running If acApp IsNot Nothing Then acApp.Visible = True

 I've always used netload, but I know some people can launch AutoCad, do stuff in the drawings, then close the drawings and exit AutoCad.  Can you show me how to do that?  I want to expand past "just netloading".

 

Thanks,

Mark

Message 31 of 34
norman.yuan
in reply to: mgorecki

I thought I knew what you are trying to do from previous posts, but the code you showed made me more confused: your code is a CommandMethod from acad managed API and can only run inside AutoCAD (after the DLL is netloaded). Tell me: why when the code is already running IN AutoCAD, yet your code still try to get an AutoCAD instance (and if failed, try to create one). It is like the running AutoCAD does not know itself is an AutoCAD session. Why?

 

BTW, if you are running NETLOADED managed code, yes, you can close AutoCAD by calling

 

Autodesk.AutoCAD.ApplicationServices.Application.Quit()

 

Let us clear things up:

 

You need to start AutoCAD from an external application (in your case, it is the web server application. If so, I mean, if the AutoCAD is to run in a unattented server by a server process, it is dangerous, because AutoCAD is designed as desktop app and offten need user to respond possible message popups. Not to mention the license policy may prohibite using AutoCAD this way), and control AutoCAD to do something and close it when done.

 

Without making things more complicated, you just need to automate AutoCAD via its COM API from the external application. And yes, you can build all the work (launching AutoCAD, open drawing to do something, saving/closing the drawing and then quitting AutoCAD) in a DLL (with referene to AutoCAD Type libabry/AutoAD ObjectDBX Common Type libabry - they are COM libraries) and load this DLL into the external app or web server(I do hope not).

 

In this case, no AutoCAD managed .NET API can be used (unless you do not mind to make things more complicated and insist to use managed API - then you need to expose your managed code as COM and properly registered it, and also make sure it is loaded into AutoCAD on startup...).

Message 32 of 34
mgorecki
in reply to: norman.yuan

Hi Norman,

Thanks for responding.  I'm sorry my post was so confusing.  I added the "launch AutoCad" section thinking I needed to have an external program call my dll to launch AutoCad in order to eventually quit out.

 

I tried the "Autodesk.AutoCAD.ApplicationServices.Application.Quit()" line, but it always creates a pop-up asking if I want to save.  I have already saved the drawing prior to the quit command.

Do you know how I can avoid the pop up?

Message 33 of 34
norman.yuan
in reply to: mgorecki

If you run the managed API code inside AutoCAD, then before you call Application.Quit(), you can close each document with method

 

Document.CloseAndDiscard()

 

Make sure the code runs in session context (e.g. the CommandMethod takes a CommandFlags.Session argument).

 

You can also use Document Pop/PushDbmod() method to reset system variable "DBMODE" of a drawing to 0 (no change), before quitting.

Message 34 of 34
mgorecki
in reply to: norman.yuan

Hi Norman, Can I email you my code?  I just don't think I uhnderstand the whole COM thing.

 

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