static void Main(string[] args) { Project prj = null; string file = ""; string folderToImport = ""; string baseTiaFld = ""; try { file = args[0]; } catch (Exception e) { Console.WriteLine(e); } try { folderToImport = args[1]; } catch (Exception e) { Console.WriteLine(e); } try { baseTiaFld = args[2]; } catch (Exception e) { Console.WriteLine(e); } var extension = "xml"; try { if (args.Length > 3) { extension = args[3]; } } catch (Exception e) { Console.WriteLine(e); } if (file.EndsWith("16")) { prj = Projects.AttachToInstanceWithFilename("16", file); } else { prj = Projects.AttachToInstanceWithFilename("15.1", file); } //var prjV151 = prj as Step7ProjectV15_1; var projectFolder = prj.ProjectStructure; var flds = baseTiaFld.Split('/'); foreach (var s in flds) { projectFolder = projectFolder.SubItems.First(x => x.Name == s); } var programFolderToCompile = projectFolder; var pgFolderToCompile = programFolderToCompile as ITIAOpennessProgramFolder; int i = 0; var fileList = BuildImportFileList(folderToImport, extension).ToList(); var count = 0; ImportFiles(); void ImportFiles() { while (count != fileList.Count() && fileList.Count() > 0) { Console.WriteLine("-----------------------------------"); Console.WriteLine("Durchlauf " + ++i); Console.WriteLine("-----------------------------------"); Console.WriteLine(); count = fileList.Count(); foreach (var importFile in fileList.ToList()) { var relativePath = importFile.Substring(folderToImport.Length + 1); var importFolder = projectFolder; foreach (var p in relativePath.Split('\\').Reverse().Skip(1).Reverse()) { 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 ITIAOpennessPlcDatatypeFolder; var pgFolder = importFolder as ITIAOpennessProgramFolder; if (dtFolder != null) { dtFolder.ImportFile(new FileInfo(importFile), true, false); } else if (pgFolder != null) { //if (importFile.ToLower().EndsWith("scl")) //{ // string srcBeforeScl = null; // var blkInfo = pgFolder.BlockInfos.FirstOrDefault(x => x.Name.ToLower() == Path.GetFileNameWithoutExtension(importFile).ToLower()); // if (blkInfo != null) // { // Console.WriteLine("SCL File with XML File: " + relativePath); // Console.WriteLine("Export current XML..."); // srcBeforeScl = blkInfo.Export(ExportFormat.Xml); // Console.WriteLine("Import SCL..."); // } // pgFolder.ImportFile(new FileInfo(importFile), true, !importFile.ToLower().EndsWith("xml")); // if (blkInfo != null) // { // Console.WriteLine("Export new XML..."); // var blkAfter = pgFolder.BlockInfos.FirstOrDefault(x => x.Name.ToLower() == Path.GetFileNameWithoutExtension(importFile).ToLower()); // var srcAfter = blkAfter.Export(ExportFormat.Xml); // Console.WriteLine("Import combined XML..."); // } //} //else { 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); } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Error Importing File: " + relativePath + " - " + ex.Message); } } } } if (fileList.Count() != 0 && extension == "xml") { 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); } } }
static void Main(string[] args) { bool hasArgs = args.Count() > 0; string file = ""; string exportPath = ""; string user = Settings.Default.DefaultUser; string password = Settings.Default.DefaultPassword; bool attach = false; Project prj = null; if (!hasArgs) { Application app = new Application(); var ask = new AskOpen(); app.Run(ask); var res = ask.Result; resetSetpoints = ask.chkResetSetpoints.IsChecked == true; removeCodeFromXml = ask.chkRemoveCode.IsChecked == true; removeAllBlanks = ask.rbRemoveAllBlanks.IsChecked == true; removeOnlyOneBlank = ask.rbRemoveOnlyOneBlank.IsChecked == true; removeNoBlanks = ask.rbRemoveNoBlanks.IsChecked == true; DisableQuickEdit(); if (object.Equals(res, false)) { OpenFileDialog op = new OpenFileDialog(); op.Filter = "TIA-Portal Project|*.ap13;*.ap14;*.ap15;*.ap15_1;*.ap16"; op.CheckFileExists = false; op.ValidateNames = false; var ret = op.ShowDialog(); if (ret == true) { file = op.FileName; } else { Console.WriteLine("Bitte S7 projekt als Parameter angeben!"); return; } if (Path.GetExtension(file) == ".ap15_1" || Path.GetExtension(file) == ".ap16") { if (InputBox.Show("Credentials", "Enter Username (or cancel if not used)", ref user) != DialogResult.Cancel) { if (InputBox.Show("Credentials", "Enter Password", ref password) != DialogResult.Cancel) { } else { user = ""; password = ""; } } else { user = ""; password = ""; } } exportPath = Path.GetDirectoryName(file); exportPath = Path.GetFullPath(Path.Combine(exportPath, "..\\Export")); } else if (res != null) { var ver = ask.Result as string; prj = Projects.AttachProject(ver); exportPath = Path.GetDirectoryName(prj.ProjectFile); exportPath = Path.GetFullPath(Path.Combine(exportPath, "..\\Export")); } else { Environment.Exit(0); } if (Directory.Exists(exportPath)) { if ( MessageBox.Show(exportPath + " wird gelöscht. Möchten Sie fortfahren?", "Sicherheitsabfrage", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { DeleteDirectory(exportPath); } else { Environment.Exit(-1); } } Directory.CreateDirectory(exportPath); } else { file = args[0]; if (args.Length > 1) { attach = bool.Parse(args[1]); } if (args.Length > 2) { removeCodeFromXml = bool.Parse(args[2]); } if (args.Length > 3) { user = args[3]; } if (args.Length > 4) { password = args[4]; } } if (prj == null) { Credentials credentials = null; if (!string.IsNullOrEmpty(user) && !string.IsNullOrEmpty(password)) { credentials = new Credentials() { Username = user, Password = new SecureString() }; foreach (char c in password) { credentials.Password.AppendChar(c); } } if (attach) { if (file.EndsWith("16")) { prj = Projects.AttachToInstanceWithFilename("16", file); } else { prj = Projects.AttachToInstanceWithFilename("15.1", file); } } else { prj = Projects.LoadProject(file, false, credentials); } } _projectType = prj.ProjectType; Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Opened Project - " + prj.ProjectType.ToString()); Console.WriteLine("Exporting to Folder: " + exportPath); Console.WriteLine(); List <string> skippedBlocksList = new List <string>(); ParseFolder(prj.ProjectStructure, exportPath, skippedBlocksList); Console.WriteLine(); Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Red; skippedBlocksList.ForEach(i => Console.WriteLine("{0}", i)); Console.WriteLine(); Console.WriteLine(skippedBlocksList.Count() + " blocks were skipped"); if (!hasArgs) { Console.ReadKey(); } }