Create new revit project parameter from dynamo

hi,

is it possible to create new revit project parameter from dynamo?

milorad

yes. :slight_smile:

1 Like

good news, but how it works? :slight_smile:

So it’s pretty straight forward. Here are the inputs for the Python Node in order:

IN[0] - Parameter Name as a String. That will be the name of your parameter.

IN[1] - Group Name as String. That will be the Shared Parameter group name. This parameter will be stored in that group.

IN[2] - Parameter Type which defines whether its a Yes/No, Text, Integer etc. type of parameter. There is a separate Python node to the left that enumerates all Parameter Types available in the project so get one of them.

IN[3] - Visible? This is a boolean True/False that will either make the parameter user visible (it will show up in the properties of an element) or not visible (user cannot see the parameter, its only accessible via the API)

IN[4] - Element Category. This is a category that you want to add the parameter to ex. Generic Model or Furniture.

IN[5] - Parameter Group. This is the group in which parameter is stored when displayed in UI. When you select an element each parameter is within a group. Most commonly used ones are Graphics, Text, Identity etc but there are many more available to place your parameter in.

IN[6] - Instance? This is a boolean True/false that determines whether this will be an instance or a type parameter.

Once you run this definition it will create the parameter in the Shared Parameters file and add it to the project.

Code for the Parameter Type node:

#Copyright(c) 2015, Konrad Sobon # @arch_laboratory, http://archi-lab.net

import clr
clr.AddReference(‘ProtoGeometry’)
from Autodesk.DesignScript.Geometry import *

Import DocumentManager and TransactionManager

clr.AddReference(“RevitServices”)
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

Import RevitAPI

clr.AddReference(“RevitAPI”)
import Autodesk
from Autodesk.Revit.DB import *
import System

#Assign your output to the OUT variable
OUT = System.Enum.GetValues(ParameterType)


Here’s code for the Parameter Group node:

#Copyright(c) 2015, Konrad Sobon # @arch_laboratory, http://archi-lab.net

import clr
clr.AddReference(‘ProtoGeometry’)
from Autodesk.DesignScript.Geometry import *

Import DocumentManager and TransactionManager

clr.AddReference(“RevitServices”)
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

Import RevitAPI

clr.AddReference(“RevitAPI”)
import Autodesk
from Autodesk.Revit.DB import *
import System

#Assign your output to the OUT variable
OUT = System.Enum.GetValues(BuiltInParameterGroup)


And here is code for the parameter creation node:

#Copyright(c) 2015, Konrad Sobon # @arch_laboratory, http://archi-lab.net

import clr
clr.AddReference(‘ProtoGeometry’)
from Autodesk.DesignScript.Geometry import *

Import DocumentManager and TransactionManager

clr.AddReference(“RevitServices”)
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

Import RevitAPI

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

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

from System.Collections.Generic import *

Import ToDSType(bool) extension method

clr.AddReference(“RevitNodes”)
import Revit
clr.ImportExtensions(Revit.Elements)
import System
#The inputs to this node will be stored as a list in the IN variable.
dataEnteringNode = IN

_paramName = IN[0]
_groupName = IN[1]
_paramType = IN[2]
_visible = IN[3]
_category = IN[4]
_paramGroup = IN[5]
_instance = IN[6]

def ParamBindingExists(_doc, _paramName, _paramType):
map = doc.ParameterBindings
iterator = map.ForwardIterator()
iterator.Reset()
while iterator.MoveNext():
if iterator.Key != None and iterator.Key.Name == _paramName and iterator.Key.ParameterType == _paramType:
paramExists = True
break
else:
paramExists = False
return paramExists

def RemoveParamBinding(_doc, _paramName, _paramType):
map = doc.ParameterBindings
iterator = map.ForwardIterator()
iterator.Reset()
while iterator.MoveNext():
if iterator.Key != None and iterator.Key.Name == _paramName and iterator.Key.ParameterType == _paramType:
definition = iterator.Key
break
message = None
if definition != None:
map.Remove(definition)
message = “Success”
return message

#“Start” the transaction
TransactionManager.Instance.EnsureInTransaction(doc)

message = None

if ParamBindingExists(doc, _paramName, _paramType):
if not RemoveParamBinding(doc, _paramName, _paramType) == “Success”:
message = “Param Binding Not Removed Successfully”
else:
message = None

try:
file = app.OpenSharedParameterFile()
except:
message = “No Shared Parameter file found.”
pass

group = file.Groups.get_Item(_groupName)
if group == None:
group = file.Groups.Create(_groupName)

if group.Definitions.Contains(group.Definitions.Item[_paramName]):
_def = group.Definitions.Item[_paramName]
else:
_def = group.Definitions.Create(_paramName, _paramType, _visible)

builtInCategory = System.Enum.ToObject(BuiltInCategory, _category.Id)
cats = app.Create.NewCategorySet()
cats.Insert(doc.Settings.Categories.get_Item(builtInCategory))
if _instance:
bind = app.Create.NewInstanceBinding(cats)
else:
bind = app.Create.NewTypeBinding(cats)
param = doc.ParameterBindings.Insert(_def, bind, _paramGroup)

“End” the transaction

TransactionManager.Instance.TransactionTaskDone()

#Assign your output to the OUT variable
if message == None:
OUT = “Success”
else:
OUT = message


I hope this helps. Please let me know if this works. I wasnt able to test this extensively. I ran it through some basic tests so do let me know if it misbehaves. Good luck!

Capture

4 Likes

konrad, i understood that milorad wants to create just a project parameter.

for that best practice is to create a temporary group, the parameter according to your script and afterwards delete the shared parameter and group again.

deleting the shared parameter and group is best done outside dynamo / revit directly from a python node

 

Peter,

You actually cannot delete the Shared Parameter or its group using Revit API (at least I am not aware of any method that can do that). What I am doing in my node is just deleting the Parameter Binding which binds it from Shared parameter file to a specific category in a model. That doesn’t erase the parameter from the Shared Param file. It allows me to change the parameter from Instance to Type without having to create a new one.

Being able to create just a Project parameter and not be able to add it to share parameters sounds like another option. I didnt think about that.

Hey Konrad, I’m very interested in this solution. I’m getting the following python script error when I try to use your python code.

Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.

File “”, line 5
clr.AddReference( ProtoGeometry )

^
SyntaxError: unexpected token ‘’

Am I missing something? I copied the code in exactly from what you posted.

Thanks for the tips!

This blog doesnt exactly support code formatting so all indentation is gone in the code that I posted. Python is very sensitive about that. Mhmmmm, idk. I can post it to my personal blog, but that wont happen until I get home.

 

konrad, i know, reading some of the guru posts i found that workaround … and up to now it is perfectly working:

here is your code till end of transaction then:
#delete shared parameter
paramfile=open(filename,‘r’)
lines = paramfile.readlines()
paramfile.close()
paramfile=open(filename,‘w’)
for line in lines:
if groupname not in line:
if parametername not in line:
paramfile.write(line)
paramfile.close()
OUT=(‘Successfully created parameter: %s’ %parametername)

Ahh, also it seems like “” got stripped from code. There should be ‘ProtoGeometry’ and not ProtoGeometry. Weird. I definitely hate posting code here.

Ha! Peter, yes, you can obviously parse the TXT file and delete the line that holds the Shared Parameter file, but that somehow seems to me like a weird thing to do. Hell with it. Doing weird stuff in Dynamo is kind of cool! Good job! Thanks for sharing.

1 Like

Hey Konrad, i sorted through the formatting issues and got the nodes working. I can see the shared parameter in the parameter file. Was it supposed to add it to the project too? I guess I thought it did, but I’m not seeing it in the parameter list.

Thanks again!

It adds it to the Project Parameters as well. Please check again. :slight_smile: It’s that last line of code that does doc.ParameterBindings.Insert(_def, bind, _paramGroup) that is adding it in.

This is what the last section of the code looks like. I had to go through and add the tabs, etc. from the code you posted so I’m wondering if I got something wrong?? Everything else appears to be working fine, it just doesn’t add the parameter to the list of project parameters.

josh, i didn’t check your code, but when developing my version of projectparameter i found out, that binding didn’t work when the parameter or group already existed in the shared parameter list.

i removed group and parameter and rerun the script.

hope that helps

 

 

Josh,

It’s the param=doc.ParameterBindings.Insert(_def, bind, _paramGroup) line. You have to un-indent that since it should be executed every time this code runs.

 

Hi Peter, I’d be interested to go the project parameter route instead of shared parameter. Did you publish a module I could look at/use for creating the project parameter? I really appreciate the help guys.

Konrad, I was able to get your code fixed up. Took some trial and error with the indenting…I have very little coding experience!

Thanks!

no didn’t publish up to now.

as it is not a selfexplaining node. will need a lot of improvement.

(parameter types…)

maybe during the next few days

sry

Peter,

Did you publish something?
I want to fill out the project parameters from an xml file, but i don’t know how to reach the project parameters.

Thanks

Oke i installed clockwork from Andreas Dieckmann,

It has a node called Document.listbuiltinParametersbyname, this way i can select a project parameter
or i can use document.projectinfo

So i can get to the parameters but how do i set them.
If i use element.setparameterbyname, than i need to insert an element and that isn’t a possibility with project parameters.