Python: Select text with same height

(Tim) #1

Hello everyone, I’m fairly new to scripting in python, but so far its cool.
I was working with an urban plan and needed to select some text of specific height. I looked around the forum and managed to find some related info which helped me a lot, but I still have a couple of questions:

def SelectTextWithSameHeight():
    # Let user pick a text object with required height
    txtObj = rs.GetObject("Select text", filter=512)
    if rs.IsText(txtObj):
        # Return current text height 
        textHeight = rs.TextObjectHeight(txtObj)


    # Select all annotation objects and store them in annotations
    annotations = rs.ObjectsByType(rs.filter.annotation)
    # Go through each annotation
    for annotation in annotations:
        # Check if it's a numeric text
        if rs.IsText(annotation) and rs.TextObjectText(annotation).isdigit():
            # And if yes, get its height
            textHeights = rs.TextObjectHeight(annotation)
            # If its height equals to preselected height,
            if textHeights == textHeight:
                # select it


if __name__=="__main__":
  1. So, there is this filter 512 for annotations, I was wondering if annotations here is just text objects or does it include other stuff like dimensions?
    txtObj = rs.GetObject("Select text", filter=512)

  2. Is there a better way to write this function or do you have any suggestions?

(Willem Derks) #2

Hi Tim,

No, there is no other filter than the 512. This indeed catches all types of annotations.
However, What I’d do in such a case, is to filter all possible texts with height first and use GetObjectEx to limit possible objects for selection:

import rhinoscriptsyntax as rs

all_annotations = rs.ObjectsByType(512)
all_texts = [obj for obj in all_annotations if rs.IsText(obj)]
height = 10
all_text_height = [obj for obj in all_texts if rs.TextObjectHeight(obj) == height]

# all_text_height contains all objects in the document with height == height

#To manually select any of the text in all_text_height
# GetObjectEx(message=None, filter=0, preselect=False, select=False, objects=None)
txtObj = rs.GetObjectEx('select text',filter = 0, preselect=False, select=False, objects=all_text_height)


(Tim) #3

Oh, that’s a good one, thanks Willem :blush: