Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Determine the Project Units: Metric or Standard

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
sam.z
6392 Views, 5 Replies

Determine the Project Units: Metric or Standard

Hello

 

How do you check if the current documents/projects metrics are Metric or Standard, ie, CM & Metres or Feet & Inches. I need to check if the project units are in Metric, if it isn't then I need to display an error and discontinue the Addin operation. If I dont and the project units are in Standard; when I go to set a pits (FamilyInstance) diameter to 1100(mm) it gets set to 1100 feet.

 

instance.get_Parameter("Diameter").SetValueString(diameter.ToString());

 

I have seen there is a Document.GetUnits() method to get a Units object but its not very clear how to determine the Project Metrics/Measurement Type using this object.

5 REPLIES 5
Message 2 of 6
jeremytammik
in reply to: sam.z

Dear Sam,

 

The units used internally by Revit are fixed and cannot be changed.

 

What you are changing by modifying what you call 'project' units is only affecting the user interface.

 

When you set a parameter representing a length, you always have to specify it in feet:

 

http://thebuildingcoder.typepad.com/blog/2011/03/internal-imperial-units.html

 

If your length is in millimetres, you need to convert it to feet before setting the parameter value.

 

This has been explained about a thousand times on The Building Coder:

 

http://thebuildingcoder.typepad.com/blog/units

 

It also provides numerous examples of how this conversion can be achieved.

 

Here is the latest one of these:

 

http://thebuildingcoder.typepad.com/blog/2013/09/boolean-operations-for-2d-polygons.html#3

 

I hope this clarifies.

 

Best regards,

 

Jeremy

 

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 3 of 6
RickyBell
in reply to: jeremytammik

Hey Sam,  I know this post is old and you've probably figured out your issue.  Though Jeremy is right about units in general, if you're looking to get the displayed units on a project level, I've found that using an Element's parameter units works.

 

Metric Project:

elem.get_Parameter("Diameter").DisplayUnitType.ToString()
"DUT_MILLIMETERS"

 

Imperial Project:
elem.get_Parameter("Diameter").DisplayUnitType.ToString()
"DUT_FRACTIONAL_INCHES"

 

Hope this helps someone who might come across this issue.

Message 4 of 6
SerhanB
in reply to: RickyBell

You can also get it from the document object;

 

 

public Result Execute(ExternalCommandData CommandData, ref string message, ElementSet elements)
{
   var units = CommandData.Application.ActiveUIDocument.Document.DisplayUnitSystem;
return Result.Succeeded; }

 

Serhan BAKIR
http://www.stand.com.tr/
Message 5 of 6
Anonymous
in reply to: SerhanB

Lately I suddenly got a lot of documents throwing an exception when accessing Document.DisplayUnitSystem so I'd recomment to wrap this in a try catch. Don't know why this is happening suddenly, never had an issue with this before.

Message 6 of 6
matthias.schneider
in reply to: Anonymous

...and it's still an issue...

Document.DisplayUnitSystem will fool you. If the file was created in imperial units and you changed all settings (general, mep, etc.) to metric  your units will still be DisplayUnit.IMPERIAL.

That means you are back to Ricky's element parameters.

 

I'm using the project base point (assuming any Revit file has one) to get a length parameter and check if the parameter's display unit is metric to either list the Value in meters or feet.

I also assume that the units in metric files will be meters, centimeters, millimeters, etc., containing "meter" and imperial files won't.

double scale = 1;
if (new FilteredElementCollector(doc).OfClass(typeof(BasePoint)).FirstOrDefault() is BasePoint basePoint)
     {
        Parameter paramBasePoint = basePoint.get_Parameter(BuiltInParameter.BASEPOINT_ELEVATION_PARAM);
        UnitType unitType = paramBasePoint.Definition.UnitType;
        string sdisplay = paramBasePoint.DisplayUnitType.ToString().ToLower();
        if (sdisplay.Contains("meter"))
           {
               scale = UnitUtils.ConvertFromInternalUnits(1, DisplayUnitType.DUT_METERS);
           }
    }

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Rail Community