From 2f5658ad935504e31a69b7add9f7554347333a19 Mon Sep 17 00:00:00 2001 From: Sven Knoerzer Date: Thu, 4 Jul 2019 09:07:23 +0200 Subject: [PATCH 1/4] Write not exported variable tables and watch tables to console in TiaGitHandler --- .../Step7ProjectV15_1Tia.cs | 2 +- TiaGitHandler/Program.cs | 49 +++++++++++++++---- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/DotNetSiemensPLCToolBoxLibrary.TIAV15_1/Step7ProjectV15_1Tia.cs b/DotNetSiemensPLCToolBoxLibrary.TIAV15_1/Step7ProjectV15_1Tia.cs index d6e10e0a..8e32efeb 100644 --- a/DotNetSiemensPLCToolBoxLibrary.TIAV15_1/Step7ProjectV15_1Tia.cs +++ b/DotNetSiemensPLCToolBoxLibrary.TIAV15_1/Step7ProjectV15_1Tia.cs @@ -469,7 +469,7 @@ public virtual string Export(ExportFormat exportFormat) var file = Path.Combine(tmp, "tmp_dnspt_" + Guid.NewGuid().ToString().Replace("{", "").Replace("}", "").Replace("-", "").Replace(" ", "") + "." + ext); if (ext == "xml") { - PlcWatchTable.Export(new FileInfo(file), Siemens.Engineering.ExportOptions.None); + PlcWatchTable.Export(new FileInfo(file), Siemens.Engineering.ExportOptions.WithDefaults); } var text = File.ReadAllText(file); File.Delete(file); diff --git a/TiaGitHandler/Program.cs b/TiaGitHandler/Program.cs index c770f45c..e803b67e 100644 --- a/TiaGitHandler/Program.cs +++ b/TiaGitHandler/Program.cs @@ -382,7 +382,7 @@ private static void ParseFolder(ProjectFolder folder, string dir, List s catch { } - + try { var nodes = xmlDoc.SelectNodes("//StructureModified"); @@ -729,27 +729,56 @@ private static void ParseFolder(ProjectFolder folder, string dir, List s { foreach (var varTab in varTabfld.TagTables) { - var vt = varTab.Export(); var file = Path.Combine(path, varTab.Name.Replace("\\", "_").Replace("/", "_") + ".xml"); - Directory.CreateDirectory(path); - File.WriteAllText(file, vt, new UTF8Encoding(true)); + try + { + var vt = varTab.Export(); + Directory.CreateDirectory(path); + File.WriteAllText(file, vt, new UTF8Encoding(true)); + } + catch + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(file + " could not be exported"); + Console.ForegroundColor = ConsoleColor.White; + } } } else if (folder is ITIAWatchAndForceTablesFolder wtfFld) { foreach (var varTab in wtfFld.WatchTables) { - var vt = varTab.Export(); var file = Path.Combine(path, varTab.Name.Replace("\\", "_").Replace("/", "_") + ".watch"); - Directory.CreateDirectory(path); - File.WriteAllText(file, vt, new UTF8Encoding(true)); + + try + { + var vt = varTab.Export(); + Directory.CreateDirectory(path); + File.WriteAllText(file, vt, new UTF8Encoding(true)); + } + catch + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(file + " could not be exported"); + Console.ForegroundColor = ConsoleColor.White; + } } foreach (var varTab in wtfFld.ForceTables) { - var vt = varTab.Export(); var file = Path.Combine(path, varTab.Name.Replace("\\", "_").Replace("/", "_") + ".force"); - Directory.CreateDirectory(path); - File.WriteAllText(file, vt, new UTF8Encoding(true)); + + try + { + var vt = varTab.Export(); + Directory.CreateDirectory(path); + File.WriteAllText(file, vt, new UTF8Encoding(true)); + } + catch (Exception e) + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(file + " could not be exported"); + Console.ForegroundColor = ConsoleColor.White; + } } } } From 25ce1131ff96a067742087854d41a3dca666f4d4 Mon Sep 17 00:00:00 2001 From: Sven Knoerzer Date: Thu, 4 Jul 2019 10:01:32 +0200 Subject: [PATCH 2/4] Remove all members from IDBS in TiaGitHandler --- TiaGitHandler/Program.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/TiaGitHandler/Program.cs b/TiaGitHandler/Program.cs index e803b67e..364e1b16 100644 --- a/TiaGitHandler/Program.cs +++ b/TiaGitHandler/Program.cs @@ -662,9 +662,21 @@ private static void ParseFolder(ProjectFolder folder, string dir, List s if (projectBlockInfo.BlockLanguage == PLCLanguage.DB && projectBlockInfo.BlockType == PLCBlockType.DB && projectBlockInfo.IsInstance) { + //try + //{ + // var nodes = xmlDoc2.SelectNodes("//smns2:BooleanAttribute[@Name='SetPoint']", ns); + // foreach (var node in nodes.Cast()) + // { + // node.ParentNode.RemoveChild(node); + // } + //} + //catch + //{ + //} + try { - var nodes = xmlDoc2.SelectNodes("//smns2:BooleanAttribute[@Name='SetPoint']", ns); + var nodes = xmlDoc2.SelectNodes("//*[local-name()='Interface']/*[local-name()='Sections']/*[local-name()='Section']/*[local-name()='Member']"); foreach (var node in nodes.Cast()) { node.ParentNode.RemoveChild(node); From c4185c488f0fb2ed661069e61f1a7f67165eac12 Mon Sep 17 00:00:00 2001 From: Sven Knoerzer Date: Fri, 5 Jul 2019 10:03:25 +0200 Subject: [PATCH 3/4] Delete awl scl file after successful import from import folder TiaImporter --- TiaImporter/Program.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/TiaImporter/Program.cs b/TiaImporter/Program.cs index 0422b5dd..ff4370ca 100644 --- a/TiaImporter/Program.cs +++ b/TiaImporter/Program.cs @@ -109,6 +109,10 @@ static void Main(string[] args) Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Imported File: " + relativePath); + if (importFile.ToLower().EndsWith("awl") || importFile.ToLower().EndsWith("scl")) + { + File.Delete(importFile); + } fileList.Remove(importFile); } From f583b809aefc5a71d6919843244a8585fdac92e2 Mon Sep 17 00:00:00 2001 From: Sven Knoerzer Date: Tue, 9 Jul 2019 17:47:43 +0200 Subject: [PATCH 4/4] Add auto compile blocks feature after failed import of scl and awl to TiaImporter --- .../Step7ProjectV15_1Tia.cs | 15 +++ TiaImporter/Program.cs | 121 ++++++++++++------ 2 files changed, 94 insertions(+), 42 deletions(-) diff --git a/DotNetSiemensPLCToolBoxLibrary.TIAV15_1/Step7ProjectV15_1Tia.cs b/DotNetSiemensPLCToolBoxLibrary.TIAV15_1/Step7ProjectV15_1Tia.cs index 8e32efeb..233c868b 100644 --- a/DotNetSiemensPLCToolBoxLibrary.TIAV15_1/Step7ProjectV15_1Tia.cs +++ b/DotNetSiemensPLCToolBoxLibrary.TIAV15_1/Step7ProjectV15_1Tia.cs @@ -18,6 +18,7 @@ using Siemens.Engineering.SW.Tags; using DotNetSiemensPLCToolBoxLibrary.General; using System.Text.RegularExpressions; +using Siemens.Engineering.Compiler; using Siemens.Engineering.SW.WatchAndForceTables; namespace DotNetSiemensPLCToolBoxLibrary.Projectfiles.V15_1 @@ -55,6 +56,9 @@ public TIAOpennessProjectFolder(Step7ProjectV15_1 Project) public virtual void ImportFile(FileInfo file, bool overwrite, bool importFromSource) { } + + public virtual void CompileBlocks() + { } } public class TIAOpennessProjectBlockInfo : ProjectBlockInfo, ITiaProjectBlockInfo @@ -680,6 +684,17 @@ public override ProjectFolder CreateFolder(string name) return newFld; } + public override void CompileBlocks() + { + CompilerResult result; + + var compiler = plcBlockGroup.GetService(); + if (compiler != null) + result = compiler.Compile(); + else + throw new ArgumentException("Parameter cannot be compiled.", nameof(plcBlockGroup)); + } + public override void ImportFile(FileInfo file, bool overwrite, bool importFromSource) { if (!importFromSource) diff --git a/TiaImporter/Program.cs b/TiaImporter/Program.cs index ff4370ca..337e7865 100644 --- a/TiaImporter/Program.cs +++ b/TiaImporter/Program.cs @@ -66,70 +66,107 @@ static void Main(string[] args) projectFolder = projectFolder.SubItems.First(x => x.Name == s); } + var programFolderToCompile = projectFolder; + var pgFolderToCompile = programFolderToCompile as Step7ProjectV15_1.TIAOpennessProgramFolder; + int i = 0; var fileList = BuildImportFileList(folderToImport, extension).ToList(); var count = 0; - while (count != fileList.Count() && fileList.Count() > 0) + + ImportFiles(); + + void ImportFiles() { - Console.WriteLine("-----------------------------------"); - Console.WriteLine("Durchlauf " + ++i); - Console.WriteLine("-----------------------------------"); - Console.WriteLine(); - count = fileList.Count(); - foreach (var importFile in fileList.ToList()) + while (count != fileList.Count() && fileList.Count() > 0) { - var relativePath = importFile.Substring(folderToImport.Length + 1); - - var importFolder = projectFolder; - foreach (var p in relativePath.Split('\\').Reverse().Skip(1).Reverse()) + Console.WriteLine("-----------------------------------"); + Console.WriteLine("Durchlauf " + ++i); + Console.WriteLine("-----------------------------------"); + Console.WriteLine(); + count = fileList.Count(); + foreach (var importFile in fileList.ToList()) { - var prevFolder = importFolder; - importFolder = importFolder.SubItems.FirstOrDefault(x => x.Name == p); - if (importFolder == null) + var relativePath = importFile.Substring(folderToImport.Length + 1); + + var importFolder = projectFolder; + foreach (var p in relativePath.Split('\\').Reverse().Skip(1).Reverse()) { - importFolder = prevFolder.CreateFolder(p); - Console.ForegroundColor = ConsoleColor.Yellow; - Console.WriteLine("Folder Created: " + p); + var prevFolder = importFolder; + importFolder = importFolder.SubItems.FirstOrDefault(x => x.Name == p); + if (importFolder == null) + { + importFolder = prevFolder.CreateFolder(p); + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine("Folder Created: " + p); + } } - } - try - { - var dtFolder = importFolder as Step7ProjectV15_1.TIAOpennessPlcDatatypeFolder; - var pgFolder = importFolder as Step7ProjectV15_1.TIAOpennessProgramFolder; - if (dtFolder != null) - { - dtFolder.ImportFile(new FileInfo(importFile), true, false); - } - else if (pgFolder != null) + try { - pgFolder.ImportFile(new FileInfo(importFile), true, !importFile.ToLower().EndsWith("xml")); + var dtFolder = importFolder as Step7ProjectV15_1.TIAOpennessPlcDatatypeFolder; + var pgFolder = importFolder as Step7ProjectV15_1.TIAOpennessProgramFolder; + + if (dtFolder != null) + { + dtFolder.ImportFile(new FileInfo(importFile), true, false); + } + else if (pgFolder != null) + { + pgFolder.ImportFile(new FileInfo(importFile), true, !importFile.ToLower().EndsWith("xml")); + } + + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine("Imported File: " + relativePath); + //if (importFile.ToLower().EndsWith("awl") || importFile.ToLower().EndsWith("scl")) + //{ + // File.Delete(importFile); + //} + + fileList.Remove(importFile); } - - Console.ForegroundColor = ConsoleColor.Green; - Console.WriteLine("Imported File: " + relativePath); - if (importFile.ToLower().EndsWith("awl") || importFile.ToLower().EndsWith("scl")) + catch (Exception ex) { - File.Delete(importFile); + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("Error Importing File: " + relativePath + " - " + ex.Message); } - - fileList.Remove(importFile); - } - catch (Exception ex) - { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine("Error Importing File: " + relativePath + " - " + ex.Message); } } } - if (fileList.Count() != 0) + if (fileList.Count() != 0 && extension == "xml") { - Console.WriteLine(count + " files could not be imported"); + Console.WriteLine(count + " files could not be imported."); //Console.ReadLine(); Environment.Exit(1); } + + if (fileList.Count() != 0 && extension != "xml") + { + Console.WriteLine(count + " files could not be imported after " + i + " runs."); + var countBeforeCompile = fileList.Count(); + var countAfterCompile = 0; + while (countAfterCompile < countBeforeCompile) + { + countBeforeCompile = fileList.Count(); + Console.WriteLine("... trying to compile project"); + pgFolderToCompile.CompileBlocks(); + Console.WriteLine("... restarting to import files"); + count = 0; + ImportFiles(); + countAfterCompile = fileList.Count(); + if (countAfterCompile == 0) + { + break; + } + } + + if (countAfterCompile == countBeforeCompile && fileList.Count() != 0) + { + Environment.Exit(1); + } + + } }