SAVE FAMILIES IN FOLDER WITH DYNAMO v.2.1

Very good evening to all.
You see I am trying to save my list of families in a specific folder… I have found several posts on this topic but most of them refer to Revit 2021 and dynamo 2.6, I am currently working in Revit 2020 and dynamo 2.1 so I can’t find packages and nodes that are compatible with the version I work with.
Someone please could help me to have a node in PYTHON SCRIPT that allows me to perform this operation to save my families in a folder.

Translated with DeepL

Since you’re in an unsupported version custom solutions are going to be hard to come by. If I was at my CPU I’d give it a shot but I don’t think I have 2020 installed.

Knowing all that, why not use the built in tool to save as a library instead of trying to build up Dynamo?

https://help.autodesk.com/view/RVT/2019/ENU/?guid=GUID-FC6CC53B-0010-4AF9-BFE2-6B044191A645

And my usual disclaimers on unsupported versions:

If something goes wrong with your project in an unsupported version there is a lot less likelihood that my colleagues in support will be able to recover any of your data.
Autodesk is not providing fixes for that version anymore, so as security vulnerabilities are found 2020 is much less likely to be patch, and in fact may not receive any such updates in the future. As such you’re putting every organization on the entire project team at risk by not upgrading.

That option allows you to save all the families that were loaded into the project…?
What I am looking for is only to extract the families that were used in the construction of the model and to be able to supervise them.

Purge unused first and you’ll just have families which are in use. :slight_smile:

If in case I only wanted to export only 4 categories, it does not allow me to export by category…??

In that case you’d have to use the export selected method.

I’ve made this for my firm in pyRevit before. The easiest way I found to do selective family exports is to target the Family symbol class, pick from a list, use the EditFamily() method, then save as/close the chosen families to a specified directory.

2 Likes

This works in my testing of 2021 - worth a shot to save some time. Also has a UI component in Revit that you may want to remove or expand on as desired.

### Configure the Python environment
import clr, os 
[clr.AddReference(i) for i in ["RevitNodes","RevitServices", "RevitAPI", "RevitAPIUI"]]
import Revit
clr.ImportExtensions(Revit.Elements)
import RevitServices
from RevitServices.Persistence import DocumentManager
import Autodesk
from Autodesk.Revit.DB import Document, SaveAsOptions
from Autodesk.Revit.UI import TaskDialog

### global variables and inputs
doc = DocumentManager.Instance.CurrentDBDocument #get the active document
families = UnwrapElement(IN[0]) #the families to save
outDir = IN[1] #the directory to save to 
saveOpts = SaveAsOptions() #create a new save as options object
saveOpts.OverwriteExistingFile = True #set the save as options objectoverwrite to true
saveOpts.MaximumBackups = 1 #set the save as options objectbackups to 1
famPaths = [] #empty list to hold the family paths
famTitles = [] #empty list to hold the family titles

### loop over the families
for fam in families: 
	famDoc = doc.EditFamily(fam) #open the family document
	famName = famDoc.Title #get the title
	famPath = os.path.join(outDir,famName) #build the path to save to 
	famDoc.SaveAs(famPath, saveOpts) #save the family 
	famDoc.Close(False) #close the document
	famPaths.append(famPath) #append the family path to the list of paths
	famTitles.append(famName) #append the family name to the lsit of names

famPaths = "\r   • ".join(famPaths) #join the4 family paths into a string
famTitles = "\r   • ".join(famTitles) #join the family names into a string
msg = "The following families were saved:\r   • {0}".format(famTitles) #configure the display message for the Revit UI
TaskDialog.Show("Dynamo Results", msg) #generate a Revit popup to let the user know what happened

OUT = "The following family files were saved:\r   • {0}".format(famPaths) #returnt he list of paths the documents were saved to into the Dynamo environment
4 Likes

a little suggestion after close :wink:

famDoc.Dispose() # Releases all resources used by the Document

2 Likes

Good afternoon.
First of all thank you for the support … You see I am using the code but it gave me an error? what am I doing wrong?

Looks like you have somehow modified the active document prior to this Python node. As such you’ll have to force the transaction closed using something like this:

TransactionManager.Instance.ForceCloseTransaction()

You’d want to out this right after the inputs are defined.

You’ll also need to load in the transaction manager; there are ample examples of doing so elsewhere on the forum which you can refer to for this, usually occurs right after loading the document manager library. :slight_smile:

Reason w dHave to do this is that you can’t edit a family from the active document while currently making a modification to the active document. This is to prevent corrupting the data streams in the file(s). Give it a shot and see if you can work it out.

Sorry, I am not fluent in Python.
I have no idea where I should insert the code snippet?

That line should be inserted between what is currently line 15 and line 16 - right before the save as options.

This line to import the transaction manager should happen right after importing the document manager, between what is now lines 7 and 8.

from RevitServices.Transactions import TransactionManager

You need to open a transaction in the project document and then close/commit once it is finished. I believe EditFamily specifically requires this of all the operations.

The line you added needs to be moved to line 7.

^ this code goes at line 26.

Your outDir variable is a list, but needs to be a single item.

Do you mean that the OUTDIR should be a list, an output list for each family…?

Nope - the input for IN[1] needs to be a single path, not a list with a single path in it.

1 Like