Jump to content

Lisp to rename selected block by a date and random string


Recommended Posts

Posted

Hello, I was wondering how could I get a LISP to select either one or various blocks (or even have the option to select all the blocks in the drawing), and then change their names to a date and an independent random string of text? For example: "YY/MM/DD/HHHH_QP8dFs2vL9JnhBr7"?

I've been having a lot of problems with CAD files coming to the office with blocks named the same but containing very different data, and it's a drag to rename each block manually. I'm only just starting on lisp language so i'm guessing something like this is way beyond my scope, but I'd really appreciate to know if this is a possibility. Thanks in advance!

Posted (edited)

modified @ronjonp lisp to work on all blocks instead of selecting one at a time.

https://www.cadtutor.net/forum/topic/76007-make-block-reference-names-unqiue/?do=findComment&comment=600502

(defun C:UB-ALL (/ s)
  (if (setq s (ssget "_X" (list '(0 . "INSERT"))))
    (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))
      (vla-converttoanonymousblock (vlax-ename->vla-object e))
    )
  )
  (princ)
)

 

As long as you don't purge the "old" block definitions are still there.

 

---EDIT

After you run above code use my code to add the date to the prefix to each block name.

;;----------------------------------------------------------------------------;;
;; Add Prefix/Suffix to Block Name
(defun C:BLKRENAME (/ blklst SS blk lst c a n)
  (vl-load-com)
  (setq blklst (vla-get-Blocks Drawing))
  (prompt "\nSelect Block(s) or Enter To Rename All")
  (if (or (setq ss (ssget '((0 . "INSERT")))) (setq ss (ssget "_X" '((0 . "INSERT")))))
    (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
      (setq blk (cdr (assoc 2 (entget e))))  ;Get Block Name
      (if (not (vl-position blk lst))        ;If Block Name is not in List Add
        (setq lst (cons blk lst))
      )
    )
  )
  (setq c 0)        ;Count
  (initget "Prefix Suffix")
  (setq rep
    (cond
      ((getkword "\n Specify your aim [Prefix/Suffix] :")) ( "Suffix")
    )
  )
  (cond
    ((= rep "Prefix")
        (setq prfx (getstring "\nEnter Prefix: "))
      (foreach n lst
        (vla-put-Name (vla-item blklst n) (strcat prfx n))
        (setq c (1+ c))
      )
    )
    ((= rep "Suffix")
        (setq sufx (getstring "\nEnter Suffix: "))
      (foreach n lst
        (vla-put-Name (vla-item blklst n) (strcat n sufx))
        (setq c (1+ c))
      )
    )
  )
  (prompt (strcat "\n" (rtos c 2 0) " Block(s) Renamed"))
  (princ)
)

 

Edited by mhupp
  • Like 1

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