aridzv Posted May 10 Posted May 10 Hi. what is rhigt syntax to use foreach to loop through a selection set created by ssget? I know how to do it with while but can't get it to work with foreach. when I use foreach the code skips the foreach loop all together and don't raise any errors. here is the code with foreach: (defun c:test (/ minln obj ly sspln i plx) (vl-load-com) (setq minln (getreal "\nLength and below to delete:")) (if (setq sspln (ssget (list (cons 0 "*POLYLINE")))) (progn (setq i (1- (sslength sspln))) (foreach ent sspln (setq ent (vlax-ename->vla-object (ssname sspln i))) (if (<= (vla-get-length ent) minln) (vla-delete ent) ) (setq i (1- i)) );foreach );progn );if (princ) );defun What is the correct way to use foreach here? thanks, aridzv. Quote
GLAVCVS Posted May 10 Posted May 10 (edited) Hi It's not possible to use foreach directly on an sset You'll need to convert the sset to a list first. For example: (vl-remove-if 'listp (mapcar 'cadr (ssnamex sset))) Edited May 10 by GLAVCVS 1 Quote
GLAVCVS Posted May 10 Posted May 10 But I have to ask you: why do you need to do this with foreach? Quote
aridzv Posted May 10 Author Posted May 10 (edited) 26 minutes ago, GLAVCVS said: But I have to ask you: why do you need to do this with foreach? The function logic seems to be the most appropriate for the task, but I remembered that it was a problem. I wanted to know if I was missing something... Edited May 10 by aridzv Quote
BIGAL Posted May 11 Posted May 11 Use Repeat for a selection set. ;This works from last to first (repeat (setq x (sslengh ss)) (setq ent (ssname ss (setq x (1- x)))) ;For 1st to last (setq x -1) (repeat (sslength ss) (setq ent (ssname ss (setq x (1+ x)))) Quote
aridzv Posted May 11 Author Posted May 11 4 hours ago, BIGAL said: Use Repeat for a selection set. (repeat (sslength ss) a little cleaner then while... (defun c:delpolybylength (/ minln obj ly sspln i plx) (vl-load-com) (setq minln (getreal "\nLength and below to delete:")) (setq obj (entget (car (entsel "\nSelect Layer Object")))) (setq ly (cdr (assoc 8 obj))) (if (setq sspln (ssget (list (cons 0 "*POLYLINE") (cons 8 ly)))) (progn (setq i -1) (repeat (sslength sspln) (setq plx (vlax-ename->vla-object (ssname sspln (setq i (1+ i))))) (if (<= (vla-get-length plx) minln) (vla-delete plx) ) );repeat );progn );if (princ) );defun Quote
Lee Mac Posted May 11 Posted May 11 In this tutorial I describe several methods for iterating over a selection set. 1 Quote
aridzv Posted May 11 Author Posted May 11 (edited) 43 minutes ago, Lee Mac said: In this tutorial I describe several methods for iterating over a selection set. yes, I read your article. that is why I didn't went for the ssnamex... So, Isn't there a relatively straightforward way to use foreach with selection set? Edited May 11 by aridzv Quote
Lee Mac Posted May 12 Posted May 12 8 hours ago, aridzv said: Isn't there a relatively straightforward way to use foreach with selection set? If there was, I would have included it in my list of examples. 1 1 Quote
Tharwat Posted May 12 Posted May 12 19 hours ago, aridzv said: yes, I read your article. that is why I didn't went for the ssnamex... So, Isn't there a relatively straightforward way to use foreach with selection set? This method is used quite a lot by @ronjonp so search in this forum then you should find many elegant and decent routines by him as I personally like reading his. 2 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.