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

Hwid #9

Merged
merged 2 commits into from
Feb 6, 2024
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
23 changes: 13 additions & 10 deletions src/tia2ax/V18_0/ApiResolver/ApiWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -716,25 +716,28 @@ public bool IsNewHardwareId(string name, IList<HwIdentifierItem> HwIdentifiers)
/// <summary>
/// Export all PLCs
/// </summary>
public void ExportAllPLCs(string exportDirectory, ProjectItem projectItem)
public void ExportAllPLCs(string exportDirectory, bool hwidOnly, ProjectItem projectItem)
{
DeleteFolder(exportDirectory);
Directory.CreateDirectory(exportDirectory);
foreach (PlcItem plcItem in projectItem.PlcItems)
{
string exportDir = String.IsNullOrEmpty(exportDirectory) ? Path.Combine(Environment.CurrentDirectory, plcItem.Name) : Path.Combine(exportDirectory, plcItem.Name);
Directory.CreateDirectory(exportDir);


ExportMappingItems(plcItem, exportDir, ExportItemType.HwInput);
ExportMappingItems(plcItem, exportDir, ExportItemType.HwOutput);
ExportMappingItems(plcItem, exportDir, ExportItemType.PlcInput);
ExportMappingItems(plcItem, exportDir, ExportItemType.PlcOutput);
if (!hwidOnly)
{
ExportMappingItems(plcItem, exportDir, ExportItemType.HwInput);
ExportMappingItems(plcItem, exportDir, ExportItemType.HwOutput);
ExportMappingItems(plcItem, exportDir, ExportItemType.PlcInput);
ExportMappingItems(plcItem, exportDir, ExportItemType.PlcOutput);
ExportCopyInputs(plcItem, exportDir);
ExportCopyOutputs(plcItem, exportDir);
ExportConfiguration(exportDir);
ExportProgram(exportDir);
}

ExportHwIdentifiers(plcItem, exportDir);
ExportCopyInputs(plcItem, exportDir);
ExportCopyOutputs(plcItem, exportDir);
ExportConfiguration(exportDir);
ExportProgram(exportDir);
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/tia2ax/V18_0/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private static void GoAhead(Options options)

var creator = new Tia2AxServices(traceWriter, apiWrapper);
creator.OpenProject(options.TiaSourceProject);
creator.GetPlcList(options.OutputProjectFolder);
creator.GetPlcList(options.OutputProjectFolder,options.HwIdOnly);
}
}

Expand All @@ -60,6 +60,9 @@ internal class Options
HelpText = "Output project folder where generator emits result.")]
public string OutputProjectFolder { get; set; }

[Option('h', "hwid-only", Required = false, HelpText = "Export only hardware idntifiers")]
public bool HwIdOnly { get; set; }


}
}
Expand Down
3 changes: 2 additions & 1 deletion src/tia2ax/V18_0/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
//"commandLineArgs": "-x D:\\github\\ix-ax\\TiaProjects\\AX_Plc1518V3_CognexDataman\\AX_Plc1518V3_CognexDataman.ap18 -o D:\\github\\ix-ax\\TiaProjects\\Export"
//"commandLineArgs": "-x D:\\github\\ix-ax\\TiaProjects\\AX_Plc1518V3_CmmtAs\\AX_Plc1518V3_CmmtAs.ap18 -o D:\\github\\ix-ax\\TiaProjects\\Export"
//"commandLineArgs": "-x D:\\github\\ix-ax\\TiaProjects\\AX_Plc1518V3_IndraDrive\\AX_Plc1518V3_IndraDrive.ap18 -o D:\\github\\ix-ax\\TiaProjects\\Export"
"commandLineArgs": "-x D:\\github\\ix-ax\\TiaProjects\\AX_Plc1518V3_Desoutter\\AX_Plc1518V3_Desoutter.ap18 -o D:\\github\\ix-ax\\TiaProjects\\Export"
"commandLineArgs": "-x D:\\github\\ix-ax\\TiaProjects\\AX_Plc1518V3_Desoutter\\AX_Plc1518V3_Desoutter.ap18 -o D:\\github\\ix-ax\\Export"
//"commandLineArgs": "-x D:\\github\\ix-ax\\TiaProjects\\HW_Plc1518V3_Desoutter\\HW_Plc1518V3_IndraDrive.ap19 -o D:\\github\\ix-ax\\TiaProjects\\Export"
}
}
}
4 changes: 2 additions & 2 deletions src/tia2ax/V18_0/Tia2AxServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,13 @@ public void CloseProject([CallerMemberName] string caller = "")
/// Retrieve the PLCs from the current project
/// </summary>
/// <param name="caller"></param>
public void GetPlcList(string outputFolder, [CallerMemberName] string caller = "")
public void GetPlcList(string outputFolder, bool hwidOnly, [CallerMemberName] string caller = "")
{
var methodBase = MethodBase.GetCurrentMethod();
if (methodBase.ReflectedType != null) _traceWriter.WriteLine(methodBase.ReflectedType.Name + "." + methodBase.Name + " called from " + caller);

_apiWrapper.GetPlcList(ProjectItem);
_apiWrapper.ExportAllPLCs(outputFolder, ProjectItem);
_apiWrapper.ExportAllPLCs(outputFolder, hwidOnly, ProjectItem);
}

#endregion // TIA Portal Project
Expand Down
132 changes: 132 additions & 0 deletions src/tia2ax/V19_0/ApiResolver/ApiResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Security.AccessControl;

namespace Tia2Ax.Utils
{
/// <summary>
/// Definition of helper functionality to resolve api dll, modules and options
/// </summary>
public static class ApiResolver
{
/// <summary>
/// Required min version of engineering dll
/// </summary>
public const string StrRequiredVersion = "V19.0";
private const string BasePath = "SOFTWARE\\Siemens\\Automation\\Openness\\";
private const string LibraryKey = "SOFTWARE\\Siemens\\Automation\\Openness\\19.0\\PublicAPI\\19.0.0.0";
private const string LibraryName = "Siemens.Engineering";

/// <summary>
/// Get version info from registry key
/// </summary>
/// <returns></returns>
public static List<string> GetEngineeringVersions()
{
RegistryKey key = GetRegistryKey(BasePath);

if (key != null)
{
try
{
var names = key.GetSubKeyNames().OrderBy(x => x).ToList();

var result = (from item in names
where Convert.ToDecimal(item.Substring(0, 4)) >= Convert.ToDecimal(StrRequiredVersion.Substring(1, 4))
select item.Substring(0, 4)).ToList();

key.Dispose();

return result;
}
finally
{
key.Dispose();
}
}

return new List<string>();
}

private static RegistryKey GetRegistryKey(string keyName)
{
RegistryKey baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
RegistryKey key = baseKey.OpenSubKey(keyName);
if (key == null)
{
baseKey.Dispose();
baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default);
key = baseKey.OpenSubKey(keyName);
}
if (key == null)
{
baseKey.Dispose();
baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
key = baseKey.OpenSubKey(keyName);
}
baseKey.Dispose();

return key;
}


/// <summary>
/// Check if openness api is installed
/// </summary>
/// <returns></returns>
public static bool IsOpennessInstalled()
{
return !string.IsNullOrEmpty(GetLibraryFilePath());
}

private static string GetLibraryFilePath()
{
using (var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
{
using (var registryKey = baseKey.OpenSubKey(LibraryKey, RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey))
{
var libraryFilePath = registryKey?.GetValue(LibraryName) as string;
if (!string.IsNullOrWhiteSpace(libraryFilePath) && File.Exists(libraryFilePath))
{
return libraryFilePath;
}
}
}
return null;
}


/// <summary>
/// Check if required TiaVersion is installed
/// </summary>
/// <returns></returns>
public static bool IsTiaInstalled()
{
try
{
ObservableCollection<string> EngineeringVersions = new ObservableCollection<string>(GetEngineeringVersions());
if (EngineeringVersions != null && EngineeringVersions.Count > 0)
{
foreach (var version in EngineeringVersions)
{
if(version == StrRequiredVersion.Substring(1, 4))
{
return true;
}
}
return false;
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return false;
}
return false;
}
}
}
Loading
Loading