Awesome @Paolo_Emilio_Serra1, that would be really cool. It seems a widespread needed functionality to run a whole folder. This would be a nice simple script to do it with too. I will get in touch.
@Paolo_Emilio_Serra1 do you use Python in Visual Studio for the API?
@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 https://cawemo.com/share/cb5df7ef-45ee-482a-84f0-112b540a89ab (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.
@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.
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 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
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.
@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.