Parameters Script Issue

Hello Everyone,

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?

code largely inspired by Dimitar Venkov
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]
families = UnwrapElement(IN[3])

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()

result =

for f1 in families:

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.
2 Likes

Hey @cgartland,

Thank you so much and that definitely helps so much. However, I was wondering what am I doing wrong?

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])
famdoc = doc.EditFamily(family)

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()

OUT = ‘Successfully added %d shared parameters to %d families’ %(len(parameters),len(result))

Blockquote

See this post for additional information. Also, be sure to paste your code as preformatted text instead of a blockquote.

image

I have also found issues using the TransactionManager with documents other than the current DBDocument. Instead, try creating a transaction manually:

famdoc = doc.EditFamily(family)

t = Transaction(famdoc, 'Describe your transaction')
t.Start()
# Do stuff here
t.Commit()
1 Like

@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))

What’s the error you got? The indentation looks a bit off but that might be because you pasted the code in the forum.

Hey Dimitar,

I got the following error:

I am basically trying to have multiple Parameter groups with multiple parameters go to a single family and/or multiple families. Any ideas?

Sincerely,
Kevin

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)

Hey Dimitar,

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?

Thanks again bud,
Kevin

what did the inputs look like?

1 Like

Hey Dimitar!

Please see both the code and inputs. I hope the screenshot shows all the inputs needed. I just do not know what is causing the issue?

"
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 :slight_smile:

Hey @Dimitar_Venkov, just wanted to follow up on what I am doing wrong? LOL

Thank you for your help bud!

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

Hey Dimitar,

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!