Author Topic: substitute block with another block, is this the best way to do it?  (Read 3312 times)

0 Members and 1 Guest are viewing this topic.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: substitute block with another block, is this the best way to do it?
« Reply #15 on: December 30, 2021, 12:56:18 PM »
I’m not where I can write code for an example but this is something that I do regularly.

Step 1: I have a simple drawing with all replacement blocks defined in it
Step 2: I insert said drawing
Step 3: Entmod assoc 2 with the new block name on each insert.
Step 4: Purge said drawing

in that way you update the block definition don't replace a block with a different block, isn't it?

Let me explain in a little more detail now that I am not typing on my phone.

Suppose I have a block in my drawing (not a dynamic block) called "block1" and i want to replace it with a block called "block2" and want to keep the exact reference points. The first thing you need to do is get the block defined in the drawing, but you also do not want to redefine "block1" because you want to use it elsewhere in the drawing or you don't want to change all of them.

This can be accomplished by simply inserting the block and then deleting it.

Now that the block definition is in the block table, you can create your selection set of inserts and simply update the block name like so:

Code - Auto/Visual Lisp: [Select]
  1. (DEFUN C:CHGBLK( / ss ssl new_value index e as)
  2.  (setq ss (ssget))
  3.  (prompt"\nReplacement block name (block must exist in drawing): ")
  4.  (setq new_value (getstring t))
  5.  (setq ssl(sslength ss))
  6.  (setq index 0)
  7.  (repeat ssl
  8.   (setq e (entget(ssname ss index)))
  9.   (setq as (assoc 2 e))
  10.   (setq e (subst (cons 2 new_value) as e))
  11.   (entmod e)
  12.   (setq index (+ 1 index))
  13.  )
  14.  (PRINC)
  15. )
  16.  


Of course this isn't the most concise or most elegant way of doing this, but it does work and I've used this tool for perhaps 20 years to do just what you are wanting to do.
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