Tekla Open API and Python.NET – Challenges and Workarounds

Tekla Open API and Python.NET – Challenges and Workarounds

As I pointed out in my previous article, using the Python.NET library to interact with the Tekla Open API brings many advantages coming from the benefits of the Python language itself. However, I also mentioned that using .NET types in Python code can cause some inconvenience.

That's true indeed, so this article is going to describe some of these challenges and present working solutions for dealing with them. All the examples described below are based on real experiences from working on actual Python applications for Tekla.

1) Accessing ArrayList

ArrayList is quite commonly used in Tekla Open API, for example, in selector methods that accept input in the ArrayList data type. Its use is relatively straightforward.

Declare it first:

from System.Collections import ArrayList        

Then populate the list:

...
point_list = ArrayList()
for point in points:
    point_list.Add(point)        

2) Accessing PointList

PointList is used in the drawings API and is quite similar to ArrayList, so its usage looks almost the same.

Declare it:

from Tekla.Structures.Drawing import PointList        

And populate it:

...
point_list = PointList()
for point in points:
    point_list.Add(point)        

It is important to note that while you can access the inner elements (e.g., by iterating with a Python for loop), direct modification of these elements is not possible. That's why I suggest storing your data in Python-native list structures and constructing ArrayList and PointList objects only before passing them as arguments to Tekla Open API methods.

3) Cloning .NET Objects

Sometimes you might need to clone a C# object. In Python, you would use copy, but for a .NET object, you have to use the native MemberwiseClone method.

Declare System:

import System        

And use:

new_part = original_part.MemberwiseClone()        

MemberwiseClone, like Python's copy, only performs a shallow copy. If you need to perform a deep copy, as far as I know, there is no analog in the .NET standard library, and a custom implementation will be required. This, however, is outside the scope of this article.

4) Passing by Reference: The Example of GetReportProperty

Some Tekla Open API methods, such as GetReportProperty, treat certain arguments as if they were passed by mutable reference and modify them in place:

public bool GetReportProperty(
    string name,
    ref double value
)        

However, in Python, primitive types (like int, float, and string) are considered immutable, so assignment does not affect the original object. Fortunately, there is a workaround. In the case of GetReportProperty, a tuple is returned, where the first element is a bool (indicating the result of the method) and the second is your requested property.

Instead of passing a value variable, an empty variable is used as an argument:

...
is_ok_x, cog_x = assembly.GetReportProperty("COG_X", float())
if is_ok_x:
    ...        

5) Using Type Filters: The Example of GetAllObjects

Some methods, like GetAllObjects, accept arguments in the form of System.Type objects.

Here's how a type filter for dimension sets can be constructed in Python:

Declare Type and Array:

from System import Type, Array        

Create a filter:

type_filter_dimension_sets = Array[Type]([DimensionSetBase,])
dimension_sets = view.GetAllObjects(type_filter_dimension_sets)        

Conclusion

The article has provided some practical examples of challenges and workarounds when integrating Python with the Tekla Open API using Python.NET.

It is by no means a comprehensive or exhaustive guide. If readers find something that could be solved in a better way, or would like to see additional topics covered, please feel free to comment on this article. Any feedback and remarks are greatly appreciated.



To view or add a comment, sign in

More articles by Vitali Mezen

Explore content categories