Generic Routine to post arbitrary commands to the AutoCAD prompt

Generic Routine to post arbitrary commands to the AutoCAD prompt

Anonymous
Not applicable
1,616 Views
7 Replies
Message 1 of 8

Generic Routine to post arbitrary commands to the AutoCAD prompt

Anonymous
Not applicable
Im trying to port the following VB6 code to VB.NET but keep hitting the wall. Has anyone managed to port this function to VB.NET? All i manage is to send random chineese unicode characters when trying to send commands. I need a generic function that works with both AutoCAD and AutoCAD LT, which disqualifies me from using any AutoCAD ActiveX APIs... Any ideas or pointers on how to port this would be highly appreciated.

Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long

Declare Function GetForegroundWindow Lib "user32" () As Long

Public Const WM_COPYDATA = &H4A

Type COPYDATASTRUCT

dwData As Long
cbData As Long
lpData As String

End Type

Public Sub SendToCommandPrompt(strMessage As String)

Dim DataStruct As COPYDATASTRUCT

DataStruct.dwData = 1

DataStruct.lpData = strMessage

DataStruct.cbData = Len(strMessage) + 2

AppActivate ThisDrawing.Application.Caption

SendMessage GetForegroundWindow, WM_COPYDATA, 0, DataStruct

End Sub
0 Likes
1,617 Views
7 Replies
Replies (7)
Message 2 of 8

jbooth
Advocate
Advocate
I have a few questions.

1. Are you using COM interop or the managed objectARX?
2. Do you not have a reference to the AutoCAD application?

If you are using COM, look for the SendCommand method of the Document object.

ex: AcadApplication.ActiveDocument.Sendcommand("zoom extents ")

If you are using managedarx, you could look at the Autodesk.Autocad.ApplicationServices.Application.DocumentManager.MdiActiveDocument.SendStringToExecute method OR look into importing acedCmd into your project:

Declare Auto Function acedCmd Lib "acad.exe" Alias "acedCmd" (ByVal resbuf As IntPtr) As Integer

Search for acedCmd in this forums to find a good example of how to use it (thanks to Tony).

Hope this helps.
0 Likes
Message 3 of 8

Anonymous
Not applicable
_
Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function

The "SendMessageA" function for ANSI, not Unicode.

You have to use the Unicode version for AutoCAD 2007 or later, which the above DllImport declaration will do.


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:[email protected]...
Im trying to port the following VB6 code to VB.NET but keep hitting the wall. Has anyone managed to port this function to VB.NET? All i manage is to send random chineese unicode characters when trying to send commands. I need a generic function that works with both AutoCAD and AutoCAD LT, which disqualifies me from using any AutoCAD ActiveX APIs... Any ideas or pointers on how to port this would be highly appreciated.

Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long

Declare Function GetForegroundWindow Lib "user32" () As Long

Public Const WM_COPYDATA = &H4A

Type COPYDATASTRUCT

dwData As Long
cbData As Long
lpData As String

End Type

Public Sub SendToCommandPrompt(strMessage As String)

Dim DataStruct As COPYDATASTRUCT

DataStruct.dwData = 1

DataStruct.lpData = strMessage

DataStruct.cbData = Len(strMessage) + 2

AppActivate ThisDrawing.Application.Caption

SendMessage GetForegroundWindow, WM_COPYDATA, 0, DataStruct

End Sub
0 Likes
Message 4 of 8

Anonymous
Not applicable
I hope Dave's not watching.

>>I need a generic function that works with both AutoCAD and AutoCAD LT, which disqualifies me from using any AutoCAD ActiveX APIs
0 Likes
Message 5 of 8

jbooth
Advocate
Advocate
LOL

Sorry, I just skimmed through the summary and went straight to the code, my bad.

Thanks for pointing that out, Nathan.
0 Likes
Message 6 of 8

Anonymous
Not applicable
Ive experimented with SendMessageW but it provides same type of problems... 😞
0 Likes
Message 7 of 8

Anonymous
Not applicable
Closest i get to a somewhat working code is the snippet below. That routine allows me to send at least one character, sometimes to autocad and makes it turn out to what i send it as... But its still far from working. Anyone got more pointers on how to get this to work is highly appreciated.

Declare Function SendMessage Lib "user32" Alias "SendMessageW" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
ByRef lParam As COPYDATASTRUCT) As Long

Structure COPYDATASTRUCT
Dim dwData As Long
Dim cbData As Long
Dim lpData As String
End Structure

Public Sub SendToCommandPrompt(ByVal strMessage As String)
Dim DataStruct As COPYDATASTRUCT
DataStruct.dwData = 1
DataStruct.lpData = strMessage
DataStruct.cbData = Marshal.SizeOf(DataStruct)

Dim ByteArray() As Byte
Dim Ptr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(DataStruct))
ReDim ByteArray(Marshal.SizeOf(DataStruct) + 1)
'now copy strcutre to Ptr pointer

Marshal.StructureToPtr(DataStruct, Ptr, False)
Marshal.Copy(Ptr, ByteArray, 0, Marshal.SizeOf(DataStruct))
'now use ByteArray ready for use
Marshal.FreeHGlobal(Ptr)

SendMessage(hwnd_autocad, WM_COPYDATA, 0, DataStruct)

End Sub
0 Likes
Message 8 of 8

Anonymous
Not applicable
Alright, function ported... finally... Posting the finished result:

Private Declare Auto Function SendMessage Lib "user32" _
(ByVal hWnd As IntPtr, _
ByVal Msg As Integer, _
ByVal wParam As IntPtr, _
ByRef lParam As COPYDATASTRUCT) As Boolean

Public Const WM_COPYDATA As Integer = &H4A


_
Structure COPYDATASTRUCT
Dim dwData As Long
Dim cbData As Long
Dim lpData As IntPtr
End Structure

Public Sub SendToCommandPrompt(ByVal strMessage As String)

Dim DataStruct As New COPYDATASTRUCT
strMessage = strMessage & Chr(0) 'Null terminated

DataStruct.dwData = 1
DataStruct.cbData = strMessage.Length * Marshal.SystemDefaultCharSize
DataStruct.lpData = Marshal.StringToCoTaskMemAuto(strMessage)

SendMessage(FindWindowWild("*AutoCAD*"), WM_COPYDATA, 0, DataStruct)
Marshal.FreeCoTaskMem(DataStruct.lpData)

End Sub
0 Likes