Author Topic: Randomize help.  (Read 2922 times)

0 Members and 1 Guest are viewing this topic.

sdp777

  • Guest
Randomize help.
« on: July 03, 2006, 10:50:03 PM »
I have been doing a couple of site plans here recently and realized the boringness of the same block repeated over and over.  So I thought about generating a random rotation and scale and apply to all the blocks of the same name selected in the drawing.

My randomize function really blows however.  If anyone has a better method, Im game.

Code: [Select]
;|

Randomize All Blocks
Version 1.0

Written by:  Steven Papke
Written on:  1/26/06

This routine asks the user to select a block.
With the block name extracted, the user then
inputs a number as a percentage to randomize
and the routine will randomize scale and rotation
of all blocks of the same name.  This is ideal
for use in site plans.

|;

(defun *error* (msg)
  (princ "\n")
  (setvar "cmdecho" 1)
  (setvar "menuecho" 1)
  (setvar "osmode" 15359)
  (setvar "polarmode" 2)
  (setvar "dynmode" 3)
  (setvar "autosnap" 63)
  (setvar "attreq" 1)
  (menucmd "s=s")
  (princ "error: ")
  (princ msg)
  (princ)
)


(defun randomize ()
;  (setq rn (+ 1(* randm (getvar "tdusrtimer"))))
  (if (not seed)
    (setq seed (getvar "DATE"))
  )
  (setq modulus    65536
        multiplier 25173
        increment  13849
        seed  (rem (+ (* multiplier seed) increment) modulus)
        random     (/ seed modulus)
  )
)
)

(defun modifyblk ()
(randomize)
(setq rotang (+ 15(* randm random)))
(setq sclfctr (/ random randm))
        (print rn)
(print rotang)
(print sclfctr)
(command "ROTATE" (ssname ss count) "" sinspt rotang) ; Rotate blocks
(command "SCALE" (ssname ss count) "" sinspt sclfctr) ; Scale blocks
)

(defun c:rnd ()
  (setq osm (getvar "osmode"))
  (setq orm (getvar "orthomode"))
  (setq plm (getvar "polarmode"))
  (setvar "menuecho" 1)
  (setvar "cmdecho" 0)
  (setvar "orthomode" 0)
  (setvar "attdia" 0)
  (setvar "attreq" 0)
  (prompt "\nSelect source block: ") ; Select Source Block
  (setq rndmbkpic (entsel))
  (setq rndmbkpicmod (car rndmbkpic))
  (setq rndmbknam (cdr (assoc 2 (entget rndmbkpicmod)))) ; Find out blockname
  (initget 1)
  (setq randm (getreal "\nAmount of Randomization (1 little-10 alot): ")) ; Set Randomization Factor
  (setq count 0)
  (prompt "\nSelect block(s) to randomize: ")
  (setq ss (ssget)) ; Select Objects
  (if ss ; If blocks are selected...
    (progn
      (setq count 0) ; Count set to 0
      (setq chgblk 0) ; Changed Blocks set to 0
      (setq ssl (sslength ss))
(princ "\nIsolating Block ")
(princ rndmbknam)
(princ " from selection set.")
(princ "\n")
  (setvar "cmdecho" 0)
      (while (< count ssl) ; Loop
(setq blk (entget (ssname ss count))) ; Get Selected Block in set
        (setq sinspt (cdr (assoc 10 blk))) ; Get Insertion Point of Selected Block
        (setq sblk (cdr (assoc 2 blk))) ; Read Selected Block Name       
(if (= rndmbknam sblk) ; Check if Block Name matches
      (progn
(if (/= (substr (cdr (assoc 2 blk)) 1 8) "*")
  (modifyblk)
)
(setq chgblk (+ 1 chgblk))
      )
)
      (setq count (+ 1 count))
      )
    )
  ) 
  (princ ssl) ; Print Statistics
  (princ " objects found.")
  (princ "\n")
  (princ chgblk)
  (princ " blocks randomized.")
  (setvar "cmdecho" 1)
  (setvar "menuecho" 1)
  (setvar "orthomode" orm)
  (setvar "osmode" osm)
  (setvar "attreq" 1)
  (setvar "polarmode" plm)
  (princ)
)
(princ "\nrnd")

sdp777

  • Guest
Re: Randomize help.
« Reply #1 on: July 03, 2006, 10:56:08 PM »
Heres a 2004 dwg to tinker with.

LE

  • Guest
Re: Randomize help.
« Reply #2 on: July 03, 2006, 11:08:33 PM »

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Randomize help.
« Reply #3 on: July 04, 2006, 09:34:56 AM »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

sdp777

  • Guest
Re: Randomize help.
« Reply #4 on: July 04, 2006, 09:38:10 AM »
Hmm.  I realize I inidicated the randomize function was problematic and thats my fault.  I was able to generate a random number without issue with the code above, and the a test of some of the defuns in that thread you referenced.  Perhaps I more clearly suggest, I need help on what happens in the modifyblk defun.  My alogrithms were a crap shoot.  Essentially, I would like the alogrithms to generate a scale factor to always remain somewhat small and the rotation angle to have larger variations.  

I also want to limit the answer randm to integers from 1 to 10.  Is there a way to conditionalize the answer better than below? Any assistance there would be helpful.

Code: [Select]
 (if (or (< ans "1") (> ans "10"))
     (progn
(randomize)
     )
     (progn
(princ "\nSorry '")
(princ ans)
(princ "' is not a valid entry...")
(princ "\nPlease try again!")
(rnd)
     )
   )
 )

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Randomize help.
« Reply #5 on: July 04, 2006, 09:57:55 AM »
Perhaps
Code: [Select]
(fix (* 10 (randomize)))
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.