Set Family Parameter Formula Issue

I’m working on copying formulas from one family to another and I’m having issues with the doc.FamilyManager.SetFormula() function.

I am using code by @Daniel_Woodcock1 that I found on the forum, and it somewhat works. The problem is that when I first run it, every parameter returns a “There is no valid family type.” error.

The weird thing is that if I were to create a new parameter and then rerun the script, almost all of the formulas will be created. There are still a few “Formula setting failed” errors (which I don’t understand either, any ideas would be appreciated), but overall, most of the formulas are successful.

Is there something I missing regarding making the parameters editable?

import clr

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc =  DocumentManager.Instance.CurrentDBDocument
app = DocumentManager.Instance.CurrentUIApplication.Application

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)

clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *

def tolist(obj1):
	if hasattr(obj1,"__iter__"): return obj1
	else: return [obj1]

pNames = tolist(IN[0])
fString = tolist(IN[1])

outList = []
errors = []
test = []
names = []

if doc.IsFamilyDocument:
	fMan = doc.FamilyManager	
	params = fMan.GetParameters()
	for pName,fStr in zip(pNames,fString):
		for p in params:
			pd = p.Definition
			pn = pd.Name
			names.append(pn)
			if pName == pn:
				if p.CanAssignFormula:
					TransactionManager.Instance.EnsureInTransaction(doc)
					if fMan.IsParameterLocked(p):
						fMan.SetParameterLocked(p,False)					
					try:
						fMan.SetFormula(p,fStr)
						test.append(pn)
						outList.append("Succeeded")
					except Exception, e:
						outList.append(e)
					TransactionManager.Instance.TransactionTaskDone()
				else:
					errors.append(pn)
	
	no =  set(names).difference(set(test))
OUT = outList, no
#For more information refer to RBG Wiki or email me (ctrl + click link): daniel.woodcock@robertbird.com

Try looking in the orchard package as there are some good nodes for this there.

Hello everyone,
I am having the exact same issue with the following error message:

“Exception: Formula setting failed”
Since I am writing the code in dynamo python, it would be good to solve the issue within the code itself.

if doc.IsFamilyDocument:
try:
fam_mgr.AddParameter(“Half_Raker”,BuiltInParameterGroup.PG_GEOMETRY,ParameterType.Length,False)
except:
“Half_Raker”==True
if True:
half_raker_param=fam_mgr.get_Parameter(“Half_Raker”)
fam_mgr.SetFormula(half_raker_param,‘Raker_Width/2’)
else:
pass

Do you mean the Orchid package :wink:
@PAnand you could take a look at the package and if there is a node that accomplish your goal you could jump into the github and have a look at how it is done :slight_smile: I know that @erfajo for the most point have both C# and Python versions of most of the nodes in his package.

Thanks @Jonathan.Olesen, I could only find a C# version of the code.
@erfajo: Would you be able to provide a python version of the node “FamilyDocument.SetFormulaByName” as well please? :slight_smile:

Thanks @erfajo
I am actually writing it in Python and want to save the code as a module, which I will import into several scripts.
Will do some research on the forum.

That did the trick for the first issue of “There is no valid family type.” Thanks!

I’m still running into the “Formula Setting Failed” error for 2 parameters, and I can’t figure out why. Have you run across this before when creating your node?

I tried to use your node, but I was getting a “This parameter cannot be assigned a formula” error, which is true, because not all the parameters that are copied from a different family are able to be assigned a formula such as a material parameter. Since there is an error, the entire node fails instead of just excepting the error. I need to functionality to be able to skip the parameters that error out. The current python node I’m working on spits out which parameter errored out on the set formula part and which parameters couldn’t be created at all.

Thanks!

I totally get it. Definitely need to set priorities on these things. Definitely appreciate your nodes and the work you’ve put in so far, so thank you!

As for the “Formula Setting Failed”, have you come across that or know what may cause that error at all, or is it just a peculiar thing that pops up?

But even after I create a family type as part of the script, 2 of the 58 parameters get this “Formula Setting Failed”. It’s not the same “There is no valid family type” and I can’t seem to figure out what is causing the “Formula Setting Failed” error.

Oh well, maybe it’s just an issue that’s not really solvable for now. 2 out of 58 isn’t the worst thing in the world to fail on.

See attached. The Excel file has all the parameters and formulas.

The process has been to create a new generic model family, and run the dynamo script in the family environment. It should read the excel file and create the parameters with the proper formula. Let me know what you think.

Thanks!

EDIT:

I think I just figured it out! I’m not importing parameter values, so all of them are set to 0 by default. The because of this, the formulas that break are the ones that are dividing by a parameter that is set to 0.

Such a dumb error. Sorry about that!

CopyFamilyParameters.dyn (8.4 KB)

FamilyParameters.xlsx (10.6 KB)

2 Likes