Skip to main content
InSource Solutions

TN Appsvr137 Using GR Access to Create and Deploy New Object Instances

insource logo large.jpg

 

Description

GR Access is a toolkit that allows custom application development to extend the capabilities of the standard Wonderware software product.  This example from InSource demonstrates the use of the GR Access toolkit to automate galaxy operations.  

 

  • Author: Michael Walker
  • Published: 06/25/2015
  • Applies to: GR Access 2014, Wonderware System Platform 2014 and later

 

 

Details

The following example application will automate the following galaxy operations:

  1. Logon to a specified galaxy
  2. Search for a specified template
  3. Generate a specified number of instances for the specified template
  4. Auto-assign the Area of each instance
  5. Auto-deploy each instance as it is created

 

The application is a simple windows form application. It prompts the user to enter a template name, the number of instances to create and then a button to create these instances.

Window Form.PNG

 

The code behind is on the 'Create New Instances' button and it randomly assigns on instance name for each object to make it unique.

 

Here is a breakdown of the code:

  • Logon to a specified galaxy

                string nodeName = Environment.MachineName;
                string galaxyName = "MWSYSSIM";
                GRAccessApp grAccess = new GRAccessApp();
                ICommandResult cmd;

                IGalaxies gals = grAccess.QueryGalaxies(nodeName);

                if (gals == null || grAccess.CommandResult.Successful == false)
                {
                    MessageBox.Show(grAccess.CommandResult.CustomMessage + grAccess.CommandResult.Text);
                    
                }

                IGalaxy galaxy = gals[galaxyName];

                galaxy.Login("", "");
                cmd = grAccess.CommandResult;

                if (!cmd.Successful)
                {
                    MessageBox.Show("Login To Galaxy Failed! " + cmd.Text.ToString() + "   :   " + cmd.CustomMessage.ToString());
                }

 

  • Search for a specified template

               string[] tagnames = { tbxTemplateName.Text.ToString()};

                IgObjects queryResult = galaxy.QueryObjectsByName(
                    EgObjectIsTemplateOrInstance.gObjectIsTemplate,
                    ref tagnames);

                cmd = galaxy.CommandResult;
                if (!cmd.Successful)
                {
                    Console.WriteLine("QueryObjectsByName Failed for template:" +
                                    cmd.Text + " : " +
                                    cmd.CustomMessage);
                    return;
                }

                ITemplate userDefinedTemplate = (ITemplate)queryResult[1];

 

  • Generate a specified number of instances for the specified template, auto-assign the Area for each instance and Auto-deploy each instance as it is created. 

 

                  DateTime now = DateTime.Now;
                for (int i = 1; i <= Convert.ToInt32(tbxInstanceCount.Text.ToString()); i++)
                {
                    string instanceName = String.Format("object_{0}_{1}_{2}_{3}"
                    , now.Hour.ToString("00")
                    , now.Minute.ToString("00")
                    , now.Second.ToString("00")
                    , i.ToString());
                    IInstance sampleinst = userDefinedTemplate.CreateInstance(instanceName, true);
                    sampleinst.Area = "Area";
                    sampleinst.Deploy(EActionForCurrentlyDeployedObjects.deployChanges,                     ESkipIfCurrentlyUndeployed.dontSkipIfCurrentlyUndeployed, EDeployOnScan.doDeployOnScan,                     EForceOffScan.doForceOffScan, ECascade.doCascade, true);

 

 

Below is all the code that performs this action. 

 

 

try
            {
                string nodeName = Environment.MachineName;
                string galaxyName = "MWSYSSIM";
                GRAccessApp grAccess = new GRAccessApp();
                ICommandResult cmd;

                IGalaxies gals = grAccess.QueryGalaxies(nodeName);

                if (gals == null || grAccess.CommandResult.Successful == false)
                {
                    MessageBox.Show(grAccess.CommandResult.CustomMessage + grAccess.CommandResult.Text);
                    
                }

                IGalaxy galaxy = gals[galaxyName];

                galaxy.Login("", "");
                cmd = grAccess.CommandResult;

                if (!cmd.Successful)
                {
                    MessageBox.Show("Login To Galaxy Failed! " + cmd.Text.ToString() + "   :   " + cmd.CustomMessage.ToString());
                }


                // seach for specified template.
                string[] tagnames = { tbxTemplateName.Text.ToString()};

                IgObjects queryResult = galaxy.QueryObjectsByName(
                    EgObjectIsTemplateOrInstance.gObjectIsTemplate,
                    ref tagnames);

                cmd = galaxy.CommandResult;
                if (!cmd.Successful)
                {
                    Console.WriteLine("QueryObjectsByName Failed for template:" +
                                    cmd.Text + " : " +
                                    cmd.CustomMessage);
                    return;
                }

                ITemplate userDefinedTemplate = (ITemplate)queryResult[1];

                // create an instance of selected template, named with current time to keep unique
                  DateTime now = DateTime.Now;
                for (int i = 1; i <= Convert.ToInt32(tbxInstanceCount.Text.ToString()); i++)
                {
                    string instanceName = String.Format("object_{0}_{1}_{2}_{3}"
                    , now.Hour.ToString("00")
                    , now.Minute.ToString("00")
                    , now.Second.ToString("00")
                    , i.ToString());
                    IInstance sampleinst = userDefinedTemplate.CreateInstance(instanceName, true);
                    sampleinst.Area = "Area";
                    sampleinst.Deploy(EActionForCurrentlyDeployedObjects.deployChanges, ESkipIfCurrentlyUndeployed.dontSkipIfCurrentlyUndeployed, EDeployOnScan.doDeployOnScan, EForceOffScan.doForceOffScan, ECascade.doCascade, true);
                                       

                }
                
                         

            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

 

Here is screenshot of the resulting objects, created and deployed. 

Application Screenshot.PNG