Jump to content
Efeezy

Replace Selected Block Or Blocks With Another Block

Recommended Posts

Efeezy

DOES ANYONE HAVE A QUICK AND EASY WAY TO REPLACE A SELECTED BLOCK. ALSO IS THERE A WAY TO REPLACE MULTIPLE SLECTED BLOCKS AND REPLACE WITH A DIFFERENT BLOCK TYPICAL FOR ALL THE BLOCKS THAT ARE SELECTED? I HAVE A BIG A$$ PROJECT DUE FRIDAY AND NEED TO SWAP OUT ALOT OF BLOCKS NOW...:cry:

Share this post


Link to post
Share on other sites
lpseifert

Ouch... not so loud

Have you tried the Express Tools' Blockreplace?

Share this post


Link to post
Share on other sites
Efeezy

LOL sorry about that. Yes, I have express tools. The Blockreplace command replaces all the blocks i just want to replace the block that I have manually selected. I pulled an old stript and trying to alter it but not sure if I can pull it off though.

Share this post


Link to post
Share on other sites
Efeezy

nope couldn't get the script to work I am a not good at writing or modifying lisp my bock i need to replace is smoke 1 with smoke rb

 

------------------------------------------

 

[b]([/b]defun c:rblk [b][color=fuchsia]([/color][/b]/ pre nb ss i en ed[b][color=fuchsia])[/color][/b][b][color=fuchsia]([/color][/b]while [b][color=navy]([/color][/b]or [b][color=maroon]([/color][/b]not pre[b][color=maroon])[/color][/b][b][color=maroon]([/color][/b]not [b][color=green]([/color][/b]snvalid pre[b][color=green])[/color][color=maroon])[/color][color=navy])[/color][/b][b][color=navy]([/color][/b]setq pre [b][color=maroon]([/color][/b]getstring [color=darkslategray]"\nExisting BLOCKs Name Prefix:   "[/color][b][color=maroon])[/color][color=navy])[/color][color=fuchsia])[/color][/b][b][color=fuchsia]([/color][/b]while [b][color=navy]([/color][/b]not nb[b][color=navy])[/color][/b][b][color=navy]([/color][/b]setq nb [b][color=maroon]([/color][/b]getstring [color=darkslategray]"\nNew BLOCK Name:   "[/color][b][color=maroon])[/color][color=navy])[/color][/b][b][color=navy]([/color][/b]cond [b][color=maroon]([/color][color=green]([/color][/b]not [b][color=blue]([/color][/b]snvalid nb[b][color=blue])[/color][color=green])[/color][/b][b][color=green]([/color][/b]setq nb nil[b][color=green])[/color][color=maroon])[/color][/b][b][color=maroon]([/color][color=green]([/color][/b]tblsearch [color=darkslategray]"BLOCK"[/color] nb[b][color=green])[/color][color=maroon])[/color][/b][b][color=maroon]([/color][color=green]([/color][/b]findfile [b][color=blue]([/color][/b]strcat nb [color=darkslategray]".DWG"[/color][b][color=blue])[/color][color=green])[/color][/b][b][color=green]([/color][/b]command [color=darkslategray]"_.INSERT"[/color] nb[b][color=green])[/color][/b][b][color=green]([/color][/b]command[b][color=green])[/color][color=maroon])[/color][/b][b][color=maroon]([/color][/b]T [b][color=green]([/color][/b]setq nb nil[b][color=green])[/color][color=maroon])[/color][color=navy])[/color][color=fuchsia])[/color][/b][b][color=fuchsia]([/color][/b]and [b][color=navy]([/color][/b]setq ss [b][color=maroon]([/color][/b]ssget [color=darkslategray]"X"[/color] [b][color=green]([/color][/b]list [b][color=blue]([/color][/b]cons 0 [color=darkslategray]"INSERT"[/color][b][color=blue])[/color][/b][b][color=blue]([/color][/b]cons 2 [b][color=red]([/color][/b]strcat pre [color=darkslategray]"*"[/color][b][color=red])[/color][color=blue])[/color][color=green])[/color][color=maroon])[/color][color=navy])[/color][/b][b][color=navy]([/color][/b]setq i [b][color=maroon]([/color][/b]sslength ss[b][color=maroon])[/color][color=navy])[/color][/b][b][color=navy]([/color][/b]while [b][color=maroon]([/color][/b]not [b][color=green]([/color][/b]minusp [b][color=blue]([/color][/b]setq i [b][color=red]([/color][/b]1- i[b][color=red])[/color][color=blue])[/color][color=green])[/color][color=maroon])[/color][/b][b][color=maroon]([/color][/b]setq en [b][color=green]([/color][/b]ssname ss i[b][color=green])[/color][/b]                    ed [b][color=green]([/color][/b]entget en[b][color=green])[/color][color=maroon])[/color][/b][b][color=maroon]([/color][/b]entmod [b][color=green]([/color][/b]subst [b][color=blue]([/color][/b]cons 2 nb[b][color=blue])[/color][/b] [b][color=blue]([/color][/b]assoc 2 ed[b][color=blue])[/color][/b] ed[b][color=green])[/color][color=maroon])[/color][/b][b][color=maroon]([/color][/b]entupd en[b][color=maroon])[/color][color=navy])[/color][color=fuchsia])[/color][/b][b][color=fuchsia]([/color][/b]prin1[b][color=fuchsia])[/color][color=black])[/color][/b]

Edited by SLW210

Share this post


Link to post
Share on other sites
lpseifert

I've had this in my archives for a long time, hope it works for you

rblock.zip

Share this post


Link to post
Share on other sites
manirpg
:D:DGood one.........its working fine...Mr.Efeezy, Are u got solution? if yes complete the target before due..................Congrats...:D:D

Share this post


Link to post
Share on other sites
gilsoto13
DOES ANYONE HAVE A QUICK AND EASY WAY TO REPLACE A SELECTED BLOCK. ALSO IS THERE A WAY TO REPLACE MULTIPLE SLECTED BLOCKS AND REPLACE WITH A DIFFERENT BLOCK TYPICAL FOR ALL THE BLOCKS THAT ARE SELECTED? I HAVE A BIG A$$ PROJECT DUE FRIDAY AND NEED TO SWAP OUT ALOT OF BLOCKS NOW...:cry:

 

try Bre.lsp from Alan Thompson... It's works quite well..

 

See updated CODE

Edited by SLW210
Removed Lisp File

Share this post


Link to post
Share on other sites
alanjt
try Bre.lsp from Alan Thompson... It's works quite well..

I forgot all about that one. Haven't used it since the project I wrote it for.

 

Original link

Share this post


Link to post
Share on other sites
qball

bre is good! :D

Share this post


Link to post
Share on other sites
alanjt
bre is good! :D

 

Glad you like it. I wish I could remove that old version gilsoto posted, but here's an updated version:

 

(defun c:BRE (/ *error* blk f ss temp)
 ;; Replace multiple instances of selected blocks (can be different) with selected block
 ;; Size and Rotation will be taken from original block and original will be deleted
 ;; Required subroutines: AT:GetSel
 ;; Alan J. Thompson, 02.09.10

 (vl-load-com)

 (defun *error* (msg)
   (and f *AcadDoc* (vla-endundomark *AcadDoc*))
   (if (and msg (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*QUIT*,")))
     (princ (strcat "\nError: " msg))
   )
 )

 (if
   (and
     (AT:GetSel
       entsel
       "\nSelect replacement block: "
       (lambda (x / e)
         (if
           (and
             (eq "INSERT" (cdr (assoc 0 (setq e (entget (car x))))))
             (/= 4 (logand (cdr (assoc 70 (tblsearch "BLOCK" (cdr (assoc 2 e))))) 4))
             (/= 4 (logand (cdr (assoc 70 (entget (tblobjname "LAYER" (cdr (assoc 8 e)))))) 4))
           )
            (setq blk (vlax-ename->vla-object (car x)))
         )
       )
     )
     (princ "\nSelect blocks to be repalced: ")
     (setq ss (ssget "_:L" '((0 . "INSERT"))))
   )
    (progn
      (setq f (not (vla-startundomark
                     (cond (*AcadDoc*)
                           ((setq *AcadDoc* (vla-get-activedocument (vlax-get-acad-object))))
                     )
                   )
              )
      )
      (vlax-for x (setq ss (vla-get-activeselectionset *AcadDoc*))
        (setq temp (vla-copy blk))
        (mapcar (function (lambda (p)
                            (vl-catch-all-apply
                              (function vlax-put-property)
                              (list temp p (vlax-get-property x p))
                            )
                          )
                )
                '(Insertionpoint Rotation XEffectiveScaleFactor YEffectiveScaleFactor
                  ZEffectiveScaleFactor
                 )
        )
        (vla-delete x)
      )
      (vla-delete ss)
      (*error* nil)
    )
 )
 (princ)
)

(defun AT:GetSel (meth msg fnc / ent good)
 ;; meth - selection method (entsel, nentsel, nentselp)
 ;; msg - message to display (nil for default)
 ;; fnc - optional function to apply to selected object
 ;; Ex: (AT:GetSel entsel "\nSelect arc: " (lambda (x) (eq (cdr (assoc 0 (entget (car x)))) "ARC")))
 ;; Alan J. Thompson, 05.25.10
 (setvar 'errno 0)
 (while (not good)
   (setq ent (meth (cond (msg)
                         ("\nSelect object: ")
                   )
             )
   )
   (cond
     ((vl-consp ent)
      (setq good (cond ((or (not fnc) (fnc ent)) ent)
                       ((prompt "\nInvalid object!"))
                 )
      )
     )
     ((eq (type ent) 'STR) (setq good ent))
     ((setq good (eq 52 (getvar 'errno))) nil)
     ((eq 7 (getvar 'errno)) (setq good (prompt "\nMissed, try again.")))
   )
 )
)

Edited by alanjt

Share this post


Link to post
Share on other sites
justindm

I'm trying to run the above but get an error: "Error: no function definition: VLAX-ENAME->VLA-OBJECT"

 

Some of the blocks I'm selecting have spaces, dashes and underscores...could that be a problem?

Share this post


Link to post
Share on other sites
alanjt

add (vl-load-com) to the code

Share this post


Link to post
Share on other sites
alanjt

Recopy the code, I added (vl-load-com).

Share this post


Link to post
Share on other sites
justindm

Perfecto!!!!!!!! :shock:

 

You're so awesome. This is awesome. You're a life saver.

Can't thank you enough. :D

Share this post


Link to post
Share on other sites
alanjt
Perfecto!!!!!!!! :shock:

 

You're so awesome. This is awesome. You're a life saver.

Can't thank you enough. :D

Enjoy.

 

 

 

..............

Share this post


Link to post
Share on other sites
yellowburros123

Thanks! Bre worked great

Share this post


Link to post
Share on other sites
alanjt
Thanks! Bre worked great

You're welcome. Be sure to use the one I posted, not the very old version gilsoto posted.

Share this post


Link to post
Share on other sites
Saqib_theleo
Glad you like it. I wish I could remove that old version gilsoto posted, but here's an updated version:

 

(defun c:BRE (/ *error* blk f ss temp)
 ;; Replace multiple instances of selected blocks (can be different) with selected block
 ;; Size and Rotation will be taken from original block and original will be deleted
 ;; Required subroutines: AT:GetSel
 ;; Alan J. Thompson, 02.09.10

 (vl-load-com)

 (defun *error* (msg)
   (and f *AcadDoc* (vla-endundomark *AcadDoc*))
   (if (and msg (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*QUIT*,")))
     (princ (strcat "\nError: " msg))
   )
 )

 (if
   (and
     (AT:GetSel
       entsel
       "\nSelect replacement block: "
       (lambda (x / e)
         (if
           (and
             (eq "INSERT" (cdr (assoc 0 (setq e (entget (car x))))))
             (/= 4 (logand (cdr (assoc 70 (tblsearch "BLOCK" (cdr (assoc 2 e))))) 4))
             (/= 4 (logand (cdr (assoc 70 (entget (tblobjname "LAYER" (cdr (assoc 8 e)))))) 4))
           )
            (setq blk (vlax-ename->vla-object (car x)))
         )
       )
     )
     (princ "\nSelect blocks to be repalced: ")
     (setq ss (ssget "_:L" '((0 . "INSERT"))))
   )
    (progn
      (setq f (not (vla-startundomark
                     (cond (*AcadDoc*)
                           ((setq *AcadDoc* (vla-get-activedocument (vlax-get-acad-object))))
                     )
                   )
              )
      )
      (vlax-for x (setq ss (vla-get-activeselectionset *AcadDoc*))
        (setq temp (vla-copy blk))
        (mapcar (function (lambda (p)
                            (vl-catch-all-apply
                              (function vlax-put-property)
                              (list temp p (vlax-get-property x p))
                            )
                          )
                )
                '(Insertionpoint Rotation XEffectiveScaleFactor YEffectiveScaleFactor
                  ZEffectiveScaleFactor
                 )
        )
        (vla-delete x)
      )
      (vla-delete ss)
      (*error* nil)
    )
 )
 (princ)
)

(defun AT:GetSel (meth msg fnc / ent good)
 ;; meth - selection method (entsel, nentsel, nentselp)
 ;; msg - message to display (nil for default)
 ;; fnc - optional function to apply to selected object
 ;; Ex: (AT:GetSel entsel "\nSelect arc: " (lambda (x) (eq (cdr (assoc 0 (entget (car x)))) "ARC")))
 ;; Alan J. Thompson, 05.25.10
 (setvar 'errno 0)
 (while (not good)
   (setq ent (meth (cond (msg)
                         ("\nSelect object: ")
                   )
             )
   )
   (cond
     ((vl-consp ent)
      (setq good (cond ((or (not fnc) (fnc ent)) ent)
                       ((prompt "\nInvalid object!"))
                 )
      )
     )
     ((eq (type ent) 'STR) (setq good ent))
     ((setq good (eq 52 (getvar 'errno))) nil)
     ((eq 7 (getvar 'errno)) (setq good (prompt "\nMissed, try again.")))
   )
 )
)

Hi Alan J,

I've been searching for a block replace Lisp for three days but couldn't find any good one. I found this lisp it is working nice, I wanted to ask is it possible to make this lisp a batch block replacement so that you don't need to open every drawing to change blocks. I have to replace old blocks from new blocks in more than 300 drawings.

If it is possible plz tell.

Thanks for your previous (BRE) Lisp too.

Share this post


Link to post
Share on other sites
Traci Dennis

Do you have a similar routine that wors with attributes? I often have to change (blocks with attributes) parts of my drawing from proposed to existing. The blocks are the same except for color and font style. Thanks

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

×