Exemple #1
0
        static void Main(string[] args)
        {
            bool   hasArgs    = args.Count() > 0;
            string file       = "";
            string exportPath = "";
            string user       = Settings.Default.DefaultUser;
            string password   = Settings.Default.DefaultPassword;


            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;

                if (object.Equals(res, false))
                {
                    OpenFileDialog op = new OpenFileDialog();
                    op.Filter          = "TIA-Portal Project|*.ap13;*.ap14;*.ap15;*.ap15_1";
                    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")
                    {
                        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)
                {
                    user = args[1];
                }
                if (args.Length > 2)
                {
                    password = args[2];
                }
            }

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

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