Purge multiple dwg files in folder

Hi everyone,

I was a bit surprised there is no topic on this one. I am currently trying to purge a bunch of drawings (Layers and unused Blockreferences). For this I couldn’t find nodes. First of all: Am I overlooking something? Is the a super easy way I am not aware of? Secondly: with the basics of Paolo
(thanks again for the work) in Run Dynamo Script on entire folder of dwg's - #17 by Paolo_Emilio_Serra1 I managed to get a Python script working. See below and attached.

def python_purge(path):

	adoc = DocumentCollectionExtension.Open(acapp.DocumentManager, path, False)  # Open for Write
	acapp.DocumentManager.CurrentDocument = adoc
	
	if adoc is None:
		return False
	
	ret = True
	err = []

	with adoc.LockDocument():
		with adoc.Database as db:
			with db.TransactionManager.StartTransaction() as t:

				bt = t.GetObject(db.BlockTableId, OpenMode.ForWrite)  # Get BlockTable
					
				oid_coll = ObjectIdCollection()
				for oid in bt:
					oid_coll.Add(oid)
				db.Purge(oid_coll)
				
				for oid in oid_coll:
					acSymTblRec = t.GetObject(oid, OpenMode.ForWrite)  
					
					try:
						acSymTblRec.Erase(True)
					except Exception as e:
						pass
				
				
				lt = t.GetObject(db.LayerTableId, OpenMode.ForWrite)  # Get LayerTable
				
				lid_coll = ObjectIdCollection()
				for lid in lt:
					lid_coll.Add(lid)
				
				db.Purge(lid_coll)
				
				for lid in lid_coll:
					acSymTblRec = t.GetObject(lid, OpenMode.ForWrite)  
					
					try:
						acSymTblRec.Erase(True)
					except Exception as e:
						err.append(e)
						
				t.Commit()
	if adoc:
		DocumentExtension.CloseAndSave(adoc, path)
	if len(err) > 0:
		ret = err	
		
	return ret

PurgeExternalDrawing.dyn (10.1 KB)

2 Likes

There are alternative tools for batch purging. I’m guessing this could be a reason why there are no posts on this?
The Batch Save Utility that comes installed with Civil 3D allows for this (it comes with a lisp routine to purge and audit) and there are some free apps such as Drawing Purge. Of course, you may want to use dynamo for this for various reasons too.

Thanks a lot for pointing out this tool, I never knew it existed!