Python script:
import clr
clr.AddReference("RevitServices")
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
# class for overwriting loaded families in the project
class FamOpt1(IFamilyLoadOptions):
def __init__(self): pass
def OnFamilyFound(self,familyInUse, overwriteParameterValues): return True
def OnSharedFamilyFound(self,familyInUse, source, overwriteParameterValues): return True
def changeParam(f1,name,make,result):
famdoc = doc.EditFamily(f1)
try: # this might fail if the parameter exists or for some other reason
trans1.EnsureInTransaction(famdoc)
par_name = [a for a in famdoc.FamilyManager.Parameters if a.Definition.Name==name][0]
if make == True:
famdoc.FamilyManager.MakeType(par_name)
else:
famdoc.FamilyManager.MakeInstance(par_name)
trans1.ForceCloseTransaction()
famdoc.LoadFamily(doc, FamOpt1())
result.append(True)
except Exception, e:
result.append([False,e])
trans1.ForceCloseTransaction()
famdoc.Close(False)
def unwrapping(ele):
try:
f1 = UnwrapElement(ele.Type.Family)
except:
f1 = UnwrapElement(ele)
return f1
trans1 = TransactionManager.Instance
trans1.ForceCloseTransaction() #just to make sure everything is closed down
# Dynamo's transaction handling is pretty poor for
# multiple documents, so we'll need to force close
# every single transaction we open
result = []
if hasattr(IN[0], "__iter__") == True and hasattr(IN[1], "__iter__") == True:
for i in range(len(IN[0])):
f1 = unwrapping(IN[0][i])
changeParam(f1,IN[1][i],IN[2],result)
elif hasattr(IN[0], "__iter__") == True and hasattr(IN[1], "__iter__") == False:
for i in IN[0]:
f1 = unwrapping(i)
changeParam(f1,IN[1],IN[2],result)
elif hasattr(IN[0], "__iter__") == False and hasattr(IN[1], "__iter__") == False:
f1 = unwrapping(IN[0])
changeParam(f1,IN[1],IN[2],result)
OUT = result
Shout out to @Dimitar_Venkov for this thread and Nathan Miller’s theprovingground.org for API help.
I haven’t been able to fully test it so let me know of any problems, but it should be able to handle:
- Single or list of families, elements, or a combination as input 0
- Single parameter as string in input 1
- Single or list of booleans for input 2; True = make parameter into Type, false into Instance
PS. I am still a novice for python and working with api, so please excuse any horrible coding standard. Those with more experience, I would love some feedback
PPS. Forgot to mention, this script works in the project space, not family editor. I misread the title. If you need specifically for family editor, I can make another version but thought project space would be more useful.