Visual LISP, AutoLISP and General Customization
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

Message 1 of 9
433 Views, 8 Replies

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

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.



Tags (1)
Labels (2)
Message 2 of 9

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.


AEC Collection 2024 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
Message 3 of 9

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

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

Message 5 of 9

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.

Message 6 of 9

@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?!






      ;; 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))





Message 7 of 9

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


Message 8 of 9
in reply to: Moshe-A

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

(setq lst (vlax-get obj 'coordinates))
(61.0 41.0 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)
Message 9 of 9
in reply to: Sea-Haven



Thank you, thought i said that to @Luke_billinghamMHLJT 




Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators

Autodesk Design & Make Report