Jump to content

Need to rename blocks randomly


Adju

Recommended Posts

I did search but didn't find a solution.

I'm trying to find something that could take all the blocks (and nested ones) in a drawing and rename them randomly. A little bit like the "paste as block" in autocad.

If i have a block called "Test", it would turn, for example, "A$C3B5F1E17".

A client want DWG files. It's OK but we don't want to give dynamic blocks (a lot of them) since he could give the DWG files to competitors.

I found "Undynamic" lisp routine that work great. Changing blocks name would be the final touch.

 

Thanks in advance.

 

Link to comment
Share on other sites

Here's a quick one for you to try:

(defun c:randomiseblocks ( / new )
    (vlax-for def (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
        (if (and  (= :vlax-false  (vla-get-islayout def)  (vla-get-isxref def))
                  (wcmatch (vla-get-name def) "~`**")
            )
            (progn
                (while (tblsearch "block" (setq new (randomname 10))))
                (vla-put-name def new)
            )
        )
    )
    (princ)
)

(defun randomname ( n / a b r )
    (setq a 65 b 90)
    (repeat n
        (setq r (cons (chr (LM:randrange a b)) r)
              a (- 113 a)
              b (- 147 b)
        )
    )
    (apply 'strcat (reverse r))
)

;; Rand  -  Lee Mac
;; PRNG implementing a linear congruential generator with
;; parameters derived from the book 'Numerical Recipes'

(defun LM:rand ( / a c m )
    (setq m   4294967296.0
          a   1664525.0
          c   1013904223.0
          $xn (rem (+ c (* a (cond ($xn) ((getvar 'date))))) m)
    )
    (/ $xn m)
)

;; Random in Range  -  Lee Mac
;; Returns a pseudo-random integral number in a given range (inclusive)

(defun LM:randrange ( a b )
    (+ (min a b) (fix (* (LM:rand) (1+ (abs (- a b))))))
)

(vl-load-com) (princ)

The above uses my set of Random Number Functions.

  • Thanks 1
Link to comment
Share on other sites

Heres another one, thanks to @Lee Mac (it uses his password generator subfoo):

(defun C:test ( / L bnm )
  (vlax-for o (vla-get-Blocks (vla-get-ActiveDocument (vlax-get-acad-object)))
    (or 
      (vl-some (function (lambda (x) (eq (vlax-get-property o x) :vlax-true))) '(IsLayout IsXRef))
      (wcmatch (setq bnm (vlax-get-property o 'Name)) "_*")
      (wcmatch (strcase bnm) "`*U*")
      (setq L (cons o L))
    )
  )
  (mapcar
    (function
      (lambda (a b) 
        (vl-catch-all-apply (function (lambda nil (vlax-put-property a 'Name b))))
      )
    )
    L
    (mapcar 'vl-list->string
      (read 
        (strcat
          "(" 
          (apply 'strcat 
            (apply 'append 
              (append '(("("))
                (subst '(")(") '("10 ")
                  (mapcar '(lambda (x) (list (strcat (itoa x) " ")))
                    (cdr (reverse (vlax-safearray->list (vlax-variant-value (password (length L) 20 7)))))
                  )
                ) 
                '((")"))
              )
            )
          )
          ")"
        )
      )
    )
  )
  (princ)
)

;; http://www.theswamp.org/index.php?topic=45220.msg504240#msg504240
;; <<!!! Slight modification !!!>>
;; Password Generator  -  Lee Mac
;; Retrieves a set of random strings from http://www.random.org
;; num - [int] Number of passwords to generate (1 - 10000)
;; len - [int] Length of each password (1 - 20)
;; bit - [int] 1=0-9, 2=A-Z, 4=a-z

(defun password ( num len bit / obj rtn )
  (if (and (< 0 num 1e4) (< 0 len 21) (< 0 bit))
    (if (setq obj (vlax-get-or-create-object "winhttp.winhttprequest.5.1"))
      (progn
        (setq rtn
          (vl-catch-all-apply
            (function
              (lambda ( )
                (vlax-invoke-method obj 'open "GET"
                  (strcat
                    "http://www.random.org/strings/?num="
                    (itoa num)
                    "&len="
                    (itoa len)
                    "&digits="
                    (if (= 1 (logand 1 bit)) "on" "off")
                    "&upperalpha="
                    (if (= 2 (logand 2 bit)) "on" "off")
                    "&loweralpha="
                    (if (= 4 (logand 4 bit)) "on" "off")
                    "&unique=on&format=plain&rnd=new"
                  )
                  :vlax-false
                )
                (vlax-invoke-method obj 'send)
                (vlax-get-property  obj 'responsebody)
              )
            )
          )
        )
        (vlax-release-object obj)
        (if (vl-catch-all-error-p rtn)
          (princ (vl-catch-all-error-message rtn))
          ;|
          (princ
            (vl-list->string
              (mapcar '(lambda ( x ) (lsh (lsh x 24) -24))
                (vlax-safearray->list (vlax-variant-value rtn))
              )
            )
          )
          |;
        )
      )
    )
    (princ "\nInvalid arguments.\nnum: integer 1-10,000\nlen: integer 1-20\nbit: 1=0-9, 2=A-Z, 4=a-z")
  )
  (princ) (if (eq 'variant (type rtn)) rtn)
)


 

Link to comment
Share on other sites

Exactly wath i was looking for !

In french we would say "La cerise sur le sundae" !. That could be translate has "The cherry on the top" !

Thank you again Lee Mac.

If i had only a fraction of your knowlege...i would be very good ! 😁

Link to comment
Share on other sites

Thanks Grrr !

 

I tried your routine but it did not worked. It charged but an error happened. I'll take a look a it.

We have to use a french version of CAD in our office and sometimes i have to add underscore before some commands in order to make it work.

I'll keep you updated.

 

Thanks again.

Link to comment
Share on other sites

1 hour ago, Adju said:

Exactly wath i was looking for !

In french we would say "La cerise sur le sundae" !. That could be translate has "The cherry on the top" !

Thank you again Lee Mac.

If i had only a fraction of your knowlege...i would be very good ! 😁

 

De rien! You're most welcome :thumbsup:

Link to comment
Share on other sites

You could also make all block anonymous.  This would insure that they could never be overwritten.

 

It would be a bit of a challenge.  -David

Link to comment
Share on other sites

Maybe something like this:

(defun c:foo (/ d)
  (vlax-for a (vla-get-blocks (setq d (vla-get-activedocument (vlax-get-acad-object))))
    (if	(= 0 (vlax-get a 'isxref))
      (vlax-for	b a
	(and (= "AcDbBlockReference" (vla-get-objectname b))
	     (vlax-write-enabled-p b)
	     (vla-converttoanonymousblock b)
	)
      )
    )
  )
  (repeat 3 (vla-purgeall d))
  (princ)
)

 

Link to comment
Share on other sites

Never heard about anonymous blocks... very good routine !

I tried it on a drawing and it work perfectly fine. I tried on 2 other ones and 1 or 2 blocks, in each drawing, didn't work. You have an idea ?

The thing with you routine is that i don't have to remove dynamic features on the blocks. It all goes down in "the meat grinder". 😀

 

Thanks again ! ✌️

Link to comment
Share on other sites

On 1/25/2019 at 7:12 PM, Adju said:

I found "Undynamic" lisp routine that work great. 

 

1 hour ago, Adju said:

that i don't have to remove dynamic features on the blocks

 

 

It seems that you are contradicting yourself. 😜

Link to comment
Share on other sites

44 minutes ago, Grrr said:

 

 

 

It seems that you are contradicting yourself. 😜

I think he/she means that when creating an anonymous block it removes the dynamic properties already so no need for the other code :).

Link to comment
Share on other sites

 

34 minutes ago, ronjonp said:

I think he/she means that when creating an anonymous block it removes the dynamic properties already so no need for the other code :).

 

I'm a "he" and that's what i was trying to say. Thanks Ronjonp !

Link to comment
Share on other sites

Apologies for misunderstanding, sometimes its tricky to get what the author of the post actually ment.

Even I sometimes do rephrase my post in order to be clear as much as I can. :geek:

Link to comment
Share on other sites

13 minutes ago, Grrr said:

Apologies for misunderstanding, sometimes its tricky to get what the author of the post actually ment.

Even I sometimes do rephrase my post in order to be clear as much as I can. :geek:

 

No problems ! It could be also because of my "french accent" :D

Not as good with my english as i would want to be, but still improving. Thanks for your patience.

Link to comment
Share on other sites

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