Click or drag to resize

Basic code examples

These code examples show basic principles for AutomationML application development using the AMLEngine.

Basic Examples

  1. Loading a Document

    C#
    using Aml.Engine.CAEX;
    
    // Load the document from a file
    var document = CAEXDocument.LoadFromFile ("myFile.aml");
    
    // Load the document from a file with indices for fast object search,
    // use this for read-only documents only
    document = CAEXDocument.LoadFromFile ("myFile.aml", createIndices:true);

  2. Saving a Document

    C#
    using Aml.Engine.CAEX;
    
    // save the document to a file
    document.SaveToFile ("myFile.aml", prettyPrint:true);

  3. Creation of a new CAEX document with InternaElements

    C#
    using Aml.Engine.CAEX;
    
    // create a new empty CAEX document
    using var document = CAEXDocument.New_CAEXDocument ();
    
    // append an empty InstanceHierarchy element to the CAEXFile object of the document, named "IH"
    var myIH = document.CAEXFile.InstanceHierarchy.Append("IH");
    
    // append an InternalElement element to the created InstanceHierarchy, named "IE"
    var myIE = myIH.InternalElement.Append("IE");

  4. Indexers

    C#
    using Aml.Engine.CAEX;
    
    using var document = CAEXDocument.New_CAEXDocument ();
    
    // create an empty InstanceHierarchy
    var myIH = document.CAEXFile.InstanceHierarchy.Append("myIH");
    
    // Get the first CAEXElement from the sequence of InstanceHierarchies
    myIH = document.CAEXFile.InstanceHierarchy[0];
    
    // Get the first CAEXElement from the sequence of InstanceHierarchies with the name "myIH"
    myIH = document.CAEXFile.InstanceHierarchy["myIH"];
    
    // Append an InternalElement named "IE1" with one child element "IE1_1"
    var ie1_1 = myIH.InternalElement.Append("IE1").Append("IE1_1");
    
    // Access the child InternalElement from the InstanceHierarchy
    // by specifying the names of the element tree
    ie1_1 = myIH.InternalElement["IE1", "IE1_1"];
    
    // Append an Attribute named "AT1" with one child attribute "AT1_1"
    var at1_1 = ie1_1.Attribute.Append("AT1").Attribute.Append("AT1_1");
    
    // Access the child Attribute from the InternalElement
    // by specifying the names of the attribute element tree
    at1_1 = myIE.Attribute["AT1", "AT1_1"];
  5. Attributes: Types and Values

    C#
    using Aml.Engine.CAEX;
    using Aml.Engine.CAEX.Extensions;
    
    using var document = CAEXDocument.New_CAEXDocument ();
    
    // create an empty InstanceHierarchy
    var myIH = document.CAEXFile.InstanceHierarchy.Append("myIH");
    
    // Append an InternalElement named "IE1"
    var ie = myIH.InternalElement.Append("IE1");
    
    // Define some Attributes with different types
    
    // creates a new attribute named "IsConnected" with AttributeDatatype "xs:boolean" and Value "false"
    ie.SetAttributeValue ("IsConnected", false);
    
    // creates a new structured attribute "Size" with properties "With" and "Height" with AttributeDatatype
    // xs:double and inital double values
    ie.Attribute.Append ("Size").SetAttributeValue ("With", 10.0);
    ie.Attribute["Size"].SetAttributeValue ("Height", 20.0);
    
    // alternatives to modify values of nested attributes
    ie.Attribute["Size/With"].AttributeValue = 15.0; // in CAEX 2.0 use "Size.With" as path
    ie.Attribute["Size","With"].AttributeValue = 15.0; // works in all CAEX versions
    ie.SetAttributeValue ("Size/With", 15.0);
  6. Traversing a Document.

    C#
    using Aml.Engine.CAEX;
    var document = CAEXDocument.LoadFromFile("myFile.aml");
    
    // browse the Instance Hierarchies in the file
    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: import internal Element
      }
    }

  7. Application of instantiation of a SystemUnitClass to insert an InternalElement.

    C#
    using Aml.Engine.CAEX;
    
    var document = CAEXDocument.New_CAEXDocument ();
    var myIH = document.CAEXFile.InstanceHierarchy.Append("myIH");
    var mySlib = document.CAEXFile.SystemUnitClassLib.Append("mySlib");
    var mySuc = mySlib.SystemUnitClass.Append("suClass");
    
    // insert a new class instance (InternalElement) to the InstanceHierarchy
    var firstIE = myIH.Insert (mySuc.CreateClassInstance());
    
    // append a new class instance (InternalElement) to the InstanceHierarchy
    var secondIE = myIH.Insert (mySuc.CreateClassInstance(), asFirst:false);

  8. Creation of class to class relations using AutomationML Base Classes.

    see also: IClassWithBaseClassReference, BaseClass, InterfaceClassLib, MakeAutomationMLBaseInterface

    C#
    using Aml.Engine.CAEX;
    using Aml.Engine.AmlObjects;
    
    var document = CAEXDocument.New_CAEXDocument ();
    
    // the first solution shows an implementation, which uses existing libraries and classes
    void methodWithClasses ()
    {
      // Add the standard AutomationMLInterfaceClassLib
      var amlBaseICLib = AutomationMLInterfaceClassLibType.InterfaceClassLib(document);
    
      var myIClib = document.CAEXFile.InterfaceClassLib.Append("myIClib");
    
      var myIC = myIClib.InterfaceClass.Append("myICClass");
    
      // Create a baseclass relation to the AutomationMLBaseInterface class
      myIC.BaseClass = amlBaseICLib.AutomationMLClass(AutomationMLInterfaceClassLib.AutomationMLBaseInterface);
    }
    
    // the second solution shows an implementation, which uses the standardized class path
    void methodWithClassPath ()
    {
      var myIClib = document.CAEXFile.InterfaceClassLib.Append("myIClib");
      var myIC = myIClib.InterfaceClass.Append("myICClass");
    
      // Create a baseclass reference using the path AutomationMLBaseInterface              
      myIC.RefBaseClassPath = AutomationMLInterfaceClassLib.AutomationMLBaseInterface;
    }