Siemens.Engineering (DLL)

See Requirements on how to import FileInfo

TiaPortal

With user interface

Siemens.Engineering.TiaPortal(Siemens.Engineering.TiaPortalMode.WithUserInterface)

or without a user interface

Siemens.Engineering.TiaPortal(Siemens.Engineering.TiaPortalMode.WithoutUserInterface)

This returns Siemens.Engineering.TiaPortal which can now be used to create a project, list devices, and so much more.

Use getattr(<soemclass here>, <module>) to get some of the class.

For example:

getattr(Siemens.Engineering.ExportOptions, "None")

Because Siemens.Engineering.ExportOptions.None returns an error.

Basic project setup

TIA = Siemens.Engineering.TiaPortal(Siemens.Engineering.TiaPortalMode.WithUserInterface)
project: Siemens.Engineering.Project = TIA.Projects.Create(FileInfo(<ABSOLUTE_PATH>), "CoolTIAProject")

Enumerating Devices

for device in project.Devices:
   print(device.Name)

Enumerating All Program Blocks

for device in project.Devices:
   for device_item in device.DeviceItems:
     container = Siemens.Engineering.IEngineeringServiceProvider(device_item).GetService[Siemens.Engineering.HW.Features.SoftwareContainer]()
        if not container: continue
        if not isinstance(container.Software, Siemens.Engineering.SW.PlcSoftware): continue
        for block in container.Software.BlockGroup.Blocks:
            print(block.Name)

To specify the PLC device, just add a conditional statement that checks for the name of the device is equivalent to what you are looking for.

Import a PLC Block from XML

for device in project.Devices:
   for device_item in device.DeviceItems:
      container = Siemens.Engineering.IEngineeringServiceProvider(device_item).GetService[Siemens.Engineering.HW.Features.SoftwareContainer]()
      if not container: continue
      if not isinstance(container.Software, Siemens.Engineering.SW.PlcSoftware): continue
      container.Software.BlockGroup.Blocks.Import(FileInfo(r"<ABSOLUTE_PATH>"), Siemens.Engineering.ImportOptions.Override)
      continue

Export a PLC Block as XML

for device in project.Devices:
   for device_item in device.DeviceItems:
      container = Siemens.Engineering.IEngineeringServiceProvider(device_item).GetService[Siemens.Engineering.HW.Features.SoftwareContainer]()
         if not container: continue
         if not isinstance(container.Software, Siemens.Engineering.SW.PlcSoftware): continue
         name = 'Main' (1)
         block = container.Software.BlockGroup.Blocks.Find(name)

         singleCompile = block.GetService[Siemens.Engineering.Compiler.ICompilable]();
         result = singleCompile.Compile()
         block.Export(FileInfo(f"{Path().absolute().as_posix()}/{name}.xml"), getattr(Siemens.Engineering.ExportOptions, "None")) (2)

         continue
1 Name of the PLC block to export
2 This gets exported to the current path of the script

Project

ProjectBase

Derived Types:

Devices

Returns a list of Siemens.Engineering.HW.Device.

HW.Device

Check its composition.

HW.DeviceComposition

CreateWithItem

typeIdentifier

Type identifier of the device to be created with sub items

name

Name of the device to be created with sub items

deviceName

The name of the device to create with subcomponents <.>

1 deviceName must be unique

SW.Blocks.PlcBlock

Export

Inconsistent blocks and PLC data types (UDT) cannot be exported.
Inconsistent blocks and PLC data types (UDT) cannot be exported.
   at Siemens.Engineering.Private.Session.Siemens.Engineering.Private.IInstanceSession.InvokeAction[TC](LifetimeContractHandle`1 lifetimeContractHandle, String name, IEnumerable`1 parameters, String fullName)
   at Siemens.Engineering.Private.InternalInstanceAccess`2.InvokeAction(String name, IEnumerable`1 parameters)
   at Siemens.Engineering.SW.Blocks.PlcBlock.Export(FileInfo path, ExportOptions exportOptions)

The fix to this is to first compile the PlcBlock as described here.

Details

a possible cause for the error is the LAD not being considered consistent. The function needs to consider the PlcBlock as consistent in order to export to xml, which can be checked by checking the PlcBlock’s IsConsistent bool.

To solve this issue, the LAD first needs to be compiled, after which the Export function should be able to be called without issue.

— JHD2
Industry Support Siemens