FazBear Posted May 22, 2009 Posted May 22, 2009 Hi All Can anyone tell me a little bit about the basics of the foreach function. I am just playing about to see if i can get it to work but it's not doing what i would expect. i have crated a new drawing and drawn a circle in modelspace, i switched to paperspace and created a vp and zoomed extentents then copied the vp 5 or 6 times. i put the circle on a new layer frozen in all view ports then tried the following: (defun C:VPT () (setq VPLIST (ssget "x" '((0 . "viewport")(8 . "0")))) (foreach VPLIST (command "mspace") (command "vplayer" "t" "**" "c" "") (command "pspace" "zoom" "e") ); end foreach ); end defun but it only thawed the layer in the last vp i entered! Anyone please advise? Quote
Freerefill Posted May 22, 2009 Posted May 22, 2009 Foreach is a lot of fun. I can see the problem you had right away, it's a syntax error. You've got the right idea, though. The first thing I'll tell you is, look through the Virtual LISP help files in AutoCAD. Knowing that they're there saved me hours upon hours of endless searching through google and banging my head against the proverbial wall. But, since I'm at it already, here's how Foreach works: (foreach var list commands) Which means, for every item in list, set it equal to var and then issue a set of commands. Let me try to put an analogy together and show you your problem at the same time. (setq num 1) (foreach var (list 1 1 1 1 1) (setq num (+ 1 num)) ) The "var" in this case is just "var", and the list in this case is (1 1 1 1 1). You can see what I'm doing for a command, I'm incrementing "num". So you may be asking, why do I have "var" if I'm doing nothing with it? Well that's because I don't -have- to. However, if I wasn't going to do anything with it, couldn't I just: (setq num 1) (repeat 5 (setq num (+ 1 num) ) That's where the power of Foreach comes in: Foreach sets whatever variable you choose equal to each item in the list, and you can then pass that variable to the commands, like so: (setq num 1) (foreach var (list 1 1 1 1 1) (setq num (+ var num) ) I hope this clears things up, if only a little. ^.^ Quote
Freerefill Posted May 22, 2009 Posted May 22, 2009 Oh, also, I should point out that (ssget) does NOT return a List, and Foreach ONLY works with a list. To apply changes to everything in a selection set, there are VLA functions that'll do the job, or you'd have to use something like: (setq ss (ssget)) (setq count 0) (repeat (sslength ss) (setq ent (ssname ss count)) ; Do stuff with ent ) If ever your code doesn't work, try testing your variables with the (type var) function to see what type of variable you're working with. Many functions will only accept a certain type. Quote
Lee Mac Posted May 22, 2009 Posted May 22, 2009 Great explanation Freerefill if you wanted to continue with using "foreach" on the selection set, and not use the "repeat" method (may have trouble with sel sets more than 32767 ents)... then this approach may work for you: (foreach ent (mapcar 'cadr (ssnamex VPLIST)) ... do something ) ; end foreach Quote
Commandobill Posted May 22, 2009 Posted May 22, 2009 if you were just trying to turn on/ THAW all the layers in all viewports why didnt you just (command "vplayer" "t" "**" "a" "") Quote
FazBear Posted May 22, 2009 Author Posted May 22, 2009 Thank all for your replies I had no idea foreach required a list to operate correctly, ill have a play with your suggestions Thanks again Commandobill, yes that would do exactly the same but im using the above as something simple so i can learn about foreach. that is my goal. Thanks again Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.