Run Dynamo Script on entire folder of dwg's

@nick.turner66KAP no I use PyCharm but it is my preference because it is free and has great features to manage Python projects

First of all let’s have a look at the logic behind it (to zoom press CTRL and use the mouse wheel, to pan press the left mouse button and drag)


From the logic it is possible to identify what are the main functions at play and what are the inputs to consider. In this case I’m using a pure AutoCAD API approach although it should be possible to simplify the code a bit calling the Dynamo nodes.

This is how it looks like at the end; it will produce also a Log file to double check the results.


The main function is the one that gets all the DWGs from the folder and subfolders recursively.
Python has the os.walk() method that does exactly what we need (L206-209)
For each DWG then it calls the inner function that does all the job.
In this case there a lot of inputs to be as flexible as possible. In Dynamo the CoordinateSystem method to create a BlockReference is basically providing the same inputs but it is using a single object to derive all of them.

There are a few best practices such as a “graceful failure” at the very beginning (L193): if not all the critical inputs are provided, the function returns its own instructions (the text from line 176-189).

The Current document (it could be an empty document) is stored to avoid Dynamo for Civil 3D complaining about the absence of a document at the end of the function, so before leaving it we just need to remember to restore the current document.


In the inner function there is a first part to open a document in the background (L99). Once it has been opened for writing, we need to make it the Current Document (L100).


What comes next is the core of the inner function and might have been done differently. I’ve tried to put together a little bit of everything to show how to safely access a document via context managers (the “with” statements to LockDocument, Database and Transaction), check and create layers, check and import a block from an external DWG, iterate through the Entities of the Model Space, test for the type of an Entity (isinstance() L146), erase an entity from the Database, create a BlockReference, deal with Transforms in AutoCAD API (Matrix3d objects L159-162) and finally add the new objects to the Database. After all that the transaction must be committed to validate the changes made and the Document must be saved in the same location.


Here is the final Dynamo file with the references used. It could have been done in a different way, the main goal was to show how it is possible to run a script on multiple documents.
Run Function on multiple files.dyn (21.2 KB)


Awesome work Paolo. Great explanation of the logic.

Paolo, the script is opening one of the documents in the folder, but not replacing the block and giving this warning. Possibly I put incorrect inputs in?

@nick.turner66KAP Can you make sure that the source file for the new block is not in the folder and its subfolders? Basically the script was trying to modify the file that contains the title block, and hence the error.
Just add these lines in the replace_block_reference() (or download the file again).


Thanks Paolo, that was it.

1 Like

C# programmer here… comes to look at dynamo… within first minute of dynamo use, notices that you need to use friggin python to import external dwgs… closes dynamo… never opens it again…

actually comes back to dynamo briefly… to check if compiled dll’s can be utilized within dynamo to expand its functionality… like load a bunch of files…? anyone know if that’s readily possible?

not bad for your first post :slight_smile: C# programmer here too, love Dynamo and respect your opinions.
First, to import DWG as Block there is a dedicated node.
Second, Zero Touch is a common way to create nodes to customize functionalities in Dynamo Zero Touch Plugin Development · DynamoDS/Dynamo Wiki · GitHub

What’s not to like about Python? if it’s the IDE in Dynamo I agree, but you can use an external editor and have the same features you have in Visual Studio when working with .NET

1 Like


Hope all is well my friend. I am working on tweaking the whole folder of dwg’s script that you helped me with (and many people have said helped them going through our posts online). I was looking to do a similar thing but import a Page Layout instead of erase and insert block. Have you seen anybody so something similar to that?

The issue is that with Europe adopting IFC format, many Arch’s and Eng’s need to export to vanilla AutoCAD dwg’s and import the IFC page layout.

Thank You,

@kievskydv The official forum language is English. This is a requirement to make search work. Please post with a translation as I edited you post above accordingly in the future.

I was doing that 30 years ago. I used AutoLISP to write a script that opened a list of DWGs with whatever commands, including LISP functions, and saved. Then I’d simply run the script.

I need to replace the block in a layout / paper space. can you update the script to use in paper space.

1 Like

In my original code you need to target the paper space rather than the model space when you are defining the BlockTableRecord (btr, line 134)

1 Like

Please can you give me a line of code for paper space.