Change Project Units

Change Project Units

ChristianMolino
Enthusiast Enthusiast
811 Views
3 Replies
Message 1 of 4

Change Project Units

ChristianMolino
Enthusiast
Enthusiast

I am working on an add-in that will export a SAT file, but using the standard project unit of foot and fractional inches is causing the sat files to export at 1/12 scale. I found that changing the global units to fractional inches exports the SAT file at the correct scale.

 

To accomplish this via the API, I am trying to save the original units, change them to fractional inches, do the exports and then change the units back to the original. I am getting really hung up with the ForgeTypeId's and cannot get the code to run. What am I doing wrong? I am extremely new to coding/the Revit API so explain it to me like I am 5 please lol!

 

 

public void ExportSATFiles(Document doc, View3D view, ElementId dualDeckId)
{
// Retrieve the project's current unit settings for length
FormatOptions originalFormatOptions = doc.GetUnits().GetFormatOptions(SpecTypeId.Length);
Units originalUnits = doc.GetUnits();

try
{
// Change the project units to fractional inches
Units tempUnits = new Units(UnitSystem.Imperial);
FormatOptions inchFormatOptions = new FormatOptions(new ForgeTypeId("autodesk.spec.aec:length.unit#fractionalInches"));
tempUnits.SetFormatOptions(SpecTypeId.Length, inchFormatOptions);

using (Transaction tx = new Transaction(doc, "Set Project Units to Fractional Inches"))
{
tx.Start();
doc.SetUnits(tempUnits);
tx.Commit();
}

// Now perform the SAT export with units set to fractional inches
PerformSATExport(doc, view, dualDeckId);

// Restore the original units
using (Transaction tx = new Transaction(doc, "Restore Original Project Units"))
{
tx.Start();
doc.SetUnits(originalUnits);
tx.Commit();
}
}
catch (Exception ex)
{
MessageBox.Show("Error during SAT export: " + ex.Message);

// Ensure the units are set back to original even if an error occurs
using (Transaction tx = new Transaction(doc, "Restore Original Project Units"))
{
tx.Start();
doc.SetUnits(originalUnits);
tx.Commit();
}
}
}
0 Likes
Accepted solutions (1)
812 Views
3 Replies
Replies (3)
Message 2 of 4

Speed_CAD
Collaborator
Collaborator
Hi Christian,

What is the error message? So that someone can help you better, it is important that you provide more information.
Mauricio Jorquera
0 Likes
Message 3 of 4

Mohamed_Arshad
Advisor
Advisor
Accepted solution

Hi @ChristianMolino 

 

     You have given the wrong ForgeTypeId in FormatOptions , I have modified your code a little bit, Kindly refer to the below code for additional reference.

 

Reference Code:

 

	public void ExportSATFiles(Document doc, View3D view, ElementId dualDeckId)
	{
		// Retrieve the project's current unit settings for length
		FormatOptions originalFormatOptions = doc.GetUnits().GetFormatOptions(SpecTypeId.Length);
		Units originalUnits = doc.GetUnits();

		try
		{
			// Change the project units to fractional inches
			Units tempUnits = new Units(UnitSystem.Imperial);
			FormatOptions inchFormatOptions = new FormatOptions(UnitTypeId.FractionalInches); ///Code Modified
			tempUnits.SetFormatOptions(SpecTypeId.Length, inchFormatOptions);

			using (Transaction tx = new Transaction(doc, "Set Project Units to Fractional Inches"))
			{
				tx.Start();
				doc.SetUnits(tempUnits);
				tx.Commit();
			}

			// Now perform the SAT export with units set to fractional inches
			PerformSATExport(doc, view, dualDeckId);

			//Restore the original units
			using (Transaction tx = new Transaction(doc, "Restore Original Project Units"))
			{
				tx.Start();
				doc.SetUnits(originalUnits);
				tx.Commit();
			}
		}
		catch (Exception ex)
		{
			MessageBox.Show("Error during SAT export: " + ex.Message);

			// Ensure the units are set back to original even if an error occurs
			using (Transaction tx = new Transaction(doc, "Restore Original Project Units"))
			{
				tx.Start();
				doc.SetUnits(originalUnits);
				tx.Commit();
			}
		}
	}

 

 

You don't want to pass string in the constructor, can directly use its UnitTypeId Class static properties like below code

 

UnitTypeId.FractionalInches

 

 

I have attached few links based on the ForeTypeId, Kindly take a look of those.

 

https://thebuildingcoder.typepad.com/files/revit_platform_api_changes_and_additions_2022.pdf 

https://thebuildingcoder.typepad.com/blog/2021/06/forgetypeid-and-units-revisited.html#5 

https://forums.autodesk.com/t5/revit-api-forum/forgetypeid-how-to-use/td-p/9439210 

 

Hope this will Helps 🙂

 


Mohamed Arshad K
Software Developer (CAD & BIM)

0 Likes
Message 4 of 4

ChristianMolino
Enthusiast
Enthusiast

Awesome, this worked. Thank you!

0 Likes