How to add sequencial text?

Discussion in 'AutoCAD' started by jongor, Feb 3, 2005.

  1. jongor

    jongor Guest

    I'm wrting a command line lisp file that will drawing electrical terminal blocks and label them sequentially, 1, 2,3 .....

    I'm confused on how to get the number needed, convert that to a string, and maintain the appropriate count. It seems like I'd have to convert the count back and forth between integer and ascii until I reach the number needed.

    (defun DTR (a)
    (* PI (/ a 180.0)))

    (defun c:tb (/ n1 n2 n3 p1 p1x p1y p2 p3 p4 t1 layr)

    (setvar "cmdecho" 0)

    (setq savemode1 (getvar "osmode"))
    (setvar "osmode" 0)
    (setvar "filedia" 0); turn off dialog boxes

    (setq layr (getvar "clayer")); get current layer before terminal construction
    ;create layers for terminal block
    (command "layer" "m" "equip" "c" "5" "" "")
    (command "layer" "m" "text" "c" "3" "" "")
    (command "layer" "m" "wiring" "c" "1" "" "")

    (setq n1a (getstring"\How many terminals? :"));get the number of terminals needed
    ;(setq n1 (getint"\How many terminals? :"));get the number of terminals needed
    (setq n1 (atoi n1a));Convert string to integer
    (setq n2 (* n1 0.25)) ;length of terminal strip
    (setq n1b (+ 2 n1)) ;number of terminal lines

    (setq p1 (getpoint "\nSelect start position of terminal block :"))
    ;(setq p5 p1)
    (setq p2 (polar p1 (DTR 0) 1))
    (setq p3 (polar p2 (DTR 270) n2))
    (setq p4 (polar p1 (DTR 270) n2))

    (command "clayer" "equip")

    (command "line" p1 p4 "")
    (command "line" p2 p3 "")

    (setq count 1)
    (while (< count n1b)
    (command "line" p1 p2 "")
    (setq p1 (polar p1 (dtr 270) 0.25))
    (setq p2 (polar p2 (dtr 270) 0.25))
    (setq count (+ 1 count)))

    (command "clayer" "text");change current layer to text
    (command "textsize" "3/32")

    (setq t1 (polar p1 (DTR 0) 0.5))
    (setq t2 (polar t1 (DTR 270) 0.125))

    ;EVERYTHING WORKS GREAT UNTIL HERE, IT PUTS THE SAME NUMBER IN EACH TERMINAL
    (setq count 1)
    (while (< count n1)
    (setq n3 (itoa n1))
    (command "text" "m" t2 "" "0" n3)
    (setq t2 (polar t2 (dtr 270) 0.25))
    (setq n1 (+ n1 1)
    (setq count (+ 1 count)))
    )

    Any help would be greatly appreciated!

    thanks
     
    jongor, Feb 3, 2005
    #1
  2. jongor

    T.Willey Guest

    Try this one. I noted what I did after the lines I changed.

    Tim

    (defun c:tb (/ n1 n2 n3 p1 p1x p1y p2 p3 p4 t1 layr)

    (setvar "cmdecho" 0)

    (setq savemode1 (getvar "osmode"))
    (setvar "osmode" 0)
    (setvar "filedia" 0); turn off dialog boxes

    (setq layr (getvar "clayer")); get current layer before terminal construction
    ;create layers for terminal block
    (command "layer" "m" "equip" "c" "5" "" "")
    (command "layer" "m" "text" "c" "3" "" "")
    (command "layer" "m" "wiring" "c" "1" "" "")

    (setq n1a (getstring"\How many terminals? :"));get the number of terminals needed
    ;(setq n1 (getint"\How many terminals? :"));get the number of terminals needed
    (setq n1 (atoi n1a));Convert string to integer
    (setq n2 (* n1 0.25)) ;length of terminal strip
    (setq n1b (+ 2 n1)) ;number of terminal lines

    (setq p1 (getpoint "\nSelect start position of terminal block :"))
    ;(setq p5 p1)
    (setq p2 (polar p1 (DTR 0) 1))
    (setq p3 (polar p2 (DTR 270) n2))
    (setq p4 (polar p1 (DTR 270) n2))

    (command "clayer" "equip")

    (command "line" p1 p4 "")
    (command "line" p2 p3 "")
    (setq t1 (polar p1 (DTR 0) 0.5)); moved this up in the code
    (setq t2 (polar t1 (DTR 270) 0.125)); moved this up in the code
    (setq count 1)
    (while (< count n1b)
    (command "line" p1 p2 "")
    (setq p1 (polar p1 (dtr 270) 0.25))
    (setq p2 (polar p2 (dtr 270) 0.25))
    (setq count (+ 1 count)))

    (command "clayer" "text");change current layer to text
    (command "textsize" "3/32")

    ;(setq t1 (polar p1 (DTR 0) 0.5))
    ;(setq t2 (polar t1 (DTR 270) 0.125))

    ;EVERYTHING WORKS GREAT UNTIL HERE, IT PUTS THE SAME NUMBER IN EACH TERMINAL
    (setq count 1)
    ;(while (< count n1)
    (repeat (atoi n1a) ; changed your while to a repeat
    (setq n3 (itoa n1))
    (command "text" "m" t2 "" "0" n3)
    (setq t2 (polar t2 (dtr 270) 0.25))
    (setq n1 (+ n1 1)) ; added a parenthesis
    ;(setq count (+ 1 count)))
    )
     
    T.Willey, Feb 3, 2005
    #2
  3. jongor

    T.Willey Guest

    Forgot to add one parenthesis at the end, so when you load it, just add one.

    Tim
     
    T.Willey, Feb 3, 2005
    #3
  4. jongor

    Jim Claypool Guest

    This one works.
    I used count for the text. If you add 1 to count and n1 count will always be
    less than n1 and it will not stop.
    You also can use getint to set n1 so there is no need to get it as a string
    and convert it.

    (defun c:tb (/ n1 n2 n3 p1 p1x p1y p2 p3 p4 t1 layr)
    (setvar "cmdecho" 0)
    (setq savemode1 (getvar "osmode"))
    (setvar "osmode" 0)
    (setvar "filedia" 0); turn off dialog boxes

    (setq layr (getvar "clayer")); get current layer before terminal
    construction
    ;create layers for terminal block
    (command "layer" "m" "equip" "c" "5" "" "")
    (command "layer" "m" "text" "c" "3" "" "")
    (command "layer" "m" "wiring" "c" "1" "" "")

    ;(setq n1a (getstring"\How many terminals? :"));get the number of terminals
    needed
    (setq n1 (getint"\How many terminals? :"));get the number of terminals
    needed
    ;(setq n1 (atoi n1a));Convert string to integer
    (setq n2 (* n1 0.25)) ;length of terminal strip
    (setq n1b (+ 2 n1)) ;number of terminal lines

    (setq p1 (getpoint "\nSelect start position of terminal block :"))
    (setq p5 p1)
    (setq p2 (polar p1 (DTR 0) 1))
    (setq p3 (polar p2 (DTR 270) n2))
    (setq p4 (polar p1 (DTR 270) n2))

    (command "clayer" "equip")

    (command "line" p1 p4 "")
    (command "line" p2 p3 "")

    (setq count 1)
    (while (< count n1b)
    (command "line" p1 p2 "")
    (setq p1 (polar p1 (dtr 270) 0.25))
    (setq p2 (polar p2 (dtr 270) 0.25))
    (setq count (+ 1 count))
    )

    (command "clayer" "text");change current layer to text
    (command "textsize" "3/32")

    (setq t1 (polar p5 (DTR 0) 0.5))
    (setq t2 (polar t1 (DTR 270) 0.125))
    (setq count 1)
    (while (<= count n1)
    (setq n3 (itoa count)) ; changed to use count instead of n1
    (command "text" "m" t2 "" "0" n3)
    (setq t2 (polar t2 (* pi 1.5) 0.25))
    ;(setq n1 (+ n1 1)) ; if you add 1 to n1, count will always be less and it
    will never stop
    (setq count (+ 1 count))
    )
    )
     
    Jim Claypool, Feb 3, 2005
    #4
  5. I suggest keeping your
    and eliminate of the (getstring) one and the
    line. Better to keep the integer so you can increment it, and convert it to
    a string only within the text command, with (itoa). Your problem area is
    incrementing both n1 and count, whereas you want n1 to remain the same while
    you compare count to it each time, and use count rather than n1 as the
    source for the (itoa) text content. Also, you want it to keep putting in
    text as long as count is less than *or equal to* n1, not just less than.
    This should work:

    (setq count 1)
    (while (<= count n1)
    (command "text" "m" t2 "" "0" (itoa count))
    (setq t2 (polar t2 (dtr 270) 0.25))
    (setq count (+ 1 count))
    )
     
    Kent Cooper, AIA, Feb 3, 2005
    #5
  6. Beat me to it, with pretty much the same approach. But I like mine better
    because you shouldn't need n3 at all.
    --
    Kent Cooper, AIA


    ...
    ......
    ......
     
    Kent Cooper, AIA, Feb 3, 2005
    #6
  7. jongor

    Jim Claypool Guest

    True, but then there are several things in there that aren't needed.
     
    Jim Claypool, Feb 3, 2005
    #7
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.