Set Parameter to Vary by Group Instance

Might be useful to people…

This is limited by the types of parameter which are allowed to vary by group instance, this does not include Yes/No or Length.

Thanks to @jeremytammik and anyone else who has done work in this area.

image
.
Mark

import clr

import System
from System import *

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
from System.Collections.Generic import *

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


doc = DocumentManager.Instance.CurrentDBDocument

# The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN
keyWord = IN[0]

def isInstance(definition_Name):
	#for some reason, returning the name of the Key in the bindings iterator
	#fails sometimes and says item can't be managed? so then restart Revit
	
	#the list of parameters in doc.ParameterBindings is only the ones which are
	#added in the Project Parameters box, so you're not getting ones which
	#just exist in families
	
	#this is the only way to see if a parameter is 'bound' to the doc as
	#a type or an instance parameter
	
	bindings = doc.ParameterBindings
	it = doc.ParameterBindings.ForwardIterator()
	defs = []
	value = False
	while(it.MoveNext()):
		d = it.Key
		b = it.Current
		if d.Name == definition_Name and b.GetType() == InstanceBinding:
			value = True
			break
	return value	



def canBeEditableInGroup(parameter):
    parameter_Type = parameter.ParameterType
    
    variable_Parameter_Types_List = [ParameterType.Text, 
                                     ParameterType.Area, 
                                     ParameterType.Volume, 
                                     ParameterType.Currency, 
                                     ParameterType.MassDensity, 
                                     ParameterType.URL, 
                                     ParameterType.Material]
    
    if parameter_Type in variable_Parameter_Types_List:
        return True
    else:
        return False
        
# Place your code below this line
output = []
#catch anything that doesn't work and output the error
sPs = FilteredElementCollector(doc).OfClass(SharedParameterElement)

try:
    sPs = FilteredElementCollector(doc).OfClass(SharedParameterElement)
    errorReport = None 

    #begin transaction
    TransactionManager.Instance.EnsureInTransaction(doc)
    for sP in sPs:
        #we are interested in the shared parameter 'definition'
        #the 'definition' is a container of the shared parameter
        #that holds the information about whether
        #the parameter is variable by instance in a group
        
        #we only want the shared parameters with our key word
        #we only want them if they are a type which can vary
        #we only want them if they are not already true
        #we only want them if they are instance parameters
        
        definition = sP.GetDefinition()  

        varies_Query = definition.VariesAcrossGroups
        

        if keyWord in definition.Name and canBeEditableInGroup(definition) and isInstance(definition.Name) and varies_Query != True:
            try:
                definition.SetAllowVaryBetweenGroups(doc, True)
                output.append("Success: " + definition.Name + " can now vary across group instances")
            except:
                import traceback
                errorReport_SP = traceback.format_exc()
                exception = ' does not support the specified value of allowVaryBetweenGroups'              
                if exception in errorReport_SP:
                    output.append("Failure: " + definition.Name + "cannot be set to vary between groups")
                else:
                    output.append([definition.Name, errorReport_SP])
    #finish transaction
    TransactionManager.Instance.TransactionTaskDone()

#Assign your output to the OUT variable.
except:
    import traceback
    errorReport = traceback.format_exc()


if errorReport == None:
    OUT = output

else:
    OUT = errorReport

OUT = output
2 Likes