Routine for TEXT and numeric values - Rounding to N decimals ?

Routine for TEXT and numeric values - Rounding to N decimals ?

braudpat
Mentor Mentor
129 Views
6 Replies
Message 1 of 7

Routine for TEXT and numeric values - Rounding to N decimals ?

braudpat
Mentor
Mentor

Hello

 

I am looking for a Lisp/VLisp routine concerning coordinates numeric TEXT entities ...

 

I have TEXTs entities (X or X - Y or X - Y -Z Coordinates) like : 

 

3.14159 

3.14159 - 6.12567

3.14159 - 6.12567 - 8.12345678

 

AND if asking for 2 decimals, I would like to have : 

 

3.14

3.14 - 6.13

3.14 - 6.13 - 8.12

 

So FIRST a question :

How any decimals (0-6) - Default = 2

 

Then a classic ACAD selection retaining ONLY TEXT entities 

 

Then for "corresponding" TEXT, ACTION !

 

ASAP an abnormal character is found : don't modify ... Go to the next ...

 

Acceptables characters : numbers, space, minus (the separator)

 

If the minus (the separator) "-"  is a problem (because possible negative values),

an other separator is possible :  *  or  /  or  #  for example

 

THANKS for your attention, Regards, Patrice (The Old French EE Froggy)

Patrice ( Supporting Troops ) - Autodesk Expert Elite
If you are happy with my answer please mark "Accept as Solution" and if very happy please give me a Kudos (Felicitations) - Thanks

Patrice BRAUD

EESignature


0 Likes
Accepted solutions (1)
130 Views
6 Replies
Replies (6)
Message 2 of 7

Kent1Cooper
Consultant
Consultant

@braudpat wrote:

....

If the minus (the separator) "-"  is a problem (because possible negative values),

....


For that part, can you rely on negative values always having no space between the minus sign and the number?  And on the separators always having a space on both sides of the minus sign?  If so, then you don't need to worry about another separator character.

 

There are routines out there to break text strings around whatever delimiter you want.  You could apply that using " - " as the delimiter.  Then to each resulting numerical substring, apply

(rtos (atof TheString) 2 2)

and put the results back together with the separators.

Kent Cooper, AIA
0 Likes
Message 3 of 7

braudpat
Mentor
Mentor

Hello @Kent1Cooper 

 

THANKS very much for your attention !

 

1) PLEASE IGNORE negative values (Minus just before 1234567890) - I can t imagine negative coordinates values !

 

2) So please waiting a beautiful routine ...

 

Bye, Pat

 

Patrice ( Supporting Troops ) - Autodesk Expert Elite
If you are happy with my answer please mark "Accept as Solution" and if very happy please give me a Kudos (Felicitations) - Thanks

Patrice BRAUD

EESignature


0 Likes
Message 4 of 7

Moshe-A
Mentor
Mentor
Accepted solution

@braudpat  hi,

 

check this command

 

enjoy

Moshe

 

;;; SetDecDigi.lsp
;;; by Moshe-A
;;; SEP 09, 2025


(vl-load-com) ; load activex support

;; String to List  -  Lee Mac
;; Separates a string using a given delimiter
;; str - [str] String to process
;; del - [str] Delimiter by which to separate the string
;; Returns: [lst] List of strings
 
(defun LM:str->lst ( str del / pos )
    (if (setq pos (vl-string-search del str))
        (cons (substr str 1 pos) (LM:str->lst (substr str (+ pos 1 (strlen del))) del))
        (list str)
    )
)


(defun c:setdecdigi (/ askint _sign _number isNumber ; local functions
		       DICTNAME DATADEC adoc saveDimzin defdec dec ss ename elist text ntext  )

 (defun askint (msg def / ask)
  (initget (+ 2 4))
  (if (not (setq ask (getint (strcat "\n" msg " <" (itoa def) ">: "))))
   (setq ask def)
   (setq def ask)
  )
 ); askint
  
 (defun _sign (n)
  (member n '(43 45 46))
 ); _sign
  
 (defun _number (n) 
  (or
    (_sign n)
    (and (>= n 48) (<= n 57))
  )
 ); _number


 (defun isNumber (str)
  (cond
   ((member str '(" " "-" "+" ".")) nil) ; case
   ((vl-every
     (function
      (lambda (n)
       (_number n)
      ); lambda
     ); function
     (vl-string->list str)
    ); vl-every
   ); case
  ); cond
 ); isNumber


 ; here start c:setdecdigi
 (setq adoc (vla-get-activedocument (vlax-get-acad-object)))
 (vla-startUndomark adoc)

 (setq DICTNAME "SetDecDigi"); const
 (setq DATADEC "ndecimal")   ; const

 (setq savDimzin (getvar "dimzin"))
 (setvar "dimzin" 0)

 (if (not (setq defdec (vlax-ldata-get DICTNAME DATADEC)))
  (setq defdec (vlax-ldata-put DICTNAME DATADEC 2))
 )
  
 (if (and
       (vlax-ldata-put DICTNAME DATADEC (setq dec (askint "Number of decimal places" defdec)))
       (setq ss (ssget '((0 . "mtext,text"))))
     )
  (foreach ename (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
   (setq elist (entget ename))
   (setq text (cdr (assoc '1 elist)))

   (setq ntext
	  (substr 
     	    (apply
             'strcat 
             (mapcar
                (function
                  (lambda (str)
	           (strcat " " (if (isNumber str) (rtos (atof str) 2 dec) str))
                  ); lambda
                ); function
               (LM:str->lst text " ")
              ); mapcar
            ); apply
           2      
          ); substr
   ); setq
    
   (entmod (subst (cons '1 ntext) (assoc '1 elist) elist))     
  ); foreach
 ); if

 (setvar "dimzin" savDimzin)

 (vla-endUndomark adoc)
 (vlax-release-object adoc)
  
 (princ)
); c:setdecdigi

 

 

 

Message 5 of 7

braudpat
Mentor
Mentor

Hello @Moshe-A 

 

WAOUH BEAUTIFUL ... THANKS

 

Best regards, Patrice

 

Patrice ( Supporting Troops ) - Autodesk Expert Elite
If you are happy with my answer please mark "Accept as Solution" and if very happy please give me a Kudos (Felicitations) - Thanks

Patrice BRAUD

EESignature


0 Likes
Message 6 of 7

Moshe-A
Mentor
Mentor

Patrice hi,

 

1. thank you for that 😀

2. made some corrections and fine tuning, so use this update.

3. this all could not be done without the beautiful function (LM:str->lst) from mr Lee Mac, thank you lee.

 

enjoy

Moshe

 

Message 7 of 7

braudpat
Mentor
Mentor

Hello @Moshe-A 

 

THANKS for your WORK !

 

I have just discovered that your BEAUTIFUL routine is OK with MTEXT and other separators :  -   ,  / 

 

THE HEALTH, Best Regards, Bye, Pat (The Old French EE Froggy) 

Patrice ( Supporting Troops ) - Autodesk Expert Elite
If you are happy with my answer please mark "Accept as Solution" and if very happy please give me a Kudos (Felicitations) - Thanks

Patrice BRAUD

EESignature


0 Likes