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

Feature/compile #127

Merged
merged 4 commits into from
Jul 9, 2019
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
17 changes: 16 additions & 1 deletion DotNetSiemensPLCToolBoxLibrary.TIAV15_1/Step7ProjectV15_1Tia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -469,7 +473,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);
Expand Down Expand Up @@ -680,6 +684,17 @@ public override ProjectFolder CreateFolder(string name)
return newFld;
}

public override void CompileBlocks()
{
CompilerResult result;

var compiler = plcBlockGroup.GetService<ICompilable>();
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)
Expand Down
63 changes: 52 additions & 11 deletions TiaGitHandler/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ private static void ParseFolder(ProjectFolder folder, string dir, List<string> s
catch
{
}

try
{
var nodes = xmlDoc.SelectNodes("//StructureModified");
Expand Down Expand Up @@ -662,9 +662,21 @@ private static void ParseFolder(ProjectFolder folder, string dir, List<string> 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<XmlNode>())
// {
// 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<XmlNode>())
{
node.ParentNode.RemoveChild(node);
Expand Down Expand Up @@ -729,27 +741,56 @@ private static void ParseFolder(ProjectFolder folder, string dir, List<string> 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;
}
}
}
}
Expand Down
117 changes: 79 additions & 38 deletions TiaImporter/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,66 +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)
try
{
dtFolder.ImportFile(new FileInfo(importFile), true, false);
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);
}
else if (pgFolder != null)
catch (Exception ex)
{
pgFolder.ImportFile(new FileInfo(importFile), true, !importFile.ToLower().EndsWith("xml"));
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error Importing File: " + relativePath + " - " + ex.Message);
}

Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Imported File: " + relativePath);

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);
}

}
}


Expand Down