Jump to content

Replace block, but keeping old attributes


Dandy

Recommended Posts

Hello,

 

I'm looking for something like the following:

 

I have a drawing with a lot of similar or nearly equal blocks. Unfortunately each block has a unique name so it's not a block reference. But each block has the same attribute list, but with block specific values.

 

What I want to do is to change all blocks against one master block, so that each block has the same name (block reference). For this I found some scripts. Unfortunately the unique block attributes get lost or will be replaced by the master block.

 

http://www.cadtutor.net/forum/showthread.php?37765-Merge-Block

 

 

I also found some scripts for copying attributes from one to another block.

 

http://www.lee-mac.com/matchattribs.html

 

BUT what I'm locking is a script that does the following.

 

1. Select the master block (A)

2. Select all other blocks (B,C,D,E, ..)

3. Starting the replacement of blocks (B,C,D,E, ..)

3.1 Copy attributes values of block B to a list

3.2 Replacing the block B against master block A

3.3 Copy back attribute values to the newly replaced block

3.4 Starting with Block C again ( steps 3.1 - 3.3)

 

Anybody who can help ?? I looked into the code of the scripts, but I'm "too" Newbee o:) to lisp to make this work.

 

Thanks in advance 8)

Dandy

Link to comment
Share on other sites

There are two ways to access info about block attributes either by their "tag name" or "creation order" in your case unless every block has the same tag names I would use the second method.

 

Please post a dwg with a few blocks in it.

 

; example of attributes by creation order
; an example pick one attributed block
(setq ss1 (ssget (list (cons 0 "INSERT"))))
(foreach att (vlax-invoke (vlax-ename->vla-object (ssname SS1 0 )) 'getattributes)
(alert (strcase (vla-get-textstring att))) ;displays attribute value
;(alert (strcase (vla-get-tagstring att))) ;displays tag name
)

Link to comment
Share on other sites

Hello Bigal,

 

thanks for replaying.

I have attached a sample.dwg

 

In this dwg are 3 blue blocks (they are irrelevant) and 4 other similar block references (white).

 

3 of them (with the base point in the blue ones) have an cryptic ID as a name "{.........}". The forth block (top right) has a name "Raumstempel".

 

What I want to achive is to replace the block with the cryptic names by a block reference of the block "Raumstempel", but before replacing the attribute values of each block (attributes of all 4 relevant blocks are the same, only values are unique) has to be stored and written back after the replacement.

 

I found a script for replacing and a script for copying attributes form Mac Lee (see my first post). But because of I'm not familar with AutoCAD programming I'm not able to combine them o:)

 

So that was the reason to ask for some support.

 

If you are someone else can help me this would be great :)

 

Thanks Dandy

 

 

sample.dwg

Link to comment
Share on other sites

Ok you can find all the blocks via this as long as they always start with {

(setq ss (ssget "x" (list( cons 0 "Insert")(cons 2 "{*"))))

 

I would read all the attributes using above swap the existing block onto a frozen layer, then insert the correct block same spot with the attribute values, when happy its all ok you could delete the old block rather than freeze it.

 

Need to replace in code above in a repeat (ssname SS1 x) where is x is a counter id from how many blocks have been found.

 

Bit busy right now some one else may jump in with a solution.

Link to comment
Share on other sites

Actually it can be a fairly simple routine :

 

You can (subst) group 2 BLOCK names as long as the replacment BLOCK definition exists in the current drawing.

 

[b][color=BLACK]([/color][/b]defun c:blk-mast [b][color=FUCHSIA]([/color][/b]/ ss i en ed[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]if [b][color=NAVY]([/color][/b]not [b][color=MAROON]([/color][/b]tblsearch [color=#2f4f4f]"BLOCK"[/color] [color=#2f4f4f]"MASTER"[/color][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
     [b][color=NAVY]([/color][/b]alert [color=#2f4f4f]"MASTER Block Not Found"[/color][b][color=NAVY])[/color][/b]
     [b][color=NAVY]([/color][/b]and [b][color=MAROON]([/color][/b]setq ss [b][color=GREEN]([/color][/b]ssget [color=#2f4f4f]"X"[/color] [b][color=BLUE]([/color][/b]list [b][color=RED]([/color][/b]cons 0 [color=#2f4f4f]"INSERT"[/color][b][color=RED])[/color][/b]
                                    [b][color=RED]([/color][/b]cons 2 [color=#2f4f4f]"A,B,C"[/color][b][color=RED])[/color][/b]
                                    [b][color=RED]([/color][/b]cons 66 1[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
          [b][color=MAROON]([/color][/b]setq i 0[b][color=MAROON])[/color][/b]
          [b][color=MAROON]([/color][/b]while [b][color=GREEN]([/color][/b]setq en [b][color=BLUE]([/color][/b]ssname ss i[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
                 [b][color=GREEN]([/color][/b]setq ed [b][color=BLUE]([/color][/b]entget en[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
                 [b][color=GREEN]([/color][/b]entmod [b][color=BLUE]([/color][/b]subst [b][color=RED]([/color][/b]cons 2 [color=#2f4f4f]"MASTER"[/color][b][color=RED])[/color][/b] [b][color=RED]([/color][/b]assoc 2 ed[b][color=RED])[/color][/b] ed[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
                 [b][color=GREEN]([/color][/b]setq i [b][color=BLUE]([/color][/b]1+ i[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]prin1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

 

A gotcha here could be any synchronize routine that goes back to the BLOCK table definitions.

 

-David

-BLKR.DWG

Link to comment
Share on other sites

Hello David,

thanks for replaying. I just checked your script and your sample dwg, it looked good and the script makes mainly what I'm looking for :-). But is it possible to provide the arguments by input selection (mouse courser) instead of a fixed list ?? Unfortunately my block names are not as simple as your sample A,B,C and picking would be much more easier than taking out the name of each block in my drawing and to placing it in a list. Thanks in advance ;-) Daniel

Link to comment
Share on other sites

Maybe :

 

[b][color=BLACK]([/color][/b]defun c:blk-mmst [b][color=FUCHSIA]([/color][/b]/ master rl ss i en ed bn[b][color=FUCHSIA])[/color][/b]

 [b][color=FUCHSIA]([/color][/b]while [b][color=NAVY]([/color][/b]not master[b][color=NAVY])[/color][/b]
        [b][color=NAVY]([/color][/b]and [b][color=MAROON]([/color][/b]princ [color=#2f4f4f]"\nSelect MASTER Replacement BLOCK:   "[/color][b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]setq ss [b][color=GREEN]([/color][/b]ssget [b][color=BLUE]([/color][/b]list [b][color=RED]([/color][/b]cons 0 [color=#2f4f4f]"INSERT"[/color][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]= [b][color=GREEN]([/color][/b]sslength ss[b][color=GREEN])[/color][/b] 1[b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]setq master [b][color=GREEN]([/color][/b]strcase [b][color=BLUE]([/color][/b]cdr [b][color=RED]([/color][/b]assoc 2 [b][color=PURPLE]([/color][/b]entget [b][color=TEAL]([/color][/b]ssname ss 0[b][color=TEAL])[/color][/b][b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]princ [b][color=NAVY]([/color][/b]strcat [color=#2f4f4f]"\n"[/color] master[b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]

 [b][color=FUCHSIA]([/color][/b]while
   [b][color=NAVY]([/color][/b]and [b][color=MAROON]([/color][/b]princ [color=#2f4f4f]"\nSelect BLOCKs To Replace:   "[/color][b][color=MAROON])[/color][/b]
        [b][color=MAROON]([/color][/b]setq ss [b][color=GREEN]([/color][/b]ssget [b][color=BLUE]([/color][/b]list [b][color=RED]([/color][/b]cons 0 [color=#2f4f4f]"INSERT"[/color][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
   [b][color=NAVY]([/color][/b]setq i 0[b][color=NAVY])[/color][/b]
   [b][color=NAVY]([/color][/b]while [b][color=MAROON]([/color][/b]setq en [b][color=GREEN]([/color][/b]ssname ss i[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
          [b][color=MAROON]([/color][/b]setq ed [b][color=GREEN]([/color][/b]entget en[b][color=GREEN])[/color][/b]
                bn [b][color=GREEN]([/color][/b]strcase [b][color=BLUE]([/color][/b]cdr [b][color=RED]([/color][/b]assoc 2 ed[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
          [b][color=MAROON]([/color][/b]cond [b][color=GREEN]([/color][/b][b][color=BLUE]([/color][/b]= bn master[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
                [b][color=GREEN]([/color][/b][b][color=BLUE]([/color][/b]member bn rl[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
                [b][color=GREEN]([/color][/b][b][color=BLUE]([/color][/b]setq rl [b][color=RED]([/color][/b]cons bn rl[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
          [b][color=MAROON]([/color][/b]setq i [b][color=GREEN]([/color][/b]1+ i[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]

 [b][color=FUCHSIA]([/color][/b]foreach e rl
   [b][color=NAVY]([/color][/b]and [b][color=MAROON]([/color][/b]setq ss [b][color=GREEN]([/color][/b]ssget [color=#2f4f4f]"X"[/color] [b][color=BLUE]([/color][/b]list [b][color=RED]([/color][/b]cons 0 [color=#2f4f4f]"INSERT"[/color][b][color=RED])[/color][/b]
                                  [b][color=RED]([/color][/b]cons 2 e[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
        [b][color=MAROON]([/color][/b]princ [b][color=GREEN]([/color][/b]strcat [color=#2f4f4f]"\n"[/color] e[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
        [b][color=MAROON]([/color][/b]setq i 0[b][color=MAROON])[/color][/b]
        [b][color=MAROON]([/color][/b]while [b][color=GREEN]([/color][/b]setq en [b][color=BLUE]([/color][/b]ssname ss i[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
               [b][color=GREEN]([/color][/b]setq ed [b][color=BLUE]([/color][/b]entget en[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
               [b][color=GREEN]([/color][/b]entmod [b][color=BLUE]([/color][/b]subst [b][color=RED]([/color][/b]cons 2 master[b][color=RED])[/color][/b] [b][color=RED]([/color][/b]assoc 2 ed[b][color=RED])[/color][/b] ed[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
               [b][color=GREEN]([/color][/b]setq i [b][color=BLUE]([/color][/b]1+ i[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]

 [b][color=FUCHSIA]([/color][/b]command [color=#2f4f4f]"_.REGEN"[/color][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]prin1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

 

 

This would not deal nested BLOCKs

 

HTH -David

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