conditional loop

Discussion in 'AutoCAD' started by jlspartz, Jan 13, 2005.

  1. jlspartz

    jlspartz Guest

    What I want to do is loop the prompt for the insulation thinkness if THK1 is nil. How do I put a condition in this setq to do that?

    (setq PT1 (getpoint "\nPick start point... ")
    PT2 (getpoint PT1 "\nPick end point or type in distance: ")
    THK1(getreal "\nEnter insulation thickness: ")
    THK2(* THK1 0.25)
    THK3(* THK1 0.40000000)
    THK4(* THK1 0.44721360)
    LEN1(distance PT1 PT2)
    LEN2(/ LEN1 THK1)
    ANG1(angle PT1 PT2)
    ANG2(+ ANG1 0.0)
    ANG3(+ ANG1 1.10714872)
    ANG4(+ ANG1 2.21429744)
    ANG5(+ ANG1 4.06888787)
    ANG6(+ ANG1 4.24874137)
    a 0
    )
     
    jlspartz, Jan 13, 2005
    #1
  2. jlspartz

    T.Willey Guest

    You would have to break up your setq into two different ones. One before you get the thickness, and one after.
    (while (not THK1)
    (setq THK1 (getdist "\n Enter thickness of insulation: "))
    )

    I like using getdist so that you can pick points on the screen not just type them in. This will only work if you localize your variables or set it THK1 to nil before the loop.

    Tim
     
    T.Willey, Jan 13, 2005
    #2
  3. jlspartz

    Tom Smith Guest

    In addition to Tim's remark, I think you should also take some
    "bulletproofing" steps. Your original version will crash on PT2 if PT1
    wasn't picked, and on any of the derived quantities if either of these or
    THK1 isn't set. One approach might be:

    (and
    (setq PT1 (getpoint "\nPick start point... "))
    (setq PT2 (getpoint PT1 "\nPick end point or type in distance: "))
    (while (not THK1)
    (setq THK1 (getdist "\n Enter thickness of insulation: ")))
    (setq
    THK2(* THK1 0.25)
    THK3(* THK1 0.40000000)
    ...
    a 0
    )
    )

    The and construction will abort the first time any of its terms is nil. So
    if PT1 is nil, you bail out of the and. This way, you won't ever get to the
    derived variable calculations unless you have the three essential ones set.

    Of course you could also use the while trick on the PT1 and PT2 prompts, or
    you could use initget to prevent null responses there. But in many cases,
    the simple and construction can serve to prevent invalid-input errors.
     
    Tom Smith, Jan 13, 2005
    #3
  4. jlspartz

    jlspartz Guest

    Thanks, works great! and I'll remember to separate the setq's from now on.
     
    jlspartz, Jan 13, 2005
    #4
  5. jlspartz

    Tom Smith Guest

    Thanks, works great! and I'll remember to separate the setq's from now
    on.

    You're welcome. Just to make sure you understand, separating the setq's
    doesn't accomplish anything by itself, it's the fact that the separate
    setq's are within an and, so that the and will abort if any of the
    individual setq's evaluate nil. Once the essential things are setq'ed, then
    there's no reason not to group the rest of the calculated setq's, as per my
    example.

    The and trick is often useful, but you have to make sure that item within
    the and will evaluate to "something" to signify success, or nil to signify
    failure.
     
    Tom Smith, Jan 14, 2005
    #5
  6. In addition to question itself, you will find you work to be much easier to
    work with, if you change your typing to lower case letters and indent the
    writing.

    Regards Matti
     
    Matti Pitkänen, Jan 14, 2005
    #6
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.