Automatically reassigning annotation style and dim scale

Hey all,

New to scripting so fingers crossed this is a simple fix. The gist of what I’m trying to do is automate a process for taking old architectural details imported from CAD and reassign their annotation style to a uniform one with a variable model space scale - that variable model space scale being determined by what the original annotation style is and assigned via a csv file. Generally, I’m able to get everything to work up until the last step - that being assigning the custom scale. Is this possible / am I missing something obvious? Thanks!

import rhinoscriptsyntax as rs
import csv
import Rhino
import System

def reassign_annotation_style():
    # Select the object
    obj = rs.GetObject("Select an annotation object", rs.filter.annotation)
    if not obj:
        return

    # Get the current annotation style
    current_style = rs.DimensionStyle(obj)

    # Prompt user to select the CSV file
    filter = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*||"
    csv_file = rs.OpenFileName("Select the annotation scales CSV file", filter)
    if not csv_file:
        print("No CSV file selected. Operation cancelled.")
        return

    # Read the CSV file for scale information
    scale_dict = {}
    try:
        with open(csv_file, 'r') as csvfile:
            reader = csv.DictReader(csvfile)
            for row in reader:
                scale_dict[row['OriginalStyle']] = float(row['ModelSpaceScale'])
    except Exception as e:
        print("Error reading CSV file: {}".format(str(e)))
        return

    # Set the new annotation style
    new_style = "Paperspace"
    rs.DimensionStyle(obj, new_style)

    # Apply the model space scale
    if current_style in scale_dict:
        scale = scale_dict[current_style]
        
        # Get the dimension style
        dim_style = rs.DimensionStyle(new_style)
        
        if dim_style:
            # Create a new dimension style with the updated scale
            new_style_name = "{}_{}_Scale".format(new_style, scale)
            new_dim_style = rs.CopyDimensionStyle(new_style, new_style_name)
            
            # Set the new scale
            rs.DimensionScale(new_dim_style, scale)
            
            # Apply the new style to the object
            rs.DimensionStyle(obj, new_style_name)
            
            print("Annotation style changed from '{}' to '{}' and scaled by {}".format(current_style, new_style_name, scale))
        else:
            print("Unable to find the '{}' dimension style.".format(new_style))
    else:
        print("Annotation style changed from '{}' to '{}', but no scale found in CSV".format(current_style, new_style))

    # Update the document
    Rhino.RhinoDoc.ActiveDoc.Views.Redraw()

# Run the function
reassign_annotation_style()

annotation style.csv (141 Bytes)
24-01 Typ Sheet Template_5.0.3dm (4.5 MB)

Hi @Caleb_Schemmel,

Here is some sample code that just uses RhinoCommon, not RhinoScriptSyntax. Perhaps this helps?

import Rhino
import scriptcontext as sc

def Test(ds_name):
    # Find some existing dimension style
    ds = sc.doc.DimStyles.FindName(ds_name)
    if not ds:
        print("{} dimension style does not exist.".format(ds_name))
        return
        
    # Print the model space scale
    print("{}.DimensionScale = {}".format(ds.Name, ds.DimensionScale))
    
    ds_new_scale = 4.0 # some scale
    ds_new_name = "{}_{}_Scale".format(ds_name, ds_new_scale)
    ds_new = sc.doc.DimStyles.FindName(ds_new_name)
    if ds_new:
        print("{} dimension style already exists.".format(ds_new_name))
        return
    
    # Copy the dimension style
    ds_new = ds.Duplicate()
    # Set the name
    ds_new.Name = ds_new_name
    # Set the model space scale
    ds_new.DimensionScale = ds_new_scale 
    # Add the copy to the document
    ds_index = sc.doc.DimStyles.Add(ds_new, False)
    
    # Find the newly added copy by index
    ds = sc.doc.DimStyles.FindIndex(ds_index)
    if ds:
        # Print the model space scale
        print("{}.DimensionScale = {}".format(ds.Name, ds.DimensionScale))

if __name__ == "__main__":
    ds_name = "Paperspace"
    Test(ds_name)

– Dale

Hey Dale,

Thank you for this. That script works but the end goal I’m going for is something that allows us to merge all of our annotation styles into a common one with the parameter being a varied model space scale. IE if I have an object with annotation style “Three - Arch” and another with style “Four - Arch”, I’d like them to both be assigned to “Paperspace” and then have a custom model space scale applied - So the object with “Three - Arch” becomes “Paperspace” with a model space scale of X as defined by the csv. I think it’s the last step of the script I posted where this starts breaking down.