Help Needed

Discussion in 'AutoCAD' started by Kenny Poong, Jan 12, 2005.

  1. Kenny Poong

    Kenny Poong Guest

    Hi NG,

    I use the lisp below to help me to add up the selected numbers.

    I would like to have comma for the total. ( 1,000 instead of 1000 )

    Can some one help me to correct it ?

    TIA.


    (defun C:Add (/ p l n e as s tot)
    (setq tot 0 p (ssget)) ; Select objects
    (if p (progn ; If any objects selected
    (setq l 0 n (sslength p))
    (while (< l n) ; For each selected
    object...
    (if (= "TEXT" ; Look for TEXT entity type
    (group 0)
    (cdr (assoc 0 (setq e (entget (ssname p l)))))
    )
    (progn
    (setq s (stripit "," (cdr (setq as (assoc 1 e)))))
    (setq tot (+ (atof s) tot))
    )
    )
    (setq l (1+ l))
    )
    )
    )
    (princ (strcat "\nTotal: " (rtos tot 2 4))) ; second 4 sets number of
    decimal places
    (setq at (car (entsel "\. Select entity to change to new total or
    enter\n")))
    (if at
    (progn
    (setq enl (entget at))
    (setq e (subst (cons 1 (rtos tot 2 4)) (assoc 1 enl) enl))
    (entmod e)
    (princ)
    )
    )
    )
    (defun stripit (schar sstring / ptr ctr fnd ts)
    (setq ptr (strlen sstring)
    ctr 0
    fnd nil
    ts ""
    )
    (repeat ptr
    (setq ctr (+ 1 ctr))
    (setq fnd (= (substr sstring ctr 1) schar))
    (if (not fnd)
    (setq ts (strcat ts (substr sstring ctr 1)))
    )
    )
    )
     
    Kenny Poong, Jan 12, 2005
    #1
  2. Kenny Poong

    Adesu Guest

    Hi Kenny, check my code may be I can help you

    ac is stand for add comma
    ; Design by Ade Suharna <>
    ; 12 January 2005
    ; Program no. 162/01/2005
    ; Edit by
    (defun c:ac (/ ent info1 ltex opt com ntex 0tex revtex ed)
    (while
    (setq ent (entget (car (entsel "\nCLICK TEXT FOR EDIT:")))
    info1 (cdr (assoc 1 ent))
    ltex (strlen info1)
    opt (fix(getreal "\nCOUNT NUMBER TO ADD COMMA: "))
    com ","
    ntex (substr info1 1 opt)
    0tex (substr info1 (1+ opt)(1- ltex))
    revtex (strcat ntex com 0tex)
    ed (subst (cons 1 revtex)(assoc 1 ent) ent))
    (entmod ed)
    )
    (princ)
    )
     
    Adesu, Jan 12, 2005
    #2
  3. Kenny Poong

    Josh Guest

    Here is a crude function I wrote years ago when I was very new to Lisp and I
    haven't touched it since (it works fine for the rare occasion I use
    it...hmmm, I should rewrite it soon)

    ;;;formats an INTEGER into the "X,XXX,XXX" format (thousands) as a string
    ;;; 1234 returns "1,234"
    ;;; 1234.56 returns "1,234"
    ;;; "1234.56" returns "1,234.56"
    ;;;if you want a REAL returned you must format <comma_a> as a STRING with
    RTOS
    (defun commatize (comma_a / comma_go comma_b
    comma_c comma_d comma_e comma_f
    cnt temp_beg temp_end
    )
    (setq comma_go t
    temp_beg ""
    temp_end ""
    )
    (cond
    ((or (= (type comma_a) 'int) (= (type comma_a) 'real))
    (setq
    comma_b (fix comma_a)
    comma_d (itoa comma_b)
    comma_e (strlen comma_d)
    )
    )
    ((= (type comma_a) 'str)
    (progn
    (setq cnt 1)
    (if (wcmatch comma_a "*`.*")
    (progn
    (while (/= (substr comma_a cnt 1) ".")
    (setq
    temp_beg (strcat temp_beg (substr comma_a cnt 1))
    cnt (1+ cnt)
    temp_end (substr comma_a cnt)
    )
    )
    (setq comma_a temp_beg)
    )
    )
    (setq
    comma_e (strlen comma_a)
    comma_d comma_a
    )
    )
    )
    (t (setq comma_go nil))
    )
    (if comma_go
    (progn
    (setq comma_f "")
    (while (> comma_e 3)
    (setq
    comma_f (strcat "," (substr comma_d (- comma_e 2)) comma_f)
    comma_d (substr comma_d 1 (- comma_e 3))
    comma_e (- comma_e 3)
    )
    )
    (setq comma_f (strcat comma_d comma_f temp_end))
    comma_f
    )
    nil
    )
    )
     
    Josh, Jan 12, 2005
    #3
  4. Kenny Poong

    Kenny Poong Guest

    Hi Josh,

    Thanks for your reply.

    Actually I'm new in LISP....how can I "format <comma_a> as a STRING with
    RTOS" ?
     
    Kenny Poong, Jan 14, 2005
    #4
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.