+ Reply to Thread
Page 1 of 5 1 2 3 ... LastLast
Results 1 to 10 of 44
  1. #1
    Senior Member
    Using
    Map 3D 2008
    Join Date
    Apr 2009
    Location
    The one after 909
    Posts
    402

    Default Change Block Name

    Registered forum members do not see this ad.

    I always seem to want to tackle the most obscure and pointless problems.

    Is there a way to change the name of a block using LISP? I gave it a try using two methods, vla-put-name and the entmod/entupd method. Here's my code, nothing too fancy:

    Code:
    (defun c:rb( / )
      (vl-load-com)
      (setq ent (car (entsel "\nSelect block: ")))
      (if (= (cdr (assoc 0 (entget ent))) "INSERT")
        (progn
          ;(vlax-put-property (vlax-ename->vla-object ent) "Name" (getstring "\nEnter new name for block: "))
          (setq entLst (entget ent))
          (setq entLst (subst (cons 2 (getstring "\nEnter new name for block: ")) (assoc 2 entLst) entLst))
          (entmod entLst)
          (entupd ent)
          )
        (princ "\nSelected object is not a block.")
        )
      (princ)
      )
    You'll notice I have both methods in there, one is simply commented out.

    The vla method returns a key error, the entmod/entupd method doesn't error but it doesn't update the block. The key error seems to be a clue... that the block isn't included in some sort of block reference table..? I've never ran into something like that, so I'm lost. Do I need to add the block to the table, and if so, how? Or is it simply not possible?

    I was also thinking I could re-create it with entmake if I had to, not sure how that would work.

    Any ideas?
    ~* And, in the end, the love you take *~
    ~* Is equal to the love you make *~
    - The Beatles -


    GrPlayground
    / Text Find/Replace / Batch Engine / Tower Defense

  2. #2
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    16,962

    Default

    I tried this, but I too get the same thing:

    Code:
    (defun c:test  (/ blk tStr)
      (if (and (setq blk (car (entsel "\nSelect Block: ")))
               (eq "AcDbBlockReference"
                   (vla-get-ObjectName
                     (setq blk
                       (vlax-ename->vla-object blk))))
               (setq tStr (getstring t "\nSpecify New Name: "))
               (snvalid tStr))
        (if (vlax-property-available-p blk 'Name T)
          (vla-put-Name blk tStr)
          (princ "\nCannot Do that!")))
      (princ))
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

  3. #3
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    16,962

    Default

    Actually this works:

    Code:
    (defun c:test  (/ blk tStr)
      (if (and (setq blk (car (entsel "\nSelect Block: ")))
               (eq "AcDbBlockReference"
                   (vla-get-ObjectName
                     (setq blk
                       (vlax-ename->vla-object blk))))
               (setq tStr (getstring t "\nSpecify New Name: "))
               (tblsearch "BLOCK" tStr))
        (if (vl-catch-all-error-p
              (vl-catch-all-apply
                (function
                  (lambda (x)
                    (vla-put-Name x tStr))) (list blk)))            
          (princ "\nCannot Do that!")))
      (princ))
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

  4. #4
    Senior Member
    Using
    Map 3D 2008
    Join Date
    Apr 2009
    Location
    The one after 909
    Posts
    402

    Default

    Quote Originally Posted by Lee Mac View Post
    Actually this works:

    Code:
    (defun c:test  (/ blk tStr)
      (if (and (setq blk (car (entsel "\nSelect Block: ")))
               (eq "AcDbBlockReference"
                   (vla-get-ObjectName
                     (setq blk
                       (vlax-ename->vla-object blk))))
               (setq tStr (getstring t "\nSpecify New Name: "))
               (tblsearch "BLOCK" tStr))
        (if (vl-catch-all-error-p
              (vl-catch-all-apply
                (function
                  (lambda (x)
                    (vla-put-Name x tStr))) (list blk)))            
          (princ "\nCannot Do that!")))
      (princ))
    That's not working for me, Lee. It doesn't error, but it doesn't seem to change the name of the block.
    ~* And, in the end, the love you take *~
    ~* Is equal to the love you make *~
    - The Beatles -


    GrPlayground
    / Text Find/Replace / Batch Engine / Tower Defense

  5. #5
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    16,962

    Default

    Quote Originally Posted by Freerefill View Post
    That's not working for me, Lee. It doesn't error, but it doesn't seem to change the name of the block.
    The new Block name must be in the drawing also.

    From ACAD help:
    BlockRef: A block reference can be assigned only the name of a valid block definition in the drawing. Assigning a block reference a unique name will not automatically create a new block definition. To create a new block definition, use the Add method to add a new Block object to the Blocks collection.
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

  6. #6
    Senior Member
    Using
    Map 3D 2008
    Join Date
    Apr 2009
    Location
    The one after 909
    Posts
    402

    Default

    But if the block name exists, that means the -block- exists, and changing the name of the block to an existing block changes the definition as well.. changing the name of a border block to the name of a title block changes the border block into a title block.

    That makes sense, but it tells me that if I wanted to keep a block the way it is but simply change the name, I'd have to re-create it from scratch, probably by getting all the entities within it...

    I wish there was an easier way
    ~* And, in the end, the love you take *~
    ~* Is equal to the love you make *~
    - The Beatles -


    GrPlayground
    / Text Find/Replace / Batch Engine / Tower Defense

  7. #7
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    16,962

    Default

    Quote Originally Posted by Freerefill View Post
    But if the block name exists, that means the -block- exists, and changing the name of the block to an existing block changes the definition as well.. changing the name of a border block to the name of a title block changes the border block into a title block.

    That makes sense, but it tells me that if I wanted to keep a block the way it is but simply change the name, I'd have to re-create it from scratch, probably by getting all the entities within it...

    I wish there was an easier way
    Can't have everything the easy way
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

  8. #8
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    16,962

    Default

    Making the new block isn't too difficult, here is a LISP a modified from an older one I wrote:

    Code:
    ;; GetBlockEntities   by Lee McDonnell   [07.05.09]
    
    (defun GetBlockEntities  (Blk Nme / tStr)
      (if (and (tblsearch "BLOCK" Blk)
               (snvalid Nme))
        (progn
          (entmake
            (list
              (cons 0 "BLOCK")
              (cons 2 Nme)
              (assoc 10
                (entget
                  (tblobjname "BLOCK" Blk)))
              (cons 70 0)))
          (mapcar 'entmake
            (mapcar 'entget
              (GetObj (tblobjname "BLOCK" Blk))))
          (entmake
            (list
              (cons 0 "ENDBLK")
              (cons 8 "0"))))))
            
    
    ; Get Sub-Entities from Table Def
    (defun GetObj  (bObj)
      (if (setq bObj (entnext bObj))
        (cons bObj (GetObj bObj))))
    
    ; Test Function
    (defun c:test (/ old new)
      (setq old (getstring t "\nOld Block: "))
      (setq new (getstring t "\nNew Block: "))
      (GetBlockEntities old new)
      (entmake
        (list
          (cons 0 "INSERT")
          (cons 2 new)
          (cons 10 (getpoint)))))
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

  9. #9
    Senior Member Commandobill's Avatar
    Using
    AutoCAD 2014
    Join Date
    Oct 2008
    Location
    Raleigh, NC
    Posts
    319

    Default

    Quote Originally Posted by Lee Mac View Post
    Can't have everything the easy way
    I believe you dont know freerefill that well... lol
    ***Commandobill***
    Abusing AutoCad Since 1999
    "Caddatude... Do you has it?"
    Sorry for my poor English, I'm American...

  10. #10
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    16,962

    Default

    Registered forum members do not see this ad.

    Quote Originally Posted by Commandobill View Post
    I believe you dont know freerefill that well... lol
    Do you guys know each other well? *Just curious*
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

Similar Threads

  1. Change the block description?
    By stevsmith in forum AutoCAD General
    Replies: 2
    Last Post: 29th Sep 2008, 12:33 pm
  2. Can you change the opacity of a block?
    By Siberian in forum AutoCAD General
    Replies: 2
    Last Post: 5th Jun 2008, 08:22 am
  3. Block Attribute answers ability to change block Physical Attributes??
    By Kaidoz in forum AutoLISP, Visual LISP & DCL
    Replies: 6
    Last Post: 19th Mar 2008, 10:25 pm
  4. Change block to different block in drawing
    By Kathy in forum AutoCAD Drawing Management & Output
    Replies: 8
    Last Post: 24th May 2006, 12:34 am
  5. Change Block Colours
    By stephenward in forum AutoCAD Drawing Management & Output
    Replies: 5
    Last Post: 19th Jan 2006, 09:07 pm

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts