Jump to content
mattdalgety

List routine that replaces named block with other named block in drawing

Recommended Posts

mattdalgety

Hi,

 

wondering if any one can help me with this one. ive had a look throuhg similar threads but cant seem to find what im after.

 

I need a lisp that essentially does what block replace does but without the required user input of selecting blocks.

 

Say i have a block called "Circle 102" and i always want to replace this circle 102 block with one of another name. e.g "block 1"

Then be able to do it to "Circle 103" with another "block 2"

 

and so on and so on.

 

If anyone can help me out as its doing my head in trying to think this one out.

 

Cheers

Share this post


Link to post
Share on other sites
rlx

Hi Mattdalgety, welcome on CadTutor

 

 

I have one of my own (but its a kind of a dragon) : http://www.cadtutor.net/forum/showthread.php?100670-RlxBlk-Replace-Redefine-blocks-visibility-states-preview-and-link-attributes&highlight=rlxblk

 

 

I believe Tharwat has one that is a bit more friendly to use but can't find it so quickly so hope he reads this post and replies too.

only found this site : https://autolispprograms.wordpress.com/hvac/hvac-application/

 

 

ah , found it : https://apps.autodesk.com/ACD/en/Detail/Index?id=4002734712009543256&appLang=en&os=Win32_64

 

 

And last but not least , I have no doubt master Lee has something on his brilliant site : http://www.lee-mac.com/index.html

 

 

gr. Rlx

Edited by rlx

Share this post


Link to post
Share on other sites
mattdalgety

Thanks mate, had a look at your routine and it looks a little bit more complicated than what im after.

 

Essentially i just need a "dumb" lisp routine that i can set once and leave it.

 

ie run command and it replaces all the nominated blocks with the other nominated blocks.

 

eg block 1 replaces block a

block 2 replaces block b

block 3 replaces block c etc.

 

What im using this for is to convert exported sprinkler symbols (or should i say lines and circles.) from revit to my own blocks.

 

as revit to cad exports these as arcs and circles, im using a command to convert these to blocks based on radius, then i use block-replace to replace say blockname: radius200 with blockname :sprinklerupright.

 

right now its a manual process for me to select each block.

 

Im looking to streamline this so it clicks once and replaces all predefined block names.

 

 

Hi Mattdalgety, welcome on CadTutor

 

 

I have one of my own (but its a kind of a dragon) : http://www.cadtutor.net/forum/showthread.php?100670-RlxBlk-Replace-Redefine-blocks-visibility-states-preview-and-link-attributes&highlight=rlxblk

 

 

I believe Tharwat has one that is a bit more friendly to use but can't find it so quickly so hope he reads this post and replies too.

 

 

And last but not least , I have no doubt master Lee has something on his brilliant site : http://www.lee-mac.com/index.html

 

 

gr. Rlx

Share this post


Link to post
Share on other sites
Tharwat

Thank you Rlx for your recommendations. :)

 

I guess this link is what the OP wants. Replace Blocks with Another

Share this post


Link to post
Share on other sites
mattdalgety

Thanks guys,

 

still doesn't look quite what im after.

 

These routines are a little too complex with the user inputs

 

what im really after is a dumbed down version of blockreplace, where instead of the user selecting the block to be replaced and then replace it, rather me being able to type out the blockname and save it into the routine so the command doesnt have to pause for the user input each time.

 

eg. run routine

 

lisp finds all blocks titles "blocka.dwg"

 

replace all blocka.dwg with block b.dwg.

 

Then search for the next one.

 

find all blocks titles "blockc.dwg"

 

replace all blockc.dwg with block d.dwg.

 

and so on.

 

 

Thank you Rlx for your recommendations. :)

 

I guess this link is what the OP wants. Replace Blocks with Another

Share this post


Link to post
Share on other sites
rlx
Thanks guys,

 

still doesn't look quite what im after.

 

These routines are a little too complex with the user inputs

 

what im really after is a dumbed down version of blockreplace, where instead of the user selecting the block to be replaced and then replace it, rather me being able to type out the blockname and save it into the routine so the command doesnt have to pause for the user input each time.

 

eg. run routine

 

lisp finds all blocks titles "blocka.dwg"

 

replace all blocka.dwg with block b.dwg.

 

Then search for the next one.

 

find all blocks titles "blockc.dwg"

 

replace all blockc.dwg with block d.dwg.

 

and so on.

 

 

Unfortunately haven't come around implementing multiple blocks & script mode for my appie and somebody just gave me a :censored: load of work :sweat: although if blocks are not dynamic and have no attributes that need to be migrated this would probably be relatively simpel.

 

 

But just to give a hint , you can use the = option from the insert comand : -insert "BlockA=c:/temp/BlockB" would redefine all blocks with name "BlockA" with definition of "BlockB" (but name would still be BlockA) , or , just name blockB on file also BlockA. Just mind the setting of expert variable. Insert it once on 0,0 for example , entdel entlast , et voila...

 

 

gr. Rlx

Edited by rlx

Share this post


Link to post
Share on other sites
Tharwat

Hi,

A quickie code that should work with regular blocks only (not Dynamic nor Attributed blocks).

(defun c:repblocks (/ blk1 blk2 msg1 msg2 doc)
 ;; Tharwat - Date: 12.Jan.2018	;;
 (setq blk1 "BlockA"
       blk2 "BlockB"
       msg1 "Block name <"
       msg2 "> could not be found in this drawing <!>"
       doc  (vla-get-activedocument (vlax-get-acad-object))
 )
 (if
   (and (or (setq sel (ssget "_X" (list '(0 . "INSERT") (cons 2 blk1))))
            (alert (strcat msg1 blk1 msg2))
        )
        (or (tblsearch "BLOCK" blk2)
            (alert (strcat msg1 blk2 msg2))
        )
   )
    (vlax-for spc (vla-get-blocks doc)
      (if (= (vla-get-islayout spc) :vlax-true)
        (vlax-for blk spc
          (and (= "AcDbBlockReference" (vla-get-objectname blk))
               (= (vla-get-name blk) blk1)
               (vla-insertblock
                 spc
                 (vla-get-insertionpoint blk)
                 blk2
                 (vla-get-xscalefactor blk)
                 (vla-get-yscalefactor blk)
                 (vla-get-zscalefactor blk)
                 (vla-get-rotation blk)
               )
               (vla-delete blk)
          )
        )
      )
    )
 )
 (princ)
) (vl-load-com)

Share this post


Link to post
Share on other sites
rlx

beautiful in its simplicity Tharwat :thumbsup:

Share this post


Link to post
Share on other sites
Tharwat
beautiful in its simplicity Tharwat :thumbsup:

 

Thank you. :)

Share this post


Link to post
Share on other sites
mattdalgety

Works like a charm.

 

Cheers guys, absolute legends. :)

 

 

Hi,

A quickie code that should work with regular blocks only (not Dynamic nor Attributed blocks).

(defun c:repblocks (/ blk1 blk2 msg1 msg2 doc)
 ;; Tharwat - Date: 12.Jan.2018	;;
 (setq blk1 "BlockA"
       blk2 "BlockB"
       msg1 "Block name <"
       msg2 "> could not be found in this drawing <!>"
       doc  (vla-get-activedocument (vlax-get-acad-object))
 )
 (if
   (and (or (setq sel (ssget "_X" (list '(0 . "INSERT") (cons 2 blk1))))
            (alert (strcat msg1 blk1 msg2))
        )
        (or (tblsearch "BLOCK" blk2)
            (alert (strcat msg1 blk2 msg2))
        )
   )
    (vlax-for spc (vla-get-blocks doc)
      (if (= (vla-get-islayout spc) :vlax-true)
        (vlax-for blk spc
          (and (= "AcDbBlockReference" (vla-get-objectname blk))
               (= (vla-get-name blk) blk1)
               (vla-insertblock
                 spc
                 (vla-get-insertionpoint blk)
                 blk2
                 (vla-get-xscalefactor blk)
                 (vla-get-yscalefactor blk)
                 (vla-get-zscalefactor blk)
                 (vla-get-rotation blk)
               )
               (vla-delete blk)
          )
        )
      )
    )
 )
 (princ)
) (vl-load-com)

Share this post


Link to post
Share on other sites
Tharwat
Works like a charm.

 

Cheers guys, absolute legends. :)

 

You are welcome anytime. :)

Share this post


Link to post
Share on other sites
David Bethel

This should work with any release of AutoCAD. Both BLOCK Table definitions must exist in the current dwg.

 

[b][color=BLACK]([/color][/b]defun c:rblk [b][color=FUCHSIA]([/color][/b]/ a b ss i en ed fe fd bl td bn in f[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]while [b][color=NAVY]([/color][/b]or [b][color=MAROON]([/color][/b]not a[b][color=MAROON])[/color][/b]
            [b][color=MAROON]([/color][/b]not [b][color=GREEN]([/color][/b]tblsearch [color=#2f4f4f]"BLOCK"[/color] a[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
        [b][color=NAVY]([/color][/b]setq a [b][color=MAROON]([/color][/b]strcase [b][color=GREEN]([/color][/b]getstring [color=#2f4f4f]"\nOld BLOCK a Name:  "[/color][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]while [b][color=NAVY]([/color][/b]or [b][color=MAROON]([/color][/b]not b[b][color=MAROON])[/color][/b]
            [b][color=MAROON]([/color][/b]not [b][color=GREEN]([/color][/b]tblsearch [color=#2f4f4f]"BLOCK"[/color] b[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
            [b][color=MAROON]([/color][/b]= a b[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
        [b][color=NAVY]([/color][/b]setq b [b][color=MAROON]([/color][/b]strcase [b][color=GREEN]([/color][/b]getstring [color=#2f4f4f]"\nNew BLOCK b Name:  "[/color][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]"\nUpdating Stand Alone BLOCK "[/color] a  [color=#2f4f4f]" To BLOCK "[/color] b [color=#2f4f4f]"\n"[/color][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]and [b][color=NAVY]([/color][/b]setq ss [b][color=MAROON]([/color][/b]ssget [color=#2f4f4f]"X"[/color] [b][color=GREEN]([/color][/b]list [b][color=BLUE]([/color][/b]cons 0 [color=#2f4f4f]"INSERT"[/color][b][color=BLUE])[/color][/b][b][color=BLUE]([/color][/b]cons 2 a[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]princ [b][color=MAROON]([/color][/b]strcat [color=#2f4f4f]"\t"[/color] [b][color=GREEN]([/color][/b]itoa [b][color=BLUE]([/color][/b]sslength ss[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b] [color=#2f4f4f]" Found"[/color][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][b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]entmod [b][color=GREEN]([/color][/b]subst [b][color=BLUE]([/color][/b]cons 2 b[b][color=BLUE])[/color][/b] [b][color=BLUE]([/color][/b]assoc 2 ed[b][color=BLUE])[/color][/b] ed[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]setq bl nil[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]princ [b][color=NAVY]([/color][/b]strcat [color=#2f4f4f]"\nSearching BLOCK "[/color] a  [color=#2f4f4f]" To BLOCK "[/color] b [color=#2f4f4f]"\n"[/color][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]while [b][color=NAVY]([/color][/b]setq td [b][color=MAROON]([/color][/b]tblnext [color=#2f4f4f]"BLOCK"[/color] [b][color=GREEN]([/color][/b]not td[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
        [b][color=NAVY]([/color][/b]princ [color=#2f4f4f]"\t\t\t\r"[/color][b][color=NAVY])[/color][/b]
        [b][color=NAVY]([/color][/b]prin1 bl[b][color=NAVY])[/color][/b]
        [b][color=NAVY]([/color][/b]setq bn [b][color=MAROON]([/color][/b]cdr [b][color=GREEN]([/color][/b]assoc  2 td[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
              fe [b][color=MAROON]([/color][/b]cdr [b][color=GREEN]([/color][/b]assoc -2 td[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
               f nil[b][color=NAVY])[/color][/b]
        [b][color=NAVY]([/color][/b]while [b][color=MAROON]([/color][/b]and [b][color=GREEN]([/color][/b]not f[b][color=GREEN])[/color][/b]
                    [b][color=GREEN]([/color][/b]setq fe [b][color=BLUE]([/color][/b]entnext fe[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
               [b][color=MAROON]([/color][/b]setq fd [b][color=GREEN]([/color][/b]entget fe[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
               [b][color=MAROON]([/color][/b]and [b][color=GREEN]([/color][/b]= [color=#2f4f4f]"INSERT"[/color] [b][color=BLUE]([/color][/b]cdr [b][color=RED]([/color][/b]assoc 0 fd[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
                    [b][color=GREEN]([/color][/b]setq in [b][color=BLUE]([/color][/b]cdr [b][color=RED]([/color][/b]assoc 2 fd[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
                    [b][color=GREEN]([/color][/b]= in a[b][color=GREEN])[/color][/b]
                    [b][color=GREEN]([/color][/b]cond [b][color=BLUE]([/color][/b][b][color=RED]([/color][/b]member in bl[b][color=RED])[/color][/b] [b][color=RED]([/color][/b]setq f T[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b]
                          [b][color=BLUE]([/color][/b]T [b][color=RED]([/color][/b]setq f T
                                  bl [b][color=PURPLE]([/color][/b]cons bn bl[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]foreach bn bl
     [b][color=NAVY]([/color][/b]setq td [b][color=MAROON]([/color][/b]tblsearch [color=#2f4f4f]"BLOCK"[/color] bn[b][color=MAROON])[/color][/b]
           fe [b][color=MAROON]([/color][/b]cdr [b][color=GREEN]([/color][/b]assoc -2 td[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
     [b][color=NAVY]([/color][/b]entmake td[b][color=NAVY])[/color][/b]
     [b][color=NAVY]([/color][/b]while fe
        [b][color=MAROON]([/color][/b]setq fd [b][color=GREEN]([/color][/b]entget fe[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
        [b][color=MAROON]([/color][/b]and [b][color=GREEN]([/color][/b]= [b][color=BLUE]([/color][/b]cdr [b][color=RED]([/color][/b]assoc 0 fd[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b] [color=#2f4f4f]"INSERT"[/color][b][color=GREEN])[/color][/b]
             [b][color=GREEN]([/color][/b]= [b][color=BLUE]([/color][/b]cdr [b][color=RED]([/color][/b]assoc 2 fd[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b] a[b][color=GREEN])[/color][/b]
             [b][color=GREEN]([/color][/b]setq fd [b][color=BLUE]([/color][/b]subst [b][color=RED]([/color][/b]cons 2 b[b][color=RED])[/color][/b] [b][color=RED]([/color][/b]assoc 2 fd[b][color=RED])[/color][/b] fd[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
        [b][color=MAROON]([/color][/b]entmake fd[b][color=MAROON])[/color][/b]
        [b][color=MAROON]([/color][/b]setq fe [b][color=GREEN]([/color][/b]entnext fe[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
     [b][color=NAVY]([/color][/b]entmake [b][color=MAROON]([/color][/b]list [b][color=GREEN]([/color][/b]cons 0 [color=#2f4f4f]"ENDBLK"[/color][b][color=GREEN])[/color][/b][b][color=GREEN]([/color][/b]cons 8 [color=#2f4f4f]"0"[/color][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]"_.REGENALL"[/color][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]prin1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

 

I know it will work with regular, anonymous and attributed

blocks. Stand alone inserts and nested blocks as well.

 

I have no clue as to dynamic

 

-David

Share this post


Link to post
Share on other sites
Lee Mac

David,

 

I don't believe that method will yield the desired results if attribute tags differ between the two block definitions.

 

Lee

Share this post


Link to post
Share on other sites
David Bethel

Lee, You're right. I did think the OP was looking for the geometry to be changed. I have many instances where the data is retained but the model is modified. -David

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×