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.
Solved! Go to Solution.
Solved by jeremytammik. Go to Solution.
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
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.
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; }
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.
...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); } }