I am running into an error when trying to incorporate multiple parameters in multiple groups into a family in one command. Not sure what the issue is exactly… Any ideas?
famdoc = doc.EditFamily(f1)
trans1.EnsureInTransaction(famdoc)
for p in parameters:
famdoc.FamilyManager.AddParameter(p, paramgroups, inst_or_typ)
trans1.ForceCloseTransaction()
famdoc.LoadFamily(doc, FamOpt1())
result.append(True)
OUT = ‘Successfully added %d shared parameters to %d families’ %(len(parameters),len(result))
Your variable “families” consists of a single family. So, attempting to loop over a single family (not a list of families) is not valid. If you are only planning on editing a single family at a time, you can remove your outermost loop:
family = UnwrapElement(IN[3])
famdoc = doc.EditFamily(family)
# etc. etc.
@cgartland, I completely understand what you mean. Thanks for helping with that formatting. I just want to figure this out as this is the last part of a very complex script.
@Dimitar_Venkov, I know you made this script a while back but am currently trying to add the parameters to just 1 family rather than the original (multiple families). Any ideas?
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 *
parameters = IN[0]
paramgroups = IN[1]
inst_or_typ = IN[2]
family = UnwrapElement(IN[3])
# 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
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 = []
famdoc = doc.EditFamily(family)
trans1.EnsureInTransaction(famdoc)
for p in parameters:
famdoc.FamilyManager.AddParameter(p, paramgroups, inst_or_typ)
trans1.ForceCloseTransaction()
famdoc.LoadFamily(doc, FamOpt1())
result.append(True)
OUT = 'Successfully added %d shared parameters to %d families' %(len(parameters),len(result))
Right now you’re feeding in a list of Parameter groups but the code expects a single one. You’ll need to iterate over each of the PGs and run the add command. Something along the lines of:
famdoc = doc.EditFamily(family)
trans1.EnsureInTransaction(famdoc)
for pg in parameterGroups:
for p in parameters:
famdoc.FamilyManager.AddParameter(p, pg, inst_or_typ)
trans1.ForceCloseTransaction()
famdoc.LoadFamily(doc, FamOpt1())
famdoc.Close(Fasle)
result.append(True)
I tried making the modifications and adjustments but seems to have thrown a new error? Not entirely sure what is going on that I am potentially missing?
"
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 *
parameters = IN[0]
paramgroup = IN[1]
inst_or_typ = IN[2]
families = UnwrapElement(IN[3])
# 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
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 = []
for f1 in families:
famdoc = doc.EditFamily(f1)
trans1.EnsureInTransaction(famdoc)
for pg in paramgroup:
for p in parameters:
famdoc.FamilyManager.AddParameter(p, pg, inst_or_typ)
trans1.ForceCloseTransaction()
famdoc.LoadFamily(doc, FamOpt1())
famdoc.Close(false)
result.append(true)
OUT = 'Successfully added %d shared parameters to %d families' %(len(parameters),len(result))
";
Any ideas and Thank you again!
Kevin
P.S. Starting this Friday, I am going to start taking C# classes and train myself in the ways of API’s to get more into Revit API
Everything looks right. Look at the error, it’s different. It’s telling you there’s already a parameter called “Structural Usage”. To add some robustness, you could add a try/catch block:
for pg in paramgroup:
for p in parameters:
try:
famdoc.FamilyManager.AddParameter(p, pg, inst_or_typ)
except:
pass
Thank you so much bud! This did it. The reason why I wanted to make this script is when we have custom Shared Parameters and get a clients file… I do not have to manually (or my team) need to add in each Shared parameter 1 by 1… Such a pain! LOL. Thank you so much and hope you had a great weekend!