Create Tags using GhPython

Hi,

Yes, you’re right! They clear each other because the clear_textdots() function looks for all the textdots in the active Rhino document and deletes each and everyone.

Sure, the new version works with multiple Python components on the canvas. Tagging and untagging works individually now, without one component interfering with another.

You can even input a tree of points (locations) and a tree of text (descriptions), instead of lists. Tagging works fine here, but deleting the tags afterwards doesn’t! I couldn’t figure out how to get it fully working with trees!

If you input your data separately as lists, everything works fine.

Here’s script version 2:

import scriptcontext as sc
from uuid import uuid4
import Rhino


ACTIVE_DOC = Rhino.RhinoDoc.ActiveDoc


def get_unique_id(_depth=0):
    """Returns a unique id that is not already a key in sticky."""
    if _depth > 50:
        raise ValueError(
            "Recursion limit exceeded. Could not get a unique id."
            )
    id = str(uuid4())
    if id not in sc.sticky.keys():
        return id
    _depth += 1
    return get_unique_id(_depth)


def create_textdot(text_str, point, height=-1, font=-1):
    """Creates a TextDot at a given location.
    
    Args:
        text_string (str): A text to display.
        point (Rhino.Geometry.Point3d): A location.
        height (int): A font height.
        font (str): A font face.
    
    Returns:
        The Rhino.Geometry.TextDot().
    """
    textdot = Rhino.Geometry.TextDot(text_str, point)
    if height > 0:
        textdot.FontHeight = height
    if font > 0: 
        textdot.FontFace = font
    return textdot


def clear_textdots(object_ids):
    """Deletes a collection of TextDots from the active Rhino document."""
    count = 0
    for id in object_ids:
        if ACTIVE_DOC.Objects.Delete(id, True):
            count += 1
    return count
    

def clear_all_textdots():
    """Deletes all TextDots from the active Rhino document."""
    textdots = ACTIVE_DOC.Objects.FindByObjectType(
                    Rhino.DocObjects.ObjectType.TextDot
                )
    if len(textdots) > 0:
        for tdot in textdots:
           ACTIVE_DOC.Objects.Delete(tdot, True)


if __name__ == "__main__":
    #sc.sticky = dict()
    #clear_all_textdots()
    
    if "component_id" not in globals():
        component_id = get_unique_id()

    try:
        clear_textdots(sc.sticky[component_id])
    except:
        pass

    if Tag:
        sc.sticky[component_id] = []
        print sc.sticky
        print "Creating TextDots:"
        for i in xrange(len(Locations)):
            attr = Rhino.DocObjects.ObjectAttributes()
            attr.ColorSource = Rhino.DocObjects.ObjectColorSource.\
                                    ColorFromObject
            attr.ObjectColor = Color;
            tdot = create_textdot(Descriptions[i], Locations[i], Size, Font)
            id = ACTIVE_DOC.Objects.AddTextDot(tdot, attr)
            sc.sticky[component_id].append(id)
        print " - {} TextDots placed".format(len(sc.sticky[component_id]))
        
    else:
        if component_id in sc.sticky.keys():
            print "Deleting TextDots:"
            num = clear_textdots(sc.sticky[component_id])
            print " - {} TextDots purged".format(len(sc.sticky[component_id]))
            sc.sticky.pop(component_id)

textdots_python_V2.gh (21.2 KB)

3 Likes