Set ActiveDocument question

Set ActiveDocument question

Anonymous
Not applicable
602 Views
7 Replies
Message 1 of 8

Set ActiveDocument question

Anonymous
Not applicable
Hi all,
I searched hundreds of posts on google first but didn't find anything about
this
probably used wrong search term("ActiveDocument")

the line below sometimes works and sometimes doesn't
does any one have the secret to how to wait for acad to realize it's changed
it's activeDoc?

Dim oDoc as AcadDocument
Dim oAcad as AcadApplication
Dim sSettingDwgNameas String
'(sSettingDwgName is open in editor at time this sub is run)

'here's the code that "should" switch documents....
oAcad.ActiveDocument = oAcad.Documents.Item(sSettingDwgName)
' - or equaly valid
oAcad.Documents.Item(sSettingDwgName).Activate

Set oDoc = oAcad.ActiveDocument
'now theoretically oDoc.Name = sSettingDwgName....but noooooooooooooo
:-)


now here's the deal, the acad editor visually always switches immediately to
the new dwg (sSettingDwgName) as if the line setting the active document
actually worked

however, code execution using the oDoc reference sometimes (70% of the time
approx) still points to the doc from which the code originated.

Obviously i have to put a timer or something in there to wait for acad to
figure out where it is, but I haven't succeeded in getting any thing to work
even 90% of the time.

This is just for a temporary routine where I select something in one doc and
then switch to another doc to zoom to a corresponding item.

any ideas?

heres a quickie function i'm trying to use
I'm not checking if doc is open because I know it is, in an actual function
i'd put more error trapping in, just keeping it simple for this post

Function SwitchToDoc(sDwgName As String, ByRef odoc As AcadDocument) As
Boolean
Dim oAcad As AcadApplication
Set oAcad = odoc.Application
'sometimes this works, sometimes it doesn't work
'doc changes in editor but focus is still on previous document
Dim dThisTime As Double
Dim dTryTime As Double
dTryTime = 5 'seconds
Dim dStartTime As Double
Dim dElapsed As Double
'IF YOU'RE ALREADY HERE, NOTHING TO DO
If oAcad.ActiveDocument.Name = sDwgName Then
Set odoc = oAcad.ActiveDocument
Debug.Print "oDoc: " & odoc.Name
SwitchToDoc = True
Exit Function
End If


Debug.Print "Before changing doc, name is " & oAcad.ActiveDocument.Name
'either of these *should* work
'oAcad.ActiveDocument = oAcad.Documents.Item(sDwgName)
oAcad.Documents.Item(sDwgName).Activate

'IF THAT WORKED, WE'RE DONE
If oAcad.ActiveDocument.Name = sDwgName Then
Set odoc = oAcad.ActiveDocument
Debug.Print "oDoc: " & odoc.Name
SwitchToDoc = True
Exit Function
End If

'SADLY MOST OF THE TIME IT DOESN'T WORK
Debug.Print "after changing doc, name is still " & odoc.Name
If odoc.Name <> sDwgName Then
dStartTime = Timer

'to avoid infinite loop
Do While dElapsed < dTryTime
If oAcad.GetAcadState.IsQuiescent = True Then
Debug.Print "ready"
SwitchToDoc = True
Exit Do
Else
Debug.Print "Not ready, timer: " & dThisTime
End If
DoEvents
Debug.Print " oDoc: " & odoc.Name
dThisTime = Timer
dElapsed = dThisTime - dStartTime
If odoc.Name <> sDwgName Then
Debug.Print "Still not right dwg <" & dElapsed & ">"
Else
Debug.Print "Finally that changed "
SwitchToDoc = True
Exit Do
End If

If dElapsed >= dTryTime Then
Debug.Print "Time ran out for doc change"
Exit Function
End If '

Loop

Else
Debug.Print "Doc change worked first time "
SwitchToDoc = True
End If
End Function
0 Likes
603 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable
How are you loading your macro ??

--
Saludos, Ing. Jorge Jimenez, SICAD S.A., Costa Rica


"MP" wrote in message
news:5228903@discussion.autodesk.com...
Hi all,
I searched hundreds of posts on google first but didn't find anything about
this
probably used wrong search term("ActiveDocument")

the line below sometimes works and sometimes doesn't
does any one have the secret to how to wait for acad to realize it's changed
it's activeDoc?

Dim oDoc as AcadDocument
Dim oAcad as AcadApplication
Dim sSettingDwgNameas String
'(sSettingDwgName is open in editor at time this sub is run)

'here's the code that "should" switch documents....
oAcad.ActiveDocument = oAcad.Documents.Item(sSettingDwgName)
' - or equaly valid
oAcad.Documents.Item(sSettingDwgName).Activate

Set oDoc = oAcad.ActiveDocument
'now theoretically oDoc.Name = sSettingDwgName....but noooooooooooooo
:-)


now here's the deal, the acad editor visually always switches immediately to
the new dwg (sSettingDwgName) as if the line setting the active document
actually worked

however, code execution using the oDoc reference sometimes (70% of the time
approx) still points to the doc from which the code originated.

Obviously i have to put a timer or something in there to wait for acad to
figure out where it is, but I haven't succeeded in getting any thing to work
even 90% of the time.

This is just for a temporary routine where I select something in one doc and
then switch to another doc to zoom to a corresponding item.

any ideas?

heres a quickie function i'm trying to use
I'm not checking if doc is open because I know it is, in an actual function
i'd put more error trapping in, just keeping it simple for this post

Function SwitchToDoc(sDwgName As String, ByRef odoc As AcadDocument) As
Boolean
Dim oAcad As AcadApplication
Set oAcad = odoc.Application
'sometimes this works, sometimes it doesn't work
'doc changes in editor but focus is still on previous document
Dim dThisTime As Double
Dim dTryTime As Double
dTryTime = 5 'seconds
Dim dStartTime As Double
Dim dElapsed As Double
'IF YOU'RE ALREADY HERE, NOTHING TO DO
If oAcad.ActiveDocument.Name = sDwgName Then
Set odoc = oAcad.ActiveDocument
Debug.Print "oDoc: " & odoc.Name
SwitchToDoc = True
Exit Function
End If


Debug.Print "Before changing doc, name is " & oAcad.ActiveDocument.Name
'either of these *should* work
'oAcad.ActiveDocument = oAcad.Documents.Item(sDwgName)
oAcad.Documents.Item(sDwgName).Activate

'IF THAT WORKED, WE'RE DONE
If oAcad.ActiveDocument.Name = sDwgName Then
Set odoc = oAcad.ActiveDocument
Debug.Print "oDoc: " & odoc.Name
SwitchToDoc = True
Exit Function
End If

'SADLY MOST OF THE TIME IT DOESN'T WORK
Debug.Print "after changing doc, name is still " & odoc.Name
If odoc.Name <> sDwgName Then
dStartTime = Timer

'to avoid infinite loop
Do While dElapsed < dTryTime
If oAcad.GetAcadState.IsQuiescent = True Then
Debug.Print "ready"
SwitchToDoc = True
Exit Do
Else
Debug.Print "Not ready, timer: " & dThisTime
End If
DoEvents
Debug.Print " oDoc: " & odoc.Name
dThisTime = Timer
dElapsed = dThisTime - dStartTime
If odoc.Name <> sDwgName Then
Debug.Print "Still not right dwg <" & dElapsed & ">"
Else
Debug.Print "Finally that changed "
SwitchToDoc = True
Exit Do
End If

If dElapsed >= dTryTime Then
Debug.Print "Time ran out for doc change"
Exit Function
End If '

Loop

Else
Debug.Print "Doc change worked first time "
SwitchToDoc = True
End If
End Function
0 Likes
Message 3 of 8

Anonymous
Not applicable
I do experience what you described. Maybe I test drawing size is too small.
However, I have run into an issue I did not notice before.

Here is my test:

1. Start Acad (I use Acad2004);
2. Open two blank drawings, named as "Drawing1.dwg" and "Drawing2.dwg";
3. With "Drawing1.dwg" as active document, run following code:

Public Sub SwitchDwg()

Dim App As AcadApplication
Set App = ThisDrawing.Application

Dim dwg As AcadDocument

Debug.Print App.ActiveDocument.Name

''Note: if "Drawing2.dwg" is already active, this line will cause error
App.ActiveDocument = App.Documents.Item("Drawing2.dwg")
App.Documents.Item("Drawing2.dwg").Activate

Debug.Print App.ActiveDocument.Name

Set dwg = App.ActiveDocument

Debug.Print "=====" & dwg.Name & "======="

End Sub


No matter how many times I test, it always get correct drawing name printed.

However, you will run into an error when you call

App.ActiveDocument=App.Documents.Item(DwgName)

if the document is already active. That is, you cannot get an AcadDocument
object and sign it to AcadApplication.ActiveDocument, if the AcadDocument
object IS the ActiveDocument.

So, my suspect that your code has error handling somewhere, like On Error
Resume Next... that prevent you from seeing the true cause of your problem?

In your logic, you should check if the ActiveDocument has the name as the
sSettingDwgName. If they are the same, you do not set
Documents.Item(sSettingDwgName) as ActiveDocument, because it is already
active.


"MP" wrote in message
news:5228903@discussion.autodesk.com...
Hi all,
I searched hundreds of posts on google first but didn't find anything about
this
probably used wrong search term("ActiveDocument")

the line below sometimes works and sometimes doesn't
does any one have the secret to how to wait for acad to realize it's changed
it's activeDoc?

Dim oDoc as AcadDocument
Dim oAcad as AcadApplication
Dim sSettingDwgNameas String
'(sSettingDwgName is open in editor at time this sub is run)

'here's the code that "should" switch documents....
oAcad.ActiveDocument = oAcad.Documents.Item(sSettingDwgName)
' - or equaly valid
oAcad.Documents.Item(sSettingDwgName).Activate

Set oDoc = oAcad.ActiveDocument
'now theoretically oDoc.Name = sSettingDwgName....but noooooooooooooo
:-)


now here's the deal, the acad editor visually always switches immediately to
the new dwg (sSettingDwgName) as if the line setting the active document
actually worked

however, code execution using the oDoc reference sometimes (70% of the time
approx) still points to the doc from which the code originated.

Obviously i have to put a timer or something in there to wait for acad to
figure out where it is, but I haven't succeeded in getting any thing to work
even 90% of the time.

This is just for a temporary routine where I select something in one doc and
then switch to another doc to zoom to a corresponding item.

any ideas?

heres a quickie function i'm trying to use
I'm not checking if doc is open because I know it is, in an actual function
i'd put more error trapping in, just keeping it simple for this post

Function SwitchToDoc(sDwgName As String, ByRef odoc As AcadDocument) As
Boolean
Dim oAcad As AcadApplication
Set oAcad = odoc.Application
'sometimes this works, sometimes it doesn't work
'doc changes in editor but focus is still on previous document
Dim dThisTime As Double
Dim dTryTime As Double
dTryTime = 5 'seconds
Dim dStartTime As Double
Dim dElapsed As Double
'IF YOU'RE ALREADY HERE, NOTHING TO DO
If oAcad.ActiveDocument.Name = sDwgName Then
Set odoc = oAcad.ActiveDocument
Debug.Print "oDoc: " & odoc.Name
SwitchToDoc = True
Exit Function
End If


Debug.Print "Before changing doc, name is " & oAcad.ActiveDocument.Name
'either of these *should* work
'oAcad.ActiveDocument = oAcad.Documents.Item(sDwgName)
oAcad.Documents.Item(sDwgName).Activate

'IF THAT WORKED, WE'RE DONE
If oAcad.ActiveDocument.Name = sDwgName Then
Set odoc = oAcad.ActiveDocument
Debug.Print "oDoc: " & odoc.Name
SwitchToDoc = True
Exit Function
End If

'SADLY MOST OF THE TIME IT DOESN'T WORK
Debug.Print "after changing doc, name is still " & odoc.Name
If odoc.Name <> sDwgName Then
dStartTime = Timer

'to avoid infinite loop
Do While dElapsed < dTryTime
If oAcad.GetAcadState.IsQuiescent = True Then
Debug.Print "ready"
SwitchToDoc = True
Exit Do
Else
Debug.Print "Not ready, timer: " & dThisTime
End If
DoEvents
Debug.Print " oDoc: " & odoc.Name
dThisTime = Timer
dElapsed = dThisTime - dStartTime
If odoc.Name <> sDwgName Then
Debug.Print "Still not right dwg <" & dElapsed & ">"
Else
Debug.Print "Finally that changed "
SwitchToDoc = True
Exit Do
End If

If dElapsed >= dTryTime Then
Debug.Print "Time ran out for doc change"
Exit Function
End If '

Loop

Else
Debug.Print "Doc change worked first time "
SwitchToDoc = True
End If
End Function
0 Likes
Message 4 of 8

Anonymous
Not applicable
Hola Jorge,



"Jorge Jimenez" wrote in message
news:5229064@discussion.autodesk.com...
How are you loading your macro ??

result is same (sometimes works sometimes doesn't )
whether I run via lisp or via vbaide f5/run

(vla-runmacro (vlax-get-acad-object) macroname)
0 Likes
Message 5 of 8

Anonymous
Not applicable
Hi Norman,
Yes, I got that error too...that's why i have the lines seen below in the
test function

If oAcad.ActiveDocument.Name = sDwgName Then
Set odoc = oAcad.ActiveDocument
Debug.Print "oDoc: " & odoc.Name
SwitchToDoc = True
Exit Function
End If




"Norman Yuan" wrote in message
news:5229134@discussion.autodesk.com...
I do experience what you described. Maybe I test drawing size is too small.
However, I have run into an issue I did not notice before.

Here is my test:

1. Start Acad (I use Acad2004);
2. Open two blank drawings, named as "Drawing1.dwg" and "Drawing2.dwg";
3. With "Drawing1.dwg" as active document, run following code:

Public Sub SwitchDwg()

Dim App As AcadApplication
Set App = ThisDrawing.Application

Dim dwg As AcadDocument

Debug.Print App.ActiveDocument.Name

''Note: if "Drawing2.dwg" is already active, this line will cause error
App.ActiveDocument = App.Documents.Item("Drawing2.dwg")
App.Documents.Item("Drawing2.dwg").Activate

Debug.Print App.ActiveDocument.Name

Set dwg = App.ActiveDocument

Debug.Print "=====" & dwg.Name & "======="

End Sub


No matter how many times I test, it always get correct drawing name printed.

However, you will run into an error when you call

App.ActiveDocument=App.Documents.Item(DwgName)

if the document is already active. That is, you cannot get an AcadDocument
object and sign it to AcadApplication.ActiveDocument, if the AcadDocument
object IS the ActiveDocument.

So, my suspect that your code has error handling somewhere, like On Error
Resume Next... that prevent you from seeing the true cause of your problem?

In your logic, you should check if the ActiveDocument has the name as the
sSettingDwgName. If they are the same, you do not set
Documents.Item(sSettingDwgName) as ActiveDocument, because it is already
active.
0 Likes
Message 6 of 8

Anonymous
Not applicable
Sorry for a big mistake in my previous post: I really mean that I DO NOT
experience the problem as you described.

"Norman Yuan" wrote in message
news:5229134@discussion.autodesk.com...
I do experience what you described. Maybe I test drawing size is too small.
However, I have run into an issue I did not notice before.

Here is my test:

1. Start Acad (I use Acad2004);
2. Open two blank drawings, named as "Drawing1.dwg" and "Drawing2.dwg";
3. With "Drawing1.dwg" as active document, run following code:

Public Sub SwitchDwg()

Dim App As AcadApplication
Set App = ThisDrawing.Application

Dim dwg As AcadDocument

Debug.Print App.ActiveDocument.Name

''Note: if "Drawing2.dwg" is already active, this line will cause error
App.ActiveDocument = App.Documents.Item("Drawing2.dwg")
App.Documents.Item("Drawing2.dwg").Activate

Debug.Print App.ActiveDocument.Name

Set dwg = App.ActiveDocument

Debug.Print "=====" & dwg.Name & "======="

End Sub


No matter how many times I test, it always get correct drawing name printed.

However, you will run into an error when you call

App.ActiveDocument=App.Documents.Item(DwgName)

if the document is already active. That is, you cannot get an AcadDocument
object and sign it to AcadApplication.ActiveDocument, if the AcadDocument
object IS the ActiveDocument.

So, my suspect that your code has error handling somewhere, like On Error
Resume Next... that prevent you from seeing the true cause of your problem?

In your logic, you should check if the ActiveDocument has the name as the
sSettingDwgName. If they are the same, you do not set
Documents.Item(sSettingDwgName) as ActiveDocument, because it is already
active.


"MP" wrote in message
news:5228903@discussion.autodesk.com...
Hi all,
I searched hundreds of posts on google first but didn't find anything about
this
probably used wrong search term("ActiveDocument")

the line below sometimes works and sometimes doesn't
does any one have the secret to how to wait for acad to realize it's changed
it's activeDoc?

Dim oDoc as AcadDocument
Dim oAcad as AcadApplication
Dim sSettingDwgNameas String
'(sSettingDwgName is open in editor at time this sub is run)

'here's the code that "should" switch documents....
oAcad.ActiveDocument = oAcad.Documents.Item(sSettingDwgName)
' - or equaly valid
oAcad.Documents.Item(sSettingDwgName).Activate

Set oDoc = oAcad.ActiveDocument
'now theoretically oDoc.Name = sSettingDwgName....but noooooooooooooo
:-)


now here's the deal, the acad editor visually always switches immediately to
the new dwg (sSettingDwgName) as if the line setting the active document
actually worked

however, code execution using the oDoc reference sometimes (70% of the time
approx) still points to the doc from which the code originated.

Obviously i have to put a timer or something in there to wait for acad to
figure out where it is, but I haven't succeeded in getting any thing to work
even 90% of the time.

This is just for a temporary routine where I select something in one doc and
then switch to another doc to zoom to a corresponding item.

any ideas?

heres a quickie function i'm trying to use
I'm not checking if doc is open because I know it is, in an actual function
i'd put more error trapping in, just keeping it simple for this post

Function SwitchToDoc(sDwgName As String, ByRef odoc As AcadDocument) As
Boolean
Dim oAcad As AcadApplication
Set oAcad = odoc.Application
'sometimes this works, sometimes it doesn't work
'doc changes in editor but focus is still on previous document
Dim dThisTime As Double
Dim dTryTime As Double
dTryTime = 5 'seconds
Dim dStartTime As Double
Dim dElapsed As Double
'IF YOU'RE ALREADY HERE, NOTHING TO DO
If oAcad.ActiveDocument.Name = sDwgName Then
Set odoc = oAcad.ActiveDocument
Debug.Print "oDoc: " & odoc.Name
SwitchToDoc = True
Exit Function
End If


Debug.Print "Before changing doc, name is " & oAcad.ActiveDocument.Name
'either of these *should* work
'oAcad.ActiveDocument = oAcad.Documents.Item(sDwgName)
oAcad.Documents.Item(sDwgName).Activate

'IF THAT WORKED, WE'RE DONE
If oAcad.ActiveDocument.Name = sDwgName Then
Set odoc = oAcad.ActiveDocument
Debug.Print "oDoc: " & odoc.Name
SwitchToDoc = True
Exit Function
End If

'SADLY MOST OF THE TIME IT DOESN'T WORK
Debug.Print "after changing doc, name is still " & odoc.Name
If odoc.Name <> sDwgName Then
dStartTime = Timer

'to avoid infinite loop
Do While dElapsed < dTryTime
If oAcad.GetAcadState.IsQuiescent = True Then
Debug.Print "ready"
SwitchToDoc = True
Exit Do
Else
Debug.Print "Not ready, timer: " & dThisTime
End If
DoEvents
Debug.Print " oDoc: " & odoc.Name
dThisTime = Timer
dElapsed = dThisTime - dStartTime
If odoc.Name <> sDwgName Then
Debug.Print "Still not right dwg <" & dElapsed & ">"
Else
Debug.Print "Finally that changed "
SwitchToDoc = True
Exit Do
End If

If dElapsed >= dTryTime Then
Debug.Print "Time ran out for doc change"
Exit Function
End If '

Loop

Else
Debug.Print "Doc change worked first time "
SwitchToDoc = True
End If
End Function
0 Likes
Message 7 of 8

Anonymous
Not applicable
Mark, you should use Lisp and the vla-sendcommand
to load your macro.
That way, you macro is loaded when your lisp is done.
This is the way to handle MDI apps.

--
Saludos, Ing. Jorge Jimenez, SICAD S.A., Costa Rica


"MP" wrote in message
news:5229113@discussion.autodesk.com...
Hola Jorge,



"Jorge Jimenez" wrote in message
news:5229064@discussion.autodesk.com...
How are you loading your macro ??

result is same (sometimes works sometimes doesn't )
whether I run via lisp or via vbaide f5/run

(vla-runmacro (vlax-get-acad-object) macroname)
0 Likes
Message 8 of 8

Anonymous
Not applicable
Jorge,
How does the way of loading affect the fact that sometimes it works and
other times not?

The dvb, in this case is already loaded because I'm working from the ide and
have already manually loaded it, and using this and other routines both
through lisp calls and through f5/run.

in any case, what command are you suggesting I call via SendCommand?

since the lisp does nothing but run the macro I didn't think there was an
issue with using vla-runmacro? can you elaborate on why that's not the
right way in this instance?


Thanks for your replies
Mark

"Jorge Jimenez" wrote in message
news:5229240@discussion.autodesk.com...
Mark, you should use Lisp and the vla-sendcommand
to load your macro.
That way, you macro is loaded when your lisp is done.
This is the way to handle MDI apps.

--
Saludos, Ing. Jorge Jimenez, SICAD S.A., Costa Rica


"MP" wrote in message
news:5229113@discussion.autodesk.com...
Hola Jorge,



"Jorge Jimenez" wrote in message
news:5229064@discussion.autodesk.com...
How are you loading your macro ??

result is same (sometimes works sometimes doesn't )
whether I run via lisp or via vbaide f5/run

(vla-runmacro (vlax-get-acad-object) macroname)
0 Likes