Skip to content

Commit

Permalink
Fixed parameter binding
Browse files Browse the repository at this point in the history
  • Loading branch information
marcantondahmen committed Nov 9, 2020
1 parent b389c05 commit 985c8b5
Showing 1 changed file with 61 additions and 59 deletions.
120 changes: 61 additions & 59 deletions revitron/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,87 +53,88 @@ def __init__(self, element, name):


@staticmethod
def isBoundToCategory(category, paramName):
"""
Test if a parameter is bound to a given category.
Args:
category (string): The category name
paramName (string): The parameter name
Returns:
boolean: Returns True if parameter is bound already
"""
import revitron

definition = None

for param in revitron.Filter().byClass(revitron.DB.SharedParameterElement).getElements():
if param.GetDefinition().Name == paramName:
definition = param.GetDefinition()
break
try:
if definition:
binding = revitron.DOC.ParameterBindings[definition]
for cat in binding.Categories:
if cat.Name == category:
return True
except:
pass


@staticmethod
def bind(category, paramName, paramType = 'Text'):
def bind(category, paramName, paramType = 'Text', typeBinding = False):
"""
Bind a new parameter to a category.
Args:
category (string): The built-in category
paramName (string): The parameter name
paramType (string): The parameter type (see `here <https://www.revitapidocs.com/2019/f38d847e-207f-b59a-3bd6-ebea80d5be63.htm>`_)
Defaults to "Text".
typeBinding (bool): Bind parameter to type instead of instance. Defaults to False.
Returns:
boolean: Returns True on success and False on error
boolean: Returns True on success and False on error.
"""
import revitron

if Parameter.isBoundToCategory(category, paramName):
return True


paramFile = revitron.APP.OpenSharedParameterFile()
group = None
definition = None


if paramFile is None:
print('Please define a shared parameters file.')
return False

for item in paramFile.Groups:
if item.Name == 'REVITRON':
group = item
break
definition = None

if not group:
group = paramFile.Groups.Create('REVITRON')

for item in group.Definitions:
if item.Name == paramName:
definition = item
# Try to get an existing parameter definition with the given name.
for group in paramFile.Groups:
for item in group.Definitions:
if item.Name == paramName:
definition = item
break
if definition:
break


group = None

# If the definition hasn't been created yet, create it in the REVITRON group.
if not definition:
for item in paramFile.Groups:
if item.Name == 'REVITRON':
group = item
break
if not group:
group = paramFile.Groups.Create('REVITRON')
pt = getattr(revitron.DB.ParameterType, paramType)
ExternalDefinitionCreationOptions = revitron.DB.ExternalDefinitionCreationOptions(paramName, pt)
definition = group.Definitions.Create(ExternalDefinitionCreationOptions)

cat = revitron.Category(category).get()
categories = revitron.APP.Create.NewCategorySet();
categories.Insert(cat)
binding = revitron.APP.Create.NewInstanceBinding(categories)

# Try to get the parameter binding for the definition.
binding = revitron.DOC.ParameterBindings[definition]

# Add the given category to the categories list as the initial item
# and try to access currently bound categories to add them as well.
# In case the given category is already among the bound categories,
# stop the further execution and return False.
# In case the category is not bound yet, remove the binding from the parameter
# binding map.
categories = [revitron.Category(category).get()]
try:
for _cat in binding.Categories:
categories.append(_cat)
if _cat.Name == category:
return False
revitron.DOC.ParameterBindings.Remove(definition)
except:
pass

# Create a new category set and add all categories, the given and the previously bound ones.
categorySet = revitron.APP.Create.NewCategorySet()
for _cat in categories:
categorySet.Insert(_cat)

# Create the binding.
if typeBinding:
binding = revitron.APP.Create.NewTypeBinding(categorySet)
else:
binding = revitron.APP.Create.NewInstanceBinding(categorySet)

revitron.DOC.ParameterBindings.Insert(definition, binding)

return True



def exists(self):
"""
Checks if a parameter exists.
Expand Down Expand Up @@ -278,14 +279,15 @@ def set(self, value, paramType = 'Text'):
paramType (string, optional): The `parameter type <https://www.revitapidocs.com/2019/f38d847e-207f-b59a-3bd6-ebea80d5be63.htm>`_. Defaults to "Text".
"""
if self.parameter == None:
if Parameter.bind(self.element.Category.Name, self.name, paramType):
from revitron import _
if Parameter.bind(self.element.Category.Name, self.name, paramType, (_(self.element).getClassName() == 'FamilySymbol')):
self.parameter = self.element.LookupParameter(self.name)
else:
print('Error setting value of parameter "{}"'.format(self.name))
return False
if not self.parameter.IsReadOnly:
self.parameter.Set(value)


class ParameterNameList:
"""
Expand Down

0 comments on commit 985c8b5

Please sign in to comment.