See the top rated post in this thread. Click here

Results 1 to 5 of 5

Thread: Get data from command line

  1. #1
    Member
    Join Date
    2020-07
    Posts
    14
    Login to Give a bone
    0

    Default Get data from command line

    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
    Last edited by estuyose791044; 2020-08-22 at 08:47 AM.

  2. #2
    Administrator Ed Jobe's Avatar
    Join Date
    2000-11
    Location
    Turlock, CA
    Posts
    6,525
    Login to Give a bone
    0

    Default Re: Get data from command line

    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.
    C:> ED WORKING....


    LinkedIn

  3. #3
    Login to Give a bone
    1

    Default Re: Get data from command line

    Quote Originally Posted by estuyose791044 View Post
    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
    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:
    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
    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.

    Please adjust the path to your AutoCAD executable and the delay times as needed to fit your specific setup and requirements.
    Last edited by Ed Jobe; 2023-09-28 at 02:24 PM. Reason: Added code tags [CODE] [/CODE]

  4. #4
    Administrator Ed Jobe's Avatar
    Join Date
    2000-11
    Location
    Turlock, CA
    Posts
    6,525
    Login to Give a bone
    0

    Default Re: Get data from command line

    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
    C:> ED WORKING....


    LinkedIn

  5. #5
    Administrator Ed Jobe's Avatar
    Join Date
    2000-11
    Location
    Turlock, CA
    Posts
    6,525
    Login to Give a bone
    0

    Default Re: Get data from command line

    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.
    C:> ED WORKING....


    LinkedIn

Similar Threads

  1. Replies: 4
    Last Post: 2014-01-01, 07:58 PM
  2. Replies: 0
    Last Post: 2009-04-15, 02:42 PM
  3. Replies: 5
    Last Post: 2006-11-09, 07:19 PM
  4. Command Line Or No Command Line?
    By H-Angus in forum ACA General
    Replies: 8
    Last Post: 2006-03-01, 12:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •