private void blockTree_AfterSelect(object sender, TreeViewEventArgs e) { bool locked = false; Monitor.TryEnter(portal, 100, ref locked); if (!locked) { return; } try { TreeNode node = blockTree.SelectedNode; if (node.Tag is IEngineeringObject) { attrList.Items.Clear(); IEngineeringObject ie = node.Tag as IEngineeringObject; foreach (EngineeringAttributeInfo ai in ie.GetAttributeInfos()) { Object value = ie.GetAttribute(ai.Name); String valStr; if (value != null) { valStr = value.ToString(); } else { valStr = "(null)"; } attrList.Items.Add(new ListViewItem(new String[] { ai.Name, valStr })); } } if (node.Tag is Siemens.Engineering.SW.Blocks.PlcBlock || node.Tag is Siemens.Engineering.Hmi.Screen.Screen || node.Tag is Siemens.Engineering.Hmi.Screen.ScreenTemplate || node.Tag is Siemens.Engineering.Hmi.Screen.ScreenPopup) { ExportBtn.Enabled = true; } else { ExportBtn.Enabled = false; } if (node.Tag is Siemens.Engineering.SW.Blocks.PlcBlockGroup || node.Tag is Siemens.Engineering.Hmi.Screen.ScreenFolder || node.Tag is Siemens.Engineering.Hmi.Screen.ScreenTemplateFolder || node.Tag is Siemens.Engineering.Hmi.Screen.ScreenPopupFolder) { ImportBtn.Enabled = true; } else { ImportBtn.Enabled = false; } } finally { Monitor.Exit(portal); } }
/// <summary>Imports a folder structure into the given destination</summary> /// <param name="targetLocation">TIA object under which the structure will be imported</param> /// <param name="folderPath">Path to the folder to import</param> /// <param name="importOption">TIA import options</param> /// <exception cref="System.ArgumentNullException">Parameter is null;targetLocation</exception> /// <exception cref="System.ArgumentException">Parameter is null or empty;folderPath</exception> /// <exception cref="System.IO.IOException"></exception> /// <exception cref="System.UnauthorizedAccessException"></exception> /// <exception cref="System.IO.DirectoryNotFoundException"></exception> private static void ImportStructure(IEngineeringObject targetLocation, string folderPath, ImportOptions importOption) { if (targetLocation == null) { throw new ArgumentNullException(nameof(targetLocation), "Parameter is null"); } if (string.IsNullOrEmpty(folderPath)) { throw new ArgumentException("Parameter is null or empty", nameof(folderPath)); } var files = Directory.GetFiles(folderPath); var dirs = Directory.GetDirectories(folderPath); foreach (var file in files) { ImportItem(targetLocation, file, importOption); } foreach (var dir in dirs) { var subDir = GetFolderByName(targetLocation, Path.GetFileName(dir)); ImportStructure(subDir as IEngineeringObject, dir, importOption); } }
public static bool SetAttribute(IEngineeringObject aIEngineeringObject, AttributeInfo aAttributeInfo) { if ((aIEngineeringObject != null) && (aAttributeInfo.Value != null)) { try { aIEngineeringObject.SetAttribute(aAttributeInfo.Name, aAttributeInfo.Value); return(true); } catch (Exception ex) { Program.FaultMessage("Could not set Attribute", ex, "AttributeValue.SetAttribute"); } } return(false); }
public static AttributeValue GetAttribute(IEngineeringObject aIEngineeringObject, string aAttributeName) { if (aIEngineeringObject != null) { try { object attributeValue = aIEngineeringObject.GetAttribute(aAttributeName); AttributeValue newItem = new AttributeValue(attributeValue); //newItem.Value = attributeValue; return(newItem); } catch (EngineeringNotSupportedException) { } catch (Exception ex) { Program.FaultMessage("Could not get Attribute", ex, "AttributeValue.GetAttribute"); } } return(null); }
protected override void DoWork() { lock (portal) { try { Stack <string> group_names = new Stack <string>(); IEngineeringObject obj = fromBlock.Parent; while (obj is PlcBlockUserGroup) { group_names.Push(((PlcBlockGroup)obj).Name); obj = obj.Parent; } PlcBlockGroup group = toPlc.BlockGroup; foreach (string group_name in group_names) { PlcBlockUserGroup child = group.Groups.Find(group_name); if (child != null) { group = child; } else { group = group.Groups.Create(group_name); } } FileInfo file = TempFile.File("export_block_", "xml"); fromBlock.Export(file, ExportOptions.None); group.Blocks.Import(file, overwrite ? ImportOptions.Override : ImportOptions.None); } catch (Exception ex) { LogMessage(MessageLog.Severity.Error, "Failed to copy from " + fromBlock.Name + " to " + toPlc.Name + ":\n" + ex.Message); return; } } }
public static AttributeInfo GetAttributeInfo(IEngineeringObject aEngineeringObject, string aAttributeName) { if (aEngineeringObject != null) { try { object attributeValue = aEngineeringObject.GetAttribute(aAttributeName); AttributeInfo returnObject = new AttributeInfo { Name = aAttributeName, Value = attributeValue }; return(returnObject); } catch (EngineeringNotSupportedException) { } catch (Exception ex) { Program.FaultMessage("Could not get Attribute", ex, "AttributeValue.GetAttributeInfo"); } } return(null); }
/// <summary> /// Exports the given object with the give options to the defined path. /// </summary> /// <param name="exportItem">Object to export</param> /// <param name="exportOption">Export options</param> /// <param name="exportPath">Folder path in which to export</param> /// <returns>String</returns> /// <exception cref="System.ArgumentNullException">Parameter is null;exportItem</exception> /// <exception cref="System.ArgumentException">Parameter is null or empty;exportPath</exception> /// <exception cref="Siemens.Engineering.EngineeringException"></exception> /// <exception cref="System.IO.IOException"></exception> /// <exception cref="System.UnauthorizedAccessException"></exception> /// <exception cref="System.IO.DirectoryNotFoundException"></exception> public static string ExportItem(IEngineeringObject exportItem, ExportOptions exportOption, string exportPath) { if (exportItem == null) { throw new ArgumentNullException(nameof(exportItem), "Parameter is null"); } if (String.IsNullOrEmpty(exportPath)) { throw new ArgumentException("Parameter is null or empty", nameof(exportPath)); } var filePath = Path.GetFullPath(exportPath); if (exportItem is PlcBlock) { var block = exportItem as PlcBlock; string blockName = GetObjectName(exportItem); if (block.ProgrammingLanguage == ProgrammingLanguage.ProDiag || block.ProgrammingLanguage == ProgrammingLanguage.ProDiag_OB || block.ProgrammingLanguage == ProgrammingLanguage.SCL) { return(null); } if (block.IsConsistent) { blockName = XmlParser.RemoveWindowsUnallowedChars(blockName); filePath = Path.Combine(filePath, blockName + ".xml"); if (File.Exists(filePath)) { File.Delete(filePath); } (exportItem as PlcBlock).Export(new FileInfo(filePath), exportOption); return(filePath); } else { MessageBox.Show("Block: " + blockName + " is inconsistent! It will not be exported.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return(null); } } if (exportItem is PlcTagTable || exportItem is PlcType || exportItem is ScreenOverview || exportItem is ScreenGlobalElements || exportItem is Screen || exportItem is TagTable || exportItem is Connection || exportItem is GraphicList || exportItem is TextList || exportItem is Cycle || exportItem is MultiLingualGraphic || exportItem is ScreenTemplate || exportItem is VBScript || exportItem is ScreenPopup || exportItem is ScreenSlidein) { Directory.CreateDirectory(filePath); filePath = Path.Combine(filePath, AdjustNames.AdjustFileName(GetObjectName(exportItem)) + ".xml"); File.Delete(filePath); var parameter = new Dictionary <Type, object>(); parameter.Add(typeof(FileInfo), new FileInfo(filePath)); parameter.Add(typeof(ExportOptions), exportOption); exportItem.Invoke("Export", parameter); return(filePath); } if (exportItem is PlcExternalSource) { //Directory.CreateDirectory(filePath); //filePath = Path.Combine(filePath, AdjustNames.AdjustFileName(GetObjectName(exportItem))); //File.Delete(filePath); //File.Create(filePath); //return filePath; } return(null); }
private void extractAlarmDefsToolStripMenuItem_Click(object sender, EventArgs e) { if (tiaPortal != null) { if (data_block_dialog == null) { data_block_dialog = new BrowseDialog(tiaPortal); data_block_dialog.Descend = TIATree.ControllerOnly; data_block_dialog.Leaf = TIATree.SharedDBOnly; data_block_dialog.AutoExpandMaxChildren = 1; data_block_dialog.AcceptText = "Extract"; data_block_dialog.Text = "Select alarm data block"; } if (data_block_dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { if (data_block_dialog.SelectedObject is DataBlock) { DataBlock block = (DataBlock)data_block_dialog.SelectedObject; try { // Extract from data base string filename = TempFile.Name("AlarmDB", "xml"); block.Export(filename, ExportOptions.WithDefaults | ExportOptions.WithReadOnly); alarmList.DataSource = null; AlarmDB.parseFile(defs, filename); System.IO.File.Delete(filename); // Extract from constant tags // Move up the tree until we find a ControllerTarget IEngineeringObject obj = (block as IEngineeringObject).Parent; while (!(obj is ControllerTarget)) { obj = obj.Parent; // Shouldn't happen, but just in case if (obj == null) { MessageBox.Show(this, "No controller found as parent"); return; } } ControllerTarget controller = (ControllerTarget)obj; List <ConstTable.Constant> consts = new List <ConstTable.Constant>(); ControllerTagTable table = controller.ControllerTagFolder.TagTables.Find("Alarms"); if (table == null) { MessageBox.Show(this, "No tag table named Alarms was found"); } else { filename = TempFile.Name("ConstantTags", "xml"); Console.WriteLine("Wrote to " + filename); table.Export(filename, ExportOptions.WithDefaults | ExportOptions.WithReadOnly); List <ConstTable.Constant> constants = ConstTable.getConstants(filename); foreach (ConstTable.Constant c in constants) { if (c.Name.StartsWith("Alarm") && c.Value is int) { AlarmDefinition a = defs.FindByID((int)c.Value); if (a != null) { a.Name = c.Name.Substring(5); } } } } defs.ValidateID(); alarmList.AutoGenerateColumns = false; alarmList.DataSource = defs; } catch (Exception ex) { MessageBox.Show("Failed to extract alarm definitions: " + ex.Message); } } } } }
private void alarmDefsToolStripMenuItem_Click(object sender, EventArgs e) { if (tiaPortal != null) { if (folder_dialog == null) { folder_dialog = new BrowseDialog(tiaPortal); folder_dialog.Descend = TIATree.ControllerOnly; folder_dialog.Leaf = (o => o is IBlockAggregation); folder_dialog.AutoExpandMaxChildren = 1; folder_dialog.AcceptText = "Generate"; folder_dialog.Text = "Select where to generate block"; } if (folder_dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { if (folder_dialog.SelectedObject is IBlockAggregation) { IBlockAggregation folder = (IBlockAggregation)folder_dialog.SelectedObject; string filename = AlarmDB.buildFile(defs); try { folder.Import(filename, ImportOptions.Override); } catch (Siemens.Engineering.EngineeringException ex) { MessageBox.Show(this, "Failed to import alarm definitions: " + ex.Message); System.IO.File.Delete(filename); return; } System.IO.File.Delete(filename); // Move up the tree until we find a ControllerTarget IEngineeringObject obj = folder.Parent; while (!(obj is ControllerTarget)) { obj = obj.Parent; // Shouldn't happen, but just in case if (obj == null) { MessageBox.Show(this, "No controller found as parent"); return; } } ControllerTarget controller = (ControllerTarget)obj; List <ConstTable.Constant> consts = new List <ConstTable.Constant>(); foreach (AlarmDefinition alarm in defs) { consts.Add(new ConstTable.Constant("Alarm" + alarm.Name, alarm.ID, alarm.Text)); } filename = ConstTable.buildFile("Alarms", consts); try { controller.ControllerTagFolder.TagTables.Import(filename, ImportOptions.Override); } catch (Siemens.Engineering.EngineeringException ex) { MessageBox.Show(this, "Failed to import constants: " + ex.Message); System.IO.File.Delete(filename); return; } System.IO.File.Delete(filename); } } } }
private void Worker_DoWork(object sender, DoWorkEventArgs e) { lock (portal) { if (worker.CancellationPending) { return; } IEngineeringObject engineering_obj = null; if (plc != null) { worker.ReportProgress(50, new NodeEnter(NodeDest.From, plc.Name, true)); worker.ReportProgress(50, new NodeEnter(NodeDest.From, "Blocks")); AddBlocks(plc.BlockGroup); worker.ReportProgress(50, new NodeExit(NodeDest.From)); if (worker.CancellationPending) { return; } worker.ReportProgress(50, new NodeEnter(NodeDest.From, "Types")); AddTypes(plc.TypeGroup); worker.ReportProgress(50, new NodeExit(NodeDest.From)); worker.ReportProgress(50, new NodeExit(NodeDest.From)); if (worker.CancellationPending) { return; } engineering_obj = plc; } if (hmi != null) { worker.ReportProgress(50, new NodeEnter(NodeDest.From, hmi.Name, true)); worker.ReportProgress(50, new NodeEnter(NodeDest.From, "Screens")); AddScreens(hmi.ScreenFolder); worker.ReportProgress(50, new NodeExit(NodeDest.From)); if (worker.CancellationPending) { return; } worker.ReportProgress(50, new NodeEnter(NodeDest.From, "Tagtables")); AddTagTables(hmi.TagFolder); worker.ReportProgress(50, new NodeExit(NodeDest.From)); worker.ReportProgress(50, new NodeExit(NodeDest.From)); engineering_obj = hmi; } while (engineering_obj != null && !(engineering_obj is Project)) { engineering_obj = engineering_obj.Parent; } Project proj = engineering_obj as Project; if (proj != null) { foreach (DeviceUserGroup group in proj.DeviceGroups) { FindDevices(group); } foreach (Device device in proj.Devices) { FindDeviceItems(device.Items); } } } }