Author Topic: Add ability to define prefix.  (Read 1326 times)

0 Members and 1 Guest are viewing this topic.

jwitt1983

  • Mosquito
  • Posts: 2
Add ability to define prefix.
« on: May 09, 2019, 03:06:31 PM »
I was searching for a lisp to rename all blocks in a dwg and came upon this code, written by Lee Mac in 2009. I'll insert the full code and a hyperlink to where I got it at the bottom.

It works as I would like, but missing a small feature that I'm assuming wouldn't be too difficult to fix.

In the place where is changes the name to "Block-", I would like it to ask me what I would like the prefix to be, then use that.

For example, I would like all blocks in a drawing to begin with 48925-, but in another drawing I would like it to start with 49134-... etc.

Any help would be greatly appreciated!

Code: [Select]
(defun c:BLR  (/ Allblocks num count ename edata bname nprefix rname)
  ;; Localise your variables buddy, good programming practice. :)
 
  (if (setq AllBlocks (ssget "_X" '((0 . "INSERT")))) ; begin if

    (progn
      (setq num (sslength AllBlocks) Count 0)
     
      (princ (strcat "\n   Total Blocks found : " (itoa num)))
     
      ;; This is where your program was failing, remember,
      ;; "princ" requires a STRING, you supplied it with an
      ;; INTEGER.

      (repeat num
        ;;this cycles number of items
       
        (setq Ename (ssname AllBlocks Count))
        ;;get entity name
        (setq Edata (entget Ename))

        (setq Count (+ 1 Count))
        (princ "\n\n   Count : ")
        (print Count)
        (princ " of ")
        (print Numblocks)
        (princ "\n\n   Edata :")
        (print Edata)

        (princ "\n\n ------------- cdr assoc 2 data ------------\n")
        (setq bName (cdr (assoc 2 Edata)))
        (print bName)
        (princ "\n=============================================\n")

        (setq nPrefix (strcase "Block-"))
        (princ "\n   nPrefix : ")
        (princ nPrefix)

        (setq rName (strcat nPrefix (itoa Count)))
        (princ "\n   rName :")
        (princ rName)

        (princ "\n   Block found!! Please Wait......") ;amusement
        (princ "\n Count = [ ")
        (print Count)
        (princ " ] sorting next.....\n :")

  ;(princ rName)
        (princ "\n   Renaming block found....")
        (command "-rename" "block" bName rName)

        )

      ) ;then
    (prompt "\n   No Block objects found!") ;else
    )
  (princ)
  ;===========================================================================Turn on command line responses
  ;(setvar "CMDECHO" 1)
  ;===========================================================================
  )

http://www.theswamp.org/index.php?topic=29839.15

ronjonp

  • Needs a day job
  • Posts: 7526
Re: Add ability to define prefix.
« Reply #1 on: May 09, 2019, 03:26:23 PM »
Try this:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:blr (/ allblocks num count ename edata bname nprefix rname)
  2.   ;; Localise your variables buddy, good programming practice. :)
  3.   (if (and (setq allblocks (ssget "_X" '((0 . "INSERT"))))
  4.            (/= "" (setq nprefix (getstring "\nEnter prefix")))
  5.            (snvalid nprefix)
  6.       )                                 ; begin if
  7.     (progn (setq num   (sslength allblocks)
  8.                  count 0
  9.            )
  10.            (princ (strcat "\n   Total Blocks found : " (itoa num)))
  11.            ;; This is where your program was failing, remember,
  12.            ;; "princ" requires a STRING, you supplied it with an
  13.            ;; INTEGER.
  14.            (repeat num
  15.              ;;this cycles number of items
  16.              (setq ename (ssname allblocks count))
  17.              ;;get entity name
  18.              (setq edata (entget ename))
  19.              (setq count (+ 1 count))
  20.              (princ "\n\n   Count : ")
  21.              (print count)
  22.              (princ " of ")
  23.              (print numblocks)
  24.              (princ "\n\n   Edata :")
  25.              (print edata)
  26.              (princ "\n\n ------------- cdr assoc 2 data ------------\n")
  27.              (setq bname (cdr (assoc 2 edata)))
  28.              (print bname)
  29.              (princ "\n=============================================\n")
  30.              (princ "\n   nPrefix : ")
  31.              (princ nprefix)
  32.              (setq rname (strcat nprefix (itoa count)))
  33.              (princ "\n   rName :")
  34.              (princ rname)
  35.              (princ "\n   Block found!! Please Wait......") ;amusement
  36.              (princ "\n Count = [ ")
  37.              (print count)
  38.              (princ " ] sorting next.....\n :") ;(princ rName)
  39.              (princ "\n   Renaming block found....")
  40.              (command "-rename" "block" bname rname)
  41.            )
  42.     )                                   ;then
  43.     (prompt "\n   No Block objects found!") ;else      
  44.   )
  45.   (princ)
  46. )
  47.  

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

jwitt1983

  • Mosquito
  • Posts: 2
Re: Add ability to define prefix.
« Reply #2 on: May 09, 2019, 05:26:30 PM »
I did notice that it causes an error when it encounters dynamic blocks, but unless you have an easy solution for that, I'd say that this is solved. Thanks ronjonp!

ronjonp

  • Needs a day job
  • Posts: 7526
Re: Add ability to define prefix.
« Reply #3 on: May 09, 2019, 05:32:50 PM »
Glad to help :)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC