; error: function undefined for argument: -6.77774
I think that particular message means that the number is not something a function can work with, though of the right "class" of input [i.e. not like the error you get if, for example, you try to feed a text string to a numerical function]. Here, it has to be the (sqrt) function -- not being built to work with imaginary numbers, it can't find the square root of a negative number. It takes the distance in XYZ terms from the Text insertion point to the closest point on the Polyline, which is the hypotenuse of a right triangle, and squares it, then squares the numerical equivalent of the text content, being [it thinks] the vertical leg of the triangle, and subtracts that from the hypotenuse squared, to get the square of Pythagoras's horizontal base leg of the triangle. It then takes the square root of that to get the length of that base leg, i.e. the distance in XY terms only of the Text from the Polyline.
....
(sqrt ; distance in XY only
(- ;; to get the horizontal base leg of the right triangle, subtract:
(expt
(distance ; in 3D ;; length of the hypotenuse
(setq ins (cdr (assoc 10 (entget mt))))
(vlax-curve-getClosestPointTo path ins)
); distance
2 ;; = squared
); expt
(expt (distof (cdr (assoc 1 (entget mt)))) 2) ;; minus the squared vertical leg
); -
); sqrt
....
So what I think is happening is that in some instance, the square of the vertical leg is larger than the square of the hypotenuse! How can that be, you ask? The text contents are rounded to two decimal places, but they actually mostly have more. If one is close enough to the path in XY terms [so the vertical leg is very close to the length of the hypotenuse], and its rounding went up from the true distance by enough, that could send its squared value over that of the hypotenuse.
The code was originally written to convert the text content to a number, because at the time I didn't realize the Text/Mtext objects were actually at their elevations, not at the elevation of the Polyline. SO: Instead, let's use their actual un-rounded vertical position:
(defun C:Within3 (/ mss path n mt ins)
(setq
mss (ssget "_X" '((0 . "TEXT")))
path (ssname (ssget "_X" '((0 . "LWPOLYLINE"))) 0)
within3 (ssadd); initially empty
); setq
(repeat (setq n (sslength Mss))
(setq mt (ssname mss (setq n (1- n))))
(if
(<
(sqrt ; distance in XY only
(-
(expt
(distance ; in 3D
(setq ins (cdr (assoc 10 (entget mt))))
(vlax-curve-getClosestPointTo path ins)
); distance
2
); expt
;; (expt (distof (cdr (assoc 1 (entget mt)))) 2); converted text content
(expt (caddr ins) 2); actual elevation, not text content
); -
); sqrt
3
); <
(ssadd mt within3); then
); if
); repeat
(sssetfirst nil within3); select/grip/highlight
(princ)
); defun
That worked for me in your drawing.
Kent Cooper, AIA