Announcements
Due to scheduled maintenance, the Autodesk Community will be inaccessible from 10:00PM PDT on Oct 16th for approximately 1 hour. We appreciate your patience during this time.
Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Incrementing variables in while loop

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
Anonymous
1588 Views, 3 Replies

Incrementing variables in while loop

Hi peoples,

 

I used to be pretty good at autolisp and am now needing to get back into it as I am developing CAD standards for a different company. I'm trying to standardize our Layouts, but I am starting with just a part of it, namely copying attributes from the old title block to the new.

 

I've got the CopyAttr.lsp figured out. But I am trying to grab all Title Blocks (only one per dwg) and all the RevBlocks, and there can be many.

 

I'm missing something simple I fear. Here's what I have so far:

 

  (defun c:uptb ( / ssoldtbrbs sslen i ssold_ent_0)
    (setq ssOldTbRbs (ssget "x" '((-4 . "<OR")
                                  ( 2 . "TB_B_L")
                                  ( 2 . "RevBlock")
                                  (-4 . "OR>")
                                 )
                      )
    )
    (setq ssLen (sslength ssOldTbRbs))
    (setq i 0)
;      (princ i) (princ)
    (while (i <= ssLen)
      (setq i (1+ i))
      (princ i) (princ)
      (set (read (strcat "ssOld_ent_" (itoa i))
;      (setq (strcat "ssOld_ent_" (itoa i))
            (ssname ssOldTbRbs i)
           )
        (princ (strcat "ssOld_ent_" (itoa i))) (princ)
        (princ i) (princ)
      )
;      (setq i 0)
;      (princ i) (princ)
    ) ; end while
) ; end defun

 

It seems to set the first block name in the selection set, but the while loop seems to be bombing. Any thought?

 

Thanks, Bill~

 

 

3 REPLIES 3
Message 2 of 4
john.uhden
in reply to: Anonymous

The first problem I see is that you've forgotten that the operator comes first, as it is a function.

 

So that...

(while (i <= ssLen)

should be...

(while (<= i sslen)

which should actually be...

(while (< i sslen)

because items in a selection set are indexed from 0 to (1- sslen)

BTW, you need not use "<OR" in your filtering.  You can use this as well, with names separated by commas...

 

(ssget "x" '((2 . "TB_B_L,RevBlock")))

But since the DXF 2 code can be used for things other than block references, I would advise...

(ssget "x" '((0 . "INSERT")(2 . "TB_B_L,RevBlock")))

That's only a cursory review.  I'm sure that the smarter ones around here will offer more sage advice.

 

John F. Uhden

Message 3 of 4
hgasty1001
in reply to: Anonymous

Hi,

 

1.- The while it's bombing because "i" it's not a function and "<=" it's not a variable, so this (i <= ssLen) will not be interpreted as it's not a valid lisp expression, it should be: (< i ssLen) as lisp it's a prefix notation language (the operator/function it's at first position ever)

 

2.-You are updating the control variable of the loop "i" at the first line of the loop (setq i (1+ i)), so the iteration will start at the second item (ssname) of the selection set as they are indexed starting at 0 (0 based index "array"). You should update (increment) the control variable at the end of the loop.

 

3.-You are resetting the control variable "i" to 0 at the end of the loop, so the loop will never reach the termination condition (i will ever be 0 at condition evaluation). If you need for a reason to reset the variable, it must be done after the loop end, i.e after the closing parenthesis of the while loop.

 

4.- The (princ) statement should be the last statement of the function if you do not want a nice nil (or the last value evaluated) printed at end of the execution.

 

Gaston Nunez

 

 

 

 

Message 4 of 4
Anonymous
in reply to: hgasty1001

Hello John & Gasty,

 

Thank you both so much for your replies. <face-palm> I knew that the operator should be first, but I was blind to it. I'll blame that silly mistake on my rusty skill set! Ha...

 

And yes, filtering by name might be better and more intuitive. I got in the habit of listing out the ssget filters to help me learn and memorize the group codes because they really stand out when they are in a column like that. And I like John's suggestion to add the 2."Insert" to the filter, good call.

 

And I also know about the 0 indexing, but thank you for pointing it out. I also committed one of the well known mistakes of coding, don't work on it for too long at any given session, because any blind spot you may have missed tends to blend in and you need to take breaks from a nagging problem. In my desperation I had seen some code which incremented the loop counter by one for the variable name, so it's more intuitive that the first block is the title block and the next 3 are revision blocks. But that should only happen temporarily for the var name, silly me. That's nice for labeling, but I considered that to be on the finishing touches phase. For what it's worth, I had the counter incrementing at the bottom of the while loop for most of my struggle.

 

I was also trying to print the variables for troubleshooting purposes.

 

I'm not at work today so I'll try it when I get back on Monday. This is embarassing, but I was even testing the i agains the ssLen on the cammand line and using it properly! Ha ha ha... Long week.

 

I'm pretty sure this will get me past the wall I was hitting and I'll mark the solution next week. I might post the final code but it's pretty clear what went wrong to me.

 

Thanks so much guys!

 

Cheers,

Bill~

 

 

Thanks guys

 

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

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report