Esempio n. 1
0
        public bool CurrentProject(TiaPortalProcess tiaProcess, TiaPortal _MyTiaPortal, Project _MyProject)
        {
            bool ProjectStatus;
            IList <TiaPortalProcess> processes = TiaPortal.GetProcesses();

            switch (processes.Count)
            {
            case 1:
                tiaProcess   = processes[0];
                _MyTiaPortal = tiaProcess.Attach();

                if (_MyTiaPortal.Projects.Count <= 0)
                {
                    ProjectStatus = false;
                    return(ProjectStatus);
                }
                _MyProject    = _MyTiaPortal.Projects[0];
                ProjectStatus = true;
                break;

            case 0:
                ProjectStatus = false;
                return(ProjectStatus);

            default:
                ProjectStatus = false;
                return(ProjectStatus);
            }
            return(ProjectStatus);
        }
Esempio n. 2
0
        static void Main()
        {
            Stopwatch sw = new Stopwatch();

            sw.Restart();

            InitTiaPortal();

            try
            {
                var project = CreateProjectIfNotExists(PROJECT_PATH);
                CreateDevices(project);
                CreateSubnet(project);
                LoadSclSources(_plcSoftware);
                InsertHmiPages(_hmiTarget);
                Compile();
                ShowBlockInEditor();
                project.Save();
            }
            finally
            {
                TiaPortalProcess tiaProcess = _tiaPortal.GetCurrentProcess();
                var process = Process.GetProcessById(tiaProcess.Id);
                _tiaPortal.Dispose();
                Console.WriteLine($"Finished in {sw.Elapsed.Minutes}:{sw.Elapsed.Seconds}");
                WindowHelper.BringProcessToFront(process);

                Console.WriteLine();
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey();
            }
        }
Esempio n. 3
0
 private void connectToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if (select_dialog == null)
     {
         select_dialog = new PortalSelect(tiaThread);
     }
     if (select_dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
     {
         TiaPortalProcess proc = select_dialog.selectedProcess();
         if (proc != null)
         {
             WaitConnect wait = new WaitConnect();
             wait.Show();
             Application.DoEvents();
             try
             {
                 tiaPortal = (TiaPortal)tiaThread.RunSync((_) => { return(proc.Attach()); }, null);
                 connectToolStripMenuItem.Enabled = false;
                 btn_connect.Enabled = false;
                 disconnectToolStripMenuItem.Enabled = true;
                 btn_disconnect.Enabled = true;
                 PortalConnected();
             }
             catch (EngineeringException ex)
             {
                 MessageBox.Show("Failed to connect to TIAPortal: " + ex.Message);
             }
             wait.Hide();
             wait.Dispose();
         }
     }
 }
Esempio n. 4
0
        private void btn_ConnectTIA(object sender, EventArgs e)
        {
            btn_Connect.Enabled = false;
            IList <TiaPortalProcess> processes = TiaPortal.GetProcesses();

            switch (processes.Count)
            {
            case 1:
                _tiaProcess = processes[0];
                MyTiaPortal = _tiaProcess.Attach();
                if (MyTiaPortal.GetCurrentProcess().Mode == TiaPortalMode.WithUserInterface)
                {
                    rdb_WithUI.Checked = true;
                }
                else
                {
                    rdb_WithoutUI.Checked = true;
                }


                if (MyTiaPortal.Projects.Count <= 0)
                {
                    txt_Status.Text     = "No TIA Portal Project was found!";
                    btn_Connect.Enabled = true;
                    return;
                }
                MyProject = MyTiaPortal.Projects[0];
                break;

            case 0:
                txt_Status.Text     = "No running instance of TIA Portal was found!";
                btn_Connect.Enabled = true;
                return;

            default:
                txt_Status.Text     = "More than one running instance of TIA Portal was found!";
                btn_Connect.Enabled = true;
                return;
            }
            txt_Status.Text            = _tiaProcess.ProjectPath.ToString();
            btn_Start.Enabled          = false;
            btn_Connect.Enabled        = true;
            btn_Dispose.Enabled        = true;
            btn_CompileHW.Enabled      = true;
            btn_CloseProject.Enabled   = true;
            btn_SearchProject.Enabled  = false;
            btn_Save.Enabled           = true;
            btn_AddHW.Enabled          = true;
            btnCreateProject.Enabled   = false;
            btnExportBlocks.Enabled    = true;
            btnExportTagTables.Enabled = true;
            btnImportBlock.Enabled     = true;
            btnImportTagTables.Enabled = true;
            btnImportUDT.Enabled       = true;
            btnExportUDT.Enabled       = true;
            btnHWConfig.Enabled        = true;
        }
Esempio n. 5
0
        private void btn_Connect_Click(object sender, EventArgs e)
        {
            btn_Connect.Enabled = false;

            switch (processes.Count)
            {
            case 1:
                _tiaProcess = processes[0];
                MyTiaPortal = _tiaProcess.Attach();
                if (MyTiaPortal.GetCurrentProcess().Mode == TiaPortalMode.WithUserInterface)
                {
                    rdb_WithUI.Checked = true;
                }
                else
                {
                    rdb_WithoutUI.Checked = true;
                }
                if (MyTiaPortal.Projects.Count <= 0)
                {
                    txt_Status.Text     = "No TIA Portal Project was found!";
                    btn_Connect.Enabled = true;
                    return;
                }
                MyProject = MyTiaPortal.Projects[0];
                GenerateTreeView();
                tiaLibraryHelper.OpenGlobalLibrary(MyTiaPortal, textBox1);
                break;

            case 0:
                txt_Status.Text     = "No running instance of TIA Portal was found!";
                btn_Connect.Enabled = true;
                return;

            default:
                txt_Status.Text     = "More than one running instance of TIA Portal was found!";
                btn_Connect.Enabled = true;
                return;
            }
            txt_Status.Text           = _tiaProcess.ProjectPath.ToString();
            btn_startTIA.Enabled      = false;
            btn_Connect.Enabled       = true;
            btn_disposeTIA.Enabled    = true;
            btn_CompileHW.Enabled     = true;
            btn_CloseProject.Enabled  = true;
            btn_SearchProject.Enabled = false;
            btn_Save.Enabled          = true;
            btn_AddHW.Enabled         = true;
            btn_deleteChkDev.Enabled  = true;
            btn_refresh.Enabled       = true;
        }
Esempio n. 6
0
        private void btn_ConnectTIA(object sender, EventArgs e)
        {
            btn_Connect.Enabled = false;
            IList <TiaPortalProcess> processes = TiaPortal.GetProcesses();

            switch (processes.Count)
            {
            case 1:
                _tiaProcess = processes[0];
                MyTiaPortal = _tiaProcess.Attach();
                if (MyTiaPortal.GetCurrentProcess().Mode == TiaPortalMode.WithUserInterface)
                {
                    rdb_WithUI.Checked = true;
                }
                else
                {
                    rdb_WithoutUI.Checked = true;
                }
                if (MyTiaPortal.Projects.Count <= 0)
                {
                    SetTextInRichTextBox(Color.Green, "No TIA Portal Project was found!");
                    btn_Connect.Enabled = true;
                    return;
                }
                MyProject = MyTiaPortal.Projects[0];
                break;

            case 0:
                SetTextInRichTextBox(Color.Red, "No running instance of TIA Portal was found!");
                btn_Connect.Enabled = true;
                return;

            default:
                SetTextInRichTextBox(Color.Red, "More than one running instance of TIA Portal was found!");
                btn_Connect.Enabled = true;
                return;
            }
            SetTextInRichTextBox(Color.Green, $"Connected to project\n{_tiaProcess.ProjectPath.ToString()}");
            btn_Start.Enabled         = false;
            btn_Connect.Enabled       = true;
            btn_Dispose.Enabled       = true;
            btn_CloseProject.Enabled  = true;
            btn_SearchProject.Enabled = false;
            btn_Save.Enabled          = true;
        }
Esempio n. 7
0
        private void StartTIA(object sender, EventArgs e)
        {
            if (rdb_WithoutUI.Checked == true)
            {
                MyTiaPortal     = new TiaPortal(TiaPortalMode.WithoutUserInterface);
                txt_Status.Text = "TIA Portal started without user interface";
                _tiaProcess     = TiaPortal.GetProcesses()[0];
            }
            else
            {
                MyTiaPortal     = new TiaPortal(TiaPortalMode.WithUserInterface);
                txt_Status.Text = "TIA Portal started with user interface";
            }

            btn_SearchProject.Enabled = true;
            btn_Dispose.Enabled       = true;
            btn_Start.Enabled         = false;
        }
        private async Task InitializeConnectionWithTiaPortalProject()
        {
            await Task.Run(() =>
            {
                if (!accessAvailable)
                {
                    IList <TiaPortalProcess> processes = TiaPortal.GetProcesses();

                    switch (processes.Count)
                    {
                    case 1:
                        _tiaProcess = processes[0];
                        MyTiaPortal = _tiaProcess.Attach();
                        break;

                    case 0:
                        throw new EngineeringTargetInvocationException(
                            "No running instance of TIA Portal was found!");

                    default:
                        throw new EngineeringTargetInvocationException(
                            "More than one running instance of TIA Portal was found!");
                    }

                    if (MyTiaPortal.Projects.Count <= 0)
                    {
                        throw new EngineeringTargetInvocationException(
                            "No open TIA Portal Project was found!");
                    }

                    MyProject = MyTiaPortal.Projects[0];

                    exclusiveAccess = MyTiaPortal.ExclusiveAccess("My Activity");
                    accessAvailable = true;
                }

                if (!transactionOpen)
                {
                    transaction     = exclusiveAccess.Transaction(MyProject, "No changes commited.");
                    transactionOpen = true;
                }
            }).ConfigureAwait(true);
        }
Esempio n. 9
0
 public ProcItem(TiaPortalProcess proc)
 {
     this.proc = proc;
 }
Esempio n. 10
0
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // if no project is open
            if (project == null)
            {
                // see if we have a open instance and use the first one
                foreach (TiaPortalProcess tiaPortalProcess in TiaPortal.GetProcesses())
                {
                    TiaPortalProcess p = TiaPortal.GetProcess(tiaPortalProcess.Id);

                    tiaPortal = p.Attach();
                    break;
                }

                // we don't habe an instance then open a new instance
                if (tiaPortal == null)
                {
                    tiaPortal = new TiaPortal(TiaPortalMode.WithUserInterface);
                }

                tiaPortal.Notification += TiaPortal_Notification;
                tiaPortal.Confirmation += TiaPortal_Confirmation;

                // let's get the projects
                ProjectComposition projects = tiaPortal.Projects;

                // no open project - then open file dialog
                if (projects.Count == 0)
                {
                    string p = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;


                    string filePath = (string)Properties.Settings.Default["PathOpenProject"];
                    if (filePath == string.Empty)
                    {
                        filePath = Application.StartupPath;
                    }

                    using (OpenFileDialog openFileDialog = new OpenFileDialog())
                    {
                        openFileDialog.Filter           = "V16 project files (*.ap16)|*.ap16|All files (*.*)|*.*";
                        openFileDialog.FilterIndex      = 1;
                        openFileDialog.InitialDirectory = filePath;

                        if (openFileDialog.ShowDialog() == DialogResult.OK)
                        {
                            Properties.Settings.Default["PathOpenProject"] = openFileDialog.FileName;
                            Properties.Settings.Default.Save();

                            // load projectpath
                            FileInfo projectPath = new FileInfo(openFileDialog.FileName);
                            try
                            {
                                project = projects.Open(projectPath);
                            }
                            catch (Exception)
                            {
                                Console.WriteLine(String.Format("Could not open project {0}", projectPath.FullName));
                                Application.Exit();
                            }
                        }
                        else
                        {
                            return; // no file selected
                        }
                    }
                }
                else
                {
                    // for now we use the first project
                    project = tiaPortal.Projects[0];
                }

                txtProject.Text = "Project: " + project.Name;

                // loop through the data
                IterateThroughDevices(project);
            }
        }
Esempio n. 11
0
        // connect to existing project
        // and read project library and devices
        private void Btn_ConnectProject_Click(object sender, EventArgs e)
        {
            // Project connection
            IList <TiaPortalProcess> processes = TiaPortal.GetProcesses();

            statusBox.AppendText("Attempting to connect..." + processes.Count + " processes found");
            statusBox.AppendText(Environment.NewLine);

            switch (processes.Count)
            {
            case 1:
                _tiaProcess = processes[0];
                MyTiaPortal = _tiaProcess.Attach();

                if (MyTiaPortal.Projects.Count <= 0)
                {
                    statusBox.AppendText("No TIA Portal Project was found!");
                    statusBox.AppendText(Environment.NewLine);
                    return;
                }
                statusBox.AppendText("Connected to running instance of TIA Portal");
                statusBox.AppendText(Environment.NewLine);
                MyProject = MyTiaPortal.Projects[0];
                break;

            case 0:
                statusBox.AppendText("No running instance of TIA Portal was found!");
                statusBox.AppendText(Environment.NewLine);
                return;

            default:
                statusBox.AppendText("More than one running instance of TIA Portal was found!");
                statusBox.AppendText(Environment.NewLine);
                return;
            }
            // Disable list boxes updating before adding new elements
            projectLibraryCheckList.BeginUpdate();
            devicesCheckList.BeginUpdate();
            blocksCheckList.BeginUpdate();

            // Read project Devices and add them to check list
            foreach (var device in MyProject.Devices)
            {
                if (device.TypeIdentifier != "System:Device.PC")
                {
                    devicesCheckList.Items.Add(device.Name);
                }
            }

            // Read project library master copies names and add them to check list
            List <String> libMasterCopies = BlockManagement.ReadProjectLibrary(MyProject.ProjectLibrary.MasterCopyFolder, "");

            foreach (string entry in libMasterCopies)
            {
                projectLibraryCheckList.Items.Add(entry);
            }

            // Read plc blocks and add them to check list
            // TO DO - Move this code into a function
            // device represents the rack
            // first element of DeviceItems (modules in the rack) is the plc
            PlcSoftware firstPlcSoftware = BlockManagement.GetSoftwareFrom(MyProject.Devices[0].DeviceItems[1]);

            List <String> blocks = new List <string>();

            if (firstPlcSoftware != null)
            {
                // Type of BlockGroup is PlcBlockSystemGroup is not compatible with type of
                // Group that is PlcBlockUserGroup so the same functions can't be applied in both cases
                // that's the reason for the exception when reading the blocks in root folder
                foreach (var block in firstPlcSoftware.BlockGroup.Blocks)
                {
                    string blockType = block.GetType().ToString();
                    blockType = blockType.Substring(blockType.LastIndexOf('.') + 1);

                    blocks.Add(block.Name + " - " + blockType + " - " + block.Number);
                }

                foreach (var group in firstPlcSoftware.BlockGroup.Groups)
                {
                    blocks.AddRange(BlockManagement.ReadBlocks(group));
                }

                blocksCheckList.Items.AddRange(blocks.ToArray());
            }

            // Enable list boxex updating after elements added
            projectLibraryCheckList.EndUpdate();
            devicesCheckList.EndUpdate();
            blocksCheckList.EndUpdate();
        }
Esempio n. 12
0
 public ProcItem(TiaPortalProcess proc)
 {
     this.proc = proc;
 }
Esempio n. 13
0
        // open project
        private void btnOpen_Click(object sender, EventArgs e)
        {
            // if no project is open
            if (project == null)
            {
                // see if we have a open instance and use the first one
                foreach (TiaPortalProcess tiaPortalProcess in TiaPortal.GetProcesses())
                {
                    TiaPortalProcess p = TiaPortal.GetProcess(tiaPortalProcess.Id);

                    tiaPortal = p.Attach();
                    Console.WriteLine("TIA Portal has been attached");
                    break;
                }

                // we don't habe an instance then open a new instance
                if (tiaPortal == null)
                {
                    tiaPortal = new TiaPortal(TiaPortalMode.WithUserInterface);
                    Console.WriteLine("TIA Portal has started");
                }

                tiaPortal.Confirmation += TiaPortal_Confirmation;
                tiaPortal.Notification += TiaPortal_Notification;

                // let's get the projects
                ProjectComposition projects = tiaPortal.Projects;

                // projects available ?
                Console.WriteLine("TIA projects " + projects.Count.ToString());

                // no open project - then open file dialog
                if (projects.Count == 0)
                {
                    Console.WriteLine("Opening Project...");

                    string filePath = string.Empty;
                    using (OpenFileDialog openFileDialog = new OpenFileDialog())
                    {
                        openFileDialog.Filter           = "V16 project files (*.ap16)|*.ap16|All files (*.*)|*.*";
                        openFileDialog.FilterIndex      = 1;
                        openFileDialog.RestoreDirectory = true;

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

                            // load projectpath
                            FileInfo projectPath = new FileInfo(filePath);
                            try
                            {
                                project = projects.Open(projectPath);
                            }
                            catch (Exception)
                            {
                                Console.WriteLine(String.Format("Could not open project {0}", projectPath.FullName));
                                Application.Exit();
                            }
                        }
                        else
                        {
                            return; // no file selected
                        }
                    }
                }
                else
                {
                    // for now we use the first project
                    project = tiaPortal.Projects[0];
                }

                //Console.WriteLine(String.Format("Project {0} is open", project.Path.FullName));
                txtProject.Text = "Project: " + project.Name;

                // loop through the data
                IterateThroughDevices(project);
            }
        }
Esempio n. 14
0
 public ProcItem(TiaPortalProcess proc, string path)
 {
     this.proc = proc;
     this.path = path;
 }