Jump to content

Lisp routine that will round decimal places


dpaulku

Recommended Posts

This is an embarrassing question, but is there a lisp routine that will round decimal places once the text is placed? This has nothing to do with the units settings; this would be just editing dumb text elements already in the file. I don't write code but I imagine that a text string could be evaluated for numbers, a decimal point and a number of characters there after. I'd even be happy with truncating the last/extra character if it couldn't be rounded.

I mistakenly exported a whole lot of number data out of one POS (Piece of Software) with three point rounding in the digits. I need/want only two so that my crew won't think I'm being **** retentive.

Any hope for this?

 

Thanks in advance

Link to comment
Share on other sites

  • Replies 51
  • Created
  • Last Reply

Top Posters In This Topic

  • Lee Mac

    16

  • alanjt

    15

  • dpaulku

    8

  • Tharwat

    8

(defun c:R2D2 (/ ss)
 (if (setq ss (ssget "_:L" '((0 . "TEXT"))))
   ((lambda (i / e l)
      (while (setq e (ssname ss (setq i (1+ i))))
        (setq l (entget e))
        (entmod (subst (cons 1 (rtos (atof (cdr (assoc 1 l))) (getvar 'lunits) 2)) (assoc 1 l) l))
      )
    )
     -1
   )
 )
 (princ)
)

Link to comment
Share on other sites

OK, so it wasn't that hard. Where do I send the money?

 

Very much appreciated, you just saved my morning!

 

xoxo

 

...Ooops, may have spoke too soon. This works well on a per pick basis but if I select by window en-mass, unpredictable results show up.

 

example: (selected as a set all separate text entities)

CL Elev: 1278.141 Gnd Elev: 1277.952 DitchElev (LT) 1267.800

 

yields:

0.00 1278.14 0.00 1277.95 0.00 1267.80

 

Perhaps it's harder to diferentiate alpha from numeric?

Link to comment
Share on other sites

OK, so it wasn't that hard. Where do I send the money?

 

Very much appreciated, you just saved my morning!

 

xoxo

Some other time. You're welcome. :)

 

:lol::lol:

 

LoL

RoundToDecimalsofTwo

It was just too perfect.

Link to comment
Share on other sites

Not much different from Alan's code, keeping with the theme lol

 

(defun c:C3PO ( / ss )

 (if (setq ss (ssget "_:L" '((0 . "TEXT"))))
   (
     (lambda ( i / e el )
       (while (setq e (ssname ss (setq i (1+ i))))
         (if (distof (cdr (assoc 1 (setq el (entget e)))))
           (entupd
             (cdr
               (assoc -1
                 (entmod
                   (subst
                     (cons 1
                       (rtos
                         (atof
                           (cdr
                             (assoc 1 el)
                           )
                         )
                         (getvar 'LUNITS)
                         2
                       )
                     )
                     (assoc 1 el)
                     el
                   )
                 )
               )
             )
           )
         )
       )
     )
     -1
   )
 )
 (princ)
)
                              

Link to comment
Share on other sites

I'll quick check it. You guys are amazing.

 

Sorry that I left out that bit about not all the text being numbers... :P

 

... now it's hugs all round!

 

Thanks again.

Link to comment
Share on other sites

So, are they all prefixed with a string? Post an example drawing. I'm going to lunch, but I'll have some time later today.

Link to comment
Share on other sites

... now it's hugs all round!

 

Thanks again.

 

Excellent :)

 

Just for completeness, assuming decimals only - this should account for numerical text mixed up with alpha:

 

(defun c:2dp ( / ss )
 ;; © Lee Mac 2010
 (if (setq ss (ssget "_:L" '((0 . "TEXT"))))
   (
     (lambda ( i / e )
       (while (setq e (ssname ss (setq i (1+ i))))
         (entupd
           (cdr
             (assoc -1
               (entmod
                 (subst
                   (cons 1
                     (apply 'strcat
                       (mapcar
                         (function
                           (lambda ( item )
                             (if (eq 'INT (type item))
                               (itoa item)
                               (if (eq 'REAL (type item))
                                 (rtos item 2 2)
                                 item
                               )
                             )
                           )
                         )
                         (LM:ParseNumbers
                           (cdr
                             (assoc 1
                               (entget e)
                             )
                           )
                         )
                       )
                     )
                   )
                   (assoc 1 (entget e)) (entget e)
                 )
               )
             )
           )
         )
       )
     )
     -1
   )
 )
 (princ)
)

(defun LM:ParseNumbers ( str / isString isNumber lst tmp )
 ;; © Lee Mac 2010
 
 (defun isString ( x lst )    
   (cond
     ( (null lst) (list x))
     
     ( (< 47 (car lst) 58)
      
       (cons x (isNumber (chr (car lst)) (cdr lst)))
     )      
     ( (= 45 (car lst))
      
       (if (and (cadr lst)
             (numberp
               (read (setq tmp (strcat "-" (chr (cadr lst)))))
             )
           )
         (cons x (isNumber tmp (cddr lst)))
         (isString (strcat x (chr (car lst))) (cdr lst))
       )
     )      
     (t (isString (strcat x (chr (car lst))) (cdr lst)))
   )
 )
 
 (defun isNumber ( x lst )    
   (cond
     ( (null lst) (list (read x)))
     
     ( (= 46 (car lst))
      
       (if (and (cadr lst)
             (numberp
               (read (setq tmp (strcat x "." (chr (cadr lst)))))
             )
           )         
         (isNumber tmp (cddr lst))
         (cons (read x) (isString (chr (car lst)) (cdr lst)))
       )
     )
     ( (< 47 (car lst) 58)
      
       (isNumber (strcat x (chr (car lst))) (cdr lst))
     )      
     (t (cons (read x) (isString (chr (car lst)) (cdr lst))))
   )
 )
 
 (if (setq lst (vl-string->list str))
   (
     (if (or (and (= 45 (car lst)) (< 47 (cadr lst) 58))
             (< 47 (car lst) 58))

       isNumber isString
     )
     (chr (car lst)) (cdr lst)
   )
 )
)

Link to comment
Share on other sites

So, are they all prefixed with a string? Post an example drawing. I'm going to lunch, but I'll have some time later today.

 

I could do that but I'd have to put everything back! :shock:

 

C3PO works as expected with some exceptions so far:

 

It wouldn't resolve 2.500% to 2.50%. It ignored it altogether.

 

If a number had no decimal point, ie "1255" it resolved those into "1255.00".

 

For text representing stations, "24+00.000"... that got resolved into "0.00". Not sure what's going on there but the "+" has to be the culprit. (Ordinarily I'd go with 24+00 but I can live w/.00)

 

I'm working with cross sections dumped out of a POS and there's a ton of them that have been heavily edited. I couldn't just go back and change the precission and re-export cause... well, it's a long story.

 

Anyway within my problem graphic pile I have elevations of grid lines (1255), offsets from centerline ("-10" left and "10" right), elevations of ground points on the sections (1245.32), stations in "0+00" format and grades as percents (2.55%). Also the other "info text" for specific section features like cross slopes, elevations at centerline and at ditch bottoms.

 

 

'Not surprised really that this quick code wouldn't do everything. I got around the warts with some quick search and replace... took two minutes. To have all my numbers now fixed is a big big help.

 

If nothing else I've learned to be aware of the number of decimal places I'm running in diferent apps as they do share data on a regular basis.

 

Thanks all once again.

 

...Wait!, I do have a sample section that was cut to build the full set so I'll send it on for your info.:oops: v2010

 

ed: While I was typing this blurb Lee augmented the code. It's either lunch break on your end or you guys are lightning typists and robotic code generators! I'll test the latest version over lunch here in Frostbite Falls...

67506-WESTXSECTIONS-Sec1902.dwg

Link to comment
Share on other sites

trying it now... command "2dp" doesn't seem to be working... It does load w/o error but then perhaps there is no test done at load in.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...