By Aaron Lu
Connector Element of some fitting family has a parameter named "Radius", to change its value, we may write code like this:
connectorElement.Radius = 0.041;
But Radius property is read-only, so it can not be compiled or even it can be, exception will be thrown when running it.
Another way is using Parameter:
var radiusPara = connectorElement.get_Parameter(BuiltInParameter.CONNECTOR_RADIUS); radiusPara.Set(0.041);
But radiusPara here is also read-only (radiusPara.IsReadOnly == true), exception will be thrown too.
As we know that radius is not changeable in UI, rather, we need to change the associated family parameter, e.g. in this case, "Normal Radius"
Then the question is how to get the associated family parameter?
Following code shows the answer:
var sb = new StringBuilder(); foreach (var connectorPara in connectorElement.GetOrderedParameters()) { foreach (FamilyParameter familyPara in doc.FamilyManager.Parameters) { foreach (Parameter associatedPara in familyPara.AssociatedParameters) { if (connectorPara.Id == associatedPara.Id && associatedPara.Element.Id == connectorElement.Id) { //associate parameter found sb.AppendLine("'" + associatedPara.Definition.Name + "(" + (BuiltInParameter)associatedPara.Id.IntegerValue + ")' <-> '" + familyPara.Definition.Name + "'"); } } } } TaskDialog td = new TaskDialog("Parameter associations"); td.MainContent = sb.ToString(); td.TitleAutoPrefix = false; td.Show();
It will display all parameter associations between ConnectorElement parameters and family parameters, let's see what it does:
- Use GetOrderedParameters() to get all parameters of connectorElement, iterate each parameter,
- Use Document.FamilyManager.Parameters to get all FamilyParameters, iterate each family parameter,
- Use FamilyParameter.AssociatedParameters to get all assocated parameters of elements in the document,
- Iterate all associated parameters, check whether the parameter of connector element is the same as the associated parameter, and they belong to the same element.
Below is the result:
For now, it is clear on how to change radius:
- Find the associated family parameter
- Set the family parameter using Document.FamilyManager.Set(familyParameter, value)
complete code is shown as below:
var doc = commandData.Application.ActiveUIDocument.Document; var uiSel = commandData.Application.ActiveUIDocument.Selection; ConnectorElement connectorElement = null; try { var reference = uiSel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, "Pick a connector"); connectorElement = doc.GetElement(reference) as ConnectorElement; if (connectorElement != null) { var radiusPara = connectorElement.get_Parameter(BuiltInParameter.CONNECTOR_RADIUS); foreach (FamilyParameter familyPara in doc.FamilyManager.Parameters) { foreach (Parameter associatedPara in familyPara.AssociatedParameters) { if (radiusPara.Id == associatedPara.Id && associatedPara.Element.Id == connectorElement.Id) { //associate parameter found using (Transaction transaction = new Transaction(doc)) { transaction.Start("Set Radius"); doc.FamilyManager.Set(familyPara, 0.041); transaction.Commit(); } } } } } } catch (Autodesk.Revit.Exceptions.OperationCanceledException) { }