Can I launch AutoCAD in ASP

Can I launch AutoCAD in ASP

Anonymous
Not applicable
748 Views
20 Replies
Message 1 of 21

Can I launch AutoCAD in ASP

Anonymous
Not applicable
Hello:
I tried to run some routine in AutoCAD(MDT) from ASP (Active Server
Pages). It took too much time and fail. Is there anyone have been work on
the same situation? If yes, could you give me some advice. Thanks in
advance.
Here is the code in my asp file:
<%
Server.ScriptTimeout = 300
Response.Write "Beging to launch AutoCAD"
dim oacad
set oacad = createobject("AutoCAD.Application")
oacad.visible = true
oacad.loaddvb Server.MapPath("test.dvb")
oacad.runmacro "test.test"
oacad.Unloaddvb "test.dvb"
oacad.quit
set oacad = nothing
Response.Write "Finish AutoCAD Macro Running"
%>

I have been run a similar VBS which work just fine. code is:

dim oacad
set oacad = createobject("AutoCAD.Application")
oacad.visible = true
oacad.loaddvb "f:\mdt projects\mdtwebdb\test.dvb"
oacad.runmacro "test.test"
oacad.Unloaddvb "f:\mdt projects\mdtwebdb\test.dvb"
oacad.quit
set oacad = nothing

Thanks
Wes
0 Likes
749 Views
20 Replies
Replies (20)
Message 2 of 21

Anonymous
Not applicable
Weslley,

I worked on a similar project and the answer is yes, it can be done.

The problem you are experiencing is that ASP can not run an out-of-process
component. In other words, you can't directly create an AutoCAD Application
object because you are launching a separate executable.

Instead, you need to encapsulate the call in a dll and create an instance of
that instead. Copy this code into a VB class module called CAcad:

Option Explicit

Private mCommand As String
Private mAcad As Object

Private Sub Class_Initialize()
mCommand = ""
Set mAcad = Nothing
End Sub

Public Property Get Command() As String
Command = mCommand
End Property

Public Property Let Command(sCommand As String)
mCommand = sCommand
End Property

Public Function Create() As Boolean
On Error Resume Next
Set mAcad = CreateObject("AutoCAD.Application.15")
If mAcad Is Nothing Then
Create = False
Else
mAcad.Visible = True
Create = True
End If
End Function

Public Function Destroy() As Boolean
Set mAcad = Nothing
Destroy = True
End Function

Public Function Run() As Boolean
If mAcad Is Nothing Then
Run = False
Else
mAcad.ActiveDocument.SendCommand mCommand
Run = True
End If
End Function

Compile the dll to Acad.dll. Now, in your ASP code you can write something
like this:

Set objAcad = CreateObject("Acad.CAcad")
objAcad.Create
objAcad.Command = "blah blah blah "
objAcad.Run
objAcad.Destroy
Set objAcad = Nothing

Make sure that you use a command to close AutoCAD or modify the class to do
it for you.

Hope this helps.

Andrew Wilford
andrew@artwork.com
0 Likes
Message 3 of 21

Anonymous
Not applicable
Thanks Andrew, I will try it right away.

Wes

"Andrew Wilford" wrote in message
news:7D1CB5E8632E812E710FCB176EFC4D1E@in.WebX.maYIadrTaRb...
> Weslley,
>
> I worked on a similar project and the answer is yes, it can be done.
>
> The problem you are experiencing is that ASP can not run an out-of-process
> component. In other words, you can't directly create an AutoCAD
Application
> object because you are launching a separate executable.
>
> Instead, you need to encapsulate the call in a dll and create an instance
of
> that instead. Copy this code into a VB class module called CAcad:
>
> Option Explicit
>
> Private mCommand As String
> Private mAcad As Object
>
> Private Sub Class_Initialize()
> mCommand = ""
> Set mAcad = Nothing
> End Sub
>
> Public Property Get Command() As String
> Command = mCommand
> End Property
>
> Public Property Let Command(sCommand As String)
> mCommand = sCommand
> End Property
>
> Public Function Create() As Boolean
> On Error Resume Next
> Set mAcad = CreateObject("AutoCAD.Application.15")
> If mAcad Is Nothing Then
> Create = False
> Else
> mAcad.Visible = True
> Create = True
> End If
> End Function
>
> Public Function Destroy() As Boolean
> Set mAcad = Nothing
> Destroy = True
> End Function
>
> Public Function Run() As Boolean
> If mAcad Is Nothing Then
> Run = False
> Else
> mAcad.ActiveDocument.SendCommand mCommand
> Run = True
> End If
> End Function
>
> Compile the dll to Acad.dll. Now, in your ASP code you can write something
> like this:
>
> Set objAcad = CreateObject("Acad.CAcad")
> objAcad.Create
> objAcad.Command = "blah blah blah "
> objAcad.Run
> objAcad.Destroy
> Set objAcad = Nothing
>
> Make sure that you use a command to close AutoCAD or modify the class to
do
> it for you.
>
> Hope this helps.
>
> Andrew Wilford
> andrew@artwork.com
>
0 Likes
Message 4 of 21

Anonymous
Not applicable
Hello Andrew:
I tried the code and test it in my localhost. I am not sure if it work or
not so I change some code, when I tried to recompile it, I got a error
message:
"Permission denied."
I tried to log out and log in my computer, it does not work
I tried to unregistry it, it is not work eiher.

could you meet the same problem before? or is there anyone know what's wrong
there?

Thanks a lot
Wes
"Weslley Wang" wrote in message
news:83209B0B50DD66DAA033073A29FD8D45@in.WebX.maYIadrTaRb...
> Thanks Andrew, I will try it right away.
>
> Wes
>
> "Andrew Wilford" wrote in message
> news:7D1CB5E8632E812E710FCB176EFC4D1E@in.WebX.maYIadrTaRb...
> > Weslley,
> >
> > I worked on a similar project and the answer is yes, it can be done.
> >
> > The problem you are experiencing is that ASP can not run an
out-of-process
> > component. In other words, you can't directly create an AutoCAD
> Application
> > object because you are launching a separate executable.
> >
> > Instead, you need to encapsulate the call in a dll and create an
instance
> of
> > that instead. Copy this code into a VB class module called CAcad:
> >
> > Option Explicit
> >
> > Private mCommand As String
> > Private mAcad As Object
> >
> > Private Sub Class_Initialize()
> > mCommand = ""
> > Set mAcad = Nothing
> > End Sub
> >
> > Public Property Get Command() As String
> > Command = mCommand
> > End Property
> >
> > Public Property Let Command(sCommand As String)
> > mCommand = sCommand
> > End Property
> >
> > Public Function Create() As Boolean
> > On Error Resume Next
> > Set mAcad = CreateObject("AutoCAD.Application.15")
> > If mAcad Is Nothing Then
> > Create = False
> > Else
> > mAcad.Visible = True
> > Create = True
> > End If
> > End Function
> >
> > Public Function Destroy() As Boolean
> > Set mAcad = Nothing
> > Destroy = True
> > End Function
> >
> > Public Function Run() As Boolean
> > If mAcad Is Nothing Then
> > Run = False
> > Else
> > mAcad.ActiveDocument.SendCommand mCommand
> > Run = True
> > End If
> > End Function
> >
> > Compile the dll to Acad.dll. Now, in your ASP code you can write
something
> > like this:
> >
> > Set objAcad = CreateObject("Acad.CAcad")
> > objAcad.Create
> > objAcad.Command = "blah blah blah "
> > objAcad.Run
> > objAcad.Destroy
> > Set objAcad = Nothing
> >
> > Make sure that you use a command to close AutoCAD or modify the class to
> do
> > it for you.
> >
> > Hope this helps.
> >
> > Andrew Wilford
> > andrew@artwork.com
> >
>
0 Likes
Message 5 of 21

Anonymous
Not applicable
Wes,

That is a known problem of the Microsoft IIS Server. Once you have created
an instance of the object you can not re-register the dll until you have
stopped and restarted the IIS server.

Create a batch file called stop.bat and add this text to it:

C:\winnt\system32\net stop iisadmin /y

Create another batch file called start.bat and add this text:

C:\winnt\system32\net start w3svc

Now, when you want to re-compile (and hence re-register) the dll, run
stop.bat, compile your dll and run start.bat. I know it sounds like a pain
but it was the only solution I found, I believe via deja or google.

Good luck!

Andrew Wilford
andrew@artwork.com
0 Likes
Message 6 of 21

Anonymous
Not applicable
Thanks
"Andrew Wilford" wrote in message
news:02952D9731B6EB831E50DF1611296280@in.WebX.maYIadrTaRb...
> Wes,
>
> That is a known problem of the Microsoft IIS Server. Once you have created
> an instance of the object you can not re-register the dll until you have
> stopped and restarted the IIS server.
>
> Create a batch file called stop.bat and add this text to it:
>
> C:\winnt\system32\net stop iisadmin /y
>
> Create another batch file called start.bat and add this text:
>
> C:\winnt\system32\net start w3svc
>
> Now, when you want to re-compile (and hence re-register) the dll, run
> stop.bat, compile your dll and run start.bat. I know it sounds like a pain
> but it was the only solution I found, I believe via deja or google.
>
> Good luck!
>
> Andrew Wilford
> andrew@artwork.com
>
0 Likes
Message 7 of 21

Anonymous
Not applicable
Hi
I have the same problem as Wessley and i have tried your method, but it does not seem to solve the problem. IE still times out when I try to launch AutoCAD from an asp page.
Could this come from my machine, OS, or version of AutoCAD (currently I'm under Win2000 and AutoCAD 2000).
This is going to drive me mad !
Thanks
Martin
0 Likes
Message 8 of 21

Anonymous
Not applicable
Hi Martin,

You must make sure to set the Acad object visible, even though it is running
on the server. If you do not, then AutoCAD will not properly initialize.

The only other problem I found, ironically, was with AutoCAD 2000i. I had
the whole thing running smoothly and when I upgraded to 2000i it failed
inexplicably. I didn't have to time to waste with it so I went back to 2000
and it worked again. I have successfully run this on a Win NT workstation
and server.

If you post the asp code I may be able to see something that could cause a
problem.

Regards,

Andrew Wilford
andrew@artwork.com
0 Likes
Message 9 of 21

Anonymous
Not applicable
The code i used at first (works fine with SolidWorks, but not with AutoCAD) :
Dim AcadApp
Set AcadApp = server.CreateObject("AutoCAD.Application")
Set AcadApp.Application.Visible = True
set AcadApp = nothing

Then I used your method :
Set objAcad = CreateObject("Acad.CAcad")
objAcad.Create
objAcad.Command = " Some command "
objAcad.Run
objAcad.Destroy
Set objAcad = Nothing

I'm thinking that i may come from some server protection prevent executables to be run locally, but i'm not sure.
Thanks indeed
Martin
0 Likes
Message 10 of 21

Anonymous
Not applicable
When you compiled the dll, was it on the server? If not, you will have to
register the dll using regsvr32 or you will be unable to create an instance
of CAcad. If you haven't done this before, simply open a dos box and execute
the command:

regsvr32.exe acad.dll

If it can't find the command then execute it from your Windows System 32
directory.

Good luck!

Andrew Wilford
andrew@artwork.com
0 Likes
Message 11 of 21

Anonymous
Not applicable
I have compiled the dll on the server and it is registered. I can access it, but when i try to execute the asp file, IE just seems to be blocked after the Create statement and it just times out. Really weird.
0 Likes
Message 12 of 21

Anonymous
Not applicable
Hi Andrew
OK, i have managed to start the acad.exe at last. I've had to grant the remote user admin rights for AutoCAD using dcomcnfg.exe. This is a temporary solution, but it is sufficient for the moment.
I have a last question for you (I promise i don't annoy you anymore...):
What is the syntax of the command that i pass to AutoCAD using the objAcad.Command and objAcad.Run ? I have tried this :
objAcad.Command = "ThisDrawing.Application.Visible = True"
objAcad.Run
but it does not make the AutoCAD session visible. Is this syntax correct ?
Thanks a lot.
Martin
0 Likes
Message 13 of 21

Anonymous
Not applicable
Hi Martin,

Pleease, ask away. I know how frustrating it can get when you have to do
something very specific which is undocumented. I'm happy to help.

The syntax of the command method is LISP. It looks like you are trying to
pass VBA commands. This method will simply pass the text to the AutoCAD
command line. I added this method because the functions I wanted to run from
the server were in LISP. If you need to execute VBA code then why not just
add it to the dll? I would show you what I mean using your example, but you
don't need to call the visible method. It is taken care of in the create
method. You won't actually see the AutoCAD session because of the way ASP
works. However, this call is necessary or AutoCAD does not initialize
correctly.

Let's say you want to call a LISP function called MyFunc. You would use:

objAcad.Command "(MyFunc) "

The trailing space is to force execution. Alternatively, if you want to call
a VBA function, then you should just add it to your dll.

Hope this helps and let me know if you hve further questions.

Regards,

Andrew Wilford
andrew@artwork.com
0 Likes
Message 14 of 21

Anonymous
Not applicable
Hi Andrew

OK, i understand now why those VBA commands would not execute...
So, the commands i send through ObjAcad.Command are either AutoCAD command line or Lisp, right ? For the moment, i'd like to test the AutoCAD session that i see in the task manager to see if it is functionnal. As i'm not an expert in AutoCAD or in Lisp, i have tried to send simple commands to AutoCAD, such as Quit, Open or New. This is how i have done :

<%
Response.Write "Loading AutoCAD:
"
Set objAcad = CreateObject("Acad.CAcad")
ObjAcad.Create
objAcad.Command = "QUIT "
objAcad.Run
objAcad.Destroy
Set objAcad = Nothing
%>

This should launch AutoCAD and close it, but it isn't, i still see an acad.exe in the task maanger. If i am missing the point here, could you tell me what kind of command to send to AutoCAD to test it, i mean to be sure that the session can be accessed.
Another thing i have seen is that if i write the following ASP code :
<%
Set objAcad = CreateObject("Acad.CAcad")
Response.Write(ObjAcad.Create)
%>
I get a "False" on my HTML page. The weird thing is that the acad.exe is visble in the task manager.
Thanks
Martin
0 Likes
Message 15 of 21

Anonymous
Not applicable
Krusty:

What you need is to understand the object model behind the scene, i.e the
Acad object model. Supposing AcadObj is the Acad application, you need a
reference to the active document or documents collection. In general there
is no need to send commands to Autocad in the way you are trying to do. Try
this to obtain the name of a (just opened) drawing:

<%
Set AcadObj = Server.CreateObject("Acad.CAcad")
AcadObj.Documents.Open("filename") ' Complete with a real dwg filename like
c:\dwg\mydwg.dwg
Response.Write(AcadObj.ActiveDocument.Name)
AcadObj.Quit
Set AcadObj = Nothing
%>

Take a look into the Autocad object model

-gnb
0 Likes
Message 16 of 21

Anonymous
Not applicable
Hi Martin,

You are absolutely correct in the commands you are trying to pass. They
should work. It is worrying that you get a false returned from the create
method. Unfortunately, if you look at the code, this implies it did not
successfully create an AutoCAD object. When you say you see it in Task
Manager, is it listed in applications or processes? If it's in applications
then it should be running. If it's only in processes then it did not
initialize properly.

Gaston is quite right in what he is telling you to try. If you are not
familiar with LISP then you should stick to VB/VBA. You do this by taking
your existing VBA code and modifying it to work with VB. For instance,
instead of:

objAcad.Command "QUIT "

You would use:

objAcad.Quit

One final thing. I'm no NT security expert, so I advise you to check what
consequences this will have before doing it. Try putting the dll in the
scripts directory of the IIS server and re-registering it. Make sure the
permissions on the file and directory are SYSTEM: Full Control and Everyone:
Change.

Hope some of this will help.

Regards,

Andrew Wilford
andrew@artwork.com
0 Likes
Message 17 of 21

Anonymous
Not applicable
Hi Andrew

I have tried all you told me and guess what ? It's not working. In fact, the autocad session i see in the task manager is only listed in processes, but seems to have initialized : just after launching the asp file, if i hit F1 it launches the help, if i hit CTRL+N, a box appears, if i hit CTRL+O, a box appears, I can close it with ALT+F4 etc...
I have also built a Shutdown method in the dll file using the ObjAcad.Quit method, but just as the other method (Run and Create, it returns False, meaning that mAcad is equal to nothing). Here is the code :

Public Function Shutdown() As Boolean
If mAcad Is Nothing Then
Shutdown = False
Else
mAcad.Application.Quit
Shutdown = True
End If
End Function

I've tried to comment the if statement to force the quit command to be passed and i receive an error message in IE :

Error Type:
Acad (0x800A005B)
Object variable or With block variable not set

I'm sure that the problem comes from a simple thing, but i just can't see where it is. Is there a flag to turn on for AutoCAD to accept external commands or something like that ?

Thanks
0 Likes
Message 18 of 21

Anonymous
Not applicable
Krusty:

This function in a dll expose the acad application

Public Function GetAcadObj()
On Error Resume Next
Set acadApp = GetObject(, "AutoCAD.Application")
If Err Then
Err.Clear
Set acadApp = CreateObject("AutoCAD.Application")
If Err Then
Debug.Print Err.Description
Exit Function
End If
End If
Set GetAcadObj = acadApp
End Function

Supposing the dll is named AcadASPdll, and the class is class1
this is what you need in the asp file

Set AcadASP = Server.CreateObject("AcadASPdll.Class1")
Set AcadApp = AcadASP.GetAcadObj

now you have the acad application object in the AcadApp variable

regards,

Gaston Nunez
0 Likes
Message 19 of 21

Anonymous
Not applicable
Hi Martin,

The only time I saw this behavior, when everything in the code seemed okay,
was when I tried 2000i. I never figured out why it didn't work so went back
to 2000. If you check your code by running it through another VB app instead
of ASP and it works, then I really don't know what else to tell you.

I'm afraid I know of no flag to switch on commands as you described.

Can you try this on another machine to see if you get the same results?

Andrew Wilford
andrew@artwork.com
0 Likes
Message 20 of 21

Anonymous
Not applicable
Hi
I've been on something else for a few days and i will try all that later. Thank you for your help and i'll let you know if it works.
Martin
0 Likes