Home/ Reference/ Find & Traverse

Deduplicate shapes by StaticID

The same object can arrive down more than one path (a group query and a container query overlap), so collect StaticIDs in a set. No traversal is finished without it.

Name

Deduplicate shapes by StaticID

Notes

The same object can arrive down more than one path (a group query and a container query overlap), so collect StaticIDs in a set. No traversal is finished without it.

Python example (runnable)

Requires pywin32. The example assumes app / doc / page / layer are already connected. See Connection & Init
Python
seen = set()          # remember what we already handled
result = []           # final result

page = doc.ActivePage
stack = [page.Shapes]                 # an explicit stack instead of recursion, so deep nesting cannot blow up

while stack:
    coll = stack.pop()                # coll : the shape collection at this level
    try:
        n = coll.Count
    except Exception:
        continue

    for i in range(1, n + 1):
        s = coll.Item(i)
        try:
            sid = s.StaticID          # the same object can arrive by more than one path
        except Exception:
            continue

        if sid in seen:               # skip what we have already seen
            continue
        seen.add(sid)
        result.append(s)

        try:
            if s.Type == 7:           # group: keep walking
                stack.append(s.Shapes)
        except Exception:
            pass

print("After dedupe:", len(result), " object(s)")

About this section: Find & Traverse

Find & Traverse — walking the shape tree.

Other entries in this section