- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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) )
Solved! Go to Solution.