Add Incremental Value to Numerical Text

Add Incremental Value to Numerical Text

Anonymous
Not applicable
1,545 Views
8 Replies
Message 1 of 9

Add Incremental Value to Numerical Text

Anonymous
Not applicable

Hi.

I have this code to add incremental value:

(Defun c:altcot ()	
(setq meu-ss(ssget '((0 . "TEXT,MTEXT"))))
  (setq value (getdist "\nValue to increment? "))
;
(setq cntr 0)
(while (< cntr (sslength meu-ss))
;
(setq en(ssname meu-ss cntr))
;
(setq enlist(entget en))
(setq s-tex(cdr(assoc 1 enlist)))
(setq n-tex(atof s-tex))
(setq nf-tex (+ value n-tex))
(setq nft-tex(rtos nf-tex 2 2))
(setq enlist(subst (cons 1 nft-tex)(assoc 1 enlist) enlist))
(entmod enlist)
;
(setq cntr(+ cntr 1))
;
)
)

It works well, but it removes second decimal number if it is zero. like:

100.45 + 0.15 = 100.6

It must be 100.60

 

Any help?

Thank you.

0 Likes
Accepted solutions (2)
1,546 Views
8 Replies
Replies (8)
Message 2 of 9

ВeekeeCZ
Consultant
Consultant

Search help for DIMZIN sysvar. 

0 Likes
Message 3 of 9

dlanorh
Advisor
Advisor
Accepted solution

Try this. It doesn't use (rtos)

(defun c:altcot ( / cme meu-ss val ent e_lst o_val n_val u fp n_txt)
  
  (setq cme (getvar 'cmdecho)
        meu-ss (ssget '((0 . "TEXT,MTEXT")))
        val (getreal "\nValue to increment? ")
  );end_setq
  
  (setvar 'cmdecho 0)

  (repeat (setq cntr (sslength meu-ss))
    (setq ent (ssname meu-ss (setq cntr (1- cntr)))
          e_lst (entget ent)
          o_val (atof (cdr (assoc 1 e_lst)))
          n_val (+ val o_val)
          u (fix n_val)
          fp (* (rem n_val 1.0) 100)
    );end_setq
    (if (> (rem fp 1.0) 0.49) (setq fp (1+ fp)))
    (setq n_txt (strcat (itoa u) "." (itoa (fix fp)))
          e_lst (subst (cons 1 n_txt) (assoc 1 e_lst) e_lst)
    );end_setq
    (entmod e_lst)
  );end_repeat
  (setvar 'cmdecho cme)
  (princ)
);end_defun

I am not one of the robots you're looking for

Message 4 of 9

ВeekeeCZ
Consultant
Consultant

@dlanorh wrote:

Try this. It doesn't use (rtos)

...
  (setvar 'cmdecho 0)
...

 

And now the real challenge - how about not using the echo too.

0 Likes
Message 5 of 9

scot-65
Advisor
Advisor
(if (and
(setq meu-ss (ssget '((0 . "TEXT,MTEXT"))))
(setq value (getdist "\nValue to increment? ")) );and
(progn

;[Someone forgot to check user input before proceeding into the main program!]

);progn
);if

GETDIST is not a desirable function to request an integer from the user?

???

Scot-65
A gift of extraordinary Common Sense does not require an Acronym Suffix to be added to my given name.

Message 6 of 9

john.uhden
Mentor
Mentor

Do a search on this forum for EDITNUM.

If you can't find it, then let me know and I'll post it again.

John F. Uhden

0 Likes
Message 7 of 9

CodeDing
Advisor
Advisor

@Anonymous ,

 

Assuming they're decimal units (the text is only numbers and periods), here's my go at it:

(defun c:ALTCOT ( / ss e cnt val txt tmp dec)
(vl-load-com)
(setq ss nil)
(prompt "\nSelect Mtext or Text: ")
(while (not (setq ss (ssget '((0 . "TEXT,MTEXT")))))
  (prompt "...nothing valid selected.")
);while
(initget 7) (setq val (getreal "\nValue to increment: "))
(setq cnt (sslength ss))
(while (<= 0 (setq cnt (1- cnt)))
  (setq e (ssname ss cnt) dec nil)
  (setq txt (cdr (assoc 1 (entget e))))
  (if (setq tmp (distof txt 2))
    (progn
      (if (setq dec (vl-string-search "." txt))
        (setq dec (- (strlen txt) (1+ dec)))
        (setq dec 0)
      );if
      (setq tmp (rtos (+ val tmp) 2 dec))
      (setq tmp (subst (cons 1 tmp) (assoc 1 (entget e)) (entget e)))
      (entmod tmp)
      (entupd e)
    );progn
  ;else
    (prompt (strcat "\n...invalid text-to-real value found: " txt))
  );if
);while
(setq ss nil) (princ "\nALTCOT Complete...") (princ) );defun

- - - - - - - - - - - - - Pre - > - > - > - > - > - > - > - > - > - > - Post (adding 0.15) - - - - - - - - - - - - -

image.pngimage.png

Best,

~DD

0 Likes
Message 8 of 9

Anonymous
Not applicable

I know that.

But I want another way to do the job without change variable DIMZIN or LUPREC

0 Likes
Message 9 of 9

john.uhden
Mentor
Mentor
Accepted solution
That could be a dilema.
If the number being added to is to the nearest tenth (X.1) and the additive
is to the nearest hundredth (X.01), do you want the solution to be to the
tenth or to the hundredth?
My EDITNUM program uses the greater precision, as in
3.3 + 0.05 = 3.35
3.3 + 0.1 = 3.4

<>
Virus-free.
www.avg.com
<>
<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

John F. Uhden