PDA

View Full Version : Looping through Autocad Sessions/Instances



TheTmd
01-20-2018, 02:18 PM
Hi all,
I'm looking for a solution to this problem: I'd like to send a drawing from one session of autocad to another. I've tried multiple search in internet and other forums, but most of the help/advise/suggestions/links found refer to Excel (or Office applications in general) (at the moment I've posted the same question in StackOverflow)

When I work in Autocad, it happens that I open 2 sessions and put one window on each monitor of my desktop PC. I do this because the drawings i use are mostly P&ID and when I've to follow a line it's better to have the next drawing just opened in the other session (hope it's clear)

Now, during the analysis i open multiple drawings, in one session or in the other, and happens that i need to just close a dwg in one session and open it in the other: I'd like to automate this operation by doing
- close the dwg in the active session (the one where i push the button, let's say the one in the right monitor)
- open the same dwg in the other session (let's say the session in the left monitor) (hope it's clear)

I've understand that to do so I need to 'point' to the other Autocad Application, but what I was thinking was a simple task is not so simple. All i can get is the Handle of the other application, but can't point to the Object of that application (to send the command to open the early closed drawing)


The code that I've been able to write (by taking the above mentioned advise) is the following




Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type


Public Function GetAcadInstances() As Collection
Dim acc As Object
Dim hwnd
Dim t As GUID




Set GetAcadInstances = New Collection
Do
hwnd = FindWindowExA(0, hwnd, "AfxMDIFrame110u", vbNullString)

'I print processID just to check if it corresponds with the one indicated in TaskManager, and so to be sure to have got the right handle
GetWindowThreadProcessId hwnd, hProcWindow
Debug.Print hProcWindow

If hwnd = 0 Then Exit Do
Call AccessibleObjectFromWindow(hwnd, OBJID_NATIVEOM, t, acc)

'I print the value of AccessibleObjectFromWindow to understand what this function is returning
Debug.Print AccessibleObjectFromWindow(hwnd, OBJID_NATIVEOM, t, acc)

If Not acc Is Nothing Then
Set AutocadApplicazione = acc.Application
End If
Loop
End Function





With this code I've understand that
- FindWindowExA is able to loop through running applications and in this way i get the handle of Autocad Windows
- AccessibleObjectFromWindow is always returning the value -2147467262 (that I think it's an error value, but haven't found much about it)
- maybe Autocad Applications doesn't have any "OBJID_NATIVEOM", and so maybe it's necessary another function to get the application object
- acc, that is the variable that should point to the object with the HWND, is always = nothing


I think that AccessibleObjectFromWindow should work not only for Office Applications, but not sure

Thanks