Skip to content
Josef edited this page Aug 14, 2021 · 21 revisions

Basic code examples

These code examples show basic principles for AutomationML application development using the AMLEngine. The CAEX classes, used in the examples, are shown in the diagram. The CAEX Classes are all defined in the Aml.Engine.CAEX namespace.

The CAEXDocument represents the base class for creating and processing AutomationML documents. There are various loading methods for processing existing documents. The most common is loading a document from a file. Other loading methods exist for streams or strings. Loading a document from a stream is required for processing packed documents of an AutomationML container.

1. Loading a Document

using Aml.Engine.CAEX;
using Aml.Engine.AmlObjects;

// loading from a file
var document = CAEXDocument.LoadFromFile ("myFile.aml");

// loading a stream from an AutomationML container
var container = new AutomationMLContainer ("myContainer.amlx");
var rootDocument = CAEXDocument.LoadFromStream ( container.RootDocumentStream());

2. Saving a Document

CAEX documents can be saved as a file, a stream or a string.

using Aml.Engine.CAEX;

CAEXDocument.SaveToFile ("myFile.aml", true);

3. Creation of a new CAEX document and adding Internal Elements

With the introduction of CAEX version 3.0, CAEX documents can be created in different versions. The standard, without explicit version specification, is the use of CAEX 3.0.

using Aml.Engine.CAEX;

// a version CAEX 3.0 document
var document = CAEXDocument.New_Document ();

// appending some content
var myIH = document.CAEXFile.InstanceHierarchy.Append("myIH");
var myIE = myIH.InternalElement.Append("myIE");

// a version 2.15 document
var document2 = CAEXDocument.New_Document (CAEXSchema.CAEX2_15);

4. Accessing properties using Indexers

There are index based access methods to get a specific element in a sequence. In most sequences in CAEX, name uniqueness is required. The element name can therefore be used as an index. Another index used is a name-value pair, the name denotes a property of an element and the value denotes the property value.

using Aml.Engine.CAEX;

var document = CAEXDocument.New_Document ();
var myIH = document.CAEXFile.InstanceHierarchy.Append("myIH");
myIH.Version = "1.0";

// Get the first CAEXElement from the sequence of elements
myIH = document.CAEXFile.InstanceHierarchy[0];

// Get the first CAEXElement from the sequence of elements with the name "myIH"
myIH = document.CAEXFile.InstanceHierarchy["myIH"];

// Get the first CAEXElement with a specific attribute AND value from the sequence of elements
myIH = document.CAEXFile.InstanceHierarchy[(Name:"Version", Value:"1.0")];

5. Traversing the Instances of a Document

The most common way to process elements is to search the element hierarchy. The direct children of an element can be searched or all descendants of an element or all descendants with certain properties.

using Aml.Engine.CAEX;
var document = CAEXDocument.LoadFromFile("myFile.aml");

// browse the Instance Hierarchies in the file to import some elements
foreach (var instanceHierary in document.CaexFile.InstanceHierarchy)
{
    // browse all InternalElements deep and import the internal Elements to your system
    foreach (var internalElement in instanceHierarchy.Descendants<InternalElementType>())
    {
        // ToDo: add code to import the InternalElement
    }
}

6. Copying Elements

7. Insertion of Elements

Clone this wiki locally