Setting the Path to the Tekla Structures bin Folder#
When using PyTekla for the first time, it is necessary to set the directory path to the Tekla Structures bin folder. To do that, you need to import a function called set_tekla_path.
There are two ways to use the Tekla Open API library with PyTekla. The first option is to use the library directly, while the second option is to use the wrappers provided by PyTekla.
importclrimportpyteklafromSystem.CollectionsimportHashtable,ArrayListfromTekla.Structures.ModelimportModel,Part,Weldmodel=Model()# Check model connection statusifnotmodel.GetConnectionStatus():print("Cannot connect with Tekla model")sys.exit()# Get all Assembly and Part objectsmodel_object_selector=model.GetModelObjectSelector()# We need to get the .NET types to use in this methodtypes=[clr.GetClrType(t)fortin(Assembly,Part)]objects_enumerator=model_object_selector.GetAllObjectsWithType(types)# objects is a ModelObjectEnumerator [https://developer.tekla.com/tekla-structures/api/22/14457]# We may want to have a Python list with all these objects for other purposesobjects_list=[oforoinobjects_enumerator]# Grab first object to get some information# obj is instance of a ModelObject subclass [https://developer.tekla.com/tekla-structures/api/22/14416]obj=objects_list[0]# Get all user propertiesuser_hash_table=Hashtable()obj.GetAllUserProperties(hash_table)# We have a .NET Hashtable [https://learn.microsoft.com/en-us/dotnet/api/system.collections.hashtable?view=net-7.0]# For future use, we may want to convert it to a Python dictionaryuser_properties_dict={k:vfork,vinzip(user_hash_table.Keys,user_hash_table.Values)}# Get some report propertiesreport_hash_table=Hashtable()str_names=ArrayList()str_names.Add("MATERIAL")str_names.Add("FINISH")double_names=ArrayList()double_names.Add("WEIGHT")int_names=ArrayList()int_names.Add("GROUP_ID")# The name of the method is confusing, it only gets the specified propertiesobj.GetAllReportProperties(str_names,double_names,int_names,report_hash_table)# Convert to Python Dict for future usereport_properties_dict={k:vfork,vinzip(report_hash_table.Keys,report_hash_table.Values)}
frompyteklaimportwrappermodel=wrapper("Model.Model")# Check model connection statusifnotmodel.get_connection_status():print("Cannot connect with Tekla model")sys.exit()# Get all Assembly and Part objects# PyTekla will find the type names in the Tekla.Structures.Model namespace [https://developer.tekla.com/tekla-structures/api/22/13460]objects_generator=model.get_objects_with_types(("Assembly","Part"))# objects is a native Python generator objects_list=list(objects_generator)# Grab first object to get some information# obj is instance of a PyTekla ModelObjectWrapper obj=objects_list[0]# Get all user propertiesuser_properties_dict=obj.get_all_user_properties()# Get some report propertiesreport_properties_dict=obj.get_multiple_report_properties(string_names=["MATERIAL","FINISH"],float_names=["WEIGHT"],int_names=["GROUP_ID"])
Note
As per the Tekla Open API documentation, if the connection to Tekla Structures is lost it cannot be re-established. To get the model connected you can try to run Tekla Structures as Admisnitrator.
A wrapper is an object that encapsulates and contains another object. It allows the wrapped object to be accessed and used through the wrapper. The wrapper can provide additional functionality and services.
Those wrappers provide a more 'pythonic' experience when working with the Tekla Open API. You can check their documentation in the API Reference section.
You can use directly those wrappers or use the handy wrap function. This function will wrap any Tekla Structures object. Let see an example:
frompyteklaimportwrapfromTekla.Structures.ModelimportMaterial,Beam# This will return a 'BaseWrapper' object.material=wrap(Material())# This will return a 'ModelObjectWrapper' object.beam=wrap(Beam())
Alternatively, you can use strings to instanciate the objects:
fromteklaimportwrap# This will find, instanciate and wrap the type Tekla.Structures.Model.Materialmaterial=wrap("Model.Material")# This will find, instanciate and wrap the type Tekla.Structures.Model.Beambeam=wrap("Model.Beam")