Function or Global Variable for ThisDrawing

Discussion in 'AutoCAD' started by Gordon Price, Oct 8, 2004.

  1. Gordon Price

    Gordon Price Guest

    I am trying to take my lisp programming up a notch (ba BAM! ;), and I am
    looking for some input on some techneques.

    Currently I have a function defined for ThisDrawing access, which looks like
    this
    (defun SM:ThisDwg (/) (vla-get-activeDocument (vlax-get-acad-object)))

    Whenever I need to reference ThisDrawing I do it by calling the function.
    However, it occurs to me that a better method might be to define a global
    variable and set it using s::startup. This way, I only actually access the
    object the once, and then access the value in the variable from then on.
    However, I have some questions about this approach...
    1: does each drawing get its own variable space, such that a global variable
    would work with multiple drawings open, with the same named variable in each
    drawing pointing to a different ThisDrawing?
    2: this approach requires spreading my code into s::startup, while the
    function call leaves all my code more self contained. Is the performance hit
    for using the function call so small as to make it not an issue, and keeping
    things together is better?
    3: If the global variable works for ThisDrawing, can you also create a
    global variable available from every drawing that is set in Acaddoc.lsp to
    the Acad object?

    Lastly, I have seen suggestions to put (vl-load-com) in every routine that
    needs access. If I know that much of my code needs it, could I not just put
    one (vl-load-com) in Acaddoc.lsp and be assured that every VL routine would
    be fine?

    Thanks for the help,
    Gordon
     
    Gordon Price, Oct 8, 2004
    #1
  2. Gordon Price

    Tom Smith Guest

    Lastly, I have seen suggestions to put (vl-load-com) in every routine that
    Gordon, I'll leave the fancy stuff for the braniacs -- you're over my head,
    and I'm just curious as to where you're heading with all this.

    But on the (vl-load-com), I run it only once in my acad.lsp and don't need
    to run it in each document or application. If I were doing a standalone lisp
    that needed to run outside the context of my customizations, then I would
    include it in that lisp.
     
    Tom Smith, Oct 8, 2004
    #2
  3. (defun ThisDrawing ()
    (defun ThisDrawing ()
    *ThisDrawing*
    )
    (setq *ThisDrawing* (vla-get-ActiveDocument (vlax-get-acad-object)))
    )
     
    Tony Tanzillo, Oct 8, 2004
    #3
  4. Gordon Price

    Gordon Price Guest

    OK Tony, you lost me there. Why the two defuns? And is this a function that
    is only run once? If so, could it be outside a defun, and inside s::startup
    so as to run each time a drawing is opened or created?

    Best,
    Gordon
     
    Gordon Price, Oct 8, 2004
    #4
  5. The intent may be obscured there. It is similar to this, without the
    conditional test:

    (defun ThisDrawing ()
    (cond (*ThisDrawing*)
    ((setq *ThisDrawing* (vla-Get-ActiveDocument
    (vlax-Get-Acad-Object))))))


    --
    R. Robert Bell



    OK Tony, you lost me there. Why the two defuns? And is this a function that
    is only run once? If so, could it be outside a defun, and inside s::startup
    so as to run each time a drawing is opened or created?

    Best,
    Gordon
     
    R. Robert Bell, Oct 8, 2004
    #5
  6. Gordon Price

    Gordon Price Guest

    Ah, so you use a function, and the function only actually goes to the
    ThisDrawing object if the global variable doesn't yet exist, otherwise it
    returns the value of the variable. If I remember right a VL function returns
    the value of the last SETQ, so you don't need to do the VBA version of
    FunctionName = GlobalVariable to end the funtion, correct?

    By the way, Robert, what happened to that web site you used to be involved
    with? I remember downloading your AU/AUGI error trap class some time ago,
    along with seeing all sorts of great code examples. Now I can't find the
    site.

    Best,
    Gordon
     
    Gordon Price, Oct 8, 2004
    #6
  7. Gordon Price

    Doug Broad Guest

    Gordon,
    The first time the function is called it
    1) redefines itself so that the next time it is called,
    it will just return the global value.
    2) sets up the global.

    After the first call, the function is reduced to
    (defun ThisDrawing () *ThisDrawing*)
     
    Doug Broad, Oct 8, 2004
    #7
  8. In contradiction to what Robert suggests, the intent is clear
    provided you understand the concept, mainly how a function
    can redefine itself the first time it is called, so that it does
    something differently in subsequent calls.
     
    Tony Tanzillo, Oct 9, 2004
    #8
  9. Gordon Price

    James Allen Guest

    Like "autoload". ;)
     
    James Allen, Oct 11, 2004
    #9
  10. Gordon Price

    Gordon Price Guest

    Thanks everyone. I knew I was going to learn something unexpected. I didn't
    realize you could redefine a defun. Cool.

    Best,
    Gordon
     
    Gordon Price, Oct 11, 2004
    #10
  11. Gordon Price

    Tom Smith Guest

    I didn't realize you could redefine a defun. Cool.

    Sure, like James said, that's how autoload works, it creates a defun on the
    fly that redefines itself first time it runs. Diabolically clever.
     
    Tom Smith, Oct 11, 2004
    #11
  12. We took the site down for a while, and haven't had the time to get a new
    host to get it back up. Some day... ;^)

    --
    R. Robert Bell



    Ah, so you use a function, and the function only actually goes to the
    ThisDrawing object if the global variable doesn't yet exist, otherwise it
    returns the value of the variable. If I remember right a VL function returns
    the value of the last SETQ, so you don't need to do the VBA version of
    FunctionName = GlobalVariable to end the funtion, correct?

    By the way, Robert, what happened to that web site you used to be involved
    with? I remember downloading your AU/AUGI error trap class some time ago,
    along with seeing all sorts of great code examples. Now I can't find the
    site.

    Best,
    Gordon
     
    R. Robert Bell, Oct 11, 2004
    #12
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.