Creating a custom command with python SelDimLess

Hi,

I am new to Python. I am trying to see if I can come up with a script for a command that selects any Dimensions that are less than a certain value (layout unit). I am not sure what went wrong here posting here to see if there is anyone who can help.

Thanks in advance.

import rhinoscriptsyntax as rs

def select_dimensions_less_than(length_threshold):
    # Get all dimension objects in the document
    dimensions = rs.ObjectsByType(rs.filter.Annotation)
    
    if not dimensions:
        print("No dimensions found in the document.")
        return
    
    # Select dimensions with length less than the threshold
    selected_dimensions = []
    for dim_id in dimensions:
        length = rs.DimensionValue(dim_id)
        if length < length_threshold:
            selected_dimensions.append(dim_id)
    
    # Select the dimensions in Rhino
    if selected_dimensions:
        rs.SelectObjects(selected_dimensions)
        print("Selected dimensions with length less than", length_threshold)
    else:
        print("No dimensions found with length less than", length_threshold)

def Command_SelectDimensionsLessThan():
    # Get user input for the threshold value
    length_threshold = rs.GetReal("Enter the threshold length:", 10)
    if length_threshold is None:
        print("Invalid threshold value.")
        return
    
    # Call the function to select dimensions
    select_dimensions_less_than(length_threshold)

# Register the command
if __name__ == "__main__":
    Command_SelectDimensionsLessThan()

Thank you

assuming you’re using python3

change this

def select_dimensions_less_than(length_threshold):
    # Get all dimension objects in the document
    dimensions = rs.ObjectsByType(rs.filter.annotation) #lowercase annotation

Hi Hogan - the main problem with the script itself is a Rhino bug - ObjectsByType() is broken at the moment - getting fixed but broken, so for now replace

rs.ObjectsByType(filter)

with something like this:

def find_dims():
    dim_ids = []
    ids = rs.NormalObjects()
    if len(ids) == 0:
        return dim_ids
    
    return [Id for Id in ids if rs.IsDimension(Id)]

-Pascal

Thanks @pascal & @adel.albloushi . Due to my terrible coding skills, I still couldn’t get the code to work. Do you know roughly when rs.ObjectByTypes() will be fixed? I will wait it out.

Thank you!

import rhinoscriptsyntax as rs

def find_dims():
    dim_ids = []
    ids = rs.NormalObjects()
    if len(ids) == 0:
        return dim_ids
    
    return [Id for Id in ids if rs.IsDimension(Id)]



def select_dimensions_less_than(length_threshold):
    # Get all dimension objects in the document
    dimensions = find_dims()
etc etc

Does that make sense?

-Pascal

The script bellow works for me in both Rhino 8.5 and 7.36

import rhinoscriptsyntax as rs

def select_dimensions_less_than(length_threshold):
    # Get all dimension objects in the document
    dimensions = rs.ObjectsByType(rs.filter.annotation)
    
    if not dimensions:
        print("No dimensions found in the document.")
        return
    
    # Select dimensions with length less than the threshold
    selected_dimensions = []
    for dim_id in dimensions:
        length = rs.DimensionValue(dim_id)
        if length < length_threshold:
            selected_dimensions.append(dim_id)
    
    # Select the dimensions in Rhino
    if selected_dimensions:
        rs.SelectObjects(selected_dimensions)
        print("Selected dimensions with length less than", length_threshold)
    else:
        print("No dimensions found with length less than", length_threshold)

def Command_SelectDimensionsLessThan():
    # Get user input for the threshold value
    length_threshold = rs.GetReal("Enter the threshold length:", 10)
    if length_threshold is None:
        print("Invalid threshold value.")
        return
    
    # Call the function to select dimensions
    select_dimensions_less_than(length_threshold)

# Register the command
if __name__ == "__main__":
    Command_SelectDimensionsLessThan()

Hi @adel.albloushi

I just updated to 8.5 and still no luck. Do you mind helping me find out what’s going on?

I got this error msg:

Message: ‘TextEntity’ object has no attribute ‘NumericValue’

Traceback:
line 329, in DimensionValue, “C:\Users\XXXX\AppData\Roaming\McNeel\Rhinoceros\8.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript\dimension.py”
line 14, in select_dimensions_less_than, “S:\where i stored the script\SelDimLess.py”
line 33, in Command_SelectDimensionsLessThan, “S:\where i stored the script\SelDimLess.py”
line 37, in , “S:\where i stored the script\SelDimLess.py”

TIA

not sure.

in rhino, go to tools → script → edit
image

in the editor window, click on the new file icon (white) → select python 3
image

paste the script there and press play

Thanks @adel.albloushi I did exactly it and these error show up:

ValueError: object_id does not refer to an Annotation

image

interesting

add this check

    selected_dimensions = []
    for dim_id in dimensions:

        ##add this check
        if rs.ObjectType(dim_id) != 512:
            continue
        ## add this

        length = rs.DimensionValue(dim_id)

@adel.albloushi

image

can you send me the file for further testing?

i think you can do this

  for dim_id in dimensions:

        ##add this check
        if rs.ObjectType(dim_id) != 512:
            continue
        ## add this

        ## also add this check
        try:
            #checks if the length can be extracted
            length = rs.DimensionValue(dim_id)
        except:
            #on fail skips this dim_id
            continue
       
        #rest of code->>

Hi there, below seems to work for me in Python 2 and 3:

import Rhino
import scriptcontext
import rhinoscriptsyntax as rs

def SelDimLess():
    
    length_threshold = rs.GetReal("Enter the threshold length:", 10)
    if not length_threshold: return
    
    my_filter = Rhino.DocObjects.ObjectType.Annotation
    for rh_obj in scriptcontext.doc.Objects.FindByObjectType(my_filter):
        if isinstance(rh_obj.Geometry, Rhino.Geometry.LinearDimension):
            length = rh_obj.Geometry.NumericValue
            if length < length_threshold:
                scriptcontext.doc.Objects.Select(rh_obj.Id, True, True)
    
    scriptcontext.doc.Views.Redraw()
    
SelDimLess()

_
c.

@clement

IT WORKS!!!

Thank you soooo much!