Vollständiger Beispielcode - TIAPortal

TIA Portal Openness: API für die Automatisierung von Engineering-Workflows

ft:publication_title
TIA Portal Openness: API für die Automatisierung von Engineering-Workflows
Product
TIAPortal
Version
V20
Publication date
01/2025
Language
de-DE
Vollständiger Beispielcode

Voraussetzung

Programmcode

Das folgende Skript für TIA Portal Openness beschreibt den kompletten Arbeitsablauf von Openness APIs für globale UMAC-Benutzer und UMC-Server.

Kopiert den nachfolgenden Programmcode in die Zwischenablage.

using System;

using Siemens.Engineering;

using Siemens.Engineering.HW;

using Siemens.Engineering.HW.Features;

using Siemens.Engineering.SW;

using Siemens.Engineering.SW.Blocks;

using Siemens.Engineering.SW.ExternalSources;

using Siemens.Engineering.SW.Tags;

using Siemens.Engineering.SW.Types;

using Siemens.Engineering.Hmi;

using HmiTarget = Siemens.Engineering.Hmi.HmiTarget;

using Siemens.Engineering.Hmi.Tag;

using Siemens.Engineering.Hmi.Screen;

using Siemens.Engineering.Hmi.Cycle;

using Siemens.Engineering.Hmi.Communication;

using Siemens.Engineering.Hmi.Globalization;

using Siemens.Engineering.Hmi.TextGraphicList;

using Siemens.Engineering.Hmi.RuntimeScripting;

using System.Collections.Generic;

using Siemens.Engineering.Compiler;

using Siemens.Engineering.Library;

using System.IO;

using System.Security;

using Siemens.Engineering.Umac;

namespace VollständigerBeispielcode

{

internal class Program

{

static void Main(string[] args)

{

TiaPortal tiaPortal = new TiaPortal();

//Registers for TIA Portal authentication for protected project opening

tiaPortal.Authentication += TiaPortal_Authentication;

Project umacProject = tiaPortal.Projects.Open(new FileInfo("ProtectedProject_Path"));

var umcServerConfigurator = umacProject.GetService<UmcServerConfigurator>();

// Getting UMC servers.

UmcServer umcServer = umcServerConfigurator.UmcServer;

// Register for UMC Server Authentication

umcServer.Authentication += UmcServer_Authentication;

var umacConfigurator = umacProject.GetService<UmacConfigurator>();

// Use case :Import User Group, Add User Group to Tia portal project , Assign & Unassign Role to UserGroup

// Delete the User group from Tia portal project.

// Retrieve an UMC User Group from UMC Server

const string UmcUserGroupName = "xxxxx";

UmcUserGroupInfo umcUserGroupInfo = umcServer.GetUserGroupByName(UmcUserGroupName);

// Add an UMC User Group into TIA portal Project.

UmcUserGroupComposition umcUserGroupComposition = umacConfigurator.UmcUserGroups;

UmcUserGroup umcUserGroup = umcUserGroupComposition.Create(umcUserGroupInfo);

// Assign and Unassign roles to User Group.

//Assign the System Role.

const string SystemRoleName = "Engineering administrator";

SystemRole engineeringAdministratorSystemRole = umacConfigurator.SystemRoles.Find(SystemRoleName);

umcUserGroup.Roles.Add(engineeringAdministratorSystemRole);

//Assign the Custom Role.

CustomRole customRole = umacConfigurator.CustomRoles.Create("CustomRole");

umcUserGroup.Roles.Add(customRole);

//Un assign the System Role.

umcUserGroup.Roles.Remove(engineeringAdministratorSystemRole);

//Un assign the Custom Role.

umcUserGroup.Roles.Remove(customRole);

//Delete the UMC user group in Tia Portal project.

UmcUserGroup umcUserGroupToDelete = umacConfigurator.UmcUserGroups.Find(UmcUserGroupName);

umcUserGroupToDelete.Delete();

// Use case :Import UMC User , Add UMC User to Tia portal project , Assign & Unassign Roles to UMC User,

// Delete the UMC User from Tia portal project.

// Import UMC User from UMC Server

const string UmcUserName = "xxxxx";

UmcUserInfo umcUserInfo = umcServer.GetUserByName(UmcUserName);

//Add UMC User to Tia Portal project

UmcUserComposition umcUserComposition = umacConfigurator.UmcUsers;

UmcUser umcUser = umcUserComposition.Create(umcUserInfo);

// Assign & Unassign Roles to UMC User

//Assign the System Role.

const string SystemRoleNameForUser = "NET Radius";

SystemRole systemRoleForUmcUser = umacConfigurator.SystemRoles.Find(SystemRoleNameForUser);

umcUser.AssignRole(systemRoleForUmcUser); //Needs to be: umcUser.Roles.Add(systemRoleForUmcUser);

//Assign the Custom Role.

CustomRole customRoleNameForUmcUser = umacConfigurator.CustomRoles.Create("CustomRoleName");

umcUser.Roles.Add(customRoleNameForUmcUser);

//Un assign the System Role.

umcUser.Roles.Remove(systemRoleForUmcUser);

//Un assign the Custom Role.

umcUser.Roles.Remove(customRoleNameForUmcUser);

//Delete the UMC User from Tia Portal project

UmcUser umcUserToDelete = umacConfigurator.UmcUsers.Find(UmcUserName);

umcUserToDelete.Delete();

}

private static void TiaPortal_Authentication(object sender, AuthenticationEventArgs e)

{

e.AuthenticationTypeProvider = AuthenticationTypeProvider.Credentials;

e.UmacCredentials.Type = UmacUserType.Project;

e.UmacCredentials.Name = "Admin";

e.UmacCredentials.SetPassword(GetSecureString("Admin123"));

}

private static void UmcServer_Authentication(object sender, UmcAuthenticationEventArgs e)

{

e.UmcCredentials.Name = "Admin";

e.UmcCredentials.SetPassword(GetSecureString("Admin123"));

}

private static SecureString GetSecureString(string password)

{

SecureString secureString = new SecureString();

foreach (char c in password)

{

secureString.AppendChar(c);

}

return secureString;

}

}

}