Hello Everyone
I want to get the texts that come after I activate the command "id". That says "Specify point: x=#### y=####"
how do I do it?
Here is the code.
Thanks
Public Sub tryer()
ThisDrawing.SendCommand "id" & vbCr
End Sub
Printable View
Hello Everyone
I want to get the texts that come after I activate the command "id". That says "Specify point: x=#### y=####"
how do I do it?
Here is the code.
Thanks
Public Sub tryer()
ThisDrawing.SendCommand "id" & vbCr
End Sub
You can't because the SendCommand method is asynchronous. You need to define your own ID command, which is pretty simple. Use the GetPoint utility and examine the x and y values of the returned point. There is sample here.
To do this, you can use AutoCAD's COM API to interact with the command line and capture the text that is displayed there. Here's an example of how you can modify your code to achieve this:
This code first checks if AutoCAD is running, and if not, it starts it. It then sends the "id" command and waits for a moment to allow AutoCAD to process it. After that, it captures the text from the command line using the GetVariable method and displays it in a message box.Code:Public Sub tryer()
Dim acadApp As Object
Dim acadDoc As Object
Dim cmdResult As String
' Get a reference to the AutoCAD application
On Error Resume Next
Set acadApp = GetObject(, "AutoCAD.Application")
On Error GoTo 0
' If AutoCAD is not running, start it
If acadApp Is Nothing Then
Shell "C:\Program Files\Autodesk\AutoCAD 2022\acad.exe", vbNormalFocus
' You may need to adjust the path to your AutoCAD executable
' and specify the correct AutoCAD version (e.g., AutoCAD 2022).
' Wait for AutoCAD to start (adjust the delay as needed)
Application.Wait (Now + TimeValue("00:00:05")) ' Wait for 5 seconds
Set acadApp = GetObject(, "AutoCAD.Application")
End If
' Get the active document
Set acadDoc = acadApp.ActiveDocument
' Send the "id" command
acadDoc.SendCommand "id" & vbCr
' Wait for a moment to allow AutoCAD to process the command
Application.Wait (Now + TimeValue("00:00:01")) ' Wait for 1 second
' Capture the text from the command line
cmdResult = acadApp.GetVariable("CmdLine")
' Display the result (you can modify this part as needed)
MsgBox "Command Line Result: " & cmdResult
' Release objects
Set acadDoc = Nothing
Set acadApp = Nothing
End Sub
Please adjust the path to your AutoCAD executable and the delay times as needed to fit your specific setup and requirements.
Hi Freida, thanks for the code sample. If you surround it with code tags, it will not lose the formatting. To do that, click on the Go Advanced button and then select your code and click on the hashtag # button.
I don't recommend getting the acad app within the current sub. It's best programming practice to modularize tasks in separate subs. The advantage is in debugging and code reuse. Once you debug a sub, you don't have to do it again. If you keep retyping the same code in multiple subs, then you have to debug it each time and introduce the chance for a typo. Below is a sub that returns an AcadApplication object. Note that you don't use shell, but GetObject and then CreateObject.
Code:Public Function GetAcad(Optional ver As String) As AcadApplication
' support multiple acad versions.
'Sample ver for AutoCAD 2023 ' ".24.2"
On Error Resume Next
Dim acApp As AcadApplication
Dim clsid As String
clsid = "AutoCAD.Application"
If Not ver = "" Then
clsid = clsid & ver
End If
Set acApp = GetObject(, clsid)
If acApp Is Nothing Then
Set acApp = CreateObject(clsid)
End If
Set GetAcad = acApp
End Function
Hi estuyose,
Sorry I didn't look at your code closer. I just answered you direct question. I should have suggested that you use the ThisDrawing.Utility.GetPoint method instead.