Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for TIA V19 #198

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup><GenerateErrorForMissingTargetingPacks>false</GenerateErrorForMissingTargetingPacks></PropertyGroup>
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net48</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<UseWPF>true</UseWPF>
</PropertyGroup>
<PropertyGroup>
<PackageId>DotNetProjects.DotNetSiemensPLCToolBoxLibrary.TIAV18</PackageId>
<Authors>DotNetProjects</Authors>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/jogibear9988/DotNetSiemensPLCToolBoxLibrary</PackageProjectUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.0.0</Version>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
</PropertyGroup>
<PropertyGroup>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LibNoDaveConnectionLibrary\DotNetSiemensPLCToolBoxLibrary.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="Siemens.Engineering">
<HintPath>..\externalDlls\siemens\V19\Siemens.Engineering.dll</HintPath>
<Private>False</Private>
<SpecificVersion>False</SpecificVersion>
</Reference>
<Reference Include="Siemens.Engineering.Hmi">
<HintPath>..\externalDlls\siemens\V19\Siemens.Engineering.Hmi.dll</HintPath>
<Private>False</Private>
<SpecificVersion>False</SpecificVersion>
</Reference>
<Reference Include="System.Windows.Forms" />
</ItemGroup>
</Project>
13 changes: 13 additions & 0 deletions DotNetSiemensPLCToolBoxLibrary.TIAV19/SelectPortalInstance.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Window x:Class="DotNetSiemensPLCToolBoxLibrary.TIAV19.SelectPortalInstance"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" WindowStyle="ToolWindow" WindowStartupLocation="CenterScreen"
Height="450" Width="800">
<Grid>
<ListBox x:Name="lstInstances" Margin="10,31,10,64"/>
<TextBlock HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" Text="Select Portal Instance" VerticalAlignment="Top"/>
<Button Content="Ok" HorizontalAlignment="Right" Margin="0,0,10,10" VerticalAlignment="Bottom" Width="130" Height="49" Click="Button_Click"/>
</Grid>
</Window>
15 changes: 15 additions & 0 deletions DotNetSiemensPLCToolBoxLibrary.TIAV19/SelectPortalInstance.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace DotNetSiemensPLCToolBoxLibrary.TIAV19
{
public partial class SelectPortalInstance
{
public SelectPortalInstance()
{
InitializeComponent();
}

private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
this.Close();
}
}
}
196 changes: 196 additions & 0 deletions DotNetSiemensPLCToolBoxLibrary.TIAV19/Step7ProjectV19.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Xml;
using DotNetSiemensPLCToolBoxLibrary.DataTypes;
using DotNetSiemensPLCToolBoxLibrary.General;
using DotNetSiemensPLCToolBoxLibrary.Projectfiles.TIA;
using DotNetSiemensPLCToolBoxLibrary.TIAV19;
using Microsoft.Win32;
using Siemens.Engineering;

namespace DotNetSiemensPLCToolBoxLibrary.Projectfiles.V19
{
public partial class Step7ProjectV19 : Project, IDisposable
{
private readonly Credentials _credentials;

private string DataFile = null;

private XmlDocument tiaProject;

internal ZipHelper _ziphelper = new ZipHelper(null);

public CultureInfo Culture { get; set; }

public Step7ProjectV19()
{
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve += currentDomain_AssemblyResolve;

AksForInstance();

LoadViaOpennessDlls();

currentDomain.AssemblyResolve -= currentDomain_AssemblyResolve;
}

public static Step7ProjectV19 AttachToInstanceWithFilename(string filename)
{
var inst = new Step7ProjectV19("");

AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve += inst.currentDomain_AssemblyResolve;
inst.AksForInstanceWithFilename(filename);
inst.LoadViaOpennessDlls();
currentDomain.AssemblyResolve -= inst.currentDomain_AssemblyResolve;

return inst;
}

private Step7ProjectV19(string notUsed)
{ }


private void AksForInstanceWithFilename(string file)
{

tiaPortal = new Siemens.Engineering.TiaPortal(Siemens.Engineering.TiaPortalMode.WithoutUserInterface);

var processes = TiaPortal.GetProcesses().ToArray();
var process = processes.First(x => x.ProjectPath!=null && x.ProjectPath.FullName == file);
tiaPortal = process.Attach();
tiapProject = tiaPortal.Projects[0];
this.ProjectFile = process.ProjectPath.ToString();
}

private void AksForInstance()
{

tiaPortal = new Siemens.Engineering.TiaPortal(Siemens.Engineering.TiaPortalMode.WithoutUserInterface);

var processes = TiaPortal.GetProcesses().ToArray();
var sLst = processes.Select(x => "Projekt : " + (x.ProjectPath != null ? x.ProjectPath.ToString() : "-")).ToArray();
AppDomain domain = AppDomain.CreateDomain("another domain");
CrossAppDomainDelegate action = () =>
{
var app = new Application();
var ask = new SelectPortalInstance();
var p = AppDomain.CurrentDomain.GetData("processes") as string[];
ask.lstInstances.ItemsSource = p;
app.Run(ask);
AppDomain.CurrentDomain.SetData("idx", ask.lstInstances.SelectedIndex);
};
domain.SetData("processes", sLst);
domain.DoCallBack(action);
var idx = (int)domain.GetData("idx");

tiaPortal = processes[idx].Attach();
tiapProject = tiaPortal.Projects[0];
this.ProjectFile = processes[idx].ProjectPath.ToString();
}

public Step7ProjectV19(string projectfile, CultureInfo culture = null) : this(projectfile, culture, null)
{
}

public Step7ProjectV19(string projectfile, CultureInfo culture = null, Credentials credentials = null)
{
_credentials = credentials;
if (culture == null)
Culture = CultureInfo.CurrentCulture;
else
Culture = culture;

AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve += currentDomain_AssemblyResolve;

ProjectFile = projectfile;

if (ProjectFile.ToLower().EndsWith("zip") || ProjectFile.ToLower().EndsWith("zap19"))
{
this._ziphelper = new ZipHelper(projectfile);
if (string.IsNullOrEmpty(ProjectFile))
ProjectFile = _ziphelper.GetFirstZipEntryWithEnding(".ap19");
if (string.IsNullOrEmpty(ProjectFile))
ProjectFile = _ziphelper.GetFirstZipEntryWithEnding(".al19");
if (string.IsNullOrEmpty(projectfile))
throw new Exception("Zip-File contains no valid TIA Project !");
}

try
{
using (var stream = _ziphelper.GetReadStream(projectfile))
{
var xmlDoc = new XmlDocument();
xmlDoc.Load(stream);

XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("x", "http://www.siemens.com/2007/07/Automation/CommonServices/DataInfoValueData");

var nd = xmlDoc.SelectSingleNode("x:Data", nsmgr);
this.ProjectName = nd.Attributes["Name"].Value;
}
}
catch (Exception)
{ }

DataFile = Path.GetDirectoryName(projectfile) + "\\System\\PEData.plf";
ProjectFolder = projectfile.Substring(0, projectfile.LastIndexOf(Path.DirectorySeparatorChar)) + Path.DirectorySeparatorChar;

//BinaryParseTIAFile();
//LoadProject();
OpenViaOpennessDlls(credentials);

currentDomain.AssemblyResolve -= currentDomain_AssemblyResolve;
}

internal XmlDocument xmlDoc;

Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
int index = args.Name.IndexOf(',');
if (index == -1)
{
return null;
}
var name = args.Name.Substring(0, index) + ".dll";

var filePathReg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Wow6432Node\\Siemens\\Automation\\_InstalledSW\\TIAP19\\Global") ??
Registry.LocalMachine.OpenSubKey("SOFTWARE\\Siemens\\Automation\\_InstalledSW\\TIAP19\\Global");

if (filePathReg != null)
{
string filePath = Path.Combine(filePathReg.GetValue("Path").ToString(), "PublicAPI\\V19");
var path = Path.Combine(filePath, name);
var fullPath = Path.GetFullPath(path);
if (File.Exists(fullPath))
{
return Assembly.LoadFrom(fullPath);
}
}

return null;
}

private object tiaExport;
private Type tiaExportType;


internal Dictionary<TiaObjectId, TiaFileObject> TiaObjects = new Dictionary<TiaObjectId, TiaFileObject>();

public override ProjectType ProjectType
{
get { return ProjectType.Tia18; }
}

protected override void LoadProject()
{
_projectLoaded = true;
}
}
}
Loading