Set Family Parameter Formula Issue


#1

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

#2

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


#3

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


#4

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.


#5

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:


#6

@PAnand
I wonder why do you need a python version if the node that is available can solve the problem!?
The code is surely available somewhere in this forum, I have seen so many tweaks on it, so surely it is here somewhere.
I just don’t understand why fix it if it ain’t broken… and my node works as far as I know :slight_smile:


#7

Be sure that there exists a default family, not just the greyed out field in the family… that is not a type. It is really a problem that Revit accept this since the code doesn’t. However, it looks like Python in some cases still accept no default family… not that I understand why Python code can do things that are not allowed!?

Can you use my nodes instead? Take a look at the Orchid package

This post might inspire you.


#8

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.


#9

When I only coded in Python I also misunderstood that a default type was needed, so my old python code doesn’t include this. I don’t know if there has been made changes in the dynamo core in 1.3.3 and 2.0 so that code for FamilyDocuments now needs to respect what is needed in the .net environment.

This is my old Python code I found in my archives…

# Common Language Runtime modules
import clr
clr.AddReference('RevitAPI')
clr.AddReference('RevitServices')

# Revit and Dynamo modules
from Autodesk.Revit.DB import FamilyManager
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application

# input assigned the IN variable
docs = IN[0]
parameter_names = IN[1]
values = IN[2]

# default document set to DocumentManager.Instance.CurrentDBDocument
if docs == 'Current.Document':
    docs = DocumentManager.Instance.CurrentDBDocument

# wrap input inside a list (if not a list)
if not isinstance(docs, list):
    docs = [docs]
if not isinstance(parameter_names, list):
    parameter_names = [parameter_names]
if not isinstance(values, list):
    values = [values]

# core data processing
for doc in docs:
    TransactionManager.Instance.EnsureInTransaction(doc)
    for idx, parameter_name in enumerate(parameter_names):
        param = doc.FamilyManager.get_Parameter(parameter_name)
        doc.FamilyManager.SetFormula(param, values[idx])
    TransactionManager.Instance.ForceCloseTransaction()

# output assigned the OUT variable
OUT = docs

#10

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!


#11

your are pointing on an issue on my todo list… I know this problem, but there is also the issue of creating a converter between Imperial and SI units, which is placed higher on my todo list.
It will come… some day :slight_smile:


#12

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?


#13

In the .net implementation, a family type must exist. So it should not be possible at all to do it without also in Python code. I have not used any time I investigating why things work that shouldn’t do it. I have just accepted how it is meant to work.


#14

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.


#15

try to share you dyn file and a test family/project, then I try to see what happens


#16

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)