Trying to write a lisp which finds text inside a p-line and elevates p-line to numerical value of text

Trying to write a lisp which finds text inside a p-line and elevates p-line to numerical value of text

Luke_billinghamMHLJT
Explorer Explorer
1,166 Views
8 Replies
Message 1 of 9

Trying to write a lisp which finds text inside a p-line and elevates p-line to numerical value of text

Luke_billinghamMHLJT
Explorer
Explorer

Hi I'm trying to write a LISP which finds text within a selected poly-line and then elevates the poly-line to the numerical value of the text. Ideally once I get this working I would like to get the code to create a selection set of all poly-lines within the drawing and then iterate this code over the set. Unfortunately I can't get this part working. It keeps returning and error due to too many arguments but I can't see where I'm oversupplying a function with arguments. If anyone could offer any assistance that would be much appreciated. Any questions let me know. Code attached.

 

 

0 Likes
Accepted solutions (1)
1,167 Views
8 Replies
Replies (8)
Message 2 of 9

neilyj666
Mentor
Mentor
Accepted solution

This is something similar that @tcorey wrote a few years ago - I am not a LISP guru so have no idea how it works but it does....

neilyj (No connection with Autodesk other than using the products in the real world)
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.

EESignature


AEC Collection 2026 UKIE (mainly Civil 3D UKIE and IW)
Win 11 Pro x64, 1Tb Primary SSD, 1Tb Secondary SSD
64Gb RAM Intel(R) Xeon(R) W-11855M CPU @ 3.2GHz
NVIDIA RTX A5000 16Gb, Dual 27" Monitor, Dell Inspiron 7760
0 Likes
Message 3 of 9

Luke_billinghamMHLJT
Explorer
Explorer

Thank you so much, so I have edited the code above so it seems to iterate over a whole drawing better but it still does not work perfectly. On some occasions it will draw the boundary for one text object around another if that makes sense? Can anybody highlight why?

Message 4 of 9

Sea-Haven
Mentor
Mentor

When things don't work the obvious is Post a dwg. Show problem areas.

0 Likes
Message 5 of 9

Luke_billinghamMHLJT
Explorer
Explorer

haha, you are quite correct I should have known better. Fortunately I seem to have fixed my error. It seems when performing large LISP operations AutoCAD stacks up certain types of functions rather than performing them sequentially which can sometimes lead to them being done in the wrong order. I seem to have accidentally fixed this by getting the script to write in the new elevation in the centre of the poly-line after each process. Not entirely sure how this fixes the issue but I would assume it creates some sort of break between each iteration in the loop.

 

Anyway, code is attached for future users, and anybody that would like to suggest any improvements.

0 Likes
Message 6 of 9

Moshe-A
Mentor
Mentor

@Luke_billinghamMHLJT  hi,

 

from what i see the error comes from (print-polyline-vertices) function on line #8 (see picture)

 

(setq z (nth (1+ i 2) coords))

 

the (1+ ...) function accepts only one argument. after you solve this you still get "bad argument type" and this comes from same loop. (vlax-get polyline-obj 'coordinates) return (x0 y0 x1 y1 x2 y2....) we are talking about a 2d pline (not 3d) and you assumed you have z?!

 

enjoy

Moshe

 

 

      ;; Function to print all vertices of the polyline
      (defun print-polyline-vertices (polyline-obj)
        (setq coords (vlax-get polyline-obj 'coordinates))
        (setq i 0)
        (while (< i (length coords))
          (setq x (nth i coords))
          (setq y (nth (1+ i) coords))
          (setq z (nth (1+ i 2) coords)) ; Z-coordinate, if needed
          (princ (strcat "\nVertex " (itoa (1+ (/ i 2))) ": " (rtos x 2 3) ", " (rtos y 2 3) ", " (rtos z 2 3)))
          (setq i (+ i 3))
        )
      )

 

 

 

 

0 Likes
Message 7 of 9

Sea-Haven
Mentor
Mentor

I do the looping through a selection set slightly different. Just my preference.

 

 

  (setq ls (ssget '((0 . "TEXT,MTEXT")))) ; Filter to select only TEXT and MTEXT objects
  (if (= ls nil)
(progn (alert "No text selected will exit now ")(exit))
)
(repeat (setq x (sslength ls))
(setq tobj (ssname ls (setq x (1- x))))

you do  not need
 

 

 

You do not need as the ssget has already filtered the text,mtext.

(if (or (= (cdr (assoc 0 (entget tobj))) "MTEXT")
              (= (cdr (assoc 0 (entget tobj))) "TEXT")) ; If it is a text or mtext object

 

0 Likes
Message 8 of 9

Sea-Haven
Mentor
Mentor

A LWPOLYLINE will only return X & Y when using get coordinates so the Z will fail, a 3dpoly PLOYLINE will return  X Y & Z.

2dpolyline
(setq lst (vlax-get obj 'coordinates))
(61.0 41.0 142.xxx-xxxxxxxx 147.121907389598 280.468502141567 87.5210295251177)
3dpoly 0,0,0 10,10,10 0,10,0
(0.0 0.0 0.0 10.0 10.0 10.0 0.0 10.0 0.0)
0 Likes
Message 9 of 9

Moshe-A
Mentor
Mentor

@Sea-Haven 

 

Thank you, thought i said that to @Luke_billinghamMHLJT 

 

Moshe

 

0 Likes