Showing results 1 to 1 of 1

Thread: Code Sample creating variable in the zenon editor

  1. #1
    Join Date
    28.07.2008
    Posts
    64

    Default Code Sample creating variable in the zenon editor

    In this example, a text file is opened and the contents of this are used to create variables in the zenon editor. The text file contains any desired number of lines. Each line includes the name and data type of a variable; these are separated by a comma (example: Variable1,BOOL).

    The Macro1 method first looks for the internal driver in the zenon editor. After this, the user is shown a file selection dialog in which he must select the text file. The method then reads the text file and creates the variables. The GetDataType method is then required to determine and assign the attendant data type when creating the variables.

    using System;
    
    using System.Windows.Forms;
    
    using System.IO;
    
    namespace WorkspaceAddin
    {
        [System.AddIn.AddIn("MyWorkspace", Version = "1.0", Publisher = "", Description = "")]
    
         public partial class MyWorkspace
         {
            private void MyWorkspace_Startup(object sender, EventArgs e)
         {
    }
    
    private void MyWorkspace_Shutdown(object sender, EventArgs e)
    {
    }
    
    public zenOn.IVarType GetDataType(zenOn.IVarType vType, string strVType)
    {
    
        //gets the corresponding vartypes for bool, int, real and strings
         switch (strVType)
         {
                 case "BOOL":
                 vType = this.ActiveDocument.VarTypes().Item("BOOL");
                 break;
    
                 case "INT":
                 vType = this.ActiveDocument.VarTypes().Item("INT");
                 break;
    
                 case "REAL":
                  vType = this.ActiveDocument.VarTypes().Item("REAL");
                 break;
    
                  case "STRING":
                  vType = this.ActiveDocument.VarTypes().Item("STRING");
                  break;
    
                  default:
                  vType = this.ActiveDocument.VarTypes().Item("INT");
                  break;
            }
            return vType;
    }
    
    public void Macro1()
    {
           //create objects that will take the intern driver and the variable type
           zenOn.IDriver zInternDriver = null;
           zenOn.IVarType vType = null;
    
           //search for the Intern driver and throw exception if no driver was found
           try
           {
                for (int nDriverCount = 0; nDriverCount < this.ActiveDocument.Drivers().Count; nDriverCount++)
                 {
                     if (this.ActiveDocument.Drivers().Item(nDriverCount).Name == "Intern")
                      {
                           zInternDriver = this.ActiveDocument.Drivers().Item(nDriverCount);
                       }
                }
            }
            catch (Exception xDrv)
            {
                  MessageBox.Show("Unable to find zenon 'Intern' driver. Error: " + xDrv.Message);
                   throw;
             }
    
            this.ActiveDocument.Variables().DoAutoSave(false);
    
             try
             {
                  OpenFileDialog VarFileSelect = new OpenFileDialog();
                   String[] strVarLine = new String[2];
     
                   //show file dialog
                   if (VarFileSelect.ShowDialog() == DialogResult.OK)
                   {
                         string strLine = string.Empty;
                         //open new stream reader with selected file
    
                         StreamReader ImportStream = new StreamReader(VarFileSelect.FileName, System.Text.Encoding.Default);
    
                         //read in line by line, split the lines when a ',' occurs and create variables
    
                        while ((strLine = ImportStream.ReadLine()) != null)
                        {
                              strVarLine = strLine.Split(new Char[] { ',' });
    
                              this.ActiveDocument.Variables().CreateVar(strVarLine[0], zInternDriver, zenOn.tpKanaltypes.tpSystemVariable, GetDataType(vType,strVarLine[1]));
    
                        }
    
                ImportStream.Close();
                 }
             }
             catch (Exception xFileRd)
             {
                 MessageBox.Show("An error occured while opening the file: " + xFileRd.Message);
                 throw;
              }
              this.ActiveDocument.Variables().DoAutoSave(true);
      }
    
    private void InternalStartup()
    {
          this.Startup += new System.EventHandler(MyWorkspace_Startup);
          this.Shutdown += new System.EventHandler(MyWorkspace_Shutdown);
    }
    
    }
    Last edited by tulon : 20th April 2010 at 15:11

Similar Threads

  1. Replies: 5
    Last Post: 30th July 2018, 10:45
  2. Word als Binär Code anzeigen
    By sknoerr in forum zenon Supervisor
    Replies: 2
    Last Post: 26th March 2009, 19:51
  3. Information Unlimited 16 - Sample
    By robertf in forum VBA
    Replies: 0
    Last Post: 28th November 2008, 13:34
  4. rapidly creating Zenon functions using import/export
    By mattsd in forum zenon Supervisor
    Replies: 3
    Last Post: 10th January 2008, 10:20
  5. Replies: 0
    Last Post: 20th August 2007, 17:04

Posting Rules

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •