Infrequent errors around the "REPEAT" function

I'm seeing odd behavior with this section of code. This routine works 90% of the time, but for some reason, I get infrequent errors. If I close brisscad and reopen it, the routine works again, all things the same. This is the code that it gets hung up on. It basically checks each entity in a SS to see if it's a line or not, removes it if it's not a line. This is part of a larger function, but this is the basically the first step.


(SETQ SS (SSGET))
(SETQ LEN (SSLENGTH SS))
(REPEAT LEN
(SETQ LEN (1- LEN))
(SETQ ENT (SSNAME SS LEN))
(IF (/= "LINE" (CDR (ASSOC '0 (ENTGET ENT))))(SSDEL ENT SS))
)


; ----- Error around expression -----
; (REPEAT LEN (SETQ LEN (1- LEN)) (SETQ ENT (SSNAME SS LEN)) (IF (/= "LINE" (VLE-ENTGETOLD '0 ENT)) (SSDEL ENT SS)))
; in file :
; C:\BriscCAD Files\Autolisp\bearings-L100-Stacked.lsp
;
; error : no function definition [REPEAT] ; expected FUNCTION at [eval]

Comments

  • Huh ... now that I look at it, the error message is changing the code to (VLE-ENTGETOLD '0 ENT).... what's that about?
  • If you want to have a selection set of LINES then you can use only one line of code with a filter list
    (SETQ SS (SSGET '((0 . "LINE")) ))
    You don't need to loop through a selection set...
    In your code you change the length of the selection set while looping through it and
    it can be that you delete entities at wrong or non existing indexes !
    It can also be that you get a situation of (REPEAT 0 .....) because LEN reaches 0 although
    the selection set still has entities....
    If for any reason you want to loop through the SS and select line only
    then just loop using a counter variable and create a new selection set adding every LINE entity from SS

    (SETQ SS (SSGET) SS2 (SSADD) )
    (SETQ LEN (SSLENGTH SS) counter LEN )
    (REPEAT LEN
    (SETQ counter (1- counter))
    (SETQ ENT (SSNAME SS counter))
    (IF (= "LINE" (CDR (ASSOC 0 (ENTGET ENT)))) (SSADD ENT SS2))
    )

    SS2 contains only lines and can be used in your routine....
  • Thanks, I'm going to give this a try, and get away from the "repeat" command there, if I can.

    (SETQ SS (SSGET '((0 . "LINE")) ))

    FWIW, I didn't write, this is my modified version of the "Bearings" Lisp that been floating around the internet forever. I'd think the way they have it set up works, because it's reading through the SS backwards, so any lines deleted would come off the end of the SS, and then move to the previous, and shouldn't mess up the count ... so it's been a head scratcher for me ... This shows the original "Bearings.lsp".

    https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/bearing-and-distance-lisp-modification/td-p/9373785
  • I looked at it and thought deleting an item was a problem, you can use SSADD to add an item to another selection set.

    I use this most times (SETQ ENT (SSNAME SS (setq counter (1- counter))))
  • I think I have it figured out... and it's almost comical. First, let me say, I didn't write it, I work in a Land Surveying office where various draftsmen over the years have written a couple hundred LISPs for automating processes. It's actually a very nice collection that I get to use. Anyway, someone used "repeat" as a variable in a different LISP that was only used occassionally. I ran everything they had through "VLIDE" this weekend and that got flagged, along with a couple of other things. So I think that explains why it was an infrequent error and restarting Brisccad fixed it every time.

    But thanks for the help. I actually "fixed" the original problem here by using the code Sakko recommended and changing the second "repeat" loop to a while loop...