# Run a script of a Dynamo in multiple dwg files

**URL:** <https://forum.dynamobim.com/t/run-a-script-of-a-dynamo-in-multiple-dwg-files/86066>\
**Category:** Civil 3D\
**Tags:** python, dynamo\
**Created:** [February 13, 2023, 8:43am UTC](https://forum.dynamobim.com/t/run-a-script-of-a-dynamo-in-multiple-dwg-files/86066 "2023-02-13T08:43:04Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![hosneyalaa](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/hosneyalaa/32/100865_2.png) [@hosneyalaa](https://forum.dynamobim.com/u/hosneyalaa)\
**Post date:** [February 14, 2023, 12:32pm UTC](https://forum.dynamobim.com/t/run-a-script-of-a-dynamo-in-multiple-dwg-files/86066/5 "2023-02-14T12:32:48Z")

</div>

hi  
You must search in layouts

Need example from

> [@Sheet Set - Replace specific block on each sheet with a new block](https://forum.dynamobim.com/t/sheet-set-replace-specific-block-on-each-sheet-with-a-new-block/77333/12):
>
> Can’t believe I actually got this working. It’s kind of a mess but it works and I’m super proud of it! First time doing a lot of stuff here for me. This will replace a block with another block by name given a folder of dwgs across all of the layouts in those dwgs. [ReplaceBlocksAcrossSheets.dyn](https://forum.dynamobim.com/uploads/short-url/5qDu1e6qunKaIpQBZPrcFmrOmyv.dyn) (18.2 KB) Thanks so much for all your help @mzjensen sob I’d be totally stuck without ya.

```auto
import clr
import os
import re

# Add Assemblies for AutoCAD APIs
clr.AddReference('acmgd')
clr.AddReference('acdbmgd')
clr.AddReference('accoremgd')

# Import references from AutoCAD
from Autodesk.AutoCAD.Runtime import *
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.EditorInput import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *

import Autodesk.AutoCAD.ApplicationServices.Application as acapp
import Autodesk.AutoCAD.ApplicationServices.DocumentCollectionExtension

#------------------------

DWGPaths = IN[0]
oldBlockReferenceName = IN[1]
newBlockReferenceName = IN[2]

#Real Code

for DWGPath in DWGPaths:
    adoc = DocumentCollectionExtension.Open(acapp.DocumentManager, DWGPath, False)

    with adoc.LockDocument():
        with adoc.Database as db:
            with db.TransactionManager.StartTransaction() as t:
                
                
                ###get ID for block reference replacement
                blockTable = t.GetObject(db.BlockTableId, OpenMode.ForWrite)
                
                newBlockID = [block for block in blockTable if block.Name == newBlockReferenceName][0]
                
                ###
                
                layoutObjectIDs = []
                layoutBlockTableIDs = []
                
                layoutDict = t.GetObject(db.LayoutDictionaryId, OpenMode.ForWrite)
                
                for layout in layoutDict:
                	if "Model" not in layout.Key:
                		layoutObjectIDs.append(layout.Value)
                
                for layoutObjectID in layoutObjectIDs:
                	layout = t.GetObject(layoutObjectID, OpenMode.ForWrite)
                	
                	layoutBlockTableID = layout.BlockTableRecordId
                	layoutBlockTableIDs.append(layoutBlockTableID)
                
                for layoutBlockTableID in layoutBlockTableIDs:
                	layoutBTR = t.GetObject(layoutBlockTableID, OpenMode.ForWrite)
                	
                	blockReferenceIDs = [blockReferenceID for blockReferenceID in layoutBTR]
                	
                	
                	
                	
                	
                	
                	
                	
                	
                	blockReferences = []
                	blockReferencePositions = []
                	
                	for blockReferenceID in blockReferenceIDs:
                		object = t.GetObject(blockReferenceID, OpenMode.ForWrite)
                		
                		if object.GetType() == Autodesk.AutoCAD.DatabaseServices.BlockReference and object.Name == oldBlockReferenceName:
                			blockReferences.append(object)
                			blockReferencePositions.append(object.Position)
                		
                		####

                		
                		for object in blockReferences:
                			object.Erase(True)
                		
            		for position in blockReferencePositions:
            			newbr = BlockReference(position, newBlockID)
            			layoutBTR.AppendEntity(newbr)
            			t.AddNewlyCreatedDBObject(newbr, True)
                		
                		
                		
                		
                
                t.Commit()
                pass
                
    if adoc:
        DocumentExtension.CloseAndSave(adoc, DWGPath)

OUT = layoutBlockTableIDs, layoutObjectIDs, blockReferenceIDs, blockReferences, blockReferencePositions, newBlockID

```

---

_[View the full topic](https://forum.dynamobim.com/t/run-a-script-of-a-dynamo-in-multiple-dwg-files/86066)._
