interesting macro problem

Discussion in 'AutoCAD' started by Mandy, Jan 31, 2005.

  1. Mandy

    Mandy Guest

    I have a macro that I want to run on startup of Autodesk Map, which will loop through all DWGs in a directory extracting object data. I got this to work by putting these lines in Acad.lsp:

    (vl-load-com)
    (vl-vbarun "C:\\MyPath\\WriteODData.dvb!ProcessODMain")

    However, I need to essentially pass a couple parameters into my macro, so from another post I found a way to do this by using the lisp "command" function and the VBA "GetString" function, so now my acad.lsp looks like this:

    (vl-load-com)
    (command "-vbarun" "C:\\MyPath\\WriteODData.dvb!ProcessODMain" "Param1" "Param2")

    and in my macro I use GetString to get the values of Param1 and Param2.

    Everything was working just fine. I had run this on a directory of 100 DWGs with no problem. But then I started running on additional data to test, and from time to time would stop on a file with "Macro Name:" in the command line, and it seemed to be waiting for user input.

    So if anyone has a solution at this point, or can explain what is happening, I would be very interested.

    Not exactly sure what the problem is, but in an attempt to work around this, or just as a test, I tried going back to the "vl-vbarun" command, instead of "vbarun". This seems to work, I do not get the problem where my program stops and is waiting for input. However, using "vl-vbarun" I do not know how to get my parameters passed in. I don't seem to be able to do it the same way I was able to with vbarun. You must not be able to use "vl-vbarun" with "command". So I'm in a bit of a Catch-22.

    Any ideas would be greatly appreciated.
     
    Mandy, Jan 31, 2005
    #1
  2. Will this work.

    (vl-load-com)
    (vl-vbarun "C:\\MyPath\\WriteODData.dvb!ProcessODMain")
    (command "Param1" "Param2")

    Regards - Nathan
     
    Nathan Taylor, Jan 31, 2005
    #2
  3. Mandy

    Mandy Guest

    Clever idea, but it doesn't work ("too many arguments" for the vl-vbarun function, I imagine)

    In addition, I ran on another large chunk of data overnight using vl-vbarun, and came in this morning to find it sitting on a file with the "Macro Name:" prompt in the command line, so I guess vl-vbarun won't fix the problem for me anyway.

    Anyone know what's going on here? And how I can avoid it?
     
    Mandy, Feb 1, 2005
    #3
  4. Mandy

    Mandy Guest

    Here's a little more info on this. I think it must be some kind of a timing issue.

    When I'm running through a directory and hit a file where it stops and gives me the "Macro Name:" prompt, if I pull that one file out and run on it alone, there's no problem. I thought maybe it was caused by the file that was processed prior to it, so threw that one in too, and still no problem. By going back a couple more files, I was able to create a small "test" directory that seems to consistently demonstrate the problem. However, if I put some breakpoints in and step through the code, everything works fine.

    So I tend to think it is some sort of timing issue, but still don't really know what the problem is or how to attempt to remedy it.

    Can anyone offer any suggestion?
     
    Mandy, Feb 1, 2005
    #4
  5. Does your VBA use SendCommand statements? They could throw off the timing
    of the macro, especially if the command being sent is slow (such as working
    with a large file). One hack solution may be to intentionally slow down
    your macro, especially after a problem SendCommand line. For example:

    SendCommand "......"
    Dim wait As Double, start As Double
    wait = 2: start = Timer 'for 2 second delay
    Do: DoEvents: Loop Until Timer >= start + wait
    ' continue on with your program

    It would be better to redesign the VBA to get rid of the SendCommand, if
    possible (assuming this is even your problem).

    HTH,
    James

    gives me the "Macro Name:" prompt, if I pull that one file out and run on it
    alone, there's no problem. I thought maybe it was caused by the file that
    was processed prior to it, so threw that one in too, and still no problem.
    By going back a couple more files, I was able to create a small "test"
    directory that seems to consistently demonstrate the problem. However, if I
    put some breakpoints in and step through the code, everything works fine.
    know what the problem is or how to attempt to remedy it.
     
    James Belshan, Feb 1, 2005
    #5
  6. Mandy

    Mandy Guest

    Thaks for the idea.

    I'm not using SendCommand. And I already tried using a timer to create a delay before opening each file, but that didn't make any difference.

    So I'm a little baffled. Is there some other cleaning up I should be doing or anything? I'm new to all of this, and have gotten as far as I am only due to this most excellent forum! But it also means there may be some really basic things I'm overlooking.

    It does seem like it could be related to larger files. The first few large chunks of data I was processing I didn't see this problem with. But then I started testing with some directories that do have many large files, and that's when I started seeing these sporadic problems.
     
    Mandy, Feb 1, 2005
    #6
  7. hi mandy,

    i just searched through my applications, but didn't find the solution, seems that i was not commenting the repair (hope my boss doesn't read that),

    i had the same problem, even not in acad.lsp but in xxx.mnl, from which i start some autoloading-functions and as far as i remember i had the same
    bug (feature?) when i used extra-lisp-lines including (princ) or (print), i don't know at the moment, if i had to add or to remove the extra-lines,
    but maybe it helps you playing with that.


    another idea would be to use 'vbastmt' instead of 'vbarun' to apply parameters to a vb-sub, for example
    _vbastmt;HMAP.HMapExecute("HMapDelPoints");

    hope any of the ideas will help,

    - alfred -
     
    Alfred NESWADBA, Feb 1, 2005
    #7
  8. Mandy

    Mandy Guest

    I was starting to suspect that this may only be happening when run from acad.lsp, and not when I run directly from the command line. I just did a couple tests, and that does appear to be the case. Why would that be?

    I could understand if I was running the command line on each drawing, but even when run manually from the command line, I'm just putting in one call to my macro, and this macro loops through all files in a directory, opening each file and extracting certain data. So I could see the initially loading could be different, but once it gets going, and is looping through the files, opening and closing them, what's the difference if it originated from the command line or from acad.lsp?

    I was also watching the Map command line as it loops through the files, and normally each time it opens a file, you see a bunch of stuff fly by, the last few things are:

    Regenerating model.
    AutoCAD menu utilities loaded _-vbarun
    Command:

    and then it sits at Command: for a bit before it throws up my Macro name and takes off running.

    On the files where it gets "stuck", after regenerating model, the next thing that comes up is the Macro Name: prompt. I think that's the problem, that it's trying to run the macro before 'Command:' has come up.

    I tried putting the running of my macro in a S::STARTUP function inside acad.lsp, as this is not supposed to run until the drawing is completely initialized. But this didn't fix it either.

    Any other ideas?
     
    Mandy, Feb 2, 2005
    #8
  9. Do you mind posting the macro you are using?

    Regards - Nathan
     
    Nathan Taylor, Feb 2, 2005
    #9
  10. Mandy

    Matt S Guest

    I have used similar syntax as shown in the following to get stuff to work from the command line

    (command "_-vbarun" "C:\\Folder\\Macro.dvb!Macro" )

    However, when calling this line from a lisp routine or menu item, I had to make it look like this.

    (command "_-vbarun" "C:/Folder/Macro.dvb!Macro)

    I believe the reason for this is the \ charachter has other meanings when called from a lisp routine or menu item.

    This may or may not be your problem.
     
    Matt S, Feb 3, 2005
    #10
  11. hi mandy,

    have you tried -vbastmt instead of -vbarun?

    one more idea could be, not to load (vl-load-com) at every drawing, i think to initialize the vb-interface one call for one acad-process should be
    enough,to check if it is loaded, i use

    (if (car (atoms-family 1 '("vl-load-com"))) (vl-load-com))

    - alfred -
     
    Alfred NESWADBA, Feb 4, 2005
    #11
  12. Mandy

    Mandy Guest

    Alfred, thanks for the suggestions.

    I couldn't get it to run using VBASTMT. This seems to be for running a VB statement and maybe not a macro. (?)

    I also tried

    (if (car (atoms-family 1 '("vl-load-com"))) (vl-load-com))

    and it didn't make any difference.

    Thanks though. Those were good suggestions.
     
    Mandy, Feb 7, 2005
    #12
  13. mandy,

    don't know why vbastmt will not work, because it starts a vba-sub including the posibility of parameters. the difference to your version could be,
    that i always load the dvb-file and then call the sub's, you start it with the fill-filename.

    other ideas to your case:

    *) i had also map as basic, mybe it helps to turn off the map-workspace before
    *) a good idea (because also faster in execution for batch-scripts) is to turn off the properties-window
    *) a ugly way, but could help for parameter: if you cannot run with vbastmt, then put your parameter into user-variables and then run with lisp
    vl-vbarun (and within that macro read back the user-variables)

    - alfred -
     
    Alfred NESWADBA, Feb 7, 2005
    #13
  14. I got vbastmt to work but still experienceing the exact problem as vbarun.
    Regards - Nathan
     
    Nathan Taylor, Feb 7, 2005
    #14
  15. Mandy

    Mandy Guest

    Okay, I received a workaround from Autodesk, so I'll post it here in case anyone else runs into this problem.

    The cause of the problem, as we somewhat suspected, is that AutoCAD has not finished intializing at the point in the S::STARTUP function where the command -VBARUN is called, and therefore, Acad.lsp is reloading when loading the next drawing even though the ACADLSPASDOC is set to 0.

    We worked around the problem by making these code changes:

    1. Put ProcessODMain in a module other than ThisDrawing. This is because we need to pass parameters to it. In my test, I put it in Module1 and changed its code to the following. You may need to put other functions such as FindFiles to Module1 as well. Or you can make them Public, so that you can call them from Module1.

    Sub ProcessODMain(ByVal Param1 As String, ByVal Param2 As String)
    AcadApplication.Visible = True
    Set acad = GetObject(, "AutoCAD.Application")
    Set AcadMap = acad.GetInterfaceObject("AutoCADMap.Application")

    'get command line parameters
    'Param1 = ThisDrawing.Utility.GetString(True) 'Dataprep Dir
    'Param2 = ThisDrawing.Utility.GetString(True) 'Territory Input Dir

    DPDir = Param1
    DwgDir = Param2

    'Loop through the directory, extracting from each dwg file

    FindFiles DwgDir, "*.dwg"

    acad.Quit
    Set acad = Nothing

    End Sub

    2. Use vbastmt instead of VBARUN. Change the acad.lsp file to the followgs.

    (defun-q mystartup ( )
    (vl-load-com)
    (command "._vbaload" "C:\\Test\\Test.dvb")
    (vla-sendcommand
    (vla-get-activedocument (vlax-get-acad-object))
    "(command \"vbastmt\" \"Module1.ProcessODMain \\\"C:\\\\test\\\", \\\"C:\\\\test\\\"\")\n"
    )
    )
    (setq s::startup (append s::startup mystartup))

    Works great, although he didn't explain why this works and my original (ignorant) way didn't, so if anyone cares to provide any further explanation, that would also be fine. I'm a little curious about the difference between vbastmt and vbarun, and the difference between the ThisDrawing module and other modules.

    Thanks much!
     
    Mandy, Feb 21, 2005
    #15
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.