Incrementing variables in while loop

Incrementing variables in while loop

Anonymous
Not applicable
1,035 Views
7 Replies
Message 1 of 8

Incrementing variables in while loop

Anonymous
Not applicable

Hi Autodesk Community,

 

I am having an issue. I currently have 48 variables named this way:

 

var1 = AB-101

var2 = AB-102

var3 = AB-103

var4 = AB-104

etc.

 

What I want to do is to use them in a while loop to create single line text, like you can see below:

 

  (setq iteration 1)
  (setq YValue -27.75)
     (while (<= iteration 16)
        (command "_text" "J" "M" (strcat "-53.5," (rtos YValue)) "4" "0" var1 "")    
        (setq YValue (+ YValue -30.5))
     (setq iteration (1+ iteration)))

The thing is, with that loop, for each iteration, the given value is AB-101. I would like to have, for each iteration, the value of the next variable.

 

So, the first loop should use the variable var 1 and give: AB-101

The second loop should use the variable var 2 and give: AB-102

The third loop should use the variable var 3 and give: AB-103

 

And so on.

 

Any ideas?

 

Thanks a lot

0 Likes
Accepted solutions (1)
1,036 Views
7 Replies
Replies (7)
Message 2 of 8

Ranjit_Singh
Advisor
Advisor

Try replacing

(command "_text" "J" "M" (strcat "-53.5," (rtos YValue)) "4" "0" var1 "")

with

 

(command "_text" "J" "M" (strcat "-53.5," (rtos yvalue)) "4" "0" (eval (read (strcat "var" (itoa iteration)))))

It will be beneficial if you posted the entire code. Is this the same problem as presented here? If so, I think the approach is unnecessarily complicated. Unless there is a specific need to copy all values to variables, you can simply read values from table and create text. Don't need variables.

 

0 Likes
Message 3 of 8

john.uhden
Mentor
Mentor

I pretty uch agree with @Ranjit_Singh.  Your while loop continually references specifically var1.

I suggest that rather than create a bundle of variables that you use a list of values, as in...

 

(setq vars (list "AB-101" "AB-102" "AB-103" etc.))

 

Then...

 

(foreach var vars
(command "_text" "J" "M" (strcat "-53.5," (rtos YValue)) "4" "0" var "")

 Do you really want separate text entities?  You could entmake or vla-add  one (1) Mtext entity instead and not need to keep adjusting YValue.

John F. Uhden

0 Likes
Message 4 of 8

Anonymous
Not applicable

Hi Ranjit,

 

When i do the replacement, it doesn't work. It says (above the command tab):

 

Specify other corner point or [Area/Dimensions/Rotation]: -50,-496.25
Command: _text
Current text style: "Standard" Text height: 4.0000 Annotative: No
Specify start point of text or [Justify/Style]: J Enter an option
[Align/Fit/Center/Middle/Right/TL/TC/TR/ML/MC/MR/BL/BC/BR]: M
Specify middle point of text: ; error: bad argument type: numberp: nil

 

Yes, It is related with my other post. The entire code makes 136 lines right now and keeps growing, so I don't think it could be beneficial to post it here. I think it may bring more confusion than clarification.

 

And ok now I follow you. You are saying there is an easy way to put the values of the cells 1 to 16 in the command line instead of var1, var2, var3, etc.?

 

If so, that could help me.

 

0 Likes
Message 5 of 8

Anonymous
Not applicable

Hi John,

 

The thing is, my table has three columns and sixteen lines, for a total of 48 values. The way it is meant to be used, my code needs to adapt to the quantity of values the user enter. He/she could simply enter 12 values (4 per column) for example. Lets say, for this situation, the user enters 48 values.

 

Then, I need to create a loop that write the text contained in the first column (16 values) to a specific place with a constant distance interval (30.5).

 

Then, I need to do this again with the second and third column.

 

Is the way I mean to use my code clearer now?

 

Thanks

 

 

 

 

0 Likes
Message 6 of 8

Ranjit_Singh
Advisor
Advisor
Accepted solution

@Anonymous wrote:

.................
[Align/Fit/Center/Middle/Right/TL/TC/TR/ML/MC/MR/BL/BC/BR]: M
Specify middle point of text: ; error: bad argument type: numberp: nil

................


Here is a sample code with 3 var values I setup and so it only iterates 3 times. As you can see it works. Compare to your existing code and identify what is different.

(defun c:somefunc  (/ iteration yvalue)
  (setq iteration 1)
  (setq yvalue -27.75)
  (while (<= iteration 3)
    (command "_text" "J" "M" (strcat "-53.5," (rtos yvalue)) "4" "0" (eval (read (strcat "var" (itoa iteration)))))
    (setq yvalue (+ yvalue -30.5)
          iteration (1+ iteration)))
  (princ))

sample_var_values.gifYou will need to share the entire code and the final goal to identify the best approach. If the code is too big then attach the .LSP file and don't paste the code.

 

0 Likes
Message 7 of 8

john.uhden
Mentor
Mentor

Yes, that is clearer.  My old 2002 does not allow for columns in Mtext, but newer versions do.  You seem to like plain text anyway.

I still recommend a list of values.

Lets say that the list (vars) has 22 items, and you want it divided into 3 columns.

(setq rows (/ (length vars) 3.0))  ; which results in 7.33 rows, which is > 7 by 1/3,

so the 1st column will be 8 rows, and the remaining columns will be 7 rows each.

That will guide you to the X,Y position for each piece of text.

 

So, you can see that the value of the list is that it not only contains all your text strings, but it has a length that can be used to determine the row count.

John F. Uhden

0 Likes
Message 8 of 8

Anonymous
Not applicable

 

I tried Ranjit's code and it works just fine in my program!

 

Thanks a lot to both of you guys, you are awesome!

 

Keep up the good work 🙂 

 

0 Likes