forked from jchristel/SampleCodeRevitBatchProcessor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RevitSharedParameterAdd.py
133 lines (116 loc) · 6.29 KB
/
RevitSharedParameterAdd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# based on building coder article:
# https://thebuildingcoder.typepad.com/blog/2012/04/adding-a-category-to-a-shared-parameter-binding.html
import clr
import System
from Autodesk.Revit.DB import *
# custom result class
import Result as res
# import InTransaction from common module
import Common as com
from Common import InTransaction
# opens a shared parameter file
def LoadSharedParameterFile(doc, path):
app = doc.Application
app.SharedParametersFilename = path
return app.OpenSharedParameterFile()
# binds a shared parameter to a revit category
# refer building coder articel referenced in header
# parameterGrouping : where parameter appears in properties section in UI
# groupName: name of group parameter is under in Shared Parameter file
# category: expects built in category, to which the parameter will be bound
def BindSharedParameter(doc, category, parameterName, groupName, parameterType, isVisible, isInstance, parameterGrouping, sharedParameterFilepath):
returnvalue = res.Result()
try:
app = doc.Application
# This is needed already here to
# store old ones for re-inserting
catSet = app.Create.NewCategorySet()
# Loop all Binding Definitions
# IMPORTANT NOTE: Categories.Size is ALWAYS 1 !?
# For multiple categories, there is really one
# pair per each category, even though the
# Definitions are the same...
iter = doc.ParameterBindings.ForwardIterator()
iter.Reset()
while iter.MoveNext():
if(iter.Key != None):
definition = iter.Key
elemBind = iter.Current
#check parameter name match
if(parameterName == definition.Name):
try:
cat = doc.Settings.Categories.get_Item(category)
if(elemBind.Categories.Contains(cat)):
# check parameter type
if(definition.ParameterType != parameterType):
returnvalue.UpdateSep(False, parameterName + ': wrong paramter type: '+ str(definition.ParameterType))
return returnvalue
#check binding type
if(isInstance):
if(elemBind.GetType() != InstanceBinding):
returnvalue.UpdateSep(False, parameterName + ': wrong binding type (looking for instance but got type)')
return returnvalue
else:
if(elemBind.GetType() != TypeBinding):
returnvalue.UpdateSep(False, parameterName + ': wrong binding type (looking for type but got instance)')
return returnvalue
# Check Visibility - cannot (not exposed)
# If here, everything is fine,
# ie already defined correctly
returnvalue.message = parameterName + ': Parameter already bound to category: ' + str(cat.Name)
return returnvalue
except Exception as e:
print(parameterName + ' : Failed to check parameter binding' + str(e))
# If here, no category match, hence must
# store "other" cats for re-inserting
else:
for catOld in elemBind.Categories:
catSet.Insert(catOld)
# If here, there is no Binding Definition for
# it, so make sure Param defined and then bind it!
defFile = LoadSharedParameterFile(doc,sharedParameterFilepath)
defGroup = defFile.Groups.get_Item(groupName)
if defGroup == None:
defGroup = defFile.Groups.Create(groupName)
if defGroup.Definitions.Contains(defGroup.Definitions.Item[parameterName]):
definition = defGroup.Definitions.Item[parameterName]
else:
opt = ExternalDefinitionCreationOptions(parameterName, parameterType)
opt.Visible = isVisible
definition = defGroup.Definitions.Create(opt)
# get category from builtin category
catSet.Insert(doc.Settings.Categories.get_Item(category))
bind = None
if(isInstance):
bind = app.Create.NewInstanceBinding(catSet)
else:
bind = app.Create.NewTypeBinding(catSet)
# There is another strange API "feature".
# If param has EVER been bound in a project
# (in above iter pairs or even if not there
# but once deleted), Insert always fails!?
# Must use .ReInsert in that case.
# See also similar findings on this topic in:
# http://thebuildingcoder.typepad.com/blog/2009/09/adding-a-category-to-a-parameter-binding.html
# - the code-idiom below may be more generic:
def action():
actionReturnValue = res.Result()
try:
if(doc.ParameterBindings.Insert(definition, bind, parameterGrouping)):
actionReturnValue.message = parameterName + ' : parameter succesfully bound'
return actionReturnValue
else:
if(doc.ParameterBindings.ReInsert(definition, bind, parameterGrouping)):
actionReturnValue.message = parameterName + ' : parameter succesfully bound'
return actionReturnValue
else:
actionReturnValue.UpdateSep(False, parameterName + ' : failed to bind parameter!')
except Exception as e:
actionReturnValue.UpdateSep(False, parameterName + ' : Failed to bind parameter with exception: ' + str(e))
return actionReturnValue
transaction = Transaction(doc,'Binding parameter')
returnvalue = com.InTransaction(transaction, action)
return returnvalue
except Exception as e:
returnvalue.UpdateSep(False, parameterName + ' : Failed to bind parameter with exception: ' + str(e))
return returnvalue