Api 2021 -> Check units that user may have set

Api 2021 -> Check units that user may have set

dante.van.wettum
Advocate Advocate
1,822 Views
4 Replies
Message 1 of 5

Api 2021 -> Check units that user may have set

dante.van.wettum
Advocate
Advocate

So in some of my addin code, im exporting data, and this is also relevant for coordinate systems.

Therefore its always important to check what the user has set the units of his project to, else many calculations may fail.

 

so to make a long story short; up untill revit 2020 i have been using this call:

DisplayUnitType displayUnitsType = commandData.Application.ActiveUIDocument.Document.GetUnits().GetFormatOptions(UnitType.UT_Length).DisplayUnits;

From here on i could simply check if the type set set to DUT_MILLIMETERS so i would be sure the conversion factor would be correct.

 

Now with the change of API 2021 i am trying to understand this ForgeTyeID.

With "GetFormatOptions" i could paste a ForgeTypeID but this would already be a set unit type (inch/meter/...).

it can no longer check what the user actually has set in the unit settings menu.

 

Alternatively i tried this new query:

FormatOptions formatoption = commandData.Application.ActiveUIDocument.Document.GetUnits().GetFormatOptions(SpecTypeId.Length);

so now im checking what the user has put in his settings menu for Units in the field for Length.

But here i find it strange to notice that The formatOptions has no exposed parameter to see the units.

yes it has "DisplayUnits" , but this is deprecated...

 

So im back to the beginning: with this new change in 2021 API, how do i check what the user has set in his units menu? just checking document DisplayUnitSystem is not safe, although its set to METRIC, a user can still just set inches in its units settings.

anyone found a way how to do this now in 2021 in a reliable way?

 format.JPG

 

deprecated.png

Accepted solutions (1)
1,823 Views
4 Replies
Replies (4)
Message 2 of 5

Sean_Page
Collaborator
Collaborator
Accepted solution

See this link and I think it may have what you need.

 

https://forums.autodesk.com/t5/revit-api-forum/forgetypeid-how-to-use/m-p/9442780/highlight/true#M46...

Sean Page, AIA, NCARB, LEED AP
Partner, Computational Designer, Architect
0 Likes
Message 3 of 5

dante.van.wettum
Advocate
Advocate

yes i already seen that link, and the snipit from the changelog.

nevertheless, i did not find a solution there.

Most of those UnitUtils are already deprecated as well.

Message 4 of 5

m.vallee
Advocate
Advocate

This is what could help you :

 

FormatOptions fo = document.GetUnits().GetFormatOptions(ut);
ForgeTypeId dut = fo.GetUnitTypeId();

if(dut == UnitTypeId.Millimeters)

{

// your code here

}

0 Likes
Message 5 of 5

Joseph_Peel
Enthusiast
Enthusiast

I managed to get the unit type set by the user and use it to convert displayed values like this;

displayArea = UnitUtils.ConvertFromInternalUnits(area, doc.GetUnits().GetFormatOptions(SpecTypeId.Area).GetUnitTypeId());

0 Likes