Hi All,
I am new to Python. I am working on making a dynamo script compatible with older versions of Revit.
My current dilemma: I have a titleblock selected and I want a boolean output about whether a parameter called “KeyplanAreaToggle” exists in the titleblock family.
My latest attempt:
import clr
clr.AddReference(‘RevitServices’)
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
clr.AddReference(‘RevitAPI’)
from Autodesk.Revit.DB import *
Get the current document
doc = DocumentManager.Instance.CurrentDBDocument
Function to check if a parameter exists in a family document
def parameter_exists(family_doc, param_name):
for param in family_doc.FamilyManager.Parameters:
if param.Definition.Name == param_name:
return True
return False
Assuming ‘family_doc’ is the selected family document in Dynamo
family_doc = UnwrapElement(IN[0])
param_name = “KeyplanAreaToggle”
Start a transaction
TransactionManager.Instance.EnsureInTransaction(doc)
Check if the parameter exists
exists = parameter_exists(family_doc, param_name)
End the transaction
TransactionManager.Instance.TransactionTaskDone()
Output the boolean result
OUT = exists
This gives me the warning message:
Thanks in advance for your help.
@Thatcher
paste your code with </>
import clr
clr.AddReference("RevitServices")
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
# 📃 Get the current document
doc = DocumentManager.Instance.CurrentDBDocument
# 🚀 Function to check if a parameter exists in a family document
def parameter_exists(family_doc, param_name):
for param in family_doc.FamilyManager.Parameters:
if param.Definition.Name == param_name:
return True
else:
return False
# 🤖 Assuming ‘family_doc’ is the selected family document in Dynamo
family_doc = UnwrapElement(IN[0])
param_name = "KeyplanAreaToggle"
# 🔓Start a transaction
TransactionManager.Instance.EnsureInTransaction(doc)
# ❗❗❗ Check if the parameter exists
exists = parameter_exists(family_doc, param_name)
# 🔒End the transaction
TransactionManager.Instance.TransactionTaskDone()
# ✅ Output the boolean result
OUT = exists
i corrected the if statement in the function
1 Like
Cheap method. This checks to see if the parameter exists in either the family instance or the type in the model.
import clr
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument
param_name = IN[0]
element = UnwrapElement(IN[1])
element_type = doc.GetElement(element.GetTypeId())
param_exists = element.LookupParameter(param_name)
type_param_exists = element_type.LookupParameter(param_name)
if param_exists or type_param_exists:
OUT = "true"
else:
OUT = "false"
1 Like
The warning on the node is that you are passing a list to the function which expects a family document. You code will have to handle this use case.
I’m not sure why you are working with Family Documents?
Here is a title block Family Document parameters vs Element parameters
This is how I would do it in Dynamo
Here’s one way in Python
import clr
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
def param_exists(element, param_name):
if isinstance(element, Element):
return bool(element.LookupParameter(param_name))
elements = UnwrapElement(IN[0])
param_name = "Volume"
if isinstance(IN[0], list):
OUT = [param_exists(element, param_name) for element in elements]
else:
OUT = param_exists(elements, param_name)
Please note you don’t need a transaction as you are only querying the document. If you absolutely need to work with Family Documents please leave a reply and I’ll provide some further assistance.
3 Likes
Thank you, @Mike.Buttery Mike.Buttery. It works perfectly.
And thanks for the other responses.
I have a Titleblock element going into IN[0]. I don’t need to get into the titleblock family at this point.
I wanted to let you see my results:
@Draxl_Andreas , It gave a null output and the message - Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
Traceback (most recent call last):
File “”, line 29, in
File “”, line 15, in parameter_exists
AttributeError: ‘List[object]’ object has no attribute ‘FamilyManager’
@staylor , It gave a null output and the message - Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
Traceback (most recent call last):
File “”, line 14, in
AttributeError: ‘List[object]’ object has no attribute ‘GetTypeId’
1 Like
My bad. I didn’t account for a list of titleblock inputs. Here is corrected code if you want.
import clr
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument
param_name = IN[0]
element = UnwrapElement(IN[1]) if isinstance(IN[1],list) else [UnwrapElement(IN[1])]
for elem in element:
element_type = doc.GetElement(elem.GetTypeId())
param_exists = elem.LookupParameter(param_name)
type_param_exists = element_type.LookupParameter(param_name)
if param_exists or type_param_exists:
OUT = "true"
else:
OUT = "false"
2 Likes