Jump to content

Recommended Posts

Posted

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.

Posted (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 by GLAVCVS
  • Like 1
Posted

But I have to ask you: why do you need to do this with foreach?

Posted (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 by aridzv
Posted

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))))

 

Posted
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

 

Posted (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 by aridzv
Posted
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.

  • Like 1
  • Thanks 1
Posted
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. 

  • Thanks 2

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...