Get selected objects in Tekla Open API (and use LINQ on them)

GetSelectedObjects().Where(...) doesn’t compile. The error doesn’t tell you why: CS0411: The type arguments for method ‘Queryable.Where’ cannot be inferred from the usage. Nothing is wrong with your lambda. The selection doesn’t implement the interface LINQ needs.

ModelObjectEnumerator is an IEnumerator, and LINQ wants an IEnumerable

ModelObjectEnumerator implements System.Collections.IEnumerator and nothing else. LINQ extends IEnumerable<T>. foreach still compiles, because C# only needs a GetEnumerator() method, but the loop variable is a plain ModelObject. So this is the loop everyone writes:

using UISelector = Tekla.Structures.Model.UI.ModelObjectSelector;  // alias: both namespaces have one

var selected = new UISelector().GetSelectedObjects();
while (selected.MoveNext())
{
    var part = selected.Current as Part;    // Current is a ModelObject: cast it yourself
    if (part == null) continue;             // a bolt, a weld, a component
    // ...
}

The alias isn’t decoration. Import both Tekla.Structures.Model and Tekla.Structures.Model.UI and a bare ModelObjectSelector is CS0104, ambiguous. Grasshopper Tekla Drawing Link has 31 of these while loops in 24 files. They work. They’re also 31 copies of the same five lines.

Two NuGet packages wrap it. Read their defaults first.

The LINQ gap is the reason two community packages exist, both MIT. Tekla.Extension from Apibim adds ToIEnumerable<T>(). It calls Reset() first, skips objects that aren’t T and swallows exceptions on the cast, a choice its comment explains with broken models. TeklaOpenApiExtensionMethods from Dawid Dyrcz adds ToList<T>(), and it has two defaults worth knowing.

First, it ships as source files in the package’s content folder. Only old packages.config projects copy those in. In an SDK-style .csproj with PackageReference, the package installs and adds nothing, and using TeklaOpenAPIExtension; fails with CS0246. Second, SelectInstances:

List<Beam> walls = selected.ToList<Beam>();                        // selectInstances: false by default
List<Beam> wallsRead = selected.ToList<Beam>(selectInstances: true);  // reads each beam from the model

With SelectInstances off, Tekla doesn’t call Select() on the objects it hands you. You get the right type and the Identifier, and the properties keep their defaults. Fast for collecting GUIDs. Empty for reading a profile. Neither package is wrong. Add one, and its defaults become yours.

A wrapper of your own, with Reset() at the top

public static class SelectionExtensions
{
    public static IEnumerable<T> ToEnumerable<T>(this ModelObjectEnumerator objects) where T : ModelObject
    {
        objects.Reset();                    // back to the start on every enumeration
        while (objects.MoveNext())
            if (objects.Current is T item)  // other types are skipped, not thrown
                yield return item;
    }
}

var walls = new UISelector().GetSelectedObjects()
    .ToEnumerable<Beam>()
    .Where(b => b.Name == "WALL")
    .ToList();

Without Reset(), Any() eats the first wall panel

The wrapper doesn’t copy the selection. It shares the enumerator’s one cursor. Leave out Reset() and every LINQ call carries on from wherever the last one stopped:

var walls = selected.ToEnumerable<Beam>();     // a wrapper WITHOUT Reset()
if (walls.Any())                               // reads wall 1, and the cursor stays there
    Release(walls.ToList());                   // starts at wall 2: one panel short, no error

Nothing throws. The release list has 11 panels instead of 12, and the missing one is whichever came first in the selection. With Reset() in the wrapper, each enumeration walks the selection again, which is another round of reads from the model. So call ToList() once and work on the list. When you only need a count, GetSize() gives it without moving the cursor.

A selected component isn’t its parts

Say you select a sandwich wall by its custom component symbol. The selection holds one Component, and ToEnumerable<Part>() skips it, exactly as the filter above was written to. You get an empty list and no warning. The parts are the component’s children, and GetChildren() returns the same enumerator type, so the same wrapper works:

var parts = selected.ToEnumerable<Component>()
    .SelectMany(c => c.GetChildren().ToEnumerable<Part>())
    .ToList();

The next time you type .Where() on a selection

  • It’s an IEnumerator. Wrap it once, in one helper, not in every file.
  • Reset() at the top, then ToList() once. Use GetSize() to count.
  • SelectInstances off means identifiers only. Turn it off only when that’s all you read.
  • Components come back as components. Descend with GetChildren().
  • The UI selector needs a model view. In a headless app it returns nothing. Query with model.GetModelObjectSelector() instead.

The first tool reads a selection. Lesson 1 of the course is a Selection Reporter, a read-only macro that lists the parts you select. You read the agent’s loop before you run it, and that’s where a missing cast or a skipped component gets caught. The Tekla Open API skill also ships ModelSweep.cs, a drop-in wrapper with Reset() built in, compile-checked on Tekla 2024, 2025 and 2026.

Tekla Open API Course: build real Tekla tools with an AI agent →

Related: Tekla Open API: print drawings to PDF and export IFC · GetReportProperty in the Tekla Open API: types, units and the silent false · What is Tekla Open API? A guide for detailers and engineers

Tags

Greg Olszewski

Greg Olszewski builds Tekla Open API tools: Grasshopper Tekla Drawing Link, Smart Select, Tekla Lookup. Ten years of C# on Tekla Structures, day job in precast. Now he teaches engineers to build their own.

About Greg ›

COURSE

Build your own Tekla tools

Learn the Tekla Open API in C#, from your first plug-in to tools your team actually uses.

See the course ›