Rename XREFs in multiple dwgs

Just spent a good 90 minutes figuring out how to xref all the .DWG files in a given directory into each other (so file A gets B, C, and D; file B gets A, C, D; etc.).

I’m going to lead off with some bad news: I won’t be posting the code directly. Going to pause briefly for everyone to vent their frustration by screaming into the void (wondering if I can track forum user timezones this way?).

Ok now for the good news: I did this in a zoom call so you can all follow along at your leisure. :slight_smile:

I am doing things this way as the intent of my efforts on the forum aren’t to provide direct code examples on how to accomplish any single task; that’s literally my day job - you’re more than welcome to contract us for such services. Instead I am here to teach people how to do amazing work, and remove as many blockers as I can which come up along the way.

Bulk processing DWGs is clearly one such blocker of sorts, and something people wan to learn, so I figured since I didn’t get such a session in to AU this year, a recording of me figuring it out live would be a good way to get this type of educational stuff out there. If I get the chance to do something more polished later, I’ll be sure to socialize as always.

Here’s the goods:
Recording 1: Passcode Required - Zoom
Passcode: uTF0kR#K

Recording 2: Passcode Required - Zoom
Passcode: uTF0kR#K

4 Likes

Dude! This is better than a solution. You’re amazing! I can’t wait to check this out. Once the kids are in bed… :joy:

1 Like

Enjoy!

At 90 minutes end to end (almost exactly) it may be something I can sneak into a community conversation, user group meeting, or something similar at a point in the future. I’ve got the replay up now and it’s 14 minutes with very little done… there is a crash around the 30 minute mark too. Sort of thing I have to polish before a formal ‘here you go’ data set.

1 Like

FYI: if you change around line 27 you can add a prefix/suffix/whatever to the xref name. This might make it so you can circumvent having to bulk rename files (unless you need those renamed too, in which case you can just use the file name as is and remove the suffix).

2 Likes

I had to speed through it (EST), but I will watch it at normal seed tomorrow. Looks amazing. I do not know Python enough to duplicate and tweak it, and may contact you for such services. Much appreciated.

1 Like

Just finished watching/working through part one. Big thanks again for sharing. I was getting frustrated trying to figure out why Autodesk.AutoCAD.ApplicationServices.DocumentCollectionExtension wasn’t importing properly. I imagine you felt the same way! Very weird that it only works in IronPython2. hmm… Also I crashed twice instead of once in 30 minutes so does that mean I win? :joy:

1 Like

Try building a ‘detacher’ after the fact… I hit… 12 crashes in the half hour before I realized I had the block tabled opened for read only… :laughing:

The reason for the issue is that CPython can’t generate classes in the same way as IronPython because in it’s case the call from inside the house. Basically you’re already in .net, so creating a class won’t work without creating an instance, and you can’t create an instance without the class… It gets somewhat messy in Revit but from what I am seeing it’s VERY messy in AutoCAD. The CPython feedback thread has more technical info if you’re interested.

1 Like

Finished working through it. It looks super tough finding what exactly is needed from the .NET API documentation. You make it look easy. There’s no way I would of got to this solution with my level of expertise but I was able to understand it while following along so thanks so much for posting this. It works super awesome. Hoping I can take these principals and apply them to iterating through documents in general.

Works awesome!!!

2 Likes

Okay,

So after watching @jacob.small’s awesome video and working a bit I believe I have a solution. This script will go through a list of “sheet drawings” in a selected file folder directory and detach all of the xrefs from the drawings. Then, the script will rename all of the xref drawings in an xref folder with a project number. Then, the script will attach all of the xref drawings in the xref folder back to the “sheet drawings”.

It has to be done in that order or it seems to break :sweat_smile: I can’t seem to get a value from “xgraph.node.out(x)” to detach xrefs if they become unreferenced. So they have to be renamed after the detach step. Not sure how else the xref could be selected to be detached if they become unreferenced, maybe the block table record? Not quite sure I’m new to all this :sweat_smile:


XrefUpdates.dyn (11.2 KB)

1 Like

This is Awesome, and much appreciated. However, detaching and attaching XREFs with different names will change the layer state, which we were trying to preserve. But we have “easy buttons” that will set the layers so it will still work for us. But there is no point to have them attached if we are going to detach them. I realize how much work you have done on this and appreciate it immensely, but if you are like me you are having fun figuring this out, on the other hand I understand if you are done with it and me. But if you are still up for the challenge, how hard would it be to change the Python script to just attach a list of XREFs, and have the selection DYN code boxes allow you to select multiple dwgs with different paths for the list. If we could have a selection option to pick the XREFs needed then it xrefed them all in to any current dwg we were in, that would improve efficiency for us and be much appreciated. Below is an image of the folder structure and sample dwgs.

I did think about that. Unfortunately I couldn’t find a method for updating an xref’s path in the API so I’m not sure how else to do it besides detach xrefs and reattach them. Which would wipe layer state overrides. If you have “easy buttons” to set layerstate, then that should hopefully fix the issue.

Yeah I’m happy to help figure it out your issue best I can, just trying to practice my dynamo skills in preparation for my new job since I’m going back to consulting from city engineering shortly. So no worries.

Okay, so no detachment necessary. You just need a script that xrefs in dwgs to a set of submittal dwgs based on selection from nodes. Shouldn’t be too bad. I’ll see if I can set something up based on your file structure.

Are the file directories all caps or are the first words capitalized except for “DWG” folder and “XREF” folder? There are conflicts in the image. Not sure if I will need this because I’ll probably just collect all of the dwgs based on the “CAD” folder and all subdirectories, but just incase it’s pertinent.

Thank you.
The directories are automatically created by the system, so the capitalization should be consistent as shown in the image below. However the dwg names are created by humans and may vary.

Directories

It does not even have to do all of the sheet dwgs in the Drawing Sets sub folder. The script could just xref in the list created by selection to the current dwg. So the workflow would be to run the Dynamo Player while a destination dwg is open. Click the Edit Inputs on the new dyn file. Select the source dwgs (which would build the list), and then hit the play button… then open the next dwg and hit the play button… repeat…
RunEditInputs

No need to open the current dwg. It can just loop through all of the sheet dwgs and add all of the selected xrefs to each one. That’s easy. I’m just trying to understand the directory structure. The exact name of the file folders is important because I need to make sure the correct .dwgs are added and not ones from archive or exhibits.

Also am I correct in my understanding (based on the previous flow chart) that one of the subfolders inside the “Drawing Sets” folder (like the construction, permit, ect folders you show in your file structure) contains .dwgs that you intend to xref in? Seems odd that they are in with sheet drawings. In that case I will have to filter them by the name “Border” and “Label” from the sheet drawing list.

Boris,

I believe this is your solution. You can use the bool toggles to turn off referencing certain xrefs. If you want to add another xref just copy one of the groups and add it to the list and change the string to something that is in the xref name.
XrefUpdates-V3.dyn (70.5 KB)

Hope this helps.

1 Like

This may be relevant to future work in this area: .NET XREF Routines

1 Like

The Border.dwg gets XREFed into the layouts (Paper Space) so that one is not needed. We have a button that XREFs in the .\Border.dwg when you are on the Layout.

oops, I did not see this one when replying to the one before it. I will try it and edit out the Border.dwg… i should be able to figure out that much :slight_smile:

Thank you… very much appreciated.

I know this sounds crazy to some. But most companies will not want their users to actually open Dynamo. They would prefer users run the player and click the input option for selections and then the Play option to run it. Dynamo is for writing script, the Player is for the users. This dyn does not respond to the Player select input option. A dyn that responded to the Player Input option and allowed users to select the XREFs to add and another input box that asked for the Destination folder would be better for Companies users.