if then X or Y

Discussion in 'AutoCAD' started by spencer1971, May 5, 2004.

  1. spencer1971

    spencer1971 Guest

    Im tyring to write a subroutine (c_sand) whish will get a keyword and then run either P_sand or S_sand respectively.

    My first attempt atusing if..

    Could anyone give me a shove in the right direction sorp keeps returning nil.

    (defun c:c_sand ()
    (initget "Select Pick")
    (setq sorp (getkword "\nHatch Style? Select/<Pick>: "))
    (if (= sorp "pick") p_sand)
    (if (= sorp "select") s_sand)
    )
     
    spencer1971, May 5, 2004
    #1
  2. spencer1971

    Chip Harper Guest

    I'm sure you'll get some better answers but I would try:

    (defun c:c_sand ()
    (initget 1 "Select Pick")
    (setq sorp (getkword "\nHatch Style? Select/<Pick>: "))
    (if (= sorp "pick") (setq sorp1 "p_sand"))
    (if (= sorp "select") (setq sorp1 "s_sand"))
    )
     
    Chip Harper, May 5, 2004
    #2
  3. 1. Note the use of a local variable. Try to remember to make
    variables local if they're only required in the function where
    they're assigned/referenced.

    2. Use the standard format for obtaining keywords
    (enclose keywords in square brackets so they are
    added to the right-click menu automatically).

    3. Since a null response implies the default ("Pick"),
    you explicitly test for the other response, and if
    that fails, you assume the default was accepted or
    entered.

    (defun C:C_SAND ( / sorp)
    (initget "Select Pick")
    (setq sorp (getkword "\nHatch style [Select/Pick] <Pick>: "))
    (if (eq sorp "Select")
    (s_sand)
    (p_sand) ;; Any response other than "Select"
    )
    ...
    (princ) ;; silent exit
    )

    Also consider supporting error handling, system variable
    save/restore, etc.
     
    Tony Tanzillo, May 5, 2004
    #3
  4. spencer1971

    ECCAD Guest

    Something like this ?
    -----------
    (defun p_sand ()
    (Prompt "\nSelected [Pick]..")
    (princ)
    )
    (defun s_sand ()
    (Prompt "\nSelected [Select]..")
    (princ)
    )
    (defun c:c_sand ()
    (initget "Select Pick S P")
    (setq sorp (strcase (getkword "\nHatch Style? Select/<Pick>: ")))
    (if (or (= sorp "P")(= sorp "PICK")) (p_sand))
    (if (or (= sorp "S")(= sorp "SELECT")) (s_sand))
    (princ)
    ); end function
    ;;

    :)) Bob
     
    ECCAD, May 5, 2004
    #4
  5. spencer1971

    David Bethel Guest

    The intiget / getkword combination is case sensitive. Capital letters
    being the short version response.

    (= sorp "Pick")
    (= sorp "Select")

    -David
     
    David Bethel, May 5, 2004
    #5
  6. spencer1971

    spencer1971 Guest

    I decided on this, It seems to work although Im not convinced its the neatest solution.

    I would like to make it default to <pick> by just hitting enter but I cannot work out how. (is it something to do with the square brackets as suggested above)

    Many thanks for all your help

    spence

    (defun c:c_sand ()
    (initget "Select Pick")
    (setq sorp (getkword "\nHatch Style? Select/<Pick>: "))
    (if (= sorp "Pick")
    (PROGN
    (P_sand)
    )
    (if (= sorp "Select")
    (PROGN
    (s_sand)
    ))))
     
    spencer1971, May 5, 2004
    #6
  7. spencer1971

    Jeff Mishler Guest

    No, It has to do with the order of the lines in your if statement....

    (if (= sorp "Select")
    (s_sand); it WAS "Select" so run s_sand
    (p_sand);it WAS NOT "Select" so run p_sand
    )

    HTH,
    Jeff

    cannot work out how. (is it something to do with the square brackets as
    suggested above)
     
    Jeff Mishler, May 5, 2004
    #7
  8. You seem to be making the same mistake that several
    other responses do, which is that you are using _TWO_
    conditional branching constructs, when you only need one.

    Look at the example I posted, and try to understand why
    only a single <if> <then> <else> is used rather than two
    distinct <if> <then> constructs.

    In other words:

    (if <condition> <then> <else>)

    Is doing the same thing as

    (if <condition1> <then>)
    (if <condition2> <else>)

    The use of two distinct <if>-<then> constructs is not really
    correct and confuses the intent of your code, because it
    implies that both <condition1> and <condition2> are inclusive
    (IOW, both of the two <conditions> can be true), but in fact,
    both conditions are exclusive (only one of the two <conditions>
    can be true, but never both).

    The sample I posted also supports the default response.




    the square brackets as suggested above)
     
    Tony Tanzillo, May 5, 2004
    #8
  9. WOW!!
    I never knew that capability existed before. It took me some digging to
    find anything about it in the Help.

    I tried this for myself:

    (defun C:Decide()
    (initget 0 "Yes No Maybe")
    (getkword "[Yes/No/Maybe]: ")
    )

    When I ran it, the right-click menu showed "Yes", "No", and "Maybe" while
    the command was active, and selecting one of those items would return the
    value.

    Thanks, Tony. I learned something new and useful today. Man, you really
    know the ins and outs of this program!

    -Rick Francken
     
    Rick Francken, May 6, 2004
    #9
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.