Code Red > AutoLISP (Vanilla / Visual)

Randomize help.

(1/2) > >>

sdp777:
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: ---;|

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

--- End code ---

sdp777:
Heres a 2004 dwg to tinker with.

LE:
See if this one works...

http://www.theswamp.org/index.php?topic=8203.msg105032#msg105032

CAB:
More on the subject.
http://www.theswamp.org/index.php?topic=2523.0

sdp777:
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: ---  (if (or (< ans "1") (> ans "10"))
     (progn
(randomize)
     )
     (progn
(princ "\nSorry '")
(princ ans)
(princ "' is not a valid entry...")
(princ "\nPlease try again!")
(rnd)
     )
   )
 )

--- End code ---

Navigation

[0] Message Index

[#] Next page

Go to full version