InvalidOperationException at Parameter.GetUnitTypeId()

InvalidOperationException at Parameter.GetUnitTypeId()

jganeshZKVQL
Contributor Contributor
772 Views
7 Replies
Message 1 of 8

InvalidOperationException at Parameter.GetUnitTypeId()

jganeshZKVQL
Contributor
Contributor

Hello,


in our c# revit addin we encountered an error when using Revit 2021 for a code part that works with Revit 2022 without a problem:

 

private static bool IsValidDoubleUnit(Parameter p)
=> !p.IsShared && p.HasValue && p.StorageType == StorageType.Double && UnitUtils.IsUnit(p.GetUnitTypeId());

 

 

The exception throws at calling: p.GetUnitTypeId().

The description says that it is: Thrown if this parameter is not of value type.

Well .... p.StorageType == StorageType.Double ... ‌‌

Is this a known issue/restriction for the handling of the new ForgeTypeId in Revit 2021?

What is the proposed strategy of avoiding this?


Shortened Stack Trace:

Autodesk.Revit.Exceptions.InvalidOperationException
bei Autodesk.Revit.DB.Parameter.GetUnitTypeId()
bei Namespace.IsValidDoubleUnit(Parameter p) in source.cs:Zeile 294.

 

Thank you for any help!

0 Likes
Accepted solutions (1)
773 Views
7 Replies
Replies (7)
Message 2 of 8

jeremy_tammik
Alumni
Alumni

What specific parameter are you testing? On which element?

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 8

jeremy_tammik
Alumni
Alumni

I asked the devteam for you.

 

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 4 of 8

jganeshZKVQL
Contributor
Contributor

These are the objects printouts: 

 

p
{Autodesk.Revit.DB.Parameter}
    Definition: {Autodesk.Revit.DB.InternalDefinition}
    DisplayUnitType: 'p.DisplayUnitType' threw an exception of type 'Autodesk.Revit.Exceptions.InvalidOperationException'
    Element: {Autodesk.Revit.DB.Plumbing.Pipe}
    GUID: 'p.GUID' threw an exception of type 'Autodesk.Revit.Exceptions.InvalidOperationException'
    HasValue: true
    Id: {-1140246}
    IsReadOnly: true
    IsShared: false
    StorageType: Double
    UserModifiable: true

 

p.Definition
{Autodesk.Revit.DB.InternalDefinition}
    BuiltInParameter: RBS_PIPE_FIXTURE_UNITS_PARAM
    Id: {-1140246}
    IsValidObject: true
    Name: "Installationseinheiten"
    ParameterGroup: PG_MECHANICAL_AIRFLOW
    ParameterType: FixtureUnit
    UnitType: UT_Number
    VariesAcrossGroups: false
    Visible: true

 

element
{Autodesk.Revit.DB.Plumbing.Pipe}
    AssemblyInstanceId: {-1}
    Category: {Autodesk.Revit.DB.Category}
    ConnectorManager: {Autodesk.Revit.DB.ConnectorManager}
    CreatedPhaseId: {372166}
    DemolishedPhaseId: {-1}
    DesignOption: null
    Diameter: 0.32808398950131235
    Document: {Autodesk.Revit.DB.Document}
    FlowState: LaminarState
    GroupId: {-1}
    Height: '((Autodesk.Revit.DB.MEPCurve)element).Height' threw an exception of type 'Autodesk.Revit.Exceptions.InvalidOperationException'
    Id: {608558}
    IsPlaceholder: false
    IsTransient: false
    IsValidObject: true
    LevelId: {-1}
    LevelOffset: -14.555326840290602
    Location: {Autodesk.Revit.DB.LocationCurve}
    MEPSystem: {Autodesk.Revit.DB.Plumbing.PipingSystem}
    Name: "Standard"
    OwnerViewId: {-1}
    Parameters: {Autodesk.Revit.DB.ParameterSet}
    ParametersMap: {Autodesk.Revit.DB.ParameterMap}
    Pinned: false
    PipeSegment: {Autodesk.Revit.DB.Plumbing.PipeSegment}
    PipeType: {Autodesk.Revit.DB.Plumbing.PipeType}
    ReferenceLevel: {Autodesk.Revit.DB.Level}
    UniqueId: "4a32aa57-7137-4718-8e76-63080685d809-0009492e"
    VersionGuid: {0db2d433-fc99-48bc-885d-9184db9efa29}
    ViewSpecific: false
    Width: '((Autodesk.Revit.DB.MEPCurve)element).Width' threw an exception of type 'Autodesk.Revit.Exceptions.InvalidOperationException'
    WorksetId: {0}

 

0 Likes
Message 5 of 8

jganeshZKVQL
Contributor
Contributor
Thank you!
Is it important for me to be able to login there?
0 Likes
Message 6 of 8

jeremy_tammik
Alumni
Alumni

No, not important and not possible, either. The link is just for me myself.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 7 of 8

david_becroft
Autodesk
Autodesk
Accepted solution

Good morning,

 

Yes, this parameter is of type ParameterType.FixtureUnit. FixtureUnit parameters have an unfortunate combination of properties. They have StorageType.Double but they are not considered to be of "value type", so Parameter.GetUnitTypeId() throws the InvalidOperationException for FixtureUnit parameters.

 

In the Revit 2021 API, there is no way to directly check whether a given parameter has this "value type" nature, so clients just have to check whether the parameter type is FixtureUnit:

private static bool IsValidDoubleUnit(Parameter p) => 
  !p.IsShared && p.HasValue && 
  p.StorageType == StorageType.Double && 
  p.Definition.ParameterType != ParameterType.FixtureUnit && 
  UnitUtils.IsUnit(p.GetUnitTypeId());

 

In the Revit 2022 API, aside from the ParameterType enumeration being deprecated, FixtureUnit was retired and replaced with Number. It is safe to call Parameter.GetUnitTypeId() on Number parameters.

Message 8 of 8

jganeshZKVQL
Contributor
Contributor

Thank you @david_becroft, this is working well!
I implemented this for the Revit 2021 compile condition only, to avoid deprecation issues in future.