We discussed about a similar topic, created some code, and posted another article long time ago.
Revit Units .NET API: Group Revit Unit Types (based on Revit Discipline)
The discussion and code in the post was focused on the UnitType enum values. Things were relatively easy there as the Revit .NET provides a straightforward API (UnitUtils.GetUnitGroup(UnitType)) to get the unit group that the unit type belongs to. The Revit .NET UnitGroup enum values indicate right the so-called disciplines, from the Revit User Interface perspective.
We’d like to do similar grouping to the Revit .NET ParameterType enum values as well, but found things are much more difficult. Revit itself internally groups those ParameterType (Type of Parameter as in the UI) well based on the Revit disciplines but obviously that functionality has not been exposed to outside. The Shared Parameter Editor of Revit can prove that:
In terms of those ParameterType enum values, they are pretty arbitrary, not following any certain naming conventions.
As can be seen, though some ParameterType enum values seem to follow some naming convention such as those HVAC ones, it is not consistent, and worse, most of others don’t follow anything. We are going to double confirm this fact with some code. Here we go:
public static Dictionary<string, List<ParameterType>> GroupParameterTypesOnDiscipline(RvtApplication app)
{
Dictionary<string, List<ParameterType>> disciplineToParameterTypes = new Dictionary<string, List<ParameterType>>();
string oriFile = app.SharedParametersFilename;
string tempFile = Path.GetTempFileName() + ".txt";
try
{
using (File.Create(tempFile)) { }
app.SharedParametersFilename = tempFile;
Definitions tempDefinitions = app.OpenSharedParameterFile().Groups.Create("TemporaryDefintionGroup").Definitions;
foreach (ParameterType pt in Enum.GetValues(typeof(ParameterType)))
{
if (pt != ParameterType.Invalid)
{
Definition def = tempDefinitions.Create(pt.ToString(), pt);
UnitGroup ug = UnitUtils.GetUnitGroup(def.UnitType);
if (disciplineToParameterTypes.ContainsKey(ug.ToString()))
{
disciplineToParameterTypes[ug.ToString()].Add(pt);
}
else
{
disciplineToParameterTypes.Add(ug.ToString(), new List<ParameterType> { pt });
}
}
}
File.Delete(tempFile);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString(), "GroupParameterTypesOnDiscipline");
}
finally
{
app.SharedParametersFilename = oriFile;
}
return disciplineToParameterTypes;
}
public static void PrintOutToDebug(Dictionary<string, List<ParameterType>> targetObj)
{
string info = "";
foreach (KeyValuePair<string, List<ParameterType>> kvp in targetObj)
{
info += string.Format("{0}" + Environment.NewLine, kvp.Key);
foreach (ParameterType pt in kvp.Value)
{
string label = pt.ToString();
try
{
label = LabelUtils.GetLabelFor(pt);
}
catch
{
System.Diagnostics.Debug.WriteLine(string.Format("{0} doesn't have a label?!", pt));
}
info += string.Format("\t{0} || {1}" + Environment.NewLine, label, pt);
}
}
System.Diagnostics.Debug.Write(info);
}
If some caller code as follows is executed in Revit, we will get some output in the Visual Studio debug window.
SharedParameterRevitHelper.PrintOutToDebug(SharedParameterRevitHelper.GroupParameterTypesOnDiscipline(CachedApp));
FamilyType doesn't have a label?!
Common
Text || Text
Integer || Integer
Number || Number
Length || Length
Area || Area
Volume || Volume
Angle || Angle
URL || URL
Material || Material
Yes/No || YesNo
Number of Poles || NumberOfPoles
Fixture Units || FixtureUnit
FamilyType || FamilyType
Load Classification || LoadClassification
Slope || Slope
Currency || Currency
Mass Density || MassDensity
Structural
Force || Force
Linear Force || LinearForce
Area Force || AreaForce
Moment || Moment
Stress || Stress
Unit Weight || UnitWeight
Thermal Expansion Coefficient || ThermalExpansion
Linear Moment || LinearMoment
Point Spring Coefficient || ForcePerLength
Rotational Point Spring Coefficient || ForceLengthPerAngle
Line Spring Coefficient || LinearForcePerLength
Rotational Line Spring Coefficient || LinearForceLengthPerAngle
Area Spring Coefficient || AreaForcePerLength
Reinforcement Volume || ReinforcementVolume
Reinforcement Length || ReinforcementLength
Acceleration || Acceleration
Bar Diameter || BarDiameter
Crack Width || CrackWidth
Displacement/Deflection || DisplacementDeflection
Energy || Energy
Frequency || StructuralFrequency
Mass || Mass
Mass per Unit Length || MassPerUnitLength
Moment of Inertia || MomentOfInertia
Surface Area per Unit Length || SurfaceArea
Period || Period
Pulsation || Pulsation
Reinforcement Area || ReinforcementArea
Reinforcement Area per Unit Length || ReinforcementAreaPerUnitLength
Reinforcement Cover || ReinforcementCover
Reinforcement Spacing || ReinforcementSpacing
Rotation || Rotation
Section Area || SectionArea
Section Dimension || SectionDimension
Section Modulus || SectionModulus
Section Property || SectionProperty
Velocity || StructuralVelocity
Warping Constant || WarpingConstant
Weight || Weight
Weight per Unit Length || WeightPerUnitLength
Mass per Unit Area || MassPerUnitArea
HVAC
Density || HVACDensity
Friction || HVACFriction
Power || HVACPower
Power Density || HVACPowerDensity
Pressure || HVACPressure
Temperature || HVACTemperature
Velocity || HVACVelocity
Air Flow || HVACAirflow
Duct Size || HVACDuctSize
Cross Section || HVACCrossSection
Heat Gain || HVACHeatGain
Roughness || HVACRoughness
Viscosity || HVACViscosity
Air Flow Density || HVACAirflowDensity
Cooling Load || HVACCoolingLoad
Cooling Load divided by Area || HVACCoolingLoadDividedByArea
Cooling Load divided by Volume || HVACCoolingLoadDividedByVolume
Heating Load || HVACHeatingLoad
Heating Load divided by Area || HVACHeatingLoadDividedByArea
Heating Load divided by Volume || HVACHeatingLoadDividedByVolume
Air Flow divided by Volume || HVACAirflowDividedByVolume
Air Flow divided by Cooling Load || HVACAirflowDividedByCoolingLoad
Area divided by Cooling Load || HVACAreaDividedByCoolingLoad
Slope || HVACSlope
Area divided by Heating Load || HVACAreaDividedByHeatingLoad
Factor || HVACFactor
Duct Insulation Thickness || HVACDuctInsulationThickness
Duct Lining Thickness || HVACDuctLiningThickness
Energy
Energy || HVACEnergy
Coefficient of Heat Transfer || HVACCoefficientOfHeatTransfer
Thermal Resistance || HVACThermalResistance
Thermal Mass || HVACThermalMass
Thermal Conductivity || HVACThermalConductivity
Specific Heat || HVACSpecificHeat
Specific Heat of Vaporization || HVACSpecificHeatOfVaporization
Permeability || HVACPermeability
Electrical
Current || ElectricalCurrent
Electrical Potential || ElectricalPotential
Frequency || ElectricalFrequency
Illuminance || ElectricalIlluminance
Luminous Flux || ElectricalLuminousFlux
Power || ElectricalPower
Apparent Power || ElectricalApparentPower
Power Density || ElectricalPowerDensity
Wire Diameter || WireSize
Efficacy || ElectricalEfficacy
Wattage || ElectricalWattage
Color Temperature || ColorTemperature
Luminous Intensity || ElectricalLuminousIntensity
Luminance || ElectricalLuminance
Temperature || ElectricalTemperature
Cable Tray Size || ElectricalCableTraySize
Conduit Size || ElectricalConduitSize
Demand Factor || ElectricalDemandFactor
Electrical Resistivity || ElectricalResistivity
Piping
Density || PipingDensity
Flow || PipingFlow
Friction || PipingFriction
Pressure || PipingPressure
Temperature || PipingTemperature
Velocity || PipingVelocity
Viscosity || PipingViscosity
Pipe Size || PipeSize
Roughness || PipingRoughness
Volume || PipingVolume
Slope || PipingSlope
Pipe Insulation Thickness || PipeInsulationThickness
As can be seen, those ParameterType enum values belonging to the Energy discipline also have the HVAC as prefix. The last ParameterType of the Piping discipline has ‘Pipe’ as prefix instead of ‘Piping’ for others. The ParameterType enum values of Common and Structural disciplines are all mixed together without any distinguishable hints.
So, we create temporary parameter Definition (ExternalDefiniton actually) for each ParameterType and retrieve the unit type information from it, then use pretty the same mechanism to group the ParameterType enum values as the old post introduced.
By the way, the Revit SharedParametersFilename should not be permanently overridden by any addin or code. That explains why we apply some code to switch between the original and the temporary shared parameter files. It is another good practise that we strongly recommend.
Also a side finding: the FamilyType ParameterType does not have a label at all!
Enjoy it!
If you find the advice and code useful, please kindly make a donation through clicking the right button from the following link:
Recent Comments