New family parameters (FamilyParameter) can be added to a family document through calling the FamilyManager.AddParameter() method. It has three different signatures and the most popular one accepts augments of the parameter name, the parameter group (BuiltInParameterGroup), the parameter type (ParameterType), and a flag to indicate if the FamilyParameter applies to instance or type.
The FamilyParameter can also be associated with a Dimension through its Label property when necessary.
The following method can create a FamilyParameter, set its value, and associate it with a Dimension:
Public Shared Sub RawAddFamilyParamterAndSetValue(ByVal dimension As Autodesk.Revit.DB.Dimension, ByVal famMan As FamilyManager, ByVal ft As FamilyType, ByVal name As String, ByVal group As BuiltInParameterGroup, ByVal type As ParameterType, _
ByVal isInstance As Boolean, ByVal value As Object)
Dim fp As FamilyParameter = famMan.AddParameter(name, group, type, isInstance)
If value IsNot Nothing Then
RawSetFamilyParameterValue(famMan, ft, fp, value)
End If
'if (value != null) fp.SetValue(famMan, ft, value);
If dimension IsNot Nothing Then
dimension.Label = fp
End If
End Sub
Public Shared Sub RawSetFamilyParameterValue(ByVal fm As FamilyManager, ByVal ft As FamilyType, ByVal fp As FamilyParameter, ByVal value As Object)
Dim curFamType As FamilyType = fm.CurrentType
fm.CurrentType = ft
Try
Select Case fp.StorageType
Case StorageType.None
Exit Select
Case StorageType.Double
If value.GetType().Equals(GetType(String)) Then
fm.Set(fp, Double.Parse(TryCast(value, String)))
Else
fm.Set(fp, Convert.ToDouble(value))
End If
Exit Select
Case StorageType.Integer
If value.GetType().Equals(GetType(String)) Then
fm.Set(fp, Integer.Parse(TryCast(value, String)))
Else
fm.Set(fp, Convert.ToInt32(value))
End If
Exit Select
Case StorageType.ElementId
If value.GetType().Equals(GetType(Autodesk.Revit.DB.ElementId)) Then
fm.Set(fp, TryCast(value, Autodesk.Revit.DB.ElementId))
ElseIf value.GetType().Equals(GetType(String)) Then
fm.Set(fp, New Autodesk.Revit.DB.ElementId(Integer.Parse(TryCast(value, String))))
Else
fm.Set(fp, New Autodesk.Revit.DB.ElementId(Convert.ToInt32(value)))
End If
Exit Select
Case StorageType.String
fm.Set(fp, value.ToString())
Exit Select
End Select
Catch
Throw New Exception("Invalid Value Input!")
Finally
fm.CurrentType = curFamType
End Try
End Sub
The help method RawSetFamilyParameterValue() has been introduced before. The FamilyParameter Writer can generate it in no time. If necessary the extension method can be generated as well and be used in the method RawAddFamilyParamterAndSetValue() instead.
FamilyParameter Creator of RevitAddinWizard can take care of all these in either C# or VB.NET.
Recent Comments