Jump to content

Replace Selected Block Or Blocks With Another Block


Efeezy

Recommended Posts

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

So, you want to replace one attribute block with another and transfer attribute values from the original block to the replacement?

Link to comment
Share on other sites

  • Replies 37
  • Created
  • Last Reply

Top Posters In This Topic

  • alanjt

    9

  • Efeezy

    3

  • lpseifert

    2

  • justindm

    2

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

 

Rename block references to use new block definition; ATTSYNC to synchronise attributes (assuming attribute tags are identical).

 

Done!

Link to comment
Share on other sites

Rename block references to use new block definition; ATTSYNC to synchronise attributes (assuming attribute tags are identical).

 

Done!

 

And you are wanting to replace all instances of said block.

Link to comment
Share on other sites

  • 4 months later...

 

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

 

Nice work alanjt . Can you update your code to replace selected instances of a block (or blocks) with another block, sync attributes.

Link to comment
Share on other sites

  • 1 month later...

i need to replace one block with another block with same coordinates. pls. somebody give LISP program. If i run that program, without opening any of drawings, in the drawing directory it should replace all blocks with what i have selected and should replace all blocks. it will be agreat help for me

Link to comment
Share on other sites

  • 6 months later...

Continue to read the thread and you will find a lisp routine that works great.. The command prompt is BRE...

Link to comment
Share on other sites

  • 6 months later...

"Error: no function definition: VLAX-ENAME->VLA-OBJECT"

Hi, I got this error when use it like justtindm. Previous time it still work ok but I don't know today I got this error.

Does it work correctly with att block?

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

Link to comment
Share on other sites

Hi, I reset the computer then it work well again. thank you, it's very cool :lol:

The problem may be cause when I load many lisp and it conflict with each other.

Link to comment
Share on other sites

If the code is already posted in the thread, please don't post it again. If I ever update the code, an older version will still reside in a post I cannot edit.

 

Glad you got it working. Add (vl-load-com) before or after the code. I have that in my startup file, so I have a tendency to forget to add it to commands. Apologies.

Link to comment
Share on other sites

  • 1 month later...

I have been looking and have not yet found what I am looking for. I have blocks that come from MicroStation into ACAD. When they come to ACAD, they have the same name, however they also have a sequential suffix.

(I.E. MH1, MH2,...) I have found a lisp that will allow me to replace all "MH#" with MH, but only one type at a time. (MH#; then run the lisp again and do LP#, etc...).

What I am getting at I would like to eventually have one lisp that will basically be, if (XXX## equal true, then replace with XXX) and do this for each type block type. I have no problem adding to a routine or building it. I really do not know where to get started.

Thank you for your time and patience in this.

rblk.lsp is what I have found for one by one- just a step above REPLACEBLOCK command.

rblk.lsp

Link to comment
Share on other sites

I have been looking and have not yet found what I am looking for. I have blocks that come from MicroStation into ACAD. When they come to ACAD, they have the same name, however they also have a sequential suffix.

(I.E. MH1, MH2,...) I have found a lisp that will allow me to replace all "MH#" with MH, but only one type at a time. (MH#; then run the lisp again and do LP#, etc...).

What I am getting at I would like to eventually have one lisp that will basically be, if (XXX## equal true, then replace with XXX) and do this for each type block type. I have no problem adding to a routine or building it. I really do not know where to get started.

Thank you for your time and patience in this.

rblk.lsp is what I have found for one by one- just a step above REPLACEBLOCK command.

 

Welcome to CADTutor.

 

I don't have time to read the earlier pages of this thread, which may already have an answer for you, but this recent thread may be of use:

 

http://forums.augi.com/showthread.php?160855-Change-a-Block-to-another-Block

 

 

 

Cheers

Link to comment
Share on other sites

  • 4 months later...
  • 1 month later...
  • 1 year later...

Hey Guys,

 

I'm very new to CAD but I am a really fast learner as I am sure many of you are. I have added this LSP into our cad and it has worked magically; however, I am having an issue for some unknown reason and I was hoping someone could point out to me what the issue may be.

 

I am designing a lighting blueprint for an engineering firm. We have several blocks for 2x4 normal, emergency lights and all other various sizes. I have had the BRE command work perfectly for our 1/2x4 blocks which has saved me a lot of time of deleting and replacing the block.

 

When I attempt to use the BRE command on the 2x4 blocks, the end result deletes the block "TO BE" replaced. Is there an issue with my blocks?

 

Here are some of the troubleshooting I have already attempted.

- I have created now blocks, saved with new names and replaced ALL blocks on the model with the new named block. Same result.

- I have edited the blocks and changed the current layer for each item within the block. Same result.

 

Any ideas?

Also, not sure if this matters. I am currently working on AutoCad 2017

 

Thanks for any attempts to assist.

Bryan

Link to comment
Share on other sites

Blocks are indeed deleted as part of the replacement process. Without an example dwg I can only guess that the blocks are mismatched in terms of scale or insertion point. Try zooming to extents after using the code.

Link to comment
Share on other sites

  • 2 years later...

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