VBA - how can I see what the VBA is sending to BricsCAD on the command prompt?

In my VBA application, I am using the .sendcommand function to copy script commands from an Excel spreadsheet, into BricsCAD. However, I am having difficulty with the script itself. What makes it particularly difficult is that BricsCAD does not show the commands and values sent to it from VBA.

One method might be to proceed each command that I send, with a text message on the command line that simply repeats the command I am sending. I recall coming across one VBA example that did just that as a demonstration. But, after well over an hour of effort, I have been unable to find that site again. And my efforts to find it in the BricsCAD VBA reference have also failed. I seem to recall it being a print to the command line, but again that is a vague memory.

Is there any way to get BricsCAD to show on the command prompt what I am sending to it?

-Joe

Comments

  • Joe Dunfee
    edited February 2017

    Well, it seems that after a few hours of effort, the magic thing that causes the solution to appear, is the very act of posting the question to the forum. I just came across one of the answers to my questions. My effort to post the code into that older thread was mangled, so that may have made it hard to find. The command to print to the command prompts is

    ACAD.ActiveDocument.Utility.Prompt "Hello from Excel!" 'Print a message to the AutoCAD® command line

    Which is from this blog,
    http://howtoautocad.com/excel-and-autocad-–-a-match-made-in-heaven-again/

    Thank you Joe for answering Joe's question.

  • The use of the utility.prompt method is not a working idea. You can't use it in the middle of a command.

    -Joe

  • pop it up in excel if you are debugging your string.

    here is a simple one,

    Sub erase_all()
    Dim acadstr As String
    acadstr = "_erase" & vbCr & "ALL" & vbCr & vbCr

    msgbox acadstr

    acadDoc.SendCommand acadstr

    End Sub

  • Thanks for the effort, but the goal is to see when in the command process that things go wrong. For example, if you omit a step, the next command in the script would be incorrect. So, it is good to see BricsCAD's response along with each entry from the VBA.

    I wonder if there is a way to read the command prompt. In my case, I have coded that each command gets sent individually. So, I might send one command, and also send that command to a text box. Then, I would read what is on the command line, and append that to the text box. In essence I am creating my own version of a command line text screen.

    -Joe

  • there is also

    debug.print acadstr

    which prints to the immediate window in excel vba editor

    use as many as you like anywhere

    then on the bricscad side, look at menuecho, cmdecho ( I always get those mixed up) and writing everything to a log file (that's an option in autocad not sure about bricscad)

  • The menuecho doesn't show anything coming from VBA. The cmdecho was already set to on. Reading the manual about cmdecho, only seem to affect LISP commands.

    But, thank you very much for the suggestions.

    -joe

  • The log file only shows what the command line shows. So, no luck there either.

    -Joe

  • You could send your command output to file instead of direct to BricsCAD, then load the file as a script to run it. Another option would be to send it to the clipboard, then paste it into BricsCAD on the command line.

    Regards,
    Jason Bourhill
    CAD Concepts

  • I did find another possible solution. If the SendKeys method is used in VBA to send my script to AutoCAD, then it really is the same as sending keystrokes from the keyboard, and everything is shown. However, in my case, my VBA program itself sometimes gets focus. So, the result is that my keystrokes are sent to the VBA program itself... thus messing up my program! I don't know why sometimes it does this, and sometimes does not.

    Perhaps the clipboard method is the best. I just did some reading about using it, and it really seems very simple.

  • Joe Dunfee
    edited June 2017

    My efforts so far, to use the copy/paste method has failed. I can get the script into my clipboard, and then physically go to BricsCAD and past the text into the command prompt, and it will function. Also, it will show all the text that I enter, which makes it MUCH easier to debug a script.

    But, when I try to do it using my program, it fails. My VBA is successfully talking to BricsCAD, because it is opening a template drawing that I plan to use. But, for some reason it won't send the clipboard. I have tried a few approaches. Here is a simplified version of my program. It just puts a very brief series of commands into the clipboard, and then should paste them into BricsCAD's command prompt.

    =======start of code==========

    'Variables
    Public acadApp As AcadApplication
    Public acadDoc As AcadDocument
    
    Sub SendScript()
    
    '**** Declare Variables ****
    Dim ScriptText As String
    
    'Variables related to the clipboard
    Dim MyClipboard As MSForms.DataObject
    Set MyClipboard = New MSForms.DataObject
    MyClipboard.SetText "" 'put empty string in text, to prepare to clear clipboard
    MyClipboard.PutInClipboard 'I am not sure why this exists, but seems to be necessary
    
    '**** Create the script and send to Clipboard  ****
    ScriptText = "text" & Chr(27) & "0" & Chr(27) & "0" & Chr(27) & "Test"
    'Put it into the clipboard
    MyClipboard.SetText ScriptText 'step one, use settext
    MyClipboard.PutInClipboard     'step two, use  putinclipboard
    
    
    'Make sure BricsCAD has focus
    acadApp.Visible = True
    acadApp.ActiveDocument.Activate
    Set acadDoc = acadApp.ActiveDocument
    
    'in case BricsCAD is not ready yet, pause a second.
    Sleep 1000
    'MsgBox ("Click OK when BricsCAD is ready")
    
    'Send the contents of the clipboard to BricsCAD.
    ' this version wasn't working ->    SendKeys "^v" & Chr(27), True
    acadDoc.SendCommand ("_pasteclip" & Chr(27))  '-but this doesn't work either
    
    End Sub
    

    =======end of code==========

    P.s, the support staff at Bricsys said that Markdown views the ' as a code indicator. VBA uses it to mark comments. So, to avoid issues with this, you can indent your code by at least 4 characters. Then, the ' will not be read by Markdown. [I just looked at my preview, and found that it still did not properly interpret the first few lines, even though they were indented. So the problem remains]
    [Edit, update on code formatting. Bricsys support added that if you use the method of "indent by at least 4 spaces", then you don't use the ' as a symbol to start code formatting]

    -Joe

  • @Joe Dunfee:
    To 'echo' the commands you send to BricsCAD this may work (untested):
    acadDoc.SendCommand ("(progn (princ \"" & commandString & "\") (princ))" & Chr(27)) 'No action just an echo. acadDoc.SendCommand (commandString & Chr(27))

  • Joe Dunfee
    edited June 2017

    Thank you for the reply.

    If I understand the program lines you show, it will use some lisp to put some text on the command line. The command itself, should be stored inside the commandString variable. However, this variable is not visible to BricsCAD.

     acadDoc.SendCommand ("(progn (princ ~~\""~~ & commandString & "\") (princ))" & Chr(27)) 'No action just an echo.
    

    In the above line, the variable commandString is not be recognized as a variable. I tried to modify it by changing the /"" to "/" without any change in behavior.

    I then tried to build up the part that seems to be the problem into a string, and then use that string in the acadDoc.SendCommand line.

            CommandString = Cells(CurrentRow, CountX).Value & Chr(27) 'put command into string, so I can use it inside embedded LISP
            CommandStringLISP = "(progn (princ \ " & CommandString & " \ ) (princ))" 'add the LISP part
            acadDoc.SendCommand (CommandStringLISP) 'No action, just showing it on the command line.
            acadDoc.SendCommand (CommandString & Chr(27)) 'Actually implement the command
    

    However, the above only generated "(progn (princ \ -insert \ ) (princ))" on the command line. Note that the -insert is my command.

    -Joe
    p.s. The above was successfully converted to code format, by making shure each line was indented by at least 4 space. But, I wonder why my code above gets its apparently random coloring? I would think that the 1st two lines would get the same color scheme. I am guessing that the ' mark toggles the color in and out of the green color.

  • Roy Klein Gebbinck
    edited June 2017

    There are several syntax issues with both of our suggestions. You should look at your use of Chr(27). This is Escape and not Enter (as I wrongly assumed when reading your code).

    So I do not think this can work:
    acadDoc.SendCommand ("_pasteclip" & Chr(27))
    But this should:
    acadDoc.SendCommand ("_pasteclip ") ' Using space as Enter.

    Why are you using double tildes? Are you perhaps mixing up SendCommand and SendKeys?

    After some testing and fixing 3 syntax issues (Properly 'escaping' double quotes; Using " " (= space) instead of Chr(27); Adding " " to the end of the Lisp expression string) I find that my suggestion can work. The VBA code below was tested inside BricsCAD. But the principle should also work in Excel.

    Sub SendCommandWithEcho(doc As Object, str As String)
    
        doc.SendCommand ("(progn (princ "">>> SENDING: \""" & str & "\"""") (princ)) ") ' No action just an echo.
        doc.SendCommand (str)
    
    End Sub
    
    Sub test()
    
        Dim doc As Object
        Set doc = ThisDrawing
        Call SendCommandWithEcho(doc, "_circle 0,0 100 ")
    
    End Sub
    
  • About the strange syntax highlighting:
    It seems the Forum software doesn't properly recognize VBA and Lisp code.

  • Thank you very much for catching the Chr(27) problem, where I should have been using Chr(13) which equals the enter key. I had introduced that problem a while back, and caught it. But, when I ran into other issues, I tried going back to an older version that I though was working. It turned out I had dug up one with the Chr(27) error.

    With that working, the idea of using the clipboard is back in operation. Your LISP approach is fine, except that I don't know LISP, and working with commands that I don't understand tend to get me into trouble.

    So correcting my earlier code to remove the Chr(27) problem gives me working code.

    I am still working out all the details and trying to make it more user friendly. I had taken out a lot of the niceties and error trapping, as I started to run into trouble, to make debugging it easier.

    -Joe

  • @Joe Dunfee
    You do not have to know any Lisp to use my suggestion. The SendCommandWithEcho Sub should work in Excel without any modifications.

This discussion has been closed.