Revit 2013 Unit Conversion Utility

Here is a Revit 2013 update of the Revit 2011

unit conversion utility
,
originally posted and

described for Revit 2010
, by

Harry Mattison
.

In his own words:

I was looking at the great

unit conversion utility
and
created a
new version upgraded to Revit 2013 that
I thought you readers might enjoy.

I ran into one issue that required a code enhancement to catch an exception thrown by many parameters, such as HVAC_Temperature, HVAC_Slope, etc.

The rest of the code handles error conditions nicely, so I was surprised to see these uncaught exceptions.

I solved it by adding a try/catch statement around the existing call to AddParameter in frmUnitConversions.cs:


  if( null == _Parameter )
  {
    // Create a new parameter
 
    try
    {
      _Parameter
        = _DbDocument.FamilyManager.AddParameter(
          parameterTypeToCreate.ToString(),
          BuiltInParameterGroup.INVALID,
          parameterTypeToCreate,
          false );
    }
    catch( Exception ex )
    {
      TaskDialog.Show( "Error",
        "Cannot create parameter '"
        + parameterTypeToCreate.ToString()
        + "' with parameter type '"
        + parameterTypeToCreate
        + "' because of exceptionn"
        + ex.Message.ToString() );
 
      return;
    }
  }

I think I figured out what is causing this and thought it would be interesting to share with your blog readers:

I am creating family parameters from a shared parameter file.
Some parameters have types that are not supported in the version of Revit that I am currently using, which is Revit Architecture.

I created this function to detect the situation:


  public static bool canGetFormatOptions(
    Document doc,
    FamilyParameter familyParameter )
  {
    try
    {
      UnitType parameterUnitType
        = ConvertParameterTypeToUnitType(
          familyParameter.Definition.ParameterType );
 
      doc.ProjectUnit.get_FormatOptions(
        parameterUnitType );
 
      return true;
    }
    catch
    {
      return false;
    }
  }

I use it like this:


  if( !canGetFormatOptions(
    famDoc, familyParameter ) )
  {
    TaskDialog.Show( "Error",
      "Cannot set parameter value.nUnit Type "
      + ConvertParameterTypeToUnitType(
        familyParameter.Definition.ParameterType )
      + " is not supported in Revit "
      + famDoc.Application.Product.ToString() );
  }

Take care.

The updated code is provided above.

Many thanks to Harry for this update with his enhancement and analysis!


Comments

2 responses to “Revit 2013 Unit Conversion Utility”

  1. Hi Jeremy,
    I am a bit of a newby when it comes to adding programming and using this utility.
    how do I go about making use of this utility??
    Regards,
    Gary

  2. Dear Gary,
    I am neither capable or willing to support you any more than I already do, by publishing all I know here for you to read.
    I would suggest working through the getting started material:
    http://thebuildingcoder.typepad.com/blog/about-the-author.html#2
    That tells you all you need to know.
    Cheers, Jeremy.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading