Message 1 of 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Dear All,
I am really struggling on this with new changes in Revit API 2022. Could some one help me with this?
I am trying to set a variable value, for some reason, based on the current document unit. I had this in previous versions implemented using a switch case. Here is my code;
this.displayUnitType = this.doc.GetUnits().GetFormatOptions(UnitType.UT_Length).DisplayUnits;
switch (this.displayUnitType)
{
case DisplayUnitType.DUT_METERS:
this.docUnitCode = 0;
break;
case DisplayUnitType.DUT_CENTIMETERS:
this.docUnitCode = 1;
break;
case DisplayUnitType.DUT_MILLIMETERS:
this.docUnitCode = 2;
break;
case DisplayUnitType.DUT_DECIMAL_FEET:
this.docUnitCode = 3;
break;
}
I am sorry but getting too much confused with the new ForgeTypeId / SpecTypeId and UnitTypeId. However, I tried this code below but Visual Studio throws the error saying that the 'case' expects a constant value.
this.docUnitType = this.doc.GetUnits().GetFormatOptions(SpecTypeId.Length).GetUnitTypeId();
switch (this.docUnitType.TypeId)
{
case UnitTypeId.Meters.TypeId:
this.docUnitCode = 0;
break;
case UnitTypeId.Centimeters.TypeId:
this.docUnitCode = 1;
break;
case UnitTypeId.Millimeters.TypeId:
this.docUnitCode = 2;
break;
case UnitTypeId.Feet.TypeId:
this.docUnitCode = 3;
break;
}
Same time, if I try this code below, it works perfectly fine.
var docUnitType = doc.GetUnits().GetFormatOptions(SpecTypeId.Length).GetUnitTypeId();
if (docUnitType.TypeId == UnitTypeId.Millimeters.TypeId)
{
TaskDialog.Show("Message", "Millimeters...");
}
Why is it not working with Switch Case?
Solved! Go to Solution.