Looking for a LISP to convet a decimal value in fraction

Looking for a LISP to convet a decimal value in fraction

jesusrperezd
Enthusiast Enthusiast
683 Views
11 Replies
Message 1 of 12

Looking for a LISP to convet a decimal value in fraction

jesusrperezd
Enthusiast
Enthusiast

Hi all,

 

I have a drawing with several TEXTs and MTEXTs that contain a decimal number. I want to convert those numbers to integer and fractions. The fraction will be calculated based on the decimal part and rounded until the 16th denominator; this is:

0: only integer number

1/2, 1/4, 3/4, 1/8, 3/8, 5/8, 7/8, 1/16, 3/,16, 5/16, 7/16, 9/16, 11/16, 13/16, 15/16.

 

Let´s say, the number is 20.25, it should be convert it to 20-1/4   ;  10.49 should be 10-1/2...

 

I would like to have Lisp that can be launched. Then, I can click on every text, and the conversion will happen. I need to maintain the text style, color, height, letter type, rotation, etc.

 

I recently tried the below code from AI but no success, if you can help me I will apreciate it.

Thanks!!

(defun round (num)
  (if (< (rem num 1.0) 0.5)
      (fix num)
      (1+ (fix num))
  )
)

(defun c:DecToFrac ( / en ed txt num int frac inc)
  (setq en (car (entsel "\nSelect MText with decimal: ")))
  (if (and en (setq ed (entget en)))
    (progn
      (setq txt (cdr (assoc 1 ed)))
      (setq num (atof txt))
      (setq int (fix num))
      (setq frac (rem num 1.0))
      (setq inc (round (* frac 16))) ; Assuming 16ths of an inch for fractions
      (setq frac-text
            (cond
              ((= inc 0) "")
              ((= inc 16) (progn (setq int (1+ int)) ""))
              ((= inc 8) "1/2")
              ((= inc 4) "1/4")
              ((= inc 12) "3/4")
              ((= inc 2) "1/8")
              ((= inc 6) "3/8")
              ((= inc 10) "5/8")
              ((= inc 14) "7/8")
              ((= inc 1) "1/16")
              ((= inc 3) "3/16")
              ((= inc 5) "5/16")
              ((= inc 7) "7/16")
              ((= inc 9) "9/16")
              ((= inc 11) "11/16")
              ((= inc 13) "13/16")
              ((= inc 15) "15/16")
            )
      )
      (setq new-txt (strcat (itoa int) (if frac-text (strcat "-" frac-text) "")))
      (entmod (subst (cons 1 new-txt) (assoc 1 ed) ed))
    )
  )
  (princ)
)
0 Likes
Accepted solutions (1)
684 Views
11 Replies
Replies (11)
Message 2 of 12

komondormrex
Mentor
Mentor

hey,

maybe the following will do

 

 

(defun c:Dec_To_Frac ( / en ed txt num int frac inc)
  (setq en (car (entsel "\nSelect MText with decimal: ")))
  (if (and en (setq ed (entget en)))
    (progn
      (setq txt (cdr (assoc 1 ed)))
      (entmod (subst (cons 1 (vl-string-translate " " "-" (rtos (atof txt) 5 3))) (assoc 1 ed) ed))
    )
  )
  (princ)
)

 

updated

 

0 Likes
Message 3 of 12

jreidKVSUZ
Advocate
Advocate

I copied your AI lisp into a file (see attached lisp file) and tested what you needed.

I think it works perfectly. It changed the fractions to decimal and rounded them up.

No other changes happened to either Mtext or Text. Same layers, colors, etc.

But it would be nice if you could type the command and say ALL vs picking them individually.

Add this lisp file to your LISP APPLICATIONS. Do it both locations for DecToFrac to autoload.

Then all you must do is type DecToFrac and it will pop up and do the work for you.

 

Hope this helps!  Happy New Years!!!

JRR!

 

 

0 Likes
Message 4 of 12

calderg1000
Mentor
Mentor

Regards @jesusrperezd 

To recognize texts with decimal separator =",", change the following line of code.

(progn
      (setq txt(vl-string-subst "." "," (cdr (assoc 1 ed))));line of code to replace
 

 


Carlos Calderon G
EESignature
>Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

0 Likes
Message 5 of 12

john.uhden
Mentor
Mentor

@jesusrperezd ,

I think all it takes is

(defun @dec2frac (a)(strcat (itoa (fix a))"-"(rtos (- a (fix a)) 4 5))) ;; corrected - sixteenth

John F. Uhden

Message 6 of 12

john.uhden
Mentor
Mentor

Umm, I think this needs some more code...

(@dec2frac 40.99) -> 40-1  (should be 41, right?)

So try this out:

 

 

(defun @dec2frac (a / frac)
  (cond
    ((= (setq frac (rtos (- a (fix a)) 4 5)) "1\"")
      (itoa (1+ (fix a)))
    )
    ((= frac "0\"")
      (itoa (fix a))
    )
    (1 (strcat (itoa (fix a))"-" frac))
  )
)

 

 

Except that rtos in architectural format includes the inch "\"" suffix.

So, should all or none contain the inch suffix?

That's sort of an inglorious format you are creating. 😕

Won't regular architectural units work for you?

 

@john.uhden - this post has been edited due to Community Rules & Etiquette violation.

John F. Uhden

0 Likes
Message 7 of 12

Kent1Cooper
Consultant
Consultant
Accepted solution

@john.uhden wrote:

.... rtos in architectural format includes the inch "\"" suffix. ....


No need for Architectural format.  Besides, for their "20.25" example, Architectural format would return not 20 1/4", but 1'-8 1/4".  Fractional format does not include the inch mark, nor anything about feet.  See >this<.

@jesusrperezd , this is an example of why it's not advisable to post the same question more than once.

Kent Cooper, AIA
0 Likes
Message 8 of 12

jesusrperezd
Enthusiast
Enthusiast

Hi guys, first of all, thanks for your time and help. I'm sorry. I have been dealing with some issues here. I will check out your codes and information as soon as possible and then get back to you.

0 Likes
Message 9 of 12

jesusrperezd
Enthusiast
Enthusiast
I'm sorry to the double post. I don't know what happened but I wasn't able to find the initial post until now, I supposed it wasn't approved or I missed something, anyway is there a way to merge them or do something?. I will check it out both post today and test the code you guys have sent.
0 Likes
Message 10 of 12

john.uhden
Mentor
Mentor

@Kent1Cooper ,

If you had noticed in the code, I had converted the whole part of the number to an integer and then the fraction  using rtos...

Command: (rtos 0.7 4 5)
"11/16\""

Command: (@dec2frac 44.7)
"44-11/16\""

I just don't know from what galaxy his units are, leaving me unsure as to the use of the inch suffix.

If he does not want the inch symbol then I could have converted the decimal using:

(strcat (rtos (* 0.7 16) 2 0) "/16")

except that will result in values like

8/16 not 1/2

2/16 not 1/8

Or maybe he should just trim away the "\"".

 

John F. Uhden

0 Likes
Message 11 of 12

Kent1Cooper
Consultant
Consultant

@john.uhden wrote:

... I had converted the whole part of the number to an integer and then the fraction  using rtos...

I just don't know from what galaxy his units are....


It seems pretty clear that their desired units are Fractional -- no inch marks anywhere in their examples.  So as the link in Message 7 does, use 5 for the mode, not 4, which also means you don't need to separate the integer part from the decimal.  Have you followed that link yet?

Kent Cooper, AIA
0 Likes
Message 12 of 12

john.uhden
Mentor
Mentor

Thank you, @Kent1Cooper .  My apologies.

I had not recollected that there was a 5 mode.

John F. Uhden

0 Likes