WHILE

Discussion in 'AutoCAD' started by tader1, Dec 9, 2004.

  1. tader1

    tader1 Guest

    hi, could have a little help w/ my while loop.
    This is to convert hours-min-sec to decimal and to check that 6 digits have been entered in the case of eg. 5.4352 would be 6 digits and continue to the rest of the program. But someone may try to enter .0045 leaving the preceding 0 out and thereby screwing up the substr count. Then the code continues for the case of 7 digits which would be eg. 45.5634 and substr picks the corresponding text out. I know this is a little shaky, maybe there's a better way to approach this? But right now the while loop doesn't work, it should continue if the getstring variable contains 6 characters if it doesn't it should print "not enough digits" and loop unitl satisfactory. TIA Shawn

    (defun dms ()

    (while (or
    (setq deg1 (getstring "\nEnter Bearing in Deg Mins Sec: "))
    (setq strl (strlen deg1))

    (if
    (= strl 6)

    (progn

    (setq deg (atof (substr deg1 1 2))
    mins (atof (substr deg1 3 2))
    sec (atof (substr deg1 5 2))
    ang_degs (+ deg (/ mins 60.0) (/ sec 3600.0))))




    (print "not enuff digits")))))
     
    tader1, Dec 9, 2004
    #1
  2. tader1

    T.Willey Guest

    Why don't you take what they give you like

    (while (not deg1)
    (setq deg1 (getstring "\n Enter bearing in Deg Mins Sec: "))
    )
    (setq chk1 (vl-string-search "." deg1))
    (if (= chk1 0)
    (setq deg 0
    mins (atof (substr deg1 2 2))
    secs (atof (substr deg1 4 2))
    )
    (setq deg (atof (substr deg1 1 chk1))
    mins (atof (substr deg1 (+ chk1 2) 2))
    secs (atof (substr deg1 (+ chk1 4) 2))
    )
    )

    Tim
     
    T.Willey, Dec 9, 2004
    #2
  3. Why don't you convert the input to a real first? Then if the user did that,
    a zero would get added automatically.

    --
    R. Robert Bell


    hi, could have a little help w/ my while loop.
    This is to convert hours-min-sec to decimal and to check that 6 digits have
    been entered in the case of eg. 5.4352 would be 6 digits and continue to the
    rest of the program. But someone may try to enter .0045 leaving the
    preceding 0 out and thereby screwing up the substr count. Then the code
    continues for the case of 7 digits which would be eg. 45.5634 and substr
    picks the corresponding text out. I know this is a little shaky, maybe
    there's a better way to approach this? But right now the while loop doesn't
    work, it should continue if the getstring variable contains 6 characters if
    it doesn't it should print "not enough digits" and loop unitl satisfactory.
    TIA Shawn

    (defun dms ()

    (while (or
    (setq deg1 (getstring "\nEnter Bearing in Deg Mins Sec: "))
    (setq strl (strlen deg1))

    (if
    (= strl 6)

    (progn

    (setq deg (atof (substr deg1 1 2))
    mins (atof (substr deg1 3 2))
    sec (atof (substr deg1 5 2))
    ang_degs (+ deg (/ mins 60.0) (/ sec 3600.0))))




    (print "not enuff digits")))))
     
    R. Robert Bell, Dec 9, 2004
    #3
  4. tader1

    T.Willey Guest

    Or once you get your string (deg1)

    (setq ang1 (angtos (angtof deg1 1) 1 4))

    Tim
     
    T.Willey, Dec 9, 2004
    #4
  5. tader1

    Tom Smith Guest

    Top of my head, I think you're making it too hard on yourself without making
    it any easier on the user. Why not prompt for the numbers separately using
    getint? (Wouldn't they all be integers?) You could use initget to filter out
    negative numbers or null responses. For instance:

    Command: Enter Bearing in Deg Mins Sec ...
    Degrees: <user response>
    Minutes: <user response>
    Seconds: <user response>
    Angle in degrees: <solution>

    If you get the input as numbers, you're part way there, no conversion to do,
    and you don't force the user to enter leading zeros, which isn't quite
    natural. IMHO, if it's three degrees, you should let them enter either 3 or
    03, as getint will. All you need to check is that each number falls within
    its acceptable range, e.g. 0 to 59 minutes. With the three numbers in hand,
    you're set to do the conversion.

    Maybe I'm missing something, but it seems like you're making it awkward for
    both programmer and user by forcing a formatted string input.
     
    Tom Smith, Dec 9, 2004
    #5
  6. tader1

    tader1 Guest

    Thnx for the posts, appreciate the time. The original lisp i found here and it did have the user put dms in seperately, but I didn't like it that way and thought that it would be better for me to have it all in one line so i would'nt have to hit the enter key; saving 2 keystrokes. But now i see y the lisp was originally written the way it was. There are certain problems w/ the way that i'm trying to do it that require alot of code to overcome, but since i'm the only one that uses time-saving lisp routines in the office, i will make sure that i keep myself aware of the problems.

    Thnx again,
    Shawn
     
    tader1, Dec 10, 2004
    #6
  7. tader1

    tader1 Guest

    ; func dms final out put variable is strdeg
    ; convert angles in degs to rads
    (defun dtr (a)
    (* pi (/ a 180.00))
    )

    (defun rtd (a)
    (/ (* a 180.00) pi)
    )

    ;(defun dms (/ degs mins secs )
    ; (setq deg (getreal "\nEnter degs: ")
    ; mins (getreal "\nEnter mins: ")
    ; sec (getreal "\nEnter secs: ")
    ; ang_degs (+ deg (/ mins 60.0) (/ sec 3600.0))))

    ; ** end setq
    ;** end func degs

    (defun dms ()
    (setq deg2 (getreal "\nEnter Bearing in Deg Mins Sec: ")
    deg1 (rtos deg2)
    strl (strlen deg1))
    (if
    (= strl 6)
    (progn

    (setq deg (atof (substr deg1 1 2))
    mins (atof (substr deg1 3 2))
    sec (atof (substr deg1 5 2))
    ang_degs (+ deg (/ mins 60.0) (/ sec 3600.0)))
    ))
    (if
    (>= strl 7)
    (progn

    (setq deg (atof (substr deg1 1 2))
    mins (atof (substr deg1 4 2))
    sec (atof (substr deg1 6 2))
    ang_degs (+ deg (/ mins 60.0) (/ sec 3600.0))))))

    ;revised to enter just one line of bearing input IN DMS (felt like the bank lady)

    (defun c:trav (/ deg1 d NE)
    (setvar "cmdecho" 0)
    (graphscr)
    (setq Stpt (Getpoint "\nPick starting point: "))
    (if (null stpt)(setq stpt (getvar "LASTPOINT")))
    (while (setq d (getdist "\nDist: "))
    (print "4=NW | 1=NE")
    (print "-----+----- 5=Last bearing")
    (print "3=SW | 2=SE Enter quadrant: ")
    (setq quad (getint))
    (if (= quad 5)(setq bear bear)
    (progn
    (dms)
    (cond ((= quad 1)(setq bear (- 90.0 ang_degs)))
    ((= quad 2)(setq bear (+ 270.0 ang_degs)))
    ((= quad 3)(setq bear (- 270.0 ang_degs)))
    ((= quad 4)(setq bear (+ 90.0 ang_degs)))
    ); end cond
    ); end progn
    ); end if
    (setq newpt (polar stpt (dtr bear) d))
    (command "LINE" stpt newpt "")
    (setq stpt newpt)
    (setvar "LASTPOINT" newpt)

    (cond ((= quad 1)(setq NE '("N " HI " E ")))
    ((= quad 2)(setq NE '("S " HI " E ")))
    ((= quad 3)(setq NE '("S " HI " W ")))
    ((= quad 4)(setq NE '("N " HI " W ")))

    )
    (setq dist (rtos d))
    (setq bear1 (apply 'strcat (subst deg1 'HI NE)))


    (print (strcat bear1 " " dist))

    ); end while
    (princ)

    )
     
    tader1, Dec 10, 2004
    #7
  8. tader1

    Jeff Mishler Guest

    FWIW, here's one I came up with awhile back, that allows the user to input
    the bearing or azimuth any way they want, i.e. 0.3000, .3, .30, 52, 52.0 are
    all valid entries.
    Code:
    ;supply a as true for azimuths or nil for quadrant bearings
    ;Example: (setq bear (getbearing nil))
    ;               (setq bear (getbearing t));for azimuth entry
    (defun getbearing (a / brg degs mins secs prmt tmp dmzn)
    (if a
    (setq prmt "\nAzimuth <DDD.MMSS>: ")
    (setq prmt "\nBearing <DD.MMSS>: ")
    )
    (setq brg (getreal prmt))
    (setq degs (fix brg)
    tmp (- brg degs)
    )
    (cond (a (if (or (> degs 359.0)
    (> tmp 0.5959))
    (progn
    (princ "\nInvalid entry, try again.")
    (setq brg nil)
    )
    )
    )
    (t (if (or (> degs 89.0)
    (> tmp 0.5959))
    (progn
    (princ "\nInvalid entry, try again.")
    (setq brg nil)
    )
    )
    )
    )
    (if brg
    (progn
    (setq dmzn (getvar "dimzin"))
    (setvar "dimzin" 0)
    (setq tmp (rtos tmp 2 4))
    (setvar "dimzin" dmzn)
    (setq mins (substr tmp 3 2))
    (setq secs (substr tmp 5))
    (setq brg (+ degs (/ (+ (atof mins) (/ (atof secs) 60)) 60)))
    )
    )
    )
    
     
    Jeff Mishler, Dec 10, 2004
    #8
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.