Rename XREFs in multiple dwgs

Good Day, (Any help would be appreciated)
We have a file setup system that creates all the sheets for us with XREFs attached. Now all we need is a way to add the Project Number to the Base.dwg XREFS.

I think these would be the steps needed:

  1. Dyn asks for the XREF name [Example Base.dwg]
  2. Ask for the new name [Example Base-012598.dwg]
  3. Open (cycle through) all dwgs in the current folder and rename the XREF to the new name.
    3b) It may be safer for it to ask for the folder name (Browse select) just in case the current folder is not the one they want.

Is this possible to do with Dynamo? Still only have my toes in the Dynamo ocean, so the more specific you could be the better. Any help on this would be much appreciated.

Thank you!

Hi @btillett,

Internally, an XREF is a block. So are you trying to rename the blocks or rename the source DWGs themselves?

Rename the source dwg, and change the name of the XREF in all of the destination dwgs. The file rename is not the most important part, that is a single step. But opening the folder full of dwgs that xref it and updating the xref name would be the value add part.

OK got it. Sorry to nitpick, but I’m curious if the file renaming could happen before adding the DWGs as XREFs to the sheets?

2 Likes

Yes, we could change the workflow to copy a set of standard sheets to our submittal folder without the base.dwg xrefs, and then the dyn would just have to xref the Base-projnumber.dwg to all of the sheets in the folder. Is that easier?

I would import shutil into a python node and loop through a file copying and renaming process.

https://docs.python.org/3/library/shutil.html

I’m not sure about adding xrefs or repairing xref paths after file name changes with dynamo.

Looks like autocad API references attaching xrefs here: Reference Objects in the Object Hierarchy

Blockquote
VB.NET
Dim strFName As String, strBlkName As String
Dim objId As Autodesk.AutoCAD.DatabaseServices.ObjectId
strFName = “c:\clients\Proj 123\grid.dwg”
strBlkName = System.IO.Path.GetFileNameWithoutExtension(strFName)
objId = Application.DocumentManager.MdiActiveDocument.Database.AttachXref(strFName, strBlkName)

I’m still an absolute beginner at accessing the .NET API so not quite sure how to implement this. But seems like updating the drawing names with python would probably be a good way to go and then call the AttachXref method in the .NET API using python.

I’d go that route - rename the files, then append all the files into each other as needed.

Not saying anyone will cover it this week, but inserting refs like this in bulk may make a good community conversation topic, as an “fyi” slide, or as demo at a future session. Autodesk Community Conversations

2 Likes

That would be awesome Jacob. You’ve been killin’ it on the calls. Love your demos.

1 Like

No promises this will make the cut. The special guest and I have a whole chunk of content coming though! :wink:

3 Likes

Good idea…

2 Likes

Any luck creating a dyn that will ask for or read a list of dwgs and then xref them into all dwgs in a selected (current) folder?

Or one that should be much simpler. Have it ask for the the dwgs, or you browse and select them, and have it remember the “list” / settings so when you open each sheet dwg, all you have to do is run the dyn from player instead of browsing out multiple times to select the dwgs to xref into the current dwg.

I personally don’t have time to create the whole workflow from scratch. If you have smaller, bite-sized questions along the way, I can try to help.

The standalone tool Reference Manager that comes with AutoCAD sounds like your solution.

1 Like

I was actually thinking this too. Can rename the files with python and use reference manager.

I tried reference Manger, where the sheet dwgs had a base and existing conditions xrefs and i thought i could just pick new ones to replace them, but it would not let me pick new XREFs with different names, it only would let me bulk change the paths for the existing XREF names. I will look at that again, but pretty sure it could not be done with Reference manager.

Rename the XREFs with Python may do the trick if the paths are relative, all we really need to do is change the XREF names to the ones with the “-Proj#” after them. Something that asked for the proj number and appended it to the XREF names may do the trick.

Here,

You can use this for renaming the files. It will only rename .dwg files.

RenameFilesWithSuffix.dyn (7.3 KB)


Much appreciated. However, the files will already be renamed. The task is to rename the the file name in the XREF Paths in the current dwg. Example if the open dwg xrefs C:\Base.dwg and C:\Existing Conditions.dwg, we would need to change the xref definition to C:\Base-1234.dwg and C:\Existing Conditions-1234.dwg. I know this sounds like something the the Reference manager should be able to do, but it will only change the path (“C:”) leading up to the name of the xref and will not rename the XREF itself.