loop

Discussion in 'AutoCAD' started by jlspartz, Feb 24, 2005.

  1. jlspartz

    jlspartz Guest

    Can I put a loop in my prompt menu?

    Here's one piece of my code and I want to set it so that each time you pick an option Horizontal or Vertical it will go into that command, then after that command come back to asking horizontal, vertical, or done again until the person chooses D for done.

    (initget "H V D")
    (setq hvd (getkword "\nHorizontally, Vertically or Done? [H/V/D] <D>: "))
    (if (not hvd)(setq hvd "D"))
    (cond
    ((= hvd "H")
    (cond
    ((= gridsize "1")(setq defshift-h "6"))
    ((= gridsize "2")(setq defshift-h "12"))
    ((= gridsize "3")(setq defshift-h "24"))
    ((= gridsize "4")(setq defshift-h "12"))
    )
    (setq shift-h (getreal (strcat "\nEnter distance to shift grid horizontally <" defshift-h ">: ")))
    (if (not shift-h)(setq shift-h (atoi defshift-h)))
    (setq center (list (+ x shift-h ) y))
    (command "hatchsetorigin" "last" "" center)
    )
    ((= hvd "V")
    (cond
    ((= gridsize "1")(setq defshift-v "6"))
    ((= gridsize "2")(setq defshift-v "12"))
    ((= gridsize "3")(setq defshift-v "12"))
    ((= gridsize "4")(setq defshift-v "24"))
    )
    (setq shift-v (getreal (strcat "\nEnter distance to shift grid vertically <" defshift-v ">: ")))
    (if (not shift-v)(setq shift-v (atoi defshift-v)))
    (setq center (list x (+ y shift-v)))
    (command "hatchsetorigin" "last" "" center)
    )
    ((= hvd "D")(command))
    )
    )
     
    jlspartz, Feb 24, 2005
    #1
  2. jlspartz

    Tom Smith Guest

    Can I put a loop in my prompt menu?

    You definitely need a while loop set up to terminate on a "Done" input.
    Tim's use of an "and" to run the "while" test certainly works, sorta like a
    "progn" group, but to my eye it's harder to read than a single line test.
    I'd abstract this out as a function, and the "getkey" function from the
    other day would work fine here. I'd also write a function to do the getreal,
    which would return the default on a null response.

    (defun getkeydef (prmpt keystr default)
    (initget 0 keystr)
    (cond
    ((getkword prmpt))
    (default)))

    (defun getrealdef (prmpt default)
    (cond
    ((getreal (strcat prmpt " <" (rtos default) ">: ")))
    (default)))

    I notice that both chunks of your "cond" are highly repetitive, differing
    mainly in h's versus v's and in which direction the shift goes. Per my reply
    elsewhere, both directions of shifting could be handled in the same manner.
    Whenever you see repetitive chunks of code, it's crying out to be
    generalized.

    There's an awful lot of business of setting default shift amounts. It seems
    you're wanting the default shift to be half a grid unit in the appropriate
    direction. This could be simpler if you kept the available grid sizes in an
    association list,:

    (setq gridsizelist '(("1" 12 12)("2" 24 24)("3" 48 24)("4" 24 48)))

    This would also make it easy to add more grid sizes, though that doesn't
    seem likely. Then for whatever gridsize has been selected (presumably
    elsewhere in your program), and the selected direction, the half-grid shift
    could easily be calculated from the list:

    (defun halfgrid (gridsizelist gridsize direction)
    (* 0.5
    (if (eq direction "Horiz")
    (cadr (assoc gridsize gridsizelist))
    (caddr (assoc gridsize gridsizelist)))))

    Then with all the busywork out of the way, assuming you've already set
    "center," "gridsize," and "gridsizelist," your loop reduces to just this:

    (while (not (eq "Done" (setq direction (getkeydef "\nShift grid
    Horiz/Vert/<Done>? " "Horiz Vert Done" "Done"))))
    (setq
    shift (getrealdef "\nDistance to shift grid" (halfgrid gridsizelist
    gridsize direction))
    displ (if (eq direction "Horiz") (list shift 0) (list 0 shift)))
    (command "hatchsetorigin" "last" "" (mapcar '+ center displ)))
     
    Tom Smith, Feb 25, 2005
    #2
  3. jlspartz

    jlspartz Guest

    This loop still isn't working correctly, I put in
    (while (not (eq "D" (setq hvd (getkword "\nHorizontally/Vertically? <Done>: "))))
    and then stuck everything inside of the while command and ended the while command, and I get it to go back to the question again after doing horizontal or vertical, but then I can't pick another option. It tells me every option is invalid the second time around.
     
    jlspartz, Mar 1, 2005
    #3
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.