Author Topic: Replace Block with a sifferent block that is in drawing (Command line)  (Read 2867 times)

0 Members and 1 Guest are viewing this topic.

MSTG007

  • Gator
  • Posts: 2603
  • I can't remeber what I already asked! I need help!
I am having a hard time trying to find a method to replace block without using the express tools BlockReplace Popup Box. I know the traditional -insert;block 1=block 2, but block 2 needs to be its own drawing. Is there a way to make it pull from within the drawing itself?
Civil3D 2020

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Here's a simple block replacement program to get you started:

Code - Auto/Visual Lisp: [Select]
  1. ;; Replace Block  -  Lee Mac
  2. ;; A simple block replacement tool.
  3.  
  4. (defun c:repblock nil
  5.     (repblock
  6.         "block1" ;; Name of block to be replaced
  7.         "block2" ;; Name of replacement block
  8.     )
  9. )
  10. (defun repblock ( old new / cmd dwg )
  11.     (cond
  12.         (   (not (tblsearch "block" old))
  13.             (princ (strcat "\nBlock \"" old "\" not defined in the active drawing."))
  14.         )
  15.         (   (not
  16.                 (or (tblsearch "block" new)
  17.                     (and (setq dwg (findfile (strcat new ".dwg")))
  18.                          (progn
  19.                              (setq cmd (getvar 'cmdecho))
  20.                              (setvar 'cmdecho 0)
  21.                              (command "_.-insert" dwg nil)
  22.                              (setvar 'cmdecho cmd)
  23.                              (tblsearch "block" new)
  24.                          )
  25.                     )
  26.                 )
  27.             )
  28.             (princ (strcat "\n\"" new ".dwg\" not found or could not be defined."))
  29.         )
  30.         (   t
  31.             (vlax-for blk (vla-get-blocks (LM:acdoc))
  32.                 (if (= :vlax-false (vla-get-isxref blk))
  33.                     (vlax-for obj blk
  34.                         (if (and (= "AcDbBlockReference" (vla-get-objectname obj))
  35.                                  (= (strcase old) (strcase (LM:blockname obj)))
  36.                                  (vlax-write-enabled-p obj)
  37.                             )
  38.                             (vla-put-name obj new)
  39.                         )
  40.                     )
  41.                 )
  42.             )
  43.             (if (= 2 (logand 2 (cdr (assoc 70 (tblsearch "block" new)))))
  44.                 (progn
  45.                     (setq cmd (getvar 'cmdecho))
  46.                     (setvar 'cmdecho 0)
  47.                     (command "_.attsync" "_n" new)
  48.                     (setvar 'cmdecho cmd)
  49.                 )
  50.             )
  51.             (vla-regen (LM:acdoc) acallviewports)
  52.         )
  53.     )
  54.     (princ)
  55. )
  56.  
  57. ;; Block Name  -  Lee Mac
  58. ;; Returns the true (effective) name of a supplied block reference
  59.                        
  60. (defun LM:blockname ( obj )
  61.     (if (vlax-property-available-p obj 'effectivename)
  62.         (defun LM:blockname ( obj ) (vla-get-effectivename obj))
  63.         (defun LM:blockname ( obj ) (vla-get-name obj))
  64.     )
  65.     (LM:blockname obj)
  66. )
  67.  
  68. ;; Active Document  -  Lee Mac
  69. ;; Returns the VLA Active Document Object
  70.  
  71. (defun LM:acdoc nil
  72.     (LM:acdoc)
  73. )
  74.  

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Quick and dirty .. no error checking

Code: [Select]
(DEFUN C:CHGBLK( / ss ssl new_value index e as)
 (setq ss (ssget))
 (prompt"\nReplacement block name (block must exist in drawing): ")
 (setq new_value (getstring t))
 (setq ssl(sslength ss))
 (setq index 0)
 (repeat ssl
  (setq e (entget(ssname ss index)))
  (setq as (assoc 2 e))
  (setq e (subst (cons 2 new_value) as e))
  (entmod e)
  (setq index (+ 1 index))
 )
 (princ)
)
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

MSTG007

  • Gator
  • Posts: 2603
  • I can't remeber what I already asked! I need help!
Sweet,
Civil3D 2020