Creating A Loop in my Lisp FIle

Discussion in 'AutoCAD' started by cakitzmann, Jun 29, 2004.

  1. cakitzmann

    cakitzmann Guest

    I am trying to creat a loop in this program.

    This program takes a piece of text and coverts it to metric and places it below the text that you pick. I am trying to get it to run multiple time so that I do not have to re-enter the program for every piece of text that I want to convert.

    Could anyone help me out?

    (defun C:metric ()
    (command "undo" "m")
    (defun rtd (a) (* (/ a pi) 180.0))
    (defun dtr (a) (* pi (/ a 180.0)))
    (setq lay (getvar "clayer"))
    (if (= (tblsearch "layer" "altdim") nil)
    (command "layer" "m" "altdim" "c" "g" "" "")
    )
    ;;if
    (prompt "Select Dimension to Convert: ")
    (setq dimen (car (entsel)))
    (cond
    ((= (cdr (assoc 0 (entget dimen))) "TEXT")
    (setq dimtext (cdr (assoc 1 (entget dimen))))
    (setq rot (cdr (assoc 50 (entget dimen))))
    (setq ang (rtd rot))
    (setq hgt (cdr (assoc 40 (entget dimen))))
    (setq wid (cdr (assoc 41 (entget dimen))))
    (setq width (* wid 0.75))
    (setq ins (cdr (assoc 11 (entget dimen))))
    (setq angg (+ ang 270.0))
    (setq angl (dtr angg))
    (setq pt (polar ins angl (* 2.0 hgt)))
    (setq num dimtext)
    (setq dimm nil)
    (setq mm 1)
    (while (and (= dimm nil) (< mm 6))
    (setq dimm (distof num mm))
    (setq mm (1+ mm))
    )
    ;;while
    (setq metrik (* dimm 25.4))
    (setq newdim (strcat "[" (rtos metrik 2 0) "mm]"))
    (command "copy" dimen "" ins pt)
    (setq data (entget (entlast)))
    (setq data (subst (cons 1 newdim) (assoc 1 data) data))
    ;; (setq data (subst (cons 41 width) (assoc 41 data) data))
    (entmod data)
    )
    ;;=
    ((= (cdr (assoc 0 (entget dimen))) "DIMENSION")
    (setq last (entlast))
    (command "explode" dimen "")
    (setq mes (entnext last))
    (while (/= (cdr (assoc 0 (entget mes))) "TEXT")
    (setq mes (entnext mes))
    )
    ;;while
    (setq entt (entget mes))
    (setq rot (cdr (assoc 50 entt)))
    (setq ang (rtd rot))
    (setq hgt (cdr (assoc 40 entt)))
    (setq wid (cdr (assoc 41 entt)))
    (setq width (* wid 0.75))
    (setq ins (cdr (assoc 10 entt)))
    (setq angg (+ ang 270.0))
    (setq ang1 (dtr angg))
    ;; (setq pt (polar ins ang1 (* 3.0 hgt)))
    ;; (setq pt (polar ins ang1 (* 2.5 hgt)))
    (setq pt (polar ins ang1 (* 2.0 hgt)))
    (setq dmm (cdr (assoc 1 entt)))
    (setq dimm nil)
    (setq mm 1)
    (while (and (= dimm nil) (< mm 6))
    (setq dimm (distof dmm mm))
    (setq mm (1+ mm))
    )
    ;;while
    (setq metrik (* dimm 25.4))
    (setq newdim (strcat "[" (rtos metrik 2 0) "mm]"))
    (setq layr (cdr (assoc 8 entt)))
    (setq styl (cdr (assoc 7 entt)))
    (command "u")
    (setq txt (getvar "textstyle"))
    (command "layer" "s" "altdim" "")
    (setvar "textstyle" styl)
    (command "text" pt ang newdim)
    (setvar "textstyle" txt)
    (command "layer" "s" lay "")
    )
    ;;=
    (T nil)
    )
    ;;cond
    (prin1)
    )
    ;;defun
     
    cakitzmann, Jun 29, 2004
    #1
  2. cakitzmann

    Rudy Tovar Guest

    (while (setq e (entsel "\nSelect Dimension to Convert: "))
    (setq en (car e))
    ;do_what_ever
    )

    You don't need to 'Prompt' anything outside the 'entsel', and you should
    away's include a '\n' for a new line, which acts as a [return].
    --

    AUTODESK
    Authorized Developer
    http://www.Cadentity.com
    MASi



    below the text that you pick. I am trying to get it to run multiple time so
    that I do not have to re-enter the program for every piece of text that I
    want to convert.
     
    Rudy Tovar, Jun 30, 2004
    #2
  3. cakitzmann

    T.Willey Guest

    You have two options
    1- Select the text with "ssget" then step through each with you lisp.

    2- Add a "while" to your selection method.

    If you have it so that multipy objects get changed within one command you may want to add an "undo" feature within your code, so that you can undo your selections instead of undoing all, IMO.

    Tim
     
    T.Willey, Jun 30, 2004
    #3
  4. cakitzmann

    cakitzmann Guest

    Could you please explain some of this a little more as I am very new at this and I am just trying to build things as I go. This is only the second thing that I have wrote. I need a little coaching with this. Thank you for your patience as I get through this.
     
    cakitzmann, Jun 30, 2004
    #4
  5. cakitzmann

    Doug Barr Guest

    below the text that you pick. I am trying to get it to run multiple time so
    that I do not have to re-enter the program for every piece of text that I want
    to convert.

    ********

    This routine simplifies what you had. It takes you at your word that the text is
    a TEXT, not a dimension.
    It has a multiplier of 1.25 for offsetting the text downward. Give it a try!
    -doug

    (defun c:metrictext ()
    (if (= (tblsearch "layer" "altdim") nil)
    (command "layer" "m" "altdim" "c" "4" "" "")
    )
    (while
    (setq a (entsel "\nPick text for conversion to metric:"))
    (setq b (car a)
    c (entget b)
    d (cdr (assoc 1 c)) ; the text value
    dd (atof d)
    ddd (* dd 25.4)
    dddd (rtos ddd 2 0)
    ddddd (strcat "[" dddd "mm]")
    e (cdr (assoc 40 c)) ; the text height
    ee (* 1.25 e) ; text height multiplier
    f (cdr (assoc 8 c))) ; the text layer
    (command "UCS" "E" b)
    (command "UCS" "Z" 180)
    (command "copy" b "" (list 0.0 ee 0.0) "")
    (command "UCS" "P")
    (command "UCS" "P")
    (setq g (entget (entlast))
    h (assoc 8 g)
    hh (cons 8 "altdim")
    g (subst hh h g)
    i (assoc 1 g)
    ii (cons 1 ddddd)
    g (subst ii i g))
    (entmod g)
    )
    (princ)
    )

    *******************
     
    Doug Barr, Jun 30, 2004
    #5
  6. cakitzmann

    Doug Barr Guest

    Or, if you like the way the original routine works, just make the changes
    indicated below. Personally, I couldn't get it to work on a dimension.
    -doug

    ************
    (defun C:metric ()
    (command "undo" "m")
    (defun rtd (a) (* (/ a pi) 180.0))
    (defun dtr (a) (* pi (/ a 180.0)))
    (setq lay (getvar "clayer"))
    (if (= (tblsearch "layer" "altdim") nil)
    (command "layer" "m" "altdim" "c" "g" "" "")
    );;if
    (WHILE
    (setq dimen (car (entsel "Select Dimension to Convert: ")))
    (cond
    ((= (cdr (assoc 0 (entget dimen))) "TEXT")
    (setq dimtext (cdr (assoc 1 (entget dimen))))
    (setq rot (cdr (assoc 50 (entget dimen))))
    (setq ang (rtd rot))
    (setq hgt (cdr (assoc 40 (entget dimen))))
    (setq wid (cdr (assoc 41 (entget dimen))))
    (setq width (* wid 0.75))
    (setq ins (cdr (assoc 11 (entget dimen))))
    (setq angg (+ ang 270.0))
    (setq angl (dtr angg))
    (setq pt (polar ins angl (* 2.0 hgt)))
    (setq num dimtext)
    (setq dimm nil)
    (setq mm 1)
    (while (and (= dimm nil) (< mm 6))
    (setq dimm (distof num mm))
    (setq mm (1+ mm))
    );;while
    (setq metrik (* dimm 25.4))
    (setq newdim (strcat "[" (rtos metrik 2 0) "mm]"))
    (command "copy" dimen "" ins pt)
    (setq data (entget (entlast)))
    (setq data (subst (cons 1 newdim) (assoc 1 data) data))
    ;; (setq data (subst (cons 41 width) (assoc 41 data) data))
    (entmod data)
    );;=
    ((= (cdr (assoc 0 (entget dimen))) "DIMENSION")
    (setq last (entlast))
    (command "explode" dimen "")
    (setq mes (entnext last))
    (while (/= (cdr (assoc 0 (entget mes))) "TEXT")
    (setq mes (entnext mes))
    );;while
    (setq entt (entget mes))
    (setq rot (cdr (assoc 50 entt)))
    (setq ang (rtd rot))
    (setq hgt (cdr (assoc 40 entt)))
    (setq wid (cdr (assoc 41 entt)))
    (setq width (* wid 0.75))
    (setq ins (cdr (assoc 10 entt)))
    (setq angg (+ ang 270.0))
    (setq ang1 (dtr angg))
    ;; (setq pt (polar ins ang1 (* 3.0 hgt)))
    ;; (setq pt (polar ins ang1 (* 2.5 hgt)))
    (setq pt (polar ins ang1 (* 2.0 hgt)))
    (setq dmm (cdr (assoc 1 entt)))
    (setq dimm nil)
    (setq mm 1)
    (while (and (= dimm nil) (< mm 6))
    (setq dimm (distof dmm mm))
    (setq mm (1+ mm))
    );;while
    (setq metrik (* dimm 25.4))
    (setq newdim (strcat "[" (rtos metrik 2 0) "mm]"))
    (setq layr (cdr (assoc 8 entt)))
    (setq styl (cdr (assoc 7 entt)))
    (command "u")
    (setq txt (getvar "textstyle"))
    (command "layer" "s" "altdim" "")
    (setvar "textstyle" styl)
    (command "text" pt ang newdim)
    (setvar "textstyle" txt)
    (command "layer" "s" lay "")
    );;=
    (T nil)
    );;cond
    );;WHILE
    (prin1)
    );;defun
     
    Doug Barr, Jun 30, 2004
    #6
  7. cakitzmann

    Rudy Tovar Guest

    Sure...

    When you specify a value to (setq e (entsel "\n<prompt>")) it remains true.
    If you simply [Enter] or fail to select anything, its return value is 'NIL'.

    So (while (setq e....)) there is a value as long as you feed it an object,
    and when the return value is 'NIL' the loop is broken or exits.

    If you declare the loop with (while (setq e (car (entsel "\n....")))
    ;do_this_or_that) the return value will cancel everything else, because you
    are also passing a function which is 'NIL'. Where? The statement '(car'
    expects the first element in the list supplied. But if the value is 'NIL'
    then an ERROR is commited and the utility crashes.

    (setq e (car (entsel))) is not the proper method to feed values, because you
    run the possibility of it crashing. This is not the proper method of coding,
    no error trap.

    While the following is correct...
    (setq e (entsel))

    (if e
    (progn
    ;do_this_or_that
    )
    )

    Or
    (while (setq e (entsel "\n<prompt>")); a value for loop or exit if 'nil'.
    ;do_this_or_that
    )
    --

    AUTODESK
    Authorized Developer
    http://www.Cadentity.com
    MASi



    this and I am just trying to build things as I go. This is only the second
    thing that I have wrote. I need a little coaching with this. Thank you for
    your patience as I get through this.
     
    Rudy Tovar, Jun 30, 2004
    #7
  8. cakitzmann

    devitg Guest

    why not to use the ALTERNATE UNITS .????
     
    devitg, Jul 1, 2004
    #8
  9. cakitzmann

    Barr Guest

    I didn't know alternate units worked on text! Slick!
     
    Barr, Jul 1, 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.