Get project length unit symbol

Get project length unit symbol

atis.sed
Advocate Advocate
1,797 Views
4 Replies
Message 1 of 5

Get project length unit symbol

atis.sed
Advocate
Advocate

Hi everyone,

I have cross searched the internet, this forum, Revit 2023 SDK and the  thebuildingcoder blog for a rather simple problem- how to display length symbol for documents units. How to get ForgeTypeID for active documents unit symbol?

 

Units docUnits = doc.GetUnits();
ForgeTypeId spec = SpecTypeId.Length;
FormatOptions myFormat = docUnits.GetFormatOptions(spec);
ForgeTypeId mySymbol = myFormat.GetSymbolTypeId();
string unitSymbol = LabelUtils.GetLabelForSymbol(mySymbol);
//Autodesk.Revit.Exceptions.ArgumentException: 'Symbol must have a definition.
Parameter name: symbolTypeId'

 

 

0 Likes
Accepted solutions (1)
1,798 Views
4 Replies
Replies (4)
Message 2 of 5

aignatovich
Advisor
Advisor
Accepted solution

Check

mySymbol.Empty();

If it's false before calling

LabelUtils.GetLabelForSymbol(mySymbol);

 

If it's true, then no symbol is specified, e.g. your unitSymbol would be an empty string

0 Likes
Message 3 of 5

esatis
Advocate
Advocate

Is it possible to get unit Symbol for length units set in Document IF the Symbol is not set in the Document ?
For example, in my document I have millimeters set as Length, with no symbol. In Text Note Type the symbol "mm" is present, BUT it is not present when snooping object. But when I LookupParameter("Text Size")?.AsValueString()   I get 3.5000 mm. So there is some automatic formatting to Text Size parameter that takes into account "mm" Symbol even if it is not set in Document.
(image below)

 

esatis_0-1682450871477.png

 

Here is my method for formatting Units and I would like to be able to add Symbol to output string even if it is not set in Doc. 

 

 

public static string ConvertUnits(double value, Document doc)
        {
            
            Units docUnits = doc.GetUnits();
            ForgeTypeId spec = SpecTypeId.Length;
            FormatOptions myFormat = docUnits.GetFormatOptions(spec);
            ForgeTypeId mySymbol = myFormat.GetSymbolTypeId();
           

            string unitSymbol = "";     
            if (!mySymbol.Empty())
            {
                unitSymbol = "";
            }
            else
            {
                unitSymbol = "//SYMBOL FOR LENGTH UNITS SET IN DOCUMENT"; 
            }
            
            return _ = UnitFormatUtils.Format(docUnits, spec, value, false) + unitSymbol;
        }

 

 

 

0 Likes
Message 4 of 5

rufaidemir
Explorer
Explorer

can you give an example please

0 Likes
Message 5 of 5

Chuong.Ho
Advocate
Advocate

This is full example for get unit symbol from parameter :

private string GetSymbolUnit(Parameter parameter)
        {
            if (parameter.StorageType != StorageType.Double) return String.Empty;
            ForgeTypeId? unitTypeId = parameter?.GetUnitTypeId();
            return GetSymbolUnit(unitTypeId);
        }
        private string GetSymbolUnit(ForgeTypeId unitTypeId)
        {
            if (!FormatOptions.CanHaveSymbol(unitTypeId))
            {
                return null;
            }

            var validSymbols = FormatOptions.GetValidSymbols(unitTypeId);
            var typeId = validSymbols?.Where(x => !x.Empty());
            foreach (var symbolId in typeId!)
            {
                return LabelUtils.GetLabelForSymbol(symbolId);
            }

            return null;
        }

And example for get from formatoption without exeption

   FormatOptions formatOptions = Doc.GetUnits().GetFormatOptions(SpecTypeId.Length);
            // get symbol unit
            ForgeTypeId forgeTypeId = formatOptions.GetUnitTypeId();
            string symbol = GetSymbolUnit(forgeTypeId);

Chuong Ho

EESignature