Thread: Need LISP Function to read text contents and convert ratio to number.

1. Need LISP Function to read text contents and convert ratio to number.

Registered forum members do not see this ad.

I have a vector map of a floor and all i am given are vectors and grade ratios (i.e. 1:155.095, 1:97.561) of the floor. The problem is that I only want to look at the areas of the floor that are below a certain grade ratio (1:69), but they are ratios set as Text Contents so AutoCAD doesnt understand that these ratios are numbers.

I am looking for a Lisp function that, for each text if a ratio across an entire floor (or the layout), copy the text (which is a ratio), find out the number that the ratio is (i.e. take the 1 and divide by 69), then replace the text with just the number (0.0145). I want this to do this for every instance, and there are over 37,000 of these texts on the floor in question.

I have attached an image that depicts what I have in the layout.

Does anyone know of a lisp function that can do this?

2. Quickly written:
Code:
```(defun c:fixratios ( / a b e i p s x )
(if (setq s (ssget "_:L" '((0 . "TEXT")(1 . "#*:#*")(1 . "~*[~.:0-9]*")(1 . "~*:*:*"))))
(repeat (setq i (sslength s))
(setq e (entget (ssname s (setq i (1- i))))
x (cdr (assoc 1 e))
p (vl-string-position 58 x)
)
(if (and (setq a (distof (substr x 1 p) 2))
(setq b (distof (substr x (+ 2 p)) 2))
(not (equal 0.0 b 1e-8))
)
(entmod (subst (cons 1 (rtos (/ a b))) (assoc 1 e) e))
)
)
)
(princ)
)```

3. Hi Lee needs a second part say a copy of the text onto a different layer where the result is < 1/69 0.0144927536 this is part of the request, maybe a seperate defun this way the code re grade remains intact.

had a little bit of time.

Code:
```; original code by Lee-mac Aug 2017
; changed to only modify 1:x below 1:69
; by Alan H Aug 2017

(defun c:fixratios ( / a b e i p s x ab)
(if (/= (tblsearch "Layer" "Xgrade") nil)
)

(if (setq s (ssget "_:L" '((0 . "TEXT")(1 . "#*:#*")(1 . "~*[~.:0-9]*")(1 . "~*:*:*"))))
(repeat (setq i (sslength s))
(setq e (vlax-ename->vla-object (ssname s (setq i (1- i)))))
(setq x (vla-get-textstring e))
(setq p (vl-string-position 58 x))
(if (and
(setq a (distof (substr x 1 p) 2))
(setq b (distof (substr x (+ 2 p)) 2))
(not (equal 0.0 b 1e-8))
(< (setq ab (/ a b)) (/ 1.0 69.0))
)
(progn
(vla-put-textstring  e (rtos ab 2 3))
)
)
)
)
(princ)
)```

4. Hi lee , nice lisp.

Could you explain

Code:
`(setq s (ssget "_:L" '((0 . "TEXT")(1 . "#*:#*")(1 . "~*[~.:0-9]*")(1 . "~*:*:*"))))`

Also it can be add the 8 dxf code to TABLE , as it is the text's layer.

5. Registered forum members do not see this ad.

Originally Posted by devitg
Could you explain
Code:
`(setq s (ssget "_:L" '((0 . "TEXT")(1 . "#*:#*")(1 . "~*[~.:0-9]*")(1 . "~*:*:*"))))`
(1 . "#*:#*")
Matches text starting with a number and containing a colon followed by a number, e.g. "1abc:2def"

(1 . "~*[~.:0-9]*")
Matches text which only contains the characters ".:0123456789", e.g. "1.23:4.56:7.89"

(1 . "~*:*:*")
Ensures that the string only contains one colon.

This doesn't account for all cases, as you could perform additional validation on the decimal point (ensuring it appears at most once on either side of the colon).

The layer filter could also be included, but it would reduce the ability for other members to easily use the code.