Jump to content
cupax

Random Select objects from selected group lisp.

Recommended Posts

cupax

Hello,

 

I need a lisp that would randomly select a certain percentage of objects from an user pre-selected group. We are creating shutters, that are randomly perforated - don't want to manually randomly delete hundreds of small circles.

 

I've found a lisp from David Bethel (in this forum's archive: http://www.cadtutor.net/forum/archive/index.php/t-52841.html) which works fine, but it would select between all objects in the drawing, even on frozen layers.

The thread is very old so I don't know if Mr. Bethel is still active on this forum.

 

How would I change this lisp, so that it would first promp me to select objects from which it would then randomly select a certain percentage?

 

Thanks,

David

Share this post


Link to post
Share on other sites
Jef!

Hi there.

 

I think David is still active, hoping he wont mind.

 

Here'S what you need. Using the implied "_I" filter instead of "x".

(good example of wanting to highlight a selection set too...)

 

(defun c:randset (/ ss sl i en el ep pct qty rs)

;;;SMadsen Random Number
(defun randnum (/ modulus multiplier increment random)
 (if (not seed)
     (setq seed (getvar "DATE")))
 (setq modulus    65536
    multiplier    25173
     increment    13849
       seed       (rem (+ (* multiplier seed) increment) modulus)
       random     (/ seed modulus)))

 (cond ((setq ss (ssget "i"))
        (sssetfirst nil nil)
        (setq sl (sslength ss)
               i -1)
        (while (setq en (ssname ss (setq i (1+ i))))
               (setq el (cons en el))))
       (T (alert "\nNo Entities Found")
          (exit)))

 (initget 7)
 (setq pct (getreal "\nPercentage To Randomly Choose:   "))
 (setq qty (fix (* sl pct 0.01)))

 (setq rs (ssadd))
 (while (> qty (sslength rs))
    (setq ep (fix (* sl (randnum)))
          en (nth ep el))
    (if (not (ssmemb en rs))
        (ssadd en rs)))
 
 (sssetfirst nil rs)
 (princ)
)

 

That will form the selection you need.

Cheers

  • Like 1

Share this post


Link to post
Share on other sites
enthralled
On 5/2/2017 at 7:15 PM, Jef! said:

Hi there.

 

I think David is still active, hoping he wont mind.

 

Here'S what you need. Using the implied "_I" filter instead of "x".

(good example of wanting to highlight a selection set too...)

 

 


(defun c:randset (/ ss sl i en el ep pct qty rs)

;;;SMadsen Random Number
(defun randnum (/ modulus multiplier increment random)
 (if (not seed)
     (setq seed (getvar "DATE")))
 (setq modulus    65536
    multiplier    25173
     increment    13849
       seed       (rem (+ (* multiplier seed) increment) modulus)
       random     (/ seed modulus)))

 (cond ((setq ss (ssget "i"))
        (sssetfirst nil nil)
        (setq sl (sslength ss)
               i -1)
        (while (setq en (ssname ss (setq i (1+ i))))
               (setq el (cons en el))))
       (T (alert "\nNo Entities Found")
          (exit)))

 (initget 7)
 (setq pct (getreal "\nPercentage To Randomly Choose:   "))
 (setq qty (fix (* sl pct 0.01)))

 (setq rs (ssadd))
 (while (> qty (sslength rs))
    (setq ep (fix (* sl (randnum)))
          en (nth ep el))
    (if (not (ssmemb en rs))
        (ssadd en rs)))
 
 (sssetfirst nil rs)
 (princ)
)
 

 

 

That will form the selection you need.

Cheers

 

Can this be modified to ask for number of objects to be selected, instead of percentage?

Share this post


Link to post
Share on other sites
Emmanuel Delay
Posted (edited)

>> Can this be modified to ask for number of objects to be selected, instead of percentage?

 

Instead of this:

 

 (setq pct (getreal "\nPercentage To Randomly Choose:   "))
 (setq qty (fix (* sl pct 0.01)))

 

do this:


(setq qty (geting "\nNumber of items to be selected: "))

 

Edited by Emmanuel Delay
  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×