Explode Dynamic Block and create new static block

Explode Dynamic Block and create new static block

colab_01
Contributor Contributor
939 Views
11 Replies
Message 1 of 12

Explode Dynamic Block and create new static block

colab_01
Contributor
Contributor

I need some help in this conde!? i can't read com autocad

 

This routine must perform the following steps:

  1. Iterates through all INSERT entities in the drawing.
  2. Checks if each entity represents a dynamic block.
  3. If a dynamic block is found, it explodes the block while preserving attribute values.
  4. Creates a new static block using the exploded entities and attribute values.
  5. Skips entities that are not dynamic blocks.
  6. Outputs a message indicating the completion of the process.

 

(defun C:ExplodeAndCreateBlocks (/ ss)
(vl-load-com)

;; Function to explode a dynamic block and preserve attribute values
(defun explode_dynamic_block (dyn_blk)
(setq att_values '())
(vlax-for obj (vla-get-attributes dyn_blk)
(setq att_value (vla-get-textstring obj))
(setq att_values (cons att_value att_values))
)
(vla-explode dyn_blk)
att_values
)

;; Function to create a new block with attribute values
(defun create_block_with_attributes (blk_name base_pt att_values)
(setq new_blk_name (strcat blk_name "_Static"))
(setq new_blk (vla-copy base_blk))
(vla-delete new_blk)
(vla-put-name new_blk new_blk_name)
(vla-insert new_blk base_pt)
(vlax-for obj (vla-get-attributes new_blk)
(vla-delete obj)
)
(vlax-for att new_blk
(vla-set-textstring att (car att_values))
(setq att_values (cdr att_values))
)
)

;; Get selection set of all dynamic block inserts
(setq ss (ssget "_X" '((0 . "INSERT"))))
(if ss
(progn
(setq total_blocks (sslength ss))
(setq count 0)
(repeat total_blocks
(setq count (1+ count))
(setq obj (ssname ss count))
(setq blk_name (cdr (assoc 2 (entget obj))))
(setq base_blk (vlax-ename->vla-object obj))
(setq base_pt (vlax-curve-getstartpoint base_blk))
(setq is_dynamic (vl-catch-all-error-p
(vl-catch-all-apply
'(lambda ()
(vla-get-isdynamicblock base_blk)
)
)
)
)
(if (not is_dynamic)
(prompt (strcat "\nSkipping block " (itoa count) " of " (itoa total_blocks) " - " blk_name " (not a dynamic block)"))
(progn
(prompt (strcat "\nProcessing block " (itoa count) " of " (itoa total_blocks) " - " blk_name))
(setq att_values (explode_dynamic_block base_blk))
(create_block_with_attributes blk_name base_pt att_values)
)
)
)
(princ "\nAll dynamic blocks exploded and converted to static blocks.")
)
(princ "\nNo dynamic blocks found in the drawing.")
)
(princ)
)

0 Likes
940 Views
11 Replies
Replies (11)
Message 2 of 12

ec-cad
Collaborator
Collaborator

(defun create_block_with_attributes (blk_name base_pt att_values)
(setq new_blk_name (strcat blk_name "_Static"))
(setq new_blk (vla-copy base_blk))<-- here you make a copy of base_blk
(vla-delete new_blk)<-- and here, you Delete it? Shouldn't that be (vla-delete base_blk) ?
(vla-put-name new_blk new_blk_name)
(vla-insert new_blk base_pt)
(vlax-for obj (vla-get-attributes new_blk)
(vla-delete obj)
)

0 Likes
Message 3 of 12

komondormrex
Mentor
Mentor

there is a converttostaticblock method, which you can use for your purpose. in case you missed it.

 

 

(vla-converttostaticblock (vlax-ename->vla-object (car (entsel "\nDyn block: "))) "Static_Block_Name")

 

 

komondormrex_0-1713553033667.png

 

0 Likes
Message 4 of 12

colab_01
Contributor
Contributor

but I want to delete the items / drawings that are in the visibilities.

 

converttostaticblock keeps the drawings but deletes the visibilities.

0 Likes
Message 5 of 12

colab_01
Contributor
Contributor

when i read the lisp, appears this message :"; error: bad argument value: AcDbCurve 43" do you know how to solve!?

0 Likes
Message 6 of 12

colab_01
Contributor
Contributor

i didn't mencioned, bu i have very limited knowledge in lisp and how to debug lisp code.

 

I was able to use the ChatGPT implementation on the OpenAI website to write the lisp code above

0 Likes
Message 7 of 12

komondormrex
Mentor
Mentor

you mean attributes?

0 Likes
Message 8 of 12

komondormrex
Mentor
Mentor

@ec-cad wrote:

(vlax-for obj (vla-get-attributes new_blk)


(vla-getattributes new_blk) gives you a variant not a collection

0 Likes
Message 9 of 12

ec-cad
Collaborator
Collaborator

komondormrex,

That code snippit was a part of the original post lisp, not mine.

I know it "gives you a variant not a collection"

I was merely asking why the OP creates a copy then deletes it.

ECCAD

0 Likes
Message 10 of 12

colab_01
Contributor
Contributor

Can you make this lisp routine perform the following steps:

  1. Iterates through all INSERT entities in the drawing.
  2. Checks if each entity represents a dynamic block.
  3. If a dynamic block is found, it explodes the block while preserving attribute values.
  4. Creates a new static block using the exploded entities and attribute values.
  5. Skips entities that are not dynamic blocks.
  6. Outputs a message indicating the completion of the process.
0 Likes
Message 11 of 12

Sea-Haven
Mentor
Mentor

Try this code by GRR.

 

Un-dynamic a block - LISP - AutoLISP, Visual LISP & DCL - AutoCAD Forums (cadtutor.net)

 

Re all done

(alert (strcat (rtos cnt 2 0) " blocks converted"))
0 Likes
Message 12 of 12

colab_01
Contributor
Contributor

Hi,

 

The only problem with that lisp, is tha it only remove the visibility option on dynamic block. and i want to erase all hidden objets.

 

this i what the lisp do, it keeps all drawings and i want to erase it, its possible!?

colab_01_0-1713862923303.png

 

0 Likes