public static void OpenProjectOnly(string ProjectPath, ref TiaPortal tiaPortal, ref Project project) { Console.WriteLine("Starting TIA Portal"); try { tiaPortal = new TiaPortal(TiaPortalMode.WithoutUserInterface); } catch (Exception e) { Program.CancelGeneration("Could not start TIAP.", e); return; } Console.WriteLine("TIA Portal has started"); ProjectComposition projects = tiaPortal.Projects; Console.WriteLine("Opening Project..."); FileInfo projectPath = new FileInfo(ProjectPath); //edit the path according to your project //Project project = null; try { project = projects.Open(projectPath); } catch (Exception e) { Program.CancelGeneration("Could not open project " + projectPath.FullName, e); return; } }
private void btn_NewProject_Click(object sender, EventArgs e) { //TiaPortal tiaPortal = new TiaPortal(); ProjectComposition projectComposition = MyTiaPortal.Projects; DirectoryInfo targetDirectory = new DirectoryInfo(TiaProjectPath); Project project = projectComposition.Create(targetDirectory, TiaProjectName); txt_Status.Text = "New Project generated"; }
private void btnCreateProject_Click(object sender, EventArgs e) { try { TiaPortal tiaPortal = MyTiaPortal; ProjectComposition projectComposition = tiaPortal.Projects; DirectoryInfo targetDirectory = new DirectoryInfo(@"C:\TiaProjects"); Project project = projectComposition.Create(targetDirectory, txtProjectName.Text); txt_Status.Text = "Project created !"; } catch (Exception ex) { txt_Status.Text = "Error while creating project, make sure you closed all projects before creating new one" + ex.Message; } }
//public static object DeviceMethods { get; private set; } //public static int Counter = 0; // static string projectVersion = "15_1"; static void Main(string[] args) { Console.WriteLine("Starting TIA Portal..."); //using (TiaPortal tiaPortal = new TiaPortal(TiaPortalMode.WithoutUserInterface)) using (TiaPortal tiaPortal = new TiaPortal(TiaPortalMode.WithUserInterface)) { Console.WriteLine("TIA Portal has started"); ProjectComposition projects = tiaPortal.Projects; Console.WriteLine("Opening Project..."); string path = @"C:\Users\CT-EVO\Desktop\Project2\Project2.ap16"; FileInfo projectPath = new FileInfo(path); Project myProject = null; try { myProject = projects.Open(projectPath); } catch (Exception) { Console.WriteLine(String.Format("Could not open project {0}", projectPath.FullName)); Console.WriteLine("Demo complete hit enter to exit"); } MainMethods.DisplayNetwork(myProject); Server.RunServer(myProject); //server is running MainMethods.DisplayNetwork(myProject); DeviceMethods.DisplayDeviceNames(myProject); MainMethods.DisplayNetwork(myProject); // necessary for withUserInterface mode //Console.WriteLine("Creating Project..."); //Project MyProject = tiaPortal.Projects.Create(new DirectoryInfo(Path.GetDirectoryName(Application.ExecutablePath)), Guid.NewGuid().ToString()); } }
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); } }
// 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); } }
public static void OpenProject(string ProjectPath, ref TiaPortal tiaPortal, ref Project project) { //First check if project already open foreach (TiaPortalProcess tiaPortalProcess in TiaPortal.GetProcesses()) { string currentProjectPath = ((tiaPortalProcess.ProjectPath != null && !string.IsNullOrEmpty(tiaPortalProcess.ProjectPath.FullName)) ? Path.GetFullPath(tiaPortalProcess.ProjectPath.FullName) : "None"); Console.WriteLine("tiaPortalProcess: " + tiaPortalProcess.Mode); Console.WriteLine("ProjectPath: " + currentProjectPath); Console.WriteLine(""); //string currentProject = ((tiaPortalProcess.ProjectPath != null && !string.IsNullOrEmpty(tiaPortalProcess.ProjectPath.FullName)) ? tiaPortalProcess.ProjectPath.FullName : "None"); if (currentProjectPath == ProjectPath) { Console.WriteLine("Attaching to TIA Portal"); try { tiaPortal = tiaPortalProcess.Attach(); project = tiaPortal.Projects[0]; return; } catch (Siemens.Engineering.EngineeringSecurityException e) { tiaPortal = null; project = null; Program.CancelGeneration("Could not start TIAP. Please achnoledge the \"Openness access\" security dialog box with [Yes] or [Yes to all] to grant access or if there was no dialog box add the user to the user group \"Siemens TIA Openness\". (see exception message below for details)", e); return; } catch (Exception e) { Program.CancelGeneration("Could not attach to running TIAP with open project.", e); project = null; } } } if ((tiaPortal == null) || (project == null)) { Console.WriteLine("Starting TIA Portal"); try { tiaPortal = new TiaPortal(TiaPortalMode.WithoutUserInterface); } catch (Siemens.Engineering.EngineeringSecurityException e) //-2146233088 { tiaPortal = null; project = null; Program.CancelGeneration("Could not start TIAP. Please achnoledge the \"Openness access\" security dialog box with [Yes] or [Yes to all] to grant access or if there was no dialog box add the user to the user group \"Siemens TIA Openness\". (see exception message below for details)", e); return; } catch (Exception e) { Program.CancelGeneration("Could not start TIAP.", e); return; } Console.WriteLine("TIA Portal has started"); ProjectComposition projects = tiaPortal.Projects; Console.WriteLine("Opening Project..."); FileInfo projectPath = new FileInfo(ProjectPath); //edit the path according to your project //Project project = null; try { project = projects.Open(projectPath); } catch (Exception e) { Program.CancelGeneration("Could not open project " + projectPath.FullName, e); return; } } }