Skip to content

Instantly share code, notes, and snippets.

@benkoshy
Last active July 8, 2024 05:20
Show Gist options
  • Save benkoshy/3d36eba4ec26457bf998e67a32088e28 to your computer and use it in GitHub Desktop.
Save benkoshy/3d36eba4ec26457bf998e67a32088e28 to your computer and use it in GitHub Desktop.
How to programmatically filter parts by part position using the Tekla "Open" API

Here is the problem: we want to filter all parts which have the part position (or "part mark") of "p/2".

How can we do this programmatically?

Check out this code snippet - it assumes you have set up your references and are ready to go!

this.model = new Model();
if (model.GetConnectionStatus())
{

        # first filter on parts
      BinaryFilterExpressionCollection filterCollection = new BinaryFilterExpressionCollection();
      var filterParts = new BinaryFilterExpression(new ObjectFilterExpressions.Type(), NumericOperatorType.IS_EQUAL, new NumericConstantFilterExpression(TeklaStructuresDatabaseTypeEnum.PART));
      filterCollection.Add(new BinaryFilterExpressionItem(filterParts, BinaryFilterOperatorType.BOOLEAN_AND));

        # then filter on part position

      BinaryFilterExpression filterPositionNumber = new BinaryFilterExpression(new PartFilterExpressions.PositionNumber(), StringOperatorType.IS_EQUAL, new StringConstantFilterExpression("p/2"));
      filterCollection.Add(new BinaryFilterExpressionItem(filterPositionNumber, BinaryFilterOperatorType.BOOLEAN_AND));             

      ModelObjectEnumerator filteredObjects = model.GetModelObjectSelector().GetObjectsByFilter(filterCollection);
      List<ModelObject> parts = new List<ModelObject>();
      foreach (ModelObject obj in filteredObjects) 
      {
              Part filteredPart = (Part)obj;
              string partMark = filteredPart.GetPartMark();    
              Debug.WriteLine(partMark);
      }
}

Unfortunately the documentation is poor, and the API is even worse!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment