Discussion:
Loading correct version of AutoCAD
(too old to reply)
Zatopek
2004-09-09 08:20:37 UTC
Permalink
I porting an old VB-app to AutoCAD 2004
I know almost nothing about VB or VBA, so i have no idea of what i'm doing.

This line
ver = AutoCAD.Application.ActiveDocument.GetVariable("acadver")
Loads AutoCAD 2002. How can i force it to use the autocad started, or at
least use 2004?

I Snatched this piece of code from another project
Set acadObj = GetObject(, "AutoCAD.Application.16")
ver = acadObj.ActiveDocument.GetVariable("acadver")

But that does not run at all.

Is there any quick solution, sort of "Aha. Just put an 'a' instead of a 'b'
there"

/M
TomD
2004-09-09 13:13:10 UTC
Permalink
Post by Zatopek
I porting an old VB-app to AutoCAD 2004
I know almost nothing about VB or VBA, so i have no idea of what i'm doing.
This line
ver = AutoCAD.Application.ActiveDocument.GetVariable("acadver")
Loads AutoCAD 2002. How can i force it to use the autocad started, or at
least use 2004?
What do you meant by that line "loads AutoCad 2002"? That line is simply
setting the version variable to the ver variable.

I'm still fairly new to this all, but I used this recently, successfully,
without specifying any version. It simply grabbed the open session
(apparently).

Dim oAcadApp As AcadApplication, oLddProj As AeccApplication
Set oAcadApp = GetObject(, "AutoCAD.Application")
Set oLddProj = oAcadApp.GetInterfaceObject("Aecc.Application")

(Note the lack of the version number within the GetObject.)

....clarifications welcome, as always, from those in the know. ;)
TomD
2004-09-09 13:34:55 UTC
Permalink
Post by TomD
What do you meant by that line "loads AutoCad 2002"?
That line is simply setting the version variable to the ver variable."
That was what i would have exptected. But it fires a new AutoCAD 2002
session.
Guess something is registred to be 2002 somewhere
Are you absolutely certain about that? If that's true, then I'm waaaaay
confused...lol.

C'mon, gurus................help us out, here.
rwilkins
2004-09-09 13:51:24 UTC
Permalink
Set App = CreateObject("AutoCAD.Application") 'Loads last used version of AutoCAD
Set App = CreateObject("AutoCAD.Application.15") ' Load AutoCAD 2002
Set App = CreateObject("AutoCAD.Application.16") 'Loads AutoCAD 2004
Set App = CreateObject("AutoCAD.Application.16.1") ' Loads AutoCAD 2005
Mike Tuersley
2004-09-09 15:21:41 UTC
Permalink
Post by rwilkins
Set App = CreateObject("AutoCAD.Application") 'Loads last used version of AutoCAD
Actually its the last version of AutoCAD the user ran. Don't use it if you
could be in a multi-install environment and want control of which version
you are firing. Below is the standard function I use - there are others but
this suits my needs. Notice that I am using late binding so there is no
reference to AutoCAD required - especially since I am not sure at trun time
which one I'll find. To use early binding, remove the Object calls and
replace with the AutoCAD specific refs commented out to the right of
Object. Also, I am tracking whether I started AutoCAD or whether it was
already running. I do this so I can shut it down if I started it.


'==== GLOBAL VARS ====
Private g_cadApp As Object 'AutoCAD AcadApplication
Private g_cadDoc As Object 'AutoCAD AcadDocument
Private g_bStartedAutoCAD As Boolean

Public Sub DoIt()
'connect to AutoCAD
g_bStartedAutoCAD = StartAutoCAD(False)
'test to see if its awake
If IsAutoCADAwake = True Then
'Do whatever.....
End If
End Sub

Public Function StartAutoCAD(blnHide As Boolean) As Boolean
'+--Fire up AutoCAD
On Error Resume Next
'Get the 2000i/2002 Application object
Set g_cadApp = GetObject(, "AutoCAD.Application.15")
'If error try connecting to 2004
If Err Then
Err.Clear
Set g_cadApp = GetObject(, "AutoCAD.Application.16")
'If error try connecting to 2005
If Err Then
Err.Clear
Set g_cadApp = GetObject("AutoCAD.Application.16.1")
'If error try starting 2000i/2002
If Err Then
Err.Clear
Set g_cadApp = CreateObject("AutoCAD.Application.15")
'If error try starting 2004
If Err Then
Err.Clear
Set g_cadApp = CreateObject("AutoCAD.Application.16")
'If error try starting 2005
If Err Then
Err.Clear
Set g_cadApp = CreateObject("AutoCAD.Application.16.1")
Do
Err.Clear
Resume
Loop While Err.Number = 429
'started AutoCAD
StartAutoCAD = True
End If
End If
Else
'found AutoCAD already running
StartACAD = False
End If
Else
'found AutoCAD already running
StartACAD = False
End If
Else
'found AutoCAD already running
StartACAD = False
End If
'Set the AutoCAD document object
Set g_cadDoc = g_cadApp.ActiveDocument
End Function

Public Function IsAutoCADAwake() As Boolean
'+-- This checks to see if AutoCAD is in a quiescent state
On Error Resume Next
Dim State As Object 'AcadState
Dim sErrMsg As String
Set State = g_cadApp.GetAcadState
If Err Then
Select Case Err.Number
Case -2147417851
Err.Clear
IsAutoCADAwake = False
Case -2147418111
Err.Clear
sErrMsg = "AutoCAD is busy...Please switch to it and" & vbCr & _
"cancel whatever command it is attempting to do."
MsgBox sErrMsg
IsAutoCADAwake = False
Case Else
sErrMsg = "Something is preventing connecting" & vbCr & _
"to AutoCAD. Please shut down and restart."
MsgBox sErrMsg
Err.Clear
Exit Function
End Select
Else
IsAutoCADAwake = IIf(State.IsQuiescent, True, False)
End If
End Function

-- Mike
___________________________
Mike Tuersley
CADalyst's CAD Clinic
Rand IMAGINiT Technologies
___________________________
the trick is to realize that there is no spoon...
TomD
2004-09-09 20:11:48 UTC
Permalink
"Mike Tuersley" <***@rand.com> wrote in message news:5v6zv1kzrfok.1cfv65bxogdka$***@40tude.net...

Thank you both for the clarification on that. That's something that doesn't
seem to be documented well. It seems that most references to doing it
assume that you're already familiar with the process.
Zatopek
2004-09-10 06:56:53 UTC
Permalink
Is there a way to get the autocad that started my app?
I do not need to start an new, if no one is running, just fail.
The current autocad!

I see that you try a lot of versions one at the time.
I do not know if the user runs 2004 or 2005.
But I do the assumption that the user only runs one at the time.

(But if not; how can one figure out in what autocad a VB app runs if there
are 10 different version running!!!!)

/M
Post by Mike Tuersley
Post by rwilkins
Set App = CreateObject("AutoCAD.Application") 'Loads last used version of AutoCAD
Actually its the last version of AutoCAD the user ran. Don't use it if you
could be in a multi-install environment and want control of which version
you are firing. Below is the standard function I use - there are others but
this suits my needs. Notice that I am using late binding so there is no
reference to AutoCAD required - especially since I am not sure at trun time
which one I'll find. To use early binding, remove the Object calls and
replace with the AutoCAD specific refs commented out to the right of
Object. Also, I am tracking whether I started AutoCAD or whether it was
already running. I do this so I can shut it down if I started it.
'==== GLOBAL VARS ====
Private g_cadApp As Object 'AutoCAD AcadApplication
Private g_cadDoc As Object 'AutoCAD AcadDocument
Private g_bStartedAutoCAD As Boolean
Public Sub DoIt()
'connect to AutoCAD
g_bStartedAutoCAD = StartAutoCAD(False)
'test to see if its awake
If IsAutoCADAwake = True Then
'Do whatever.....
End If
End Sub
Public Function StartAutoCAD(blnHide As Boolean) As Boolean
'+--Fire up AutoCAD
On Error Resume Next
'Get the 2000i/2002 Application object
Set g_cadApp = GetObject(, "AutoCAD.Application.15")
'If error try connecting to 2004
If Err Then
Err.Clear
Set g_cadApp = GetObject(, "AutoCAD.Application.16")
'If error try connecting to 2005
If Err Then
Err.Clear
Set g_cadApp = GetObject("AutoCAD.Application.16.1")
'If error try starting 2000i/2002
If Err Then
Err.Clear
Set g_cadApp = CreateObject("AutoCAD.Application.15")
'If error try starting 2004
If Err Then
Err.Clear
Set g_cadApp = CreateObject("AutoCAD.Application.16")
'If error try starting 2005
If Err Then
Err.Clear
Set g_cadApp = CreateObject("AutoCAD.Application.16.1")
Do
Err.Clear
Resume
Loop While Err.Number = 429
'started AutoCAD
StartAutoCAD = True
End If
End If
Else
'found AutoCAD already running
StartACAD = False
End If
Else
'found AutoCAD already running
StartACAD = False
End If
Else
'found AutoCAD already running
StartACAD = False
End If
'Set the AutoCAD document object
Set g_cadDoc = g_cadApp.ActiveDocument
End Function
Public Function IsAutoCADAwake() As Boolean
'+-- This checks to see if AutoCAD is in a quiescent state
On Error Resume Next
Dim State As Object 'AcadState
Dim sErrMsg As String
Set State = g_cadApp.GetAcadState
If Err Then
Select Case Err.Number
Case -2147417851
Err.Clear
IsAutoCADAwake = False
Case -2147418111
Err.Clear
sErrMsg = "AutoCAD is busy...Please switch to it and" & vbCr & _
"cancel whatever command it is attempting to do."
MsgBox sErrMsg
IsAutoCADAwake = False
Case Else
sErrMsg = "Something is preventing connecting" & vbCr & _
"to AutoCAD. Please shut down and restart."
MsgBox sErrMsg
Err.Clear
Exit Function
End Select
Else
IsAutoCADAwake = IIf(State.IsQuiescent, True, False)
End If
End Function
-- Mike
___________________________
Mike Tuersley
CADalyst's CAD Clinic
Rand IMAGINiT Technologies
___________________________
the trick is to realize that there is no spoon...
Mike Tuersley
2004-09-10 14:46:00 UTC
Permalink
Zatopek, can you clarify what your code is/does? You have conflicting
Post by Zatopek
Is there a way to get the autocad that started my app?
This states AutoCAD is starting your app, in which case just pass the
instance of AutoCAD itself
Post by Zatopek
I do not need to start an new, if no one is running, just fail.
The current autocad!
This states that your app is starting itself. In which case, just remove
the conditional statements that CreateObject and leave only the GetObject
conditions. Once you get the object, just read the AutoCAD Version varaible
or add your own variable that tells you which one the function connected
to.

Which is it?
Post by Zatopek
I see that you try a lot of versions one at the time.
I do not know if the user runs 2004 or 2005.
But I do the assumption that the user only runs one at the time.
So do I and so does the code I posted! The code only returns a single
instance of AutoCAD based on which one it finds first. If all you want is
2004 or 2005, revise the code to just check for 16 and 16.1. The order in
which you check obviously determines which one is your preference.
Post by Zatopek
(But if not; how can one figure out in what autocad a VB app runs if there
are 10 different version running!!!!)
There may be 10 different versions INSTALLED, NOT RUNNING! After reading
the previous comments in this post, you should have your answer to this. If
not, re-post and I'll try explaining it differently.


-- Mike
___________________________
Mike Tuersley
CADalyst's CAD Clinic
Rand IMAGINiT Technologies
___________________________
the trick is to realize that there is no spoon...
Zatopek
2004-09-10 19:23:14 UTC
Permalink
Got me!
I don't know what i'm doing.

I got a VB6 app for 2002. For some reason it checks the version somewhere.
I debudgged it when 2002 was running (starting VB6 and stepping) so i got a
clue what i is doing.

First attempt to port it was just to debug it when 2004 was running.
The line mentioned just launched 2002. Suprised!!

One thing that puzzles me with Acad and VB is the EXE.
If the user starts my app from whitin acad, I wanted to know the acad
version that launched it, and if the user clicked the exe (that i do not
like) i wanted to trap that and just die.

But this is history now.
I decided to rewrite the app (small, i admit that) in VB.NET as a lab to see
if i can grasp that. (see another thread)
.NET apps at least runs whitin one instance of autocad.

Thanks a lot for the answers.

/M
Post by Mike Tuersley
Zatopek, can you clarify what your code is/does? You have conflicting
Post by Zatopek
Is there a way to get the autocad that started my app?
This states AutoCAD is starting your app, in which case just pass the
instance of AutoCAD itself
Post by Zatopek
I do not need to start an new, if no one is running, just fail.
The current autocad!
This states that your app is starting itself. In which case, just remove
the conditional statements that CreateObject and leave only the GetObject
conditions. Once you get the object, just read the AutoCAD Version varaible
or add your own variable that tells you which one the function connected
to.
Which is it?
Post by Zatopek
I see that you try a lot of versions one at the time.
I do not know if the user runs 2004 or 2005.
But I do the assumption that the user only runs one at the time.
So do I and so does the code I posted! The code only returns a single
instance of AutoCAD based on which one it finds first. If all you want is
2004 or 2005, revise the code to just check for 16 and 16.1. The order in
which you check obviously determines which one is your preference.
Post by Zatopek
(But if not; how can one figure out in what autocad a VB app runs if there
are 10 different version running!!!!)
There may be 10 different versions INSTALLED, NOT RUNNING! After reading
the previous comments in this post, you should have your answer to this. If
not, re-post and I'll try explaining it differently.
-- Mike
___________________________
Mike Tuersley
CADalyst's CAD Clinic
Rand IMAGINiT Technologies
___________________________
the trick is to realize that there is no spoon...
Mike Tuersley
2004-09-10 20:06:58 UTC
Permalink
Post by Zatopek
One thing that puzzles me with Acad and VB is the EXE.
If the user starts my app from whitin acad, I wanted to know the acad
version that launched it, and if the user clicked the exe (that i do not
like) i wanted to trap that and just die.
What can I say but it soundss poorly written. Running an EXE from within
AutoCAD makes it outt of process which is slow and not very good. If it was
to be accessed within AutoCAD, it should've been an ActiveX DLL. But, all
that aside, you could have had the EXE pass something so it knew AutoCAD,
and which version of it, had started it up.
Post by Zatopek
But this is history now.
I decided to rewrite the app (small, i admit that) in VB.NET as a lab to see
if i can grasp that. (see another thread)
.NET apps at least runs whitin one instance of autocad.
Only now because of 2005 - you'll get the same thing once 2006 is out =)
You'll also notice in that thread that you're stilll limited to ActiveX
even though you're in .NET :(

-- Mike
___________________________
Mike Tuersley
CADalyst's CAD Clinic
Rand IMAGINiT Technologies
___________________________
the trick is to realize that there is no spoon...
Zatopek
2004-09-10 20:37:06 UTC
Permalink
"What can I say but it soundss poorly written"
Second that!!!

"Only now because of 2005 - you'll get the same thing once 2006 is out "
But than you can control the "netload". No more clicking on exes

Rats.
But thanks.

Case closed

/M
Post by Mike Tuersley
Post by Zatopek
One thing that puzzles me with Acad and VB is the EXE.
If the user starts my app from whitin acad, I wanted to know the acad
version that launched it, and if the user clicked the exe (that i do not
like) i wanted to trap that and just die.
What can I say but it soundss poorly written. Running an EXE from within
AutoCAD makes it outt of process which is slow and not very good. If it was
to be accessed within AutoCAD, it should've been an ActiveX DLL. But, all
that aside, you could have had the EXE pass something so it knew AutoCAD,
and which version of it, had started it up.
Post by Zatopek
But this is history now.
I decided to rewrite the app (small, i admit that) in VB.NET as a lab to see
if i can grasp that. (see another thread)
.NET apps at least runs whitin one instance of autocad.
Only now because of 2005 - you'll get the same thing once 2006 is out =)
You'll also notice in that thread that you're stilll limited to ActiveX
even though you're in .NET :(
-- Mike
___________________________
Mike Tuersley
CADalyst's CAD Clinic
Rand IMAGINiT Technologies
___________________________
the trick is to realize that there is no spoon...
Mike Tuersley
2004-09-11 04:37:05 UTC
Permalink
ReOpened ;-) You can control the netload how? If your app will work in both
2005 & 2006, you'll still need to determine which is which I think - unless
you keep two - one for each version.

-- Mike
___________________________
Mike Tuersley
CADalyst's CAD Clinic
Rand IMAGINiT Technologies
___________________________
the trick is to realize that there is no spoon...

Zatopek
2004-09-09 13:16:49 UTC
Permalink
Post by TomD
What do you meant by that line "loads AutoCad 2002"?
That line is simply setting the version variable to the ver variable."
That was what i would have exptected. But it fires a new AutoCAD 2002
session.
Guess something is registred to be 2002 somewhere
Post by TomD
Post by Zatopek
I porting an old VB-app to AutoCAD 2004
I know almost nothing about VB or VBA, so i have no idea of what i'm
doing.
Post by Zatopek
This line
ver = AutoCAD.Application.ActiveDocument.GetVariable("acadver")
Loads AutoCAD 2002. How can i force it to use the autocad started, or at
least use 2004?
What do you meant by that line "loads AutoCad 2002"? That line is simply
setting the version variable to the ver variable.
I'm still fairly new to this all, but I used this recently, successfully,
without specifying any version. It simply grabbed the open session
(apparently).
Dim oAcadApp As AcadApplication, oLddProj As AeccApplication
Set oAcadApp = GetObject(, "AutoCAD.Application")
Set oLddProj = oAcadApp.GetInterfaceObject("Aecc.Application")
(Note the lack of the version number within the GetObject.)
....clarifications welcome, as always, from those in the know. ;)
Continue reading on narkive:
Loading...