Jump to content

Recommended Posts

Posted

Is there an easy way to use the BLOCKREPLACE command to replace multiple different blocks at once (e.g. replace 50 different blocks with another 50 different blocks)?

 

E.g. Replace blockA with blockF

Replace blockB with blockG etc

 

From what I have found through Google given BLOCKREPALCE is an express tool it can't be called up in a lisp program?

 

What I am looking for is a method to run a script/lisp which will replace blocks of certain names with other blocks of other names. Is this something a script could do?

  • Replies 20
  • Created
  • Last Reply

Top Posters In This Topic

  • Tharwat

    8

  • tailgame

    5

  • ttray33y

    2

  • Organic

    1

Top Posters In This Topic

Posted Images

Posted

Hi,

 

You can write your won program to replace blocks with another via preparing a list of '((Old_Block_Name . New_Block_Name)(........)) then you make a selection set of the inserted old block names and iterate through each one of them to replace the old with the new one as per arranged in the above-said list .

 

Good luck.

Posted

try this. BRE by alanjt

[url]http://www.cadtutor.net/forum/showthread.php?48458-Replace-Selected-Block-Or-Blocks-With-Another-Block&p=359585&viewfull=1#post359585[/url]

 

this is what i've been using

Posted
try this. BRE by alanjt

[url]http://www.cadtutor.net/forum/showthread.php?48458-Replace-Selected-Block-Or-Blocks-With-Another-Block&p=359585&viewfull=1#post359585[/url]

 

this is what i've been using

you can only select 1 replacement block with this routine.

Posted
Is there an easy way to use the BLOCKREPLACE command to replace multiple different blocks at once (e.g. replace 50 different blocks with another 50 different blocks)?

 

E.g. Replace blockA with blockF

Replace blockB with blockG etc

 

From what I have found through Google given BLOCKREPALCE is an express tool it can't be called up in a lisp program?

 

What I am looking for is a method to run a script/lisp which will replace blocks of certain names with other blocks of other names. Is this something a script could do?

 

Use this lisp.

(defun bup (block / *error* oc spc doc block)
 (vl-load-com)
 (defun *error* (msg)
   (and oc (setvar 'CMDECHO oc))
   (or	(wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
(princ (strcat "\n** Error: " msg " **"))
   )
   (princ)
 )

 (setq	spc
 (if
   (or
     (eq AcModelSpace
	 (vla-get-ActiveSpace
	   (setq doc
		  (vla-get-ActiveDocument
		    (vlax-get-acad-object)
		  )
	   )
	 )
     )
     (eq :vlax-true (vla-get-MSpace doc))
   )
    (vla-get-ModelSpace doc)
    (vla-get-PaperSpace doc)
 )
 )

 (setq oc (getvar 'CMDECHO))
 (setvar 'CMDECHO 0)

 (if (setq block (findfile block))
   (progn
     (vla-delete
(vla-insertblock
  spc
  (vlax-3D-point '(0. 0. 0.))
  block
  1.
  1.
  1.
  0.
)
     )
     (vl-cmdf "_.attsync" "_N" (vl-filename-base block))
   )
 )

 (setvar 'CMDECHO oc)
 (princ)
)
)

 

create the script like this:

(bup "C:\\AutoCAD\\RIG1\\menu\\B15.dwg")
(bup "C:\\AutoCAD\\RIG1\\menu\\A15.dwg")
(bup "C:\\AutoCAD\\RIG1\\menu\\C15.dwg")
(bup "C:\\AutoCAD\\RIG1\\menu\\D15.dwg")
(bup "C:\\AutoCAD\\RIG1\\menu\\TI.dwg")
(bup "C:\\AutoCAD\\RIG1\\menu\\E15.dwg")
(bup "C:\\AutoCAD\\RIG1\\menu\\IX23395.dwg")
(bup "C:\\AutoCAD\\RIG1\\menu\\T.dwg")
(bup "C:\\AutoCAD\\RIG1\\menu\\pia.dwg")

where every *.dwg on that script is the new block for replacement.

 

NOTE: you need wblock of new blocks. (see above script)

Posted

Hi ,

 

Here I wrote something in this regard to replace a list of block names "old" with "new" and so on.

The program should NOT work on Attributed , Dynamic blocks and it is better to get sure that the old blocks are NOT on locked/off layers .

 

(defun c:Test  (/ lst ss i sn en f)
 ;;--------------------------------------------;;
 ;; Author : Tharwat . Date: 09.June.2015	;;
 ;; A function to replace a set of block names ;;
 ;; with a nother as per listed in the list	;;
 ;;--------------------------------------------;;
 (setq lst '(("old" . "new")
             ("one" . "two")
             ("sad" . "fun")
             )
       )
 (if (setq ss (ssget "_X" (list '(0 . "INSERT")
                                (cons 2 (apply 'strcat (mapcar '(lambda (x) (strcat (car x) ",")) lst))))))
   (repeat (setq i (sslength ss))
     (setq sn (ssname ss (setq i (1- i)))
           en (entget sn))
     (if (and (setq f (assoc (cdr (assoc 2 en)) lst))
              (tblsearch "BLOCK" (cdr f))
              (entmake (append (list '(0 . "INSERT") (cons 2 (cdr f)))
                (vl-remove-if-not '(lambda (v) (member (car v) '(6 8 10 41 42 43 50 62))) en)))
            )
        (entdel sn)
        )
     )
   )
 (princ)
 )

Posted

http://forums.augi.com/showthread.php?160855-Change-a-Block-to-another-Block&p=1288519&viewfull=1#post1288519

 

(vl-load-com)

(defun c:RB () (c:ReplaceBlock))
(defun c:ReplaceBlock (/ *error* blockName ok acDoc space oBlock)

 (defun *error* (msg)
   (if acDoc
     (vla-endundomark acDoc)
   )
   (cond ((not msg))                                                   ; Normal exit
         ((member msg '("Function cancelled" "quit / exit abort")))    ; <esc> or (quit)
         ((princ (strcat "\n** Error: " msg " ** ")))                  ; Fatal error, display it
   )
   (princ)
 )

 (if
   (and
     (ssget "_:L" '((0 . "INSERT")))
     (or (/= ""
             (setq blockName
                    (strcase
                      (getstring
                        T
                        (strcat "\nEnter replacement block name "
                                (if *ReplacementBlockName*
                                  (strcat "<" *ReplacementBlockName* ">: ")
                                  ": "
                                )
                        )
                      )
                    )
             )
         )
         (setq blockName *ReplacementBlockName*)
     )
     (setq *ReplacementBlockName* blockName)
     (or (and (tblsearch "block" blockName) (setq ok T))
         (setq blockName (findfile (strcat blockName ".dwg")))
     )
   )
    (progn
      (vla-startundomark
        (setq acDoc (vla-get-activedocument (vlax-get-acad-object)))
      )
      (setq space (vlax-get acDoc
                            (if (= 1 (getvar 'cvport))
                              'paperspace
                              'modelspace
                            )
                  )
      )
      (vlax-for x (vla-get-activeselectionset acDoc)
        (vla-put-layer
          (setq oBlock (vla-insertblock
                         space
                         (vla-get-insertionpoint x)
                         blockName
                         (vla-get-xscalefactor x)
                         (vla-get-yscalefactor x)
                         (vla-get-zscalefactor x)
                         (vla-get-rotation x)
                       )
          )
          (vla-get-layer x)
        )
        (vla-put-color oBlock (vla-get-color x))
        (vla-delete x)

        ;; only insert from file on first run
        (if (not ok)
          (progn
            (setq blockName (vl-filename-base blockName))
            (setq ok T)
          )
        )
      )
    )
 )
 (*error* nil)
)

  • 9 months later...
Posted

Hello Tharwat

The lisp is great!! It is simple to use too.

 

Just one question, if old and new blocks are on different layers, is it possible to replace layer information? For example, if there are old block with layer 0 and new block with layer 1, when the old block is replaced with the new block, the new block layer would be 1. Currently the new replaced block layer is same as the old block layer.

 

Thank you.

Posted
Hello Tharwat

The lisp is great!! It is simple to use too.

 

Thank you.

You are welcome, and welcome to CADTutor.

 

Just one question, if old and new blocks are on different layers, is it possible to replace layer information? For example, if there are old block with layer 0 and new block with layer 1, when the old block is replaced with the new block, the new block layer would be 1. Currently the new replaced block layer is same as the old block layer.

 

You might be miss understood how the program works, actually the program should replace the old found blocks with the new blocks straight from the Block table so there won't be any layer name assigned to it to replace it with the old one.

Posted

Hello Tharwat,

Thanks for the reply.

I tested the lisp on several files, but I get the same result. After run the lisp, i was expecting old blocks replaced with new blocks. It replace the blocks but not applying property of the blocks.

 

 

I attached the one of the sample file I used for testing. attached test.lisp which is listed below.

I added old block names (imported -A, B,C..) and new blocks names (new-A,B,C..) on the list.

 

 

Is there any setting i'm missing?

 

I'm currently using Autocad 2014 and save the file in 2004 version.

 

Once again, thank you very much for the responds.

 

 

 

(defun c:Test (/ lst ss i sn en f)

;;--------------------------------------------;;

;; Author : Tharwat . Date: 09.June.2015 ;;

;; A function to replace a set of block names ;;

;; with a nother as per listed in the list ;;

;;--------------------------------------------;;

(setq lst '(("imported - A" . "NEW - A")

("imported - B" . "NEW - B")

("imported - C" . "NEW - C")

("imported - D" . "NEW - D")

("imported - E" . "NEW - E")

("imported - F" . "NEW - E")

 

)

)

(if (setq ss (ssget "_X" (list '(0 . "INSERT")

(cons 2 (apply 'strcat (mapcar '(lambda (x) (strcat (car x) ",")) lst))))))

(repeat (setq i (sslength ss))

(setq sn (ssname ss (setq i (1- i)))

en (entget sn))

(if (and (setq f (assoc (cdr (assoc 2 en)) lst))

(tblsearch "BLOCK" (cdr f))

(entmake (append (list '(0 . "INSERT") (cons 2 (cdr f)))

(vl-remove-if-not '(lambda (v) (member (car v) '(6 8 10 41 42 43 50 62))) en)))

)

(entdel sn)

)

)

)

(princ)

)

BLOCK REPLACE SAMPLE.dwg

test.lsp

BLOCK REPLACE SAMPLE.jpg

Posted

Hi,

 

I can modify the program to make it working as in your attached drawing but what would happen if you have two block references of the NEW block and there are on different layers?

If you use the program Replace Block with another block from the Express Tools, I think that should help in your case. otherwise you should have only one NEW block existed in the 'lst' variable and it does not matter how many OLD block references you have in your drawing.

Posted

Hello Tharwat,

 

"you should have only one NEW block existed in the 'lst' variable and it does not matter how many OLD block references you have in your drawing."

It is correct, each new blocks (reference block) will have a unique name.

 

Currently, the blocks are replaced but it kept property (layer) of old blocks.refer to "BLOCK REPLACE SAMPLE BEFORE AND AFTER.jpg" (the way express menu does)

 

I prefer if it could replace block and layers as shown on "PREFERRED BLOCK REPLACE .jpg"

 

Once again, thank you for the reply.

PREFERRED BLOCK REPLACE .jpg

BLOCK REPLACE SAMPLE BEFORE AND AFTER.jpg

Posted

Hi,

 

You did not get my point.

Suppose you have two block references in your drawing and each one of them on a different layer so which layer name the program should pick to assign it to the related old block reference in the list?

Posted

Hello Tharwat,

 

If that happens, i think it is ok to pick any layers or cancel the lisp whichever is easy.

 

 

Maybe I'm not fully understand, but all reference blocks will be unique in the beginning.

 

For example, there will be only one "NEW - A" block and "NEW - E" block in the drawing.(I added "New - E" on the sample to show the relationship. )

 

If there are multiple "NEW - E" blocks on the drawing because the "New - E" block is used for a reference block and replaced other blocks, all "New - E"blocks should have same layers.

 

Is it clear?

 

Let me know.

Posted
Hello Tharwat,

 

If that happens, i think it is ok to pick any layers or cancel the lisp whichever is easy.

 

Maybe I'm not fully understand, but all reference blocks will be unique in the beginning.

 

For example, there will be only one "NEW - A" block and "NEW - E" block in the drawing.(I added "New - E" on the sample to show the relationship. )

 

If there are multiple "NEW - E" blocks on the drawing because the "New - E" block is used for a reference block and replaced other blocks, all "New - E"blocks should have same layers.

 

 

Hi,

 

Please try the following and let me know:

 

(defun c:Test  (/ lst ss i sn en bn nw od o n)
 ;;--------------------------------------------;;
 ;; Author : Tharwat . Date: 23.Mar.2015	;;
 ;; A function to replace a set of block names ;;
 ;; with a nother as per listed in the list	;;
 ;;--------------------------------------------;;
 (setq lst '(("imported - A" . "NEW - A")
             ("imported - B" . "NEW - B")
             ("imported - C" . "NEW - C")
             ("imported - D" . "NEW - D")
             ("imported - E" . "NEW - E")
             ("imported - F" . "NEW - E")
             )
       )
 (if (setq ss (ssget "_X" (list '(0 . "INSERT") (cons 2 (apply 'strcat (mapcar '(lambda (x)
                                                                                  (strcat (car x) "," (cdr x) ","))
                                                                               lst)))
                                )
                     )
           )
   (repeat (setq i (sslength ss))
     (setq sn (ssname ss (setq i (1- i)))
           en (entget sn)
           bn (cdr (assoc 2 en))
           )
     (if (assoc bn lst)
       (setq od (cons (list bn en sn) od))
       (if (not (member bn nw))
         (setq nw (cons (list bn en) nw))
         )
       )
     )
   )
 (if (and nw od)
   (foreach x  od
     (and (setq o (assoc (car x) lst))
          (setq n (assoc (cdr o) nw))
          (entmake
            (append
              (list '(0 . "INSERT") (cons 2 (car n)))
              (vl-remove-if-not
                '(lambda (j) (member (car j) '(10 41 42 43 50)))
                (cadr x))
              (vl-remove-if-not
                '(lambda (k) (member (car k) '(6 8 62)))
                (cadr n))))
          (entdel (caddr x))
          )
     )
   )
 (princ)
 )(vl-load-com)

Posted

Hello Tharwat,

Yes it is What I was looking for!:D

Thank you very much.

Have a wonderful afternoon!!!

Posted
Hello Tharwat,

Yes it is What I was looking for!:D

Thank you very much.

Have a wonderful afternoon!!!

 

You are most welcome. :D

  • 4 years later...
Posted
On 6/9/2015 at 6:41 AM, Tharwat said:

Hi ,

 

Here I wrote something in this regard to replace a list of block names "old" with "new" and so on.

The program should NOT work on Attributed , Dynamic blocks and it is better to get sure that the old blocks are NOT on locked/off layers .

 

 


(defun c:Test  (/ lst ss i sn en f)
 ;;--------------------------------------------;;
 ;; Author : Tharwat . Date: 09.June.2015	;;
 ;; A function to replace a set of block names ;;
 ;; with a nother as per listed in the list	;;
 ;;--------------------------------------------;;
 (setq lst '(("old" . "new")
             ("one" . "two")
             ("sad" . "fun")
             )
       )
 (if (setq ss (ssget "_X" (list '(0 . "INSERT")
                                (cons 2 (apply 'strcat (mapcar '(lambda (x) (strcat (car x) ",")) lst))))))
   (repeat (setq i (sslength ss))
     (setq sn (ssname ss (setq i (1- i)))
           en (entget sn))
     (if (and (setq f (assoc (cdr (assoc 2 en)) lst))
              (tblsearch "BLOCK" (cdr f))
              (entmake (append (list '(0 . "INSERT") (cons 2 (cdr f)))
                (vl-remove-if-not '(lambda (v) (member (car v) '(6 8 10 41 42 43 50 62))) en)))
            )
        (entdel sn)
        )
     )
   )
 (princ)
 )
 

 

I'm sorry to dig this up from the grave but this is just what I was looking for. I was wondering if there was a way to insert the new block at a different scale as the old block? For example, the old block is at scale 1, but the new block is at scale 100. When inserted, the new block is 100 times larger than it needs to be.

Posted
On 2/9/2021 at 7:46 PM, AKitchens said:

I'm sorry to dig this up from the grave but this is just what I was looking for. I was wondering if there was a way to insert the new block at a different scale as the old block? For example, the old block is at scale 1, but the new block is at scale 100. When inserted, the new block is 100 times larger than it needs to be.

 

Just replace the part codes that related to entmake function then replace it with the following ones.

(entmake (append (list '(0 . "INSERT") (cons 2 (cdr f)))
                 (vl-remove-if-not '(lambda (v) (member (car v) '(6 8 10 50 62))) en)
                 '((41 . 100.0) (42 . 100.0) (43 . 100.0))))

 

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