Change Dimension Type
In this post, we discussed creating dimensions with the Revit API.
But that script doesn't give us any control over dimension types, which might be an issue. The first time I ran it, for instance, it returned a dimension with nine decimal places:
Today, we'll talk about creating and customising a new dimension type, so we can have our new dimensions look exactly as we'd like. For the sake of argument, we'll say we want to convert our dimension to millimetres, with one decimal place.
Unfortunately, it's not possible (as far as I know) to create a new dimension type from nothing. We need to duplicate an existing type and edit its format options. (For the sake of simplicity, we'll assume that all our existing dimensions are (and should remain) of the same type.
doc = DocumentManager.Instance.CurrentDBDocument dimensions = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Dimensions).WhereElementIsNotElementType().ToElements() #Get existing dimension types. This will allow us to decide whether to create a new type or overwrite an existing one. existing_dimension_types = Fec(doc).OfClass(DimensionType).ToElements() #Assign existing types to a dictionary to make differentiating them easier. existing_dimension_type_dict = {} for dimension_type in existing_dimension_types: name = dimension_type.get_Parameter(BuiltInParameter.SYMBOL_NAME_PARAM).AsString() existing_dimension_type_dict[name] = dimension_type #Get the current dimension type (We are assuming that all dimensions are of the same type). target_dimension = dimensions[0] existing_dimension_type_id = target_dimension.GetTypeId() existing_dimension_type = doc.GetElement(existing_dimension_type_id) #Either create or get our new dimension type new_dimension_type_name = "New_Type_Name" if new_dimension_type_name not in existing_dimension_type_dict: #It's good practice to ensure that we don't already have a type with our desired name (if we try to give our type a name that already exists, we'll get an error). TransactionManager.Instance.EnsureInTransaction(doc) new_dimension_type = existing_dimension_type.Duplicate("New_Type_Name") #If we don't already have a type with our desired name, we'll create it now. TransactionManager.Instance.TransactionTaskDone() else: new_dimension_type = existing_dimension_type_dict[new_dimension_type_name] #If we already have a type with our desired name, we'll assign it to a variable so we can access it later.
Now we have our target dimension type assigned to the new_dimension_type variable, we can edit its format options. We do this by creating a new instance of FormatOptions() and setting its properties.
There are a few properties to play with, and it's best to check out the FormatOptions Class in the Revit API documentation to see which choices you have. In this example, we'll just specify an accuracy of 0.1mm.
Once we've got our FormatOptions() right, we need to pass it to SetUnitsFormatOptions() to overwrite our type's current format options.
format_options = FormatOptions(UnitTypeId.Millimeters) #UnitTypeId lets Revit know which units we're working in (note the American spelling here!) format_options.UseDefault = False #By default, Revit will try to use the file's default dimension settings. We need to specify that we want to do otherwise. #Specify desired format options. format_options.Accuracy = 0.1 #Overwrite new_dimension_type's format options: TransactionManager.Instance.EnsureInTransaction(doc) set_format = new_dimension_type.SetUnitsFormatOptions(format_options) TransactionManager.Instance.TransactionTaskDone()
Our new type should now be set up. All we need to do now is iterate back through our dimensions change their type from existing_dimension_type to new_dimension_type.
new_dimension_type_id = new_dimension_type.Id for dimension in dimensions: dimension_type_parameter = dimension.get_Parameter(BuiltInParameter.ELEM_FAMILY_AND_TYPE_PARAM) #This parameter controls the type of each dimension. It stores its value as an Element ID. More information can be accessed via Revit Lookup. TransactionManager.Instance.EnsureInTransaction(doc) dimension_type_parameter.Set(new_dimension_type_id) TransactionManager.Instance.TransactionTaskDone()
The dimension should now look how we want.
Comments
Post a Comment