Need help adding feature to lisp

Discussion in 'AutoCAD' started by WashCaps37, Aug 11, 2004.

  1. WashCaps37

    WashCaps37 Guest

    Hello, I'm not extremely familiar with writing LISP so I am seeking help with adding a feature to the following routine.

    First of all, I use the routine below to cycle through each layout inserting a titleblock at coordinates defined within the routine. My question is: What do I need to add and change on the following routine so I am prompted to select an insertion point in the first layout of the cycle and that insertion point is the set insertion point for the titleblock for all of the layouts in the cycle? Hope this makes any sense. Thanks in advance!

    (defun c:borderupdate ()

    (foreach a (layoutList) ;cycles through layouts
    (setq count 0) ;sets a counter to cycle through object in each layout

    (command "Layout" "set" a) ;specifed layout from the foreach statement

    (progn ;the following is a group of operations to be preformed together
    (setq ss (ssget "x")) ;makes a selection set of all entities in a layout tab
    (repeat (sslength ss) ;this will repeat an number of operations to be performed on each entity
    (setq ename (ssname ss count)) ;gets entity name
    (setq entlist (entget ename)) ;gets entity list of dotted pairs
    (setq bordername (cdr (assoc 2 entlist))) ;retrieves group code 2 information, group code 2 hold the block name if the entity is a block
    (if (= bordername "Border") ;if the block named ____ is found, then
    (entdel ename) ;the block will be delected
    )
    (setq count (+ 1 count)) ;1 is added to count to cycle to next entity
    ) ;end of program above

    (progn ;the following is a group of operations to be preformed together
    (setq oldAtt(getvar "attreq")) ;this information obtains current settion for attibuted blocks
    (setvar "attreq" 0) ;set attributes to not be shown at the promp or window
    (command "-insert" "NEW BORDER" "0.1738, -0.2751" 1 1 0) ;inserts new border at coordinates
    (setvar "attreq" oldAtt) ;sets attreq value back to original
    );end of program above

    );end repeat

    );end for each

    );end entire program
     
    WashCaps37, Aug 11, 2004
    #1
  2. WashCaps37

    T.Willey Guest

    You could use and (if statement. ie:

    (if inspt
    (command "-insert" "NEW BORDER" inspt 1 1 0)
    (command "-insert" "NEW BORDER"
    (setq inspt (getpoint "\nInsetion point: ")) <- this will store the point you pick
    1 1 0)
    ); end if

    Hope that helps.
    Tim
     
    T.Willey, Aug 11, 2004
    #2
  3. WashCaps37

    T.Willey Guest

    You should also make that variable (inspt) localize within that function. I noticed that none of your variables are, but if you run this once, then again, the point will be set and you won't get prompted to selected it for the first placement like you are looking for.

    Tim
     
    T.Willey, Aug 11, 2004
    #3
  4. WashCaps37

    WashCaps37 Guest

    Thanks Tim for the help. It's very much appreciated. I'll give your suggestion a try. Although I'm not quite sure as to what you mean by making the variable (inspt) localized within the function. Could you please elaborate on that?
     
    WashCaps37, Aug 11, 2004
    #4
  5. WashCaps37

    T.Willey Guest

    I learned that here also. When you write a lisp it's better practice to localize the variable that you use withine that lisp to that lisp only. To localize your variables you would:
    (defun c:test (/ tmp)
    (setq tmp "This is local")
    (setq tmp1 "This is not local")
    (princ)
    )

    Now when you run this it sets tmp and tmp1, but after you fun it type at the command line !tmp and !tmp1. Notice the difference. That is because we made tmp local within the test function.


    Another thing you can do is make functions dependant on something. Example:
    (defun test2 (tmp1 / tmp)
    (setq tmp tmp1)
    )

    At the command line type (test2 "anything"). Now tmp will be set to "anything". Hope this helps and not confuses you more.

    Tim
     
    T.Willey, Aug 11, 2004
    #5
  6. WashCaps37

    Tom Smith Guest

    A variation on Tim's suggestion:

    (initget 1) ;ensures a point will get picked
    (if (null inspt (setq inspt (getpoint "\nInsertion point: ")))) ;gets a
    point and stores it if it wasn't already
    (command "-insert" "NEW BORDER" inspt 1 1 0) ;then use it

    Note that neither version will work unless you declare your variables local!
    Please do this before proceeding any further. IMHO you should make it a
    habit to always add a variable's name to the locals as soon as you use it --
    otherwise it can become a laborious task to go back later and find them all.
     
    Tom Smith, Aug 11, 2004
    #6
  7. WashCaps37

    Tom Smith Guest

    To expand on Tim's explanation, by declaring a variable local to your
    function, you guarantee that only exists during the time your function is
    running, doesn't conflict with a global variable (even of the same name),
    and it vanishes to nil when your function ends.

    A global variable is one that's floating around in memory for the rest of
    your Acad session. If you don't make a practice of declaring variables local
    to your function, inevitably you'll begin having conflicts -- like on
    function defaulting to an "inspt" that was set by a different function. This
    will lead to program bugginess that's a nightmare to sort out.
     
    Tom Smith, Aug 11, 2004
    #7
  8. WashCaps37

    Tom Smith Guest

    Oops, a missing parenthesis! Sorry! This should work:

    (initget 1)
    (if (null inspt)
    (setq inspt (getpoint "\nInsertion point: ")))
    (command "-insert" "NEW BORDER" inspt 1 1 0)
     
    Tom Smith, Aug 11, 2004
    #8
  9. WashCaps37

    T.Willey Guest

    Thanks Tom, better explaination. I couldn't think of the words, so I used and example.

    Tim
     
    T.Willey, Aug 11, 2004
    #9
  10. WashCaps37

    WashCaps37 Guest

    Thanks Tom! I'm starting to see the light at the end of the tunnel.
     
    WashCaps37, Aug 11, 2004
    #10
  11. WashCaps37

    WashCaps37 Guest

    Works great! Thanks for all the help guys!
     
    WashCaps37, Aug 12, 2004
    #11
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.