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: 

Change DisplayUnitSystem or Unit Formats

13 REPLIES 13
SOLVED
Reply
Message 1 of 14
ratno123s
3282 Views, 13 Replies

Change DisplayUnitSystem or Unit Formats

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...??

13 REPLIES 13
Message 2 of 14
jeremytammik
in reply to: ratno123s

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

Message 3 of 14
ratno123s
in reply to: jeremytammik

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

Message 4 of 14
ratno123s
in reply to: ratno123s

Currently i am using revit 2017 to test

Message 5 of 14
jeremytammik
in reply to: ratno123s

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

Message 6 of 14
ratno123s
in reply to: jeremytammik

Got it...

Document.SetUnit() !!!

Thanks Jeremy... You are a great teacher

Message 7 of 14
jeremytammik
in reply to: ratno123s

🙂

 

Thank you very much for your appreciation and confirmation!

 

Cheers,

 

Jeremy

 



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

Message 8 of 14
ratno123s
in reply to: ratno123s

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

 

Is that correct?

Message 9 of 14
ratno123s
in reply to: ratno123s

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

Message 10 of 14
Houba3314
in reply to: ratno123s

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
in reply to: Houba3314

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
in reply to: jeremy_tammik

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
in reply to: Houba3314

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
in reply to: jeremy_tammik

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 

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

Post to forums  

Forma Design Contest


Rail Community