Python examples

Creating an instance of a .NET class

This example shows how to add a .NET-Assembly as a reference to the CLR (Common Language Runtime) and how to create an instance of a class.

Creating an instance of the class Loehnert.TypeAndResult.Station
import clr (1)
clr.AddReference("Loehnert.TypeAndResult") (2)
from Loehnert.TypeAndResult import Station (3)

station = Station("my_station") (4)
1 Import of the CLR (Common Language Runtime) module
2 To use a .NET assembly, it must be added to the CLR as a reference
3 Import the Station type from the Loehnert.TypeAndResult namespace
4 Calling the constructor
Find many useful tips on .NET integration in the IronPython .NET integration documentation

Creating a measure cycle and evaluating an attribute

Create a measure cycle and a measure process and evaluate an attribute. The station has a TypeData module and a WorkPiece module. The type data from the type data example are loaded.

WorkPiece.SetWorkPiece("1234") (1)
measure_cycle = WorkPiece.GetNewMeasureCycle() (2)
measure_process = measure_cycle.AddNewMeasureProcess("HighVoltageTest") (3)

current = 0.004 # 4mA (4)
measure_process.EvaluateAttribute("Current", current) (5)

measure_process.Evaluate() (6)
measure_cycle.Evaluate() (7)
WorkPiece.RemoveWorkPiece() (8)
1 Load the workpiece (test item) via an ID. The unique ID "1234" could be a trace ID or a serial number
2 Create a measuring cycle for a workpiece
3 Create a measuring process for the "HighVoltageTest" process
4 Generate measured value, this would be generated by a measuring device
5 Evaluate "Current" attribute
6 Evaluate measure process
7 Evaluate measuring cycle
8 Remove workpiece from the workpiece module

Figure 1 shows the result view in the service window of the WorkPiece module. The overall classification of the measure cycle is rated as "Fail" in the selected example because not all attributes of the HighVoltage process were measured.

Example result view
Figure 1. View of the results, service window of the WorkPiece module

Display a dialogue

This example shows you how to display a dialogue with an input field.

import clr
clr.AddReference("Loehnert.Controls") (1)
from Loehnert.Controls.Dialogs import DialogService

dialog = DialogService.GetDialogBuilder()\ (2)
.WithTitle("Enter Serial Number")\ (3)
.WithInput(str, "Serial Number")\ (4)
.WithOkButton().IsDefaultButton()\  (5) (6)
.WithCancelButton()\ (7)
.ThrowExceptionOnCancel()\ (8)
.Build()

result = dialog.Show() (9)

serial_number = result.Values[0] (10)
1 Add the .NET assembly "Loehnert.Controls.dll" to the CLR as a reference
2 Create a "DialogueBuilder"
3 Add a title
4 Add an input field for a character string
5 Add OK button
6 Set OK button as default button which reacts to the Enter key
7 Add cancel button
8 An exception should be thrown on cancellation (see OperationCanceledException), this leads to the script being cancelled
9 Display dialogue
10 Get the serial number from the result of the dialogue
Example Input dialogue
Figure 2. View of the input dialogue
Find further examples of dialogues in the DialogBuilder documentation.

Logging

This example creates a logger and generates a log entry.

import clr

clr.AddReference("Loehnert.Lisrt")

from Loehnert.Lisrt.Messaging import MessageService
from Loehnert.Lisrt.Contracts import LogLevel

logger = MessageService.Instance.LoggerFactory.GetLogger("MyLogger") (1)
logger.Log(LogLevel.Info, "This is a log entry with the log level Info") (2)
1 Create a logger with the name "MyLogger"
2 Create a log entry with the log level "Info"

Type data inheritance

The following example shows how type data can be combined using Inheritance.

import clr

clr.AddReference("Caliburn.Micro")
clr.AddReference("Loehnert.TypeAndResult")
clr.AddReference("Loehnert.Lisrt.TypeAndResult")

from Caliburn.Micro import IoC
from Loehnert.TypeAndResult import TypeData as type_data_class
from Loehnert.TypeAndResult.Provider import ITypeDataProvider
from Loehnert.Lisrt.TypeAndResult.GeminiModule import GeminiTypeDataModule


def combine_type_data(types):
    provider = IoC.Get[GeminiTypeDataModule]().DefaultProvider  # type: ITypeDataProvider (1)
    combined_type_data = None
    for type_identifier in types:
        current_type_data = provider.Load(type_identifier)
        combined_type_data = current_type_data if combined_type_data == None else type_data_class.Inherit(combined_type_data, current_type_data) (2)

    return combined_type_data

types = ["Type_A", "Type_B", "Type_C"]
type_data = combine_type_data(types)
type_data.TypeIdentifier = "MySuperType" (3)
TypeData.SetTypeData(type_data) (4)
1 Get standard type data provider (e.g. XML file data source)
2 Inherit type data
3 The combined type data is given a new name
4 Load the type data into the type data module
inherit type datascript example
Figure 3. Class diagram of the type data

Last modified 13.09.2023