Change DisplayUnitSystem or Unit Formats

Change DisplayUnitSystem or Unit Formats

ratno123s
Contributor Contributor
4,209 Views
13 Replies
Message 1 of 14

Change DisplayUnitSystem or Unit Formats

ratno123s
Contributor
Contributor

If the doc.DisplayUnitSystem is set to Imperial and I change all the Units format by SetFormatOptions to metric format, will that be same as a doc.DisplayUnitSystem set to Metric...??

0 Likes
Accepted solutions (1)
4,210 Views
13 Replies
Replies (13)
Message 2 of 14

jeremytammik
Autodesk
Autodesk

Have you tried it out yet? What is the result, please? Thank you!

 



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

0 Likes
Message 3 of 14

ratno123s
Contributor
Contributor

Since you asked me to test myself... I wrote the following code and bumped into another problem..The Unit formats doesn't change!!! Even when I change the units_doc.SetFormatOptions(ut, nFt);!!!

I opened two documents in my Revit Session. One with the basic Imperial template and the other one with basic Metric template. Then i tried to copy the units.FormatOptions of one document to another document for all the UnitTypes...There was not exceptions faced while debugging.. and "quick watch" ing while debug suggested that the Units.Formatoptions had changed.. However when the program ended nothing reflected in the revit .. as if no changes had taken place...

below is the code:

Document doc;
UIDocument uiDoc;
Application app;
UIApplication uiApp;

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
this.doc = commandData.Application.ActiveUIDocument.Document;
this.uiDoc = commandData.Application.ActiveUIDocument;
this.app = commandData.Application.Application;
this.uiApp = commandData.Application;
try
{
 Document doc1 = this.app.Documents.Cast<Document>().Where(x => x.Title != this.doc.Title).FirstOrDefault();
Units units_doc = this.doc.GetUnits();
Units units_1 = doc1.GetUnits();
List<UnitType> unitTypes = Enum.GetValues(typeof(UnitType)).Cast<UnitType>().ToList();//UnitUtils.GetValidUnitTypes().ToList();
foreach (var ut in unitTypes.Where(x => x != UnitType.UT_Undefined && x != UnitType.UT_Custom))
{
try
{
FormatOptions fmtOpts_1 = units_1.GetFormatOptions(ut);
FormatOptions fmtOpts_doc = units_doc.GetFormatOptions(ut);
//units_doc.SetFormatOptions(ut, fmtOpts_1); //since this did not work i wrote the below two lines
FormatOptions ft = new FormatOptions(fmtOpts_1);
ft.UseDefault = false;
units_doc.SetFormatOptions(ut, ft);
fmtOpts_doc = units_doc.GetFormatOptions(ut); // during debug this showed that the unit formats had changed.
}
catch (Exception ex)
{
}
}
return Result.Succeeded; } catch(Exception ex) { return Result.Failed; } }

 

 

next I tried with just one simple code on a imperial template revit file:

 

try
{
UnitType ut = UnitType.UT_Length;
FormatOptions ft_doc = units_doc.GetFormatOptions(ut);
//FormatOptions ft_1 = units_1.GetFormatOptions(ut);
FormatOptions nFt = new FormatOptions();
nFt.UseDefault = false;
nFt.DisplayUnits = DisplayUnitType.DUT_MILLIMETERS;
units_doc.SetFormatOptions(ut, nFt);
}
catch (Exception ex)
{
}

 

even this did not work!!!

Is not possible to change the format options through code??

Please help

0 Likes
Message 4 of 14

ratno123s
Contributor
Contributor

Currently i am using revit 2017 to test

0 Likes
Message 5 of 14

jeremytammik
Autodesk
Autodesk

When in doubt, always first check the Revit SDK samples.

 

In this case, the Units sample may be a good place to look.

 

Cheers,

 

Jeremy

 



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

0 Likes
Message 6 of 14

ratno123s
Contributor
Contributor

Got it...

Document.SetUnit() !!!

Thanks Jeremy... You are a great teacher

0 Likes
Message 7 of 14

jeremytammik
Autodesk
Autodesk

🙂

 

Thank you very much for your appreciation and confirmation!

 

Cheers,

 

Jeremy

 



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

0 Likes
Message 8 of 14

ratno123s
Contributor
Contributor

According to my findings... The Document.DisplayUnitSystem depends solely on the format of Units for UnitType.UT_Length....

 

Is that correct?

0 Likes
Message 9 of 14

ratno123s
Contributor
Contributor
Accepted solution

Just by changing the Unit format of UT_Length the DisplayUnitSystem of the document changes automatically changes

0 Likes
Message 10 of 14

Houba3314
Enthusiast
Enthusiast

hello , i want to change the unit of my project from meter to millimeter  i can do that directly? or i stick to change the value of my parameter from meter to millimeter

Message 11 of 14

jeremy_tammik
Alumni
Alumni

I would suggest researching this from the end user point of view and determining the best practices there before even starting to think about doing anything on this programmatically. Otherwise, you could easily end up shooting yourself in the foot:

  

https://duckduckgo.com/?q=revit+change+project+units

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 12 of 14

Houba3314
Enthusiast
Enthusiast

I tried this but it doesnt seem to work

 

Units units = doc.GetUnits();

// Change the display units of length to feet
units.GetFormatOptions(UnitType.UT_Length).DisplayUnits = DisplayUnitType.DUT_FEET_FRACTIONAL_INCHES;

// Apply the new units to the project
doc.SetUnits(units);

Message 13 of 14

jeremy_tammik
Alumni
Alumni

Maybe it does work. Create a wall in your project that is 1 metre long. Look at its length in the user interface. Change the project units. Look at its length in the user interface again. Hasn't it changed?

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 14 of 14

Houba3314
Enthusiast
Enthusiast

when  i do that manually project units and change it does work indeed but for some reason when i run the code it doesnt i will try again maybe my revit is slow or something