Autocad LISP slows down over the time

Autocad LISP slows down over the time

danko_mateRAGZK
Explorer Explorer
1,134 Views
16 Replies
Message 1 of 17

Autocad LISP slows down over the time

danko_mateRAGZK
Explorer
Explorer

Hi there!

 

Please find the LISP that i attached. My main problem is, that in Autocad 2022 the LISP starts its work with a greater speed and it slows down over the time as i was able to check its progress with circle drawing. The function of the LISP is the following: at first i select texts with the ssget command, and then polylines. The LISP should check what are the two closest polylines to a text (with some offset) and save the lengths of the polylines and content of the text in a table which i can export later. It works really slowly, 330 texts and 660 polylines takes 5 or more minutes. 

Could you please suggest a solution to make the process faster, or some any other tips?

 

Thank You!

 

Máté

0 Likes
1,135 Views
16 Replies
Replies (16)
Message 2 of 17

ВeekeeCZ
Consultant
Consultant

Post a sample dwg

0 Likes
Message 3 of 17

danko_mateRAGZK
Explorer
Explorer

Here you go. You should turn off all the layers except the one named Cable table, and if the LISP ask a question in Hungarian (Which transformer's cables do you want to measure?), type a number from 1-6. 

0 Likes
Message 4 of 17

ВeekeeCZ
Consultant
Consultant

Are you the author? 

 

This line has the wrong syntax:

(while /= i 0 

0 Likes
Message 5 of 17

danko_mateRAGZK
Explorer
Explorer

Yes i am. It worked for me, sorry, maybe you can try to change it to (repeat (sslength ss1)...)

0 Likes
Message 6 of 17

ВeekeeCZ
Consultant
Consultant

Well, the repeat is slightly more efficient, but it would not make much difference.

But as the author, you should be able to fix that syntax!

 

One of the issues causing slowness, is that you don't localize the variables. You are filling the memory with the massive amount of data and never release it. Localize all the variables.

 

(defun c:Test ( / colwidth cp1 cp2 curspace d1 d2 doc i i2 i3 l1 l1min l2 l2min line1 line2 list1 list2 list3 list4 min_d1 min_d2
numcolumns numrows objtable pt pt1 pt2 ptt r rowheight ss1 ss2 trafo trafo1 trafo2 txt x1 x2 y y1 y2)

...

 

0 Likes
Message 7 of 17

ВeekeeCZ
Consultant
Consultant

Another thing. Don't convert enames to vla-objects. 

First, vlax-curve-getClosestPointTo does not really need it - it works with enames too.

Second, if you need it, convert them all at once, save it, then work with them. This repeated conversion slows down the process massively.

 

vla-get-length needs vla-objects, but you can use (vlax-curve-getdistatparam (vlax-curve-getendparam...

0 Likes
Message 8 of 17

ВeekeeCZ
Consultant
Consultant

And explain how the loops work. It's not obvious to me.

0 Likes
Message 9 of 17

danko_mateRAGZK
Explorer
Explorer

I could never understand why is that localisation useful, I didn't know that global variables use that much memory, so thank you. 

Yes, i am the author, but i learnt this type of programming from ChatGPT, and from ready-made codes, by the way, the LISP was not indicating any warning message or error for me, or i was using an older version of it. (I use a LISP, named Starfile to load the Test.lsp and this command is on a shortcut key, so it could happened that i failed to load it, but i could run an older versioan of it.)

The loops' works are the following: In the outer while cycle the program inspects the texts, gets there middle coordinates, and offsets it in double directions with a constant value to get pt1 and pt2 points. From these point(s) it scans all of the polylines (in two inner cycles by the directions) and chooses the one, which is the closest to it, gets it's number from a list, and with this number gets it's length from an other list. After all we'll get the content of the texts, and 2 lengths of the closest polylines in 2 directions. 

0 Likes
Message 10 of 17

ВeekeeCZ
Consultant
Consultant

Ok then. So I guess you're able to incorporate the above suggestions by yourself.

 

If the performance does not improve enough, make a sketch of the described loops algorithm and I will try to rewrite them in a more efficient form.

 

BTW the correct syntax is (while (/= i 0) (...)). 

 

 

One more suggestion thought. Repeatedly turning levels off and on also slows things down. Especially if you use dimming mode. I would rather just use selection filtering applied to layers... http://www.lee-mac.com/ssget.html

0 Likes
Message 11 of 17

danko_mateRAGZK
Explorer
Explorer

Thanks for your help! I have to learn a lot more about it. 

Can these problems cause that when i start to run the LISP it works in a more efficient way? The code contains some commands to draw circles (but these lines are in comment), if you delete the comment sign, you can observe, how fast it starts the work and how slows it over the time. 

0 Likes
Message 12 of 17

ВeekeeCZ
Consultant
Consultant

All the above-noted issues slow things down. Fix one after another and see how the performance goes. 

0 Likes
Message 13 of 17

john.kaulB9QW2
Advocate
Advocate

@danko_mateRAGZK 

Your [ChatGTP's] coding style is horrible (for you)! Monolithic functions are not how you'd program in Lisp (or any language) because that style will quickly drive you crazy trying to keep all the pieces straight. You program small chunks of code that do very specific things and after you have all the pieces you assemble them. One style that is popular in Lisp is called "bottom up programming".  You can view a writeup I did on the subject here:

Bottom-up design (theswamp.org)

 

It may seem like a step back, but it would be FAR faster to read the first few chapters of SICP and learn how to program properly (instead of trying to decipher and learn at the same time). 

Structure and Interpretation of Computer Programs (mit.edu)

another swamper
0 Likes
Message 14 of 17

ВeekeeCZ
Consultant
Consultant

Here is a spoiler for you. Select both texts and plines at once. Recommend setting plwidth to 0 to actually see the selection highlight.

While sorting takes a few seconds, table creation takes tremendous time. 

 

And BTW, I don't think CahtGPT is a good way to learn coding. I would prefer a proven classical approach, from theoretical foundations to complex approaches. ChatGPT creates a nonsensical mess that you don't even understand. We can see this mess here quite often. Simply not there yet to be a useful feature.

 

Edit: Code updated with ronjonp's suggestion.

0 Likes
Message 15 of 17

ronjonp
Mentor
Mentor

@ВeekeeCZ wrote:

While sorting takes a few seconds, table creation takes tremendous time. 


@ВeekeeCZ This will fix the table generation speed:

ronjonp_0-1701734015045.png

 

Message 16 of 17

Sea-Haven
Mentor
Mentor

Just a comment, like Ronjonp I tend to use Insertrow as I read a selection set, and making like 200 rows took 5 minutes, setting regeneratetablesuppressed answer is like 3 seconds. Its probably same speed as making all rows up front. 

0 Likes
Message 17 of 17

ВeekeeCZ
Consultant
Consultant

Right, thank you for reminding me of this one. Tables are not my hobby.

Someone should also tell ChatGPT 🙂

0 Likes