Skip to content

Instantly share code, notes, and snippets.

@CEXT-Dan
Created November 5, 2025 01:11
Show Gist options
  • Select an option

  • Save CEXT-Dan/87675323bf83b053daa155217710bc45 to your computer and use it in GitHub Desktop.

Select an option

Save CEXT-Dan/87675323bf83b053daa155217710bc45 to your computer and use it in GitHub Desktop.
explode blocks in multiple drawings with Python for AutoCAD
# see https://github.com/CEXT-Dan/PyRx
import traceback
from pyrx import Db, Ed, Ge, Ap, Rx, Gs
import os
# add mod to path
def make_mod_path(filepath, suffix="_mod_"):
base, extension = os.path.splitext(filepath)
new_filepath = f"{base}{suffix}{extension}"
return new_filepath
# find refs named with name in modelspace
def get_refs_from_model(db: Db.Database, name: str):
filtered_refs = []
ms = db.modelSpace()
refs = [Db.BlockReference(id) for id in ms.objectIds(Db.BlockReference.desc())]
for ref in refs:
if ref.getBlockName() == name:
ref.upgradeOpen()
filtered_refs.append(ref)
return filtered_refs
# explode
def exploderefs(db: Db.Database, name: str):
for ref in get_refs_from_model(db, name):
ref.explodeToOwnerSpace()
ref.erase()
# define a new command
@Ap.Command()
def multiexplode():
try:
for file in Ap.Application.listFilesInPath("E:\\temp", ".dwg"):
sdb = Db.Database(False, True)
sdb.readDwgFile(file)
sdb.closeInput(True)
exploderefs(sdb, "cartiglio")
sdb.saveAs(make_mod_path(file))
except Exception as err:
traceback.print_exception(err)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment