Jump to content

Recommended Posts

Posted

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:

 

(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?

  • Replies 43
  • Created
  • Last Reply

Top Posters In This Topic

  • Lee Mac

    18

  • Freerefill

    10

  • David Bethel

    7

  • Commandobill

    2

Posted

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

 

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

Posted

Actually this works:

 

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

Posted
Actually this works:

 

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

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

Posted

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 :(

Posted
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 :P

Posted

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

 

[i][color=#990099];; GetBlockEntities   by Lee McDonnell   [07.05.09][/color][/i]

[b][color=RED]([/color][/b][b][color=BLUE]defun[/color][/b] GetBlockEntities  [b][color=RED]([/color][/b]Blk Nme [b][color=BLUE]/[/color][/b] tStr[b][color=RED])[/color][/b]
 [b][color=RED]([/color][/b][b][color=BLUE]if[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]and[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]tblsearch[/color][/b] [b][color=#ff00ff]"BLOCK"[/color][/b] Blk[b][color=RED])[/color][/b]
          [b][color=RED]([/color][/b][b][color=BLUE]snvalid[/color][/b] Nme[b][color=RED])[/color][/b][b][color=RED])[/color][/b]
   [b][color=RED]([/color][/b][b][color=BLUE]progn[/color][/b]
     [b][color=RED]([/color][/b][b][color=BLUE]entmake[/color][/b]
       [b][color=RED]([/color][/b][b][color=BLUE]list[/color][/b]
         [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] [b][color=#009900]0[/color][/b] [b][color=#ff00ff]"BLOCK"[/color][/b][b][color=RED])[/color][/b]
         [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] [b][color=#009900]2[/color][/b] Nme[b][color=RED])[/color][/b]
         [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]10[/color][/b]
           [b][color=RED]([/color][/b][b][color=BLUE]entget[/color][/b]
             [b][color=RED]([/color][/b][b][color=BLUE]tblobjname[/color][/b] [b][color=#ff00ff]"BLOCK"[/color][/b] Blk[b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b]
         [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] [b][color=#009900]70[/color][/b] [b][color=#009900]0[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b]
     [b][color=RED]([/color][/b][b][color=BLUE]mapcar[/color][/b] [b][color=DARKRED]'[/color][/b][b][color=BLUE]entmake[/color][/b]
       [b][color=RED]([/color][/b][b][color=BLUE]mapcar[/color][/b] [b][color=DARKRED]'[/color][/b][b][color=BLUE]entget[/color][/b]
         [b][color=RED]([/color][/b]GetObj [b][color=RED]([/color][/b][b][color=BLUE]tblobjname[/color][/b] [b][color=#ff00ff]"BLOCK"[/color][/b] Blk[b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b]
     [b][color=RED]([/color][/b][b][color=BLUE]entmake[/color][/b]
       [b][color=RED]([/color][/b][b][color=BLUE]list[/color][/b]
         [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] [b][color=#009900]0[/color][/b] [b][color=#ff00ff]"ENDBLK"[/color][/b][b][color=RED])[/color][/b]
         [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] [b][color=#009900]8[/color][/b] [b][color=#ff00ff]"0"[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b]
       

[i][color=#990099]; Get Sub-Entities from Table Def[/color][/i]
[b][color=RED]([/color][/b][b][color=BLUE]defun[/color][/b] GetObj  [b][color=RED]([/color][/b]bObj[b][color=RED])[/color][/b]
 [b][color=RED]([/color][/b][b][color=BLUE]if[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] bObj [b][color=RED]([/color][/b][b][color=BLUE]entnext[/color][/b] bObj[b][color=RED])[/color][/b][b][color=RED])[/color][/b]
   [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] bObj [b][color=RED]([/color][/b]GetObj bObj[b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b]

[i][color=#990099]; Test Function[/color][/i]
[b][color=RED]([/color][/b][b][color=BLUE]defun[/color][/b] c:test [b][color=RED]([/color][/b][b][color=RED][color=Blue]/[/color][/color][/b][color=Black] old new[/color][b][color=RED])[/color][/b]
 [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] old [b][color=RED]([/color][/b][b][color=BLUE]getstring[/color][/b] [color=Blue][b]t[/b][/color] [b][color=#ff00ff]"\nOld Block: "[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b]
 [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] new [b][color=RED]([/color][/b][b][color=BLUE]getstring t[/color][/b] [b][color=#ff00ff]"\nNew Block: "[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b]
 [b][color=RED]([/color][/b]GetBlockEntities old new[b][color=RED])[/color][/b]
 [b][color=RED]([/color][/b][b][color=BLUE]entmake[/color][/b]
   [b][color=RED]([/color][/b][b][color=BLUE]list[/color][/b]
     [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] [b][color=#009900]0[/color][/b] [b][color=#ff00ff]"INSERT"[/color][/b][b][color=RED])[/color][/b]
     [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] [b][color=#009900]2[/color][/b] [color=Black]new[/color][b][color=RED])[/color][/b]
     [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] [b][color=#009900]10[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]getpoint[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b]

Posted
Can't have everything the easy way :P

 

I believe you dont know freerefill that well... lol

Posted
I believe you dont know freerefill that well... lol

 

Do you guys know each other well? *Just curious*

Posted

There is always an easier way!!

 

.... though it was provided by Lee a few weeks ago >.>''

 

(defun c:rb( / ss blkNam listy ent)
 (setq ss (ssadd))
 (while (not (snvalid (setq blkNam (getstring "\nEnter new name for block: ")))) (princ "\nName not acceptable, please try again."))
 (setq listy (GetBlockEntities (cdr (assoc 2 (entget (setq ent (car (entsel))))))))
 (foreach forvar listy
   (entmake (entget forVar))
   (setq ss (ssadd (entlast) ss))
   )
 (command "-block" blknam (list 0 0 0) ss "")
 (command "-insert" blkNam (cdr (assoc 10 (entget ent))) 1 1 0)
 (command "erase" ent "")
 )

(defun GetBlockEntities  (Blk / tStr)
 (if (tblsearch "BLOCK" Blk)
   (GetObj (tblobjname "BLOCK" Blk))))

(defun GetObj  (bObj)
 (if (setq bObj (entnext bObj))
   (cons bObj (GetObj bObj))))

Not a snowballs chance in Heck I'd be able to figure out that GetBlockEntities function on my own.

 

Of course, I'm not sure if any of that is strictly proper, or if there should be more ways to catch errors. It works, though.

 

And yes, he's the Joker, I'm the Batman.

 

EDIT: Looking at it now, I should probably test to see if the insertion point is correct.. maybe transform it into the current UCS..

Posted
Do you guys know each other well? *Just curious*

 

He sits a cube away...

Posted

Double fail! Lee, yours doesn't seem to catch the attributes, though it does create the rest, and mine doesn't create anything if there are attributes. That shall have to be fixed! *workworkwork*

Posted
He sits a cube away...

 

Ahhh... I see :P

 

Double fail! Lee, yours doesn't seem to catch the attributes, though it does create the rest, and mine doesn't create anything if there are attributes. That shall have to be fixed! *workworkwork*

 

Yes, mine will not work for Attributed blocks, but will be quicker making the block than using the "command-call-cop-out" like yours... :P

Posted

Yes, mine will not work for Attributed blocks, but will be quicker making the block than using the "command-call-cop-out" like yours... :P

 

... *burned*

 

 

 

 

haha, cheers Lee :P

Posted
... *burned*

 

 

 

 

haha, cheers Lee :P

 

Hehe :D we are such nerds...

Posted

And don't forget about attribute checking. -David

Posted
And don't forget about attribute checking. -David

 

David,

 

I can't understand why my posted routine does not entmake the ATTDEF's as well :huh:

 

I cycle through the block definition, so the attributes should surely be picked up and created :unsure:

Posted

Bit 2 has to be set in group 70 in the BLOCK table header. And then 66 must be set to 1 in the INSERT definition. -David

Posted
Bit 2 has to be set in group 70 in the BLOCK table header. And then 66 must be set to 1 in the INSERT definition. -David

 

Ahh, I see - thanks David :D

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