Form1

16.5. Finisirano do subneta
mail@pastecode.io avatar
unknown
csharp
a year ago
16 kB
0
Indexable
Never
using Microsoft.VisualBasic;
using Siemens.Engineering;
using Siemens.Engineering.HW;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using TIAManagement;
using static Controller;
using View = Siemens.Engineering.HW.View;

namespace TIAGenerator
{
    public partial class Form1 : Form
    {
        readonly TiaApplication tiaApp = new TiaApplication();
        //ConnectionBuilder connection = new ConnectionBuilder();

        public static string projectName;
        public static string _textPath;
        public string mytext;
        public int counter= 0;
        public int counterSubnet = 0;
        public static StreamReader streamReader2;
        public string stringWithMultipleSpaces;
        public static StreamReader streamReader3;
        public static StreamReader streamReader4;
        public bool isGsd = false;

        public IList<Device> subDevice = null;
        public IList<Device> Devices = null;
  

        public Form1()
        {
            InitializeComponent();
         
        }
        public void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "Opening TIA Portal...";
            tiaApp.OpenTiaPortal();
            tiaApp.SetInstanceID();

            tiaIDTextBox.Text = tiaApp.GetInstanceID().ToString();
            textBox1.Text = "TIA Portal is running...";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            tiaApp.DisposeTiaPortal();
            tiaIDTextBox.Text = "0";

            IList<TiaPortalProcess> tiaPortalProcesses = TiaPortal.GetProcesses();
            foreach (TiaPortalProcess tiaPortalProcess in tiaPortalProcesses)
            {
                foreach (TiaPortalSession session in tiaPortalProcess.AttachedSessions)
                {
                    tiaIDTextBox.Text = session.ProcessId.ToString();
                    tiaIDTextBox.Text = "0";
                }
            }
            textBox1.Text = "Disposed";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            projectName = Interaction.InputBox("Type project name", "text", "", 10, 20);
            tiaApp.AddNewProject(projectName);
            
            textBox1.Text = "New project added";

        }

        private void button4_Click(object sender, EventArgs e)
        {
            //if (_projectPath.ShowDialog() == DialogResult.OK)
            //{
            //    textBox1.Text = _projectPath.FileName;
            //    //Get the path of specified file
            //    filePath = _projectPath.FileName;

            //    //Read the contents of the file into a stream
            //    var fileStream = _projectPath.OpenFile();
            //}
            tiaApp.OpenExistingProject();
            tiaApp.SetInstanceID();
            tiaApp.project.ShowHwEditor(View.Topology);
            
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //tiaApp.CloseProject(); // dodat save i nazvat save and close
            //tiaApp.CreateGlobalSubnet();
        }

        private void AddNewGSD_Click(object sender, EventArgs e)
        {
            //tiaApp.CreateGsdDevice("GSD NAME");

            //Plc plc = new Plc(CpuType.S71200, "192.168.0.10", 0, 0);  //namjestit adresu  //razlicite IP adrese, Name device
            //plc.Open();
            textBox1.Text = "New GSD device added";
        }

        private void AddNewHMI_Click(object sender, EventArgs e)
        {
            tiaApp.CreateHMI("HMI NAME");
            textBox1.Text = "New HMI device added";
        }

        private void Import_Connections_Click(object sender, EventArgs e)
        {
            textBox1.Text = "Open a text file";

            OpenFileDialog openFileDialog3 = new OpenFileDialog
            {
                InitialDirectory = @"D:\",
                Title = "Browse Tia Portal Text Files",

                CheckFileExists = true,
                CheckPathExists = true,

                DefaultExt = "txt",
                Filter = "text files (*.txt)|*.txt",
                FilterIndex = 2,
                RestoreDirectory = true,

                ReadOnlyChecked = true,
                ShowReadOnly = true
            };

            if (openFileDialog3.ShowDialog() == DialogResult.OK)
            {
                //Get the path of specified file
                _textPath = openFileDialog3.FileName;

                //FileInfo textPath = new FileInfo(_textPath);

                MessageBox.Show(_textPath);
            }
            try
            {
                streamReader3 = new StreamReader(_textPath); //get the file
                stringWithMultipleSpaces = streamReader3.ReadToEnd(); //load file to string
                stringWithMultipleSpaces = stringWithMultipleSpaces.Replace(Environment.NewLine, Environment.NewLine + "  ");
                stringWithMultipleSpaces = Regex.Replace(stringWithMultipleSpaces, @"\r\n?|\n", "");
                streamReader3.Close();

            }
            catch (Exception)

            {
                MessageBox.Show("Open TIA first!");
            }
            if (streamReader3 != null)
            {



                Regex r2 = new Regex("  +"); //specify delimiter (spaces)
                string[] words = r2.Split(stringWithMultipleSpaces); //(convert string to array of words)
                ArrayList list = new ArrayList();
                foreach (System.String W in words)
                {
                    /*list.Add(W);*/                                  //poslagat vrijednosti u multidimenziomalnu/arraylist matricu, a onda citat red po red i izvrsavat
                    switch (counter)
                    {
                    //    case 0:
                    //        ConnectionBuilder connection = new ConnectionBuilder(W);
                    //        counter++;
                    //        break;

                    //    case 1:
                    //        connection.WithConnectionType(W); //ethernet  i sl
                    //                                          //mastercopy za blokove varijabli, video 012
                    //        counter++;                                                         //update: necemo radit blokove varijabli

                    //        break;

                    //    case 2:
                    //        connection.connectionType = W;

                    //        counter++;
                    //        break;

                    //    case 3:
                    //        connection.sourceDevice = W;

                    //        counter++;
                    //        break;

                    //    case 4:
                    //        connection.sourcePort = W;

                    //        counter++;
                    //        break;

                    //    case 5:
                    //        connection.destinationDevice = W;

                    //        counter++;
                    //        break;
                    //    case 6:
                    //        connection.destinationPort = W;

                    //        counter++;
                    //        break;

                    //    case 7:
                    //        connection.Build();


                    }

                }
            }


        }

        private void button6_Click(object sender, EventArgs e)
        {
            textBox1.Text = "Open a text file";

            OpenFileDialog openFileDialog2 = new OpenFileDialog
            {
                InitialDirectory = @"D:\",
                Title = "Browse Tia Portal Text Files",

                CheckFileExists = true,
                CheckPathExists = true,

                DefaultExt = "txt",
                Filter = "text files (*.txt)|*.txt",
                FilterIndex = 2,
                RestoreDirectory = true,

                ReadOnlyChecked = true,
                ShowReadOnly = true
            };

            if (openFileDialog2.ShowDialog() == DialogResult.OK)
            {
                //Get the path of specified file
                _textPath = openFileDialog2.FileName;

                //FileInfo textPath = new FileInfo(_textPath);

                MessageBox.Show(_textPath);
            }
            
            try 
            {
                streamReader2 = new StreamReader(_textPath); //get the file
                stringWithMultipleSpaces = streamReader2.ReadToEnd(); //load file to string
                stringWithMultipleSpaces = stringWithMultipleSpaces.Replace(Environment.NewLine, Environment.NewLine + "  ");
                stringWithMultipleSpaces = Regex.Replace(stringWithMultipleSpaces, @"\r\n?|\n", "");
                streamReader2.Close();

            } 
            catch (Exception)
            
            {
                MessageBox.Show("Open TIA first!");
            }

            if (streamReader2 != null)
            {



                Regex r = new Regex("  +"); //specify delimiter (spaces)
                string[] words = r.Split(stringWithMultipleSpaces); //(convert string to array of words)
                ArrayList list = new ArrayList();
                foreach (System.String W in words)
                {
                    /*list.Add(W);*/                                  //poslagat vrijednosti u multidimenziomalnu/arraylist matricu, a onda citat red po red i izvrsavat
                    switch (counter)
                    {
                        case 0:
                            tiaApp.getDeviceVersion = W;
                            counter++;
                            break;

                        case 1:
                            tiaApp.getDeviceOrderNumber = W;                                   //mastercopy za blokove varijabli, video 012
                            counter++;                                                         //update: necemo radit blokove varijabli

                            break;

                        case 2:
                            tiaApp.getDeviceName = W;

                            if (tiaApp.getDeviceName.Contains("Plc"))
                            {
                               // MessageBox.Show("Ide preko if plc");
                                tiaApp.plc = true;
                                isGsd = false;
                            }
                            else if (tiaApp.getDeviceName.Contains("HMI"))
                            {
                               // MessageBox.Show("Ide preko else if hmi");
                                tiaApp.plc = false;
                                isGsd = false;
                            }
                            else if (tiaApp.getDeviceName.Contains("Gsd"))
                            {
                                tiaApp.plc = false;
                                isGsd = true;
                               // MessageBox.Show("Ide preko else");
                            }

                            counter++;
                            break;

                        case 3:
                            tiaApp.getSubnetMask = W;

                            counter++;
                            break;

                        case 4:
                            tiaApp.getDeviceIpAddress = W;

                            if (isGsd == true)
                            {
                                tiaApp.CreateGsdDevice();
                            }
                            else if (isGsd == false)
                            {
                                tiaApp.CreateDevice();
                            }
                            

                            
                            tiaApp.networkInterface = tiaApp.FindNetworkInterface(tiaApp.project.Devices.Last().DeviceItems);
                            

                            if (tiaApp.plc == true)
                            {
                                tiaApp.devices = tiaApp.project.Devices.Last();

                                //MessageBox.Show(tiaApp.project.Devices.Last().ToString());
                                tiaApp.GetPlcSoftware(tiaApp.project.Devices.Last());

                                    tiaApp.project.ShowHwEditor(View.Network);
                                    
                                    tiaApp.ConnectSubnet();
                                //tiaApp.FillIoController(tiaApp.project.Devices.Last(), );
                                //if (tiaApp.FindRail(tiaApp.project.Devices.Last().DeviceItems).CanPlugNew("OrderNumber:6ES7 521-1BH00-0AB0", "DI 16x24VDC HF_1", 4))
                                //{
                                //    DeviceItem newPluggedDeviceItem = tiaApp.project.Devices.Last().PlugNew("OrderNumber:6ES7 521-1BH00-0AB0", "DI 16x24VDC HF_1", 4);
                                //} 
                                //else
                                //{
                                //    MessageBox.Show("False na CanPlugNew");
                                //}

                            }
                            if (tiaApp.plc == false && isGsd == false)
                            {
                                tiaApp.hmiDevice = tiaApp.project.Devices.Last();
                                
                                tiaApp.GetPlcSoftware(tiaApp.project.Devices.Last());
                                
                                tiaApp.networkInterface.Nodes[0].SetAttribute("Address", tiaApp.getDeviceIpAddress);
                                tiaApp.networkInterface.Nodes[0].SetAttribute("SubnetMask", tiaApp.getSubnetMask);

                                tiaApp.project.ShowHwEditor(View.Network);
                                tiaApp.ConnectSubnet();
                                
                             }
                             if (isGsd == true)
                            {
                                tiaApp.networkInterface.Nodes[0].SetAttribute("Address", tiaApp.getDeviceIpAddress);
                                tiaApp.networkInterface.Nodes[0].SetAttribute("SubnetMask", tiaApp.getSubnetMask);
                                tiaApp.ConnectSubnet();
                                
                            }
                             

                            counter = 0;

                            textBox1.Text = "Device" + tiaApp.getDeviceName + "has been succesfully added";

                            break;
                        default:
  
                            counter = 0;
                            
                            break;
                    }

                    textBox1.Text = "Device(s) have been succesfully added";
                    
                }
                tiaApp.project.ShowHwEditor(View.Network);

            }

            //foreach (object item in list)
            //{
            //   mytext += item.ToString() + Environment.NewLine;    //ispis za pregled varijabli
            //}

            //textBox1.Text = mytext;


        }

        private void Import_IO_Cards_Click(object sender, EventArgs e)
        {

        }
    }
}