Example: create and populate a galaxy
- Last UpdatedJan 16, 2025
- 3 minute read
The steps below demonstrate how to use GRAccess through C# and Visual Studio.
This procedure creates an ArchestrA Galaxy and populates it with a single instance.
Build and execute the program below. It creates a Galaxy called "Example1" and a single instance of a $UserDefined template with a name based on the current date and time.
Note: The C# console application should work with ATL support.
-
Using Visual Studio, create a new C# console application "CreateGalaxyExample".
-
On the Main menu, select Project / Add Reference and add a reference to the GRAccess primary interop assembly. By default, this is: C:\Windows\assembly\GAC_MSIL\ArchestrA.GRAccess\2.0.0.0__23106a86e706d0ae\ArchestrA.GRAccess.dll
-
Replace the contents of the generated C# source file with the following text:
Note: This code can be found in C:\Program Files (x86)\ArchestrA\Samples\CreateGalaxyCSharp.
// ------------------------------------------------------------------------------------------------------------
// <copyright company="AVEVA Software, LLC" file="Main.cs">
// © 2022 AVEVA Software, LLC. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <summary>
//
// </summary>
// ------------------------------------------------------------------------------------------------------------
using System;
using ArchestrA.GRAccess;
class CreateGalaxyExample
{
[STAThread]
static void Main()
{
string nodeName = Environment.MachineName;
string galaxyName = "Example1";
// create GRAccessAppClass object
GRAccessApp grAccess = new GRAccessAppClass();
// try to get galaxy
IGalaxies gals = grAccess.QueryGalaxies(nodeName);
if (gals == null || grAccess.CommandResult.Successful == false)
{
Console.WriteLine(grAccess.CommandResult.CustomMessage + grAccess.CommandResult.Text);
return;
}
IGalaxy galaxy = gals[galaxyName];
ICommandResult cmd;
// create galaxy if it doesn't already exist
if( galaxy == null )
{
grAccess.CreateGalaxy(
galaxyName,
nodeName,
false, // no security
EAuthenticationMode.galaxyAuthenticationMode,
"" );
cmd = grAccess.CommandResult;
if (!cmd.Successful)
{
Console.WriteLine("Create Galaxy Named Example1 Failed: " +
cmd.Text + " : " +
cmd.CustomMessage);
return;
}
galaxy = grAccess.QueryGalaxies( nodeName )[ galaxyName ];
}
// log in
galaxy.Login( "", "" );
cmd = galaxy.CommandResult;
if (!cmd.Successful)
{
Console.WriteLine("Login to galaxy Example1 Failed :" +
cmd.Text + " : " +
cmd.CustomMessage);
return;
}
// get the $UserDefined template
string [] tagnames = { "$UserDefined" };
IgObjects queryResult = galaxy.QueryObjectsByName(
EgObjectIsTemplateOrInstance.gObjectIsTemplate,
ref tagnames );
cmd = galaxy.CommandResult;
if (!cmd.Successful)
{
Console.WriteLine("QueryObjectsByName Failed for $UserDefined Template :" +
cmd.Text + " : " +
cmd.CustomMessage);
return;
}
ITemplate userDefinedTemplate = (ITemplate) queryResult[1];
// create an instance of $UserDefined, named with current time
DateTime now = DateTime.Now;
string instanceName = String.Format( "sample_object_{0}_{1}_{2}"
, now.Hour.ToString( "00" )
, now.Minute.ToString( "00" )
, now.Second.ToString( "00" ));
IInstance sampleinst = userDefinedTemplate.CreateInstance(instanceName, true );
//How to edit the object ?
sampleinst.CheckOut();
sampleinst.AddUDA("Names",
MxDataType.MxString,
MxAttributeCategory.MxCategoryWriteable_USC_Lockable,
MxSecurityClassification.MxSecurityOperate,
true,
5);
IAttributes attrs = sampleinst.ConfigurableAttributes;
//Diplay first 5 attribute names from collection
for (int i = 1; i <= 5; i++)
{
IAttribute attrb = attrs[i];
Console.WriteLine(attrb.Name);
}
IAttribute attr1 = attrs["Names"];
MxValue mxv = new MxValueClass();
// we don't need to check that attribute is array type or not
// because we set it as array type when we addUDA.
// I am just showing example, you can do like this.
if (attr1.UpperBoundDim1 > 0)
{
for (int i = 1; i <= attr1.UpperBoundDim1; i++)
{
MxValue mxvelement = new MxValueClass();
mxvelement.PutString("string element number " + i.ToString());
mxv.PutElement(i, mxvelement);
}
attr1.SetValue(mxv);
}
sampleinst.Save();
sampleinst.CheckIn("Check in after addUDA");
galaxy.Logout();
Console.WriteLine();
Console.Write("Press ENTER to quit: ");
string dummy;
dummy = Console.ReadLine();
}
}