// delete block / data type private void mnuBlockDelete_Click(object sender, EventArgs e) { if (treeView1.SelectedNode.Tag is PlcBlock) { PlcBlock block = (PlcBlock)treeView1.SelectedNode.Tag; DialogResult dlg = MessageBox.Show("Do you really want to delete the block " + block.Name + "?", "Delete block", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dlg == DialogResult.Yes) { block.Delete(); IterateThroughDevices(project); } } if (treeView1.SelectedNode.Tag is PlcStruct) { PlcStruct block = (PlcStruct)treeView1.SelectedNode.Tag; DialogResult dlg = MessageBox.Show("Do you really want to delete the data type " + block.Name + "?", "Delete data type", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dlg == DialogResult.Yes) { block.Delete(); IterateThroughDevices(project); } } }
public Block(PlcBlock plcBlock) { _block = plcBlock; _parent = (PlcBlockGroup)_block.Parent; Name = plcBlock.Name; SetChangeableState(); }
private void ExportBlocks(PlcSoftware plcSoftware) { PlcBlock _plcblock = plcSoftware.BlockGroup.Blocks.Find(txt_BlockName.Text); //PlcBlock _plcblock = plcSoftware.BlockGroup.Blocks.Find("Empty_FC"); _plcblock.Export(new FileInfo(string.Format(_FBD_Export.SelectedPath + @"\" + "{0}.xml", _plcblock.Name)), ExportOptions.WithDefaults); }
/// <summary> /// Delete block from given folder /// </summary> /// <param name="blockName">Name of the block to be deleted</param> /// <param name="software">Software on which to look for the block</param public static void DeleteBlock(string blockName, PlcBlockUserGroup software, TextBox log) { PlcBlock blockToDelete = null; foreach (PlcBlock block in software.Blocks) { if (blockName.Equals(block.Name)) { blockToDelete = block; } } if (blockToDelete != null) { blockToDelete.Delete(); log.AppendText("Block " + blockName + " to be deleted found in " + software.Name + " folder"); log.AppendText(Environment.NewLine); } else { log.AppendText("Block " + blockName + " not found in " + software.Name + " folder"); log.AppendText(Environment.NewLine); // if block was not found in current group check subgroups foreach (PlcBlockUserGroup group in software.Groups) { DeleteBlock(blockName, group, log); } } }
static void HandleBlock(PlcBlock block, PlcSoftware software) { PlcExternalSourceSystemGroup externalSourceGroup = software.ExternalSourceGroup; //Console.WriteLine(block.Name + " " + block.GetType()+ " " +block.ProgrammingLanguage); if (programLanguageToExtension.ContainsKey(block.ProgrammingLanguage.ToString())) { string extension; programLanguageToExtension.TryGetValue(block.ProgrammingLanguage.ToString(), out extension); string folder_prefix; programLanguageToFolderPrefixExtension.TryGetValue(block.ProgrammingLanguage.ToString(), out folder_prefix); var fileInfo = new FileInfo(exportLocation + folder_prefix + block.Name + extension); var blocks = new List <PlcBlock>() { block }; try { if (File.Exists(fileInfo.FullName)) { File.Delete(fileInfo.FullName); } Console.WriteLine(block.Name + " to " + fileInfo.FullName); externalSourceGroup.GenerateSource(blocks, fileInfo, GenerateOptions.None); } catch (Exception exc) { Console.WriteLine(exc.ToString()); } } }
public CopyPlcBlockTask(TiaPortal portal, PlcBlock from, PlcSoftware to, bool overwrite) { this.portal = portal; fromBlock = from; toPlc = to; this.overwrite = overwrite; Selected = true; Description = "Copy block " + from.Name + " to " + to.Name; }
public CreateHmiTagsFromBlockTask(TiaPortal portal, PlcBlock block, TagFolder folder, string function, ConstantLookup constants) { this.portal = portal; this.folder = folder; this.block = block; this.function = function; this.constants = constants; Description = "Update HMI tag table " + function + " for block " + block.Name; }
private static PlcBlock GetPlcEntry(string selector, int minSize) { if (!_plc.TryGetValue(selector, out PlcBlock plcblock)) { plcblock = new PlcBlock(minSize); _plc.Add(selector, plcblock); return(plcblock); } plcblock.UpdateBlockSize(minSize); return(plcblock); }
/// <summary>Generates a source file from the given PlcBlock</summary> /// <param name="exportItem">The export item.</param> /// <param name="exportPath">The export path.</param> /// <param name="options"></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 GenerateSourceFromBlock(PlcBlock exportItem, string exportPath, GenerateOptions options) { 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.IsKnowHowProtected) { Directory.CreateDirectory(filePath); switch (exportItem.ProgrammingLanguage) { case ProgrammingLanguage.DB: filePath = Path.Combine(filePath, AdjustNames.AdjustFileName(exportItem.Name) + ".db"); break; case ProgrammingLanguage.SCL: filePath = Path.Combine(filePath, AdjustNames.AdjustFileName(exportItem.Name) + ".scl"); break; case ProgrammingLanguage.STL: filePath = Path.Combine(filePath, AdjustNames.AdjustFileName(exportItem.Name) + ".awl"); break; default: return(null); } if (File.Exists(filePath)) { File.Delete(filePath); } IEngineeringInstance temp = exportItem; do { temp = temp.Parent; }while (!(temp is PlcSoftware)); (temp as PlcSoftware).ExternalSourceGroup.GenerateSource(new[] { exportItem }, new FileInfo(filePath), options); return(filePath); } throw new EngineeringException(string.Format(CultureInfo.InvariantCulture, "Block: '{0}' is Know-how protected! \r\n 'Generate source from block' is not possible on know how protected blocks!", exportItem.Name)); }
public static bool ExportBlock(PlcBlock block, string filePath) { try { if (File.Exists(filePath)) { File.Delete(filePath); } block.Export(new FileInfo(filePath), ExportOptions.None); } catch (Exception ex) { Trace.TraceError("Exception during export:" + Environment.NewLine + ex + Environment.NewLine + "Block Name: " + block.Name + Environment.NewLine + " Path: " + filePath); return(false); } return(true); }
public void HandleDataBlock(PlcBlock block) { if (block.ProgrammingLanguage != ProgrammingLanguage.DB) { return; } try { BlockType instance_of = (BlockType)block.GetAttribute("InstanceOfType"); if (instance_of != BlockType.UDT) { return; } } catch (EngineeringNotSupportedException) { } if (!block.IsConsistent) { Log?.LogMessage(MessageLog.Severity.Warning, "Skipped block " + block.Name + " because it is inconsistent. Compile block to make it consistent."); return; } FileInfo path = TempFile.File("block_", "xml"); try { Console.WriteLine("Exporting: " + block.Name); block.Export(path, ExportOptions.WithDefaults); ParseXML_DB(path); } catch (Siemens.Engineering.EngineeringTargetInvocationException e) { Log?.LogMessage(MessageLog.Severity.Error, "Failed to export block " + block.Name + ": " + e.Message); } finally { try { path.Delete(); } catch (IOException e) { Log?.LogMessage(MessageLog.Severity.Error, "Failed to delete temporary file: " + e.Message); } } }
private PlcBlock findPlcBlockName(string name, PlcBlockGroup blocks) { PlcBlock block = blocks.Blocks.Find(name); if (block == null) { foreach (PlcBlockGroup group in blocks.Groups) { block = findPlcBlockName(name, group); if (block != null) { break; } } } return(block); }
public static PlcBlock GetPlcEntry(string selector, int minSize = -1) { if (!_plc.TryGetValue(selector, out PlcBlock plcblock)) { lock (_plc) { if (!_plc.TryGetValue(selector, out plcblock)) { plcblock = new PlcBlock(minSize > 0 ? minSize : 0); _plc.Add(selector, plcblock); return(plcblock); } } } if (minSize > 0) { plcblock.UpdateBlockSize(minSize); } return(plcblock); }
public static void InsertBlocksFromSclSource(PlcSoftware plcSoftware, string sourceName, string sourcePath) { // Create source var source = plcSoftware.ExternalSourceGroup.ExternalSources.FirstOrDefault(obj => obj.Name.Equals(sourceName)); if (source == null) { Console.WriteLine("Load SCL sources..."); source = plcSoftware.ExternalSourceGroup.ExternalSources.CreateFromFile(sourceName, sourcePath); } // Create block from source PlcBlock block = plcSoftware.BlockGroup.Blocks.FirstOrDefault(obj => obj.Name.Equals(sourceName)); if (block == null) { Console.WriteLine("Generate block..."); GenerateBlockOption options = GenerateBlockOption.KeepOnError; source.GenerateBlocksFromSource(options); } }
static public XmlDocument ExportPlcBlockXML(PlcBlock block) { FileInfo path = TempFile.File("export_block_", "xml"); try { block.Export(path, ExportOptions.WithDefaults); XmlDocument doc = LoadDoc(path.ToString()); return(doc); } finally { try { path.Delete(); } catch (IOException e) { Console.WriteLine("Failed to delete temporary file: " + e.Message); } } }
private void mnuBlockDelete_Click(object sender, EventArgs e) { // delete block / data type if (treeView1.SelectedNode.Tag is PlcBlock) { PlcBlock block = (PlcBlock)treeView1.SelectedNode.Tag; if (MessageYesNo("Do you really want to delete the block " + block.Name + "?", "Delete block") == DialogResult.Yes) { try { block.Delete(); IterateThroughDevices(project); } catch (Exception ex) { MessageError(ex.Message, "Exception"); } } } else if (treeView1.SelectedNode.Tag is PlcStruct) { PlcStruct block = (PlcStruct)treeView1.SelectedNode.Tag; if (MessageYesNo("Do you really want to delete the data type " + block.Name + "?", "Delete data type") == DialogResult.Yes) { try { block.Delete(); IterateThroughDevices(project); } catch (Exception ex) { MessageError(ex.Message, "Exception"); } } } }
private static void LoadSclSources(PlcSoftware plcSoftware) { // Create source var sourceName = "CheckArray"; var source = plcSoftware.ExternalSourceGroup.ExternalSources.FirstOrDefault(obj => obj.Name.Equals(sourceName)); if (source == null) { Console.WriteLine("Load SCL sources..."); string sourcePath = @"\\Mac\Home\Documents\GitHub\ibKastl.ApiShowcase\data\Siemens\CheckArray.scl"; source = plcSoftware.ExternalSourceGroup.ExternalSources.CreateFromFile(sourceName, sourcePath); } // Create block from source PlcBlock block = plcSoftware.BlockGroup.Blocks.FirstOrDefault(obj => obj.Name.Equals(sourceName)); if (block == null) { Console.WriteLine("Generate block..."); GenerateBlockOption options = GenerateBlockOption.KeepOnError; source.GenerateBlocksFromSource(options); } }
internal TIAOpennessProjectBlockInfo(PlcBlock plcBlock) { this.plcBlock = plcBlock; }
private static void HandleDataBlock(NodeHandler handler, PlcBlock block) { handler.Enter(block, block.Name); handler.Exit(block); }
public HMItagInfo(PlcBlock block, TagFolder folder, string function) { this.block = block; this.folder = folder; this.function = function; }
void UpdatePresetValues( PlcSoftware plcSoftware, Dictionary <string, PresetGroup> preset_groups) { ConstantLookup constants = new ConstantLookup(); constants.Populate(tiaPortal, plcSoftware); PlcBlockGroup preset_group = plcSoftware.BlockGroup.Groups.Find("Preset"); if (preset_group == null) { MessageBox.Show("No group named Preset found for PLC " + plcSoftware.Name); return; } foreach (string group_name in preset_groups.Keys) { string preset_db_name = "sDB_Preset_" + group_name; PlcBlock preset_db = preset_group.Blocks.Find(preset_db_name); if (preset_db == null) { MessageBox.Show("No block named " + preset_db_name + " found for PLC " + plcSoftware.Name); return; } XmlDocument doc; try { doc = TIAutils.ExportPlcBlockXML(preset_db); } catch (Exception ex) { MessageBox.Show("Failed to export block " + preset_db_name + ": " + ex.Message); return; } if (doc.DocumentElement.SelectSingleNode("/Document/SW.Blocks.GlobalDB//if:Section[@Name='Static']", XMLUtil.nameSpaces) is XmlElement static_elem) { var infos = preset_groups[group_name].presets; foreach (PresetInfo info in infos) { PresetValueParser.SetPresetValue(static_elem, info.tag.tagPath, constants, info.values); PresetValueParser.SetPresetEnabled(static_elem, info.tag.tagPath, constants, info.enabled); } PresetValueParser.SetPresetNames(static_elem, constants, preset_groups[group_name].preset_names); PresetValueParser.SetPresetColors(static_elem, constants, preset_groups[group_name].preset_colors); } else { MessageBox.Show("No static section found for " + preset_db_name); return; } try { var group = (Siemens.Engineering.SW.Blocks.PlcBlockGroup)preset_db.Parent; var name = group.Name; TIAutils.ImportPlcBlockXML(doc, group); } catch (Exception ex) { MessageBox.Show("Failed to import block " + preset_db_name + ": " + ex.Message); return; } } }
private void exportButton_Click(object sender, EventArgs e) { if (savePresetList.ShowDialog() == System.Windows.Forms.DialogResult.OK) { try { ConstantLookup constants = new ConstantLookup(); constants.Populate(tiaPortal, plcSoftware); Dictionary <string, List <PresetTag> > tag_groups = tagGroups(presetList); PlcBlockGroup plc_preset_group = plcSoftware.BlockGroup.Groups.Find("Preset"); if (plc_preset_group == null) { MessageBox.Show("No group named Preset found for PLC " + plcSoftware.Name); return; } Dictionary <string, PresetGroup> preset_groups = new Dictionary <string, PresetGroup>(); foreach (string group_name in tag_groups.Keys) { PresetGroup group = new PresetGroup(); string preset_db_name = "sDB_Preset_" + group_name; PlcBlock preset_db = plc_preset_group.Blocks.Find(preset_db_name); if (preset_db == null) { MessageBox.Show("No block named " + preset_db_name + " found for PLC " + plcSoftware.Name); return; } XmlDocument doc; try { doc = TIAutils.ExportPlcBlockXML(preset_db); } catch (Exception ex) { MessageBox.Show("Failed to export block " + preset_db_name + ": " + ex.Message); return; } if (doc.DocumentElement.SelectSingleNode("/Document/SW.Blocks.GlobalDB//if:Section[@Name='Static']", XMLUtil.nameSpaces) is XmlElement static_elem) { group.preset_names = PresetValueParser.GetPresetNames(static_elem, constants); group.preset_colors = PresetValueParser.GetPresetColors(static_elem, constants); group.presets = new List <PresetDocument.PresetInfo>(); var tags = tag_groups[group_name]; foreach (var tag in tags) { var values = PresetValueParser.GetPresetValue(static_elem, tag.tagPath, constants); var enabled = PresetValueParser.GetPresetEnabled(static_elem, tag.tagPath, constants); Console.WriteLine(tag.tagPath + ":" + (string.Join(",", values))); group.presets.Add(new PresetDocument.PresetInfo() { tag = tag, values = values, enabled = enabled }); } preset_groups[group_name] = group; } else { MessageBox.Show("No static section found for " + preset_db_name); return; } } PresetDocument.Save(savePresetList.FileName, preset_groups, cultureComboBox.SelectedItem.ToString()); } catch (Exception ex) { MessageBox.Show("Failed to export preset list: " + ex.Message); } } }
private void exportToolStripMenuItem_Click(object sender, EventArgs e) { // nothing selected if (treeView1.SelectedNode == null) { return; } // only blocks and structs if (!(treeView1.SelectedNode.Tag is PlcBlock) && !(treeView1.SelectedNode.Tag is PlcStruct)) { return; } FolderBrowserDialog folderDialog = new FolderBrowserDialog(); folderDialog.Description = "Select export path"; folderDialog.SelectedPath = Application.StartupPath + "\\Export"; DialogResult res = folderDialog.ShowDialog(); // ok now save if (res == DialogResult.OK) { try { // for plcBlocks like OB,FB,FC if (treeView1.SelectedNode.Tag is PlcBlock) { PlcBlock block = (PlcBlock)treeView1.SelectedNode.Tag; if (block.IsConsistent) { string fPath = Application.StartupPath + "\\Export\\" + block.ProgrammingLanguage.ToString() + "_" + block.Name + "_" + "V" + block.HeaderVersion.ToString() + ".xml"; fPath = GetNextFileName(fPath); FileInfo f = new FileInfo(fPath); block.Export(f, ExportOptions.None); MessageOK("File " + Path.GetFileName(fPath) + " has beed exported", "Export"); } else { MessageError("Block " + block.Name + " is not consistent. Please compile", "Export"); } } // for data types if (treeView1.SelectedNode.Tag is PlcStruct) { PlcStruct block = (PlcStruct)treeView1.SelectedNode.Tag; if (block.IsConsistent) { string fPath = Application.StartupPath + "\\Export\\plcType_" + block.Name + "_" + block.ModifiedDate.ToShortDateString() + ".xml"; fPath = GetNextFileName(fPath); FileInfo f = new FileInfo(fPath); block.Export(f, ExportOptions.None); MessageOK("File " + Path.GetFileName(fPath) + " has beed exported", "Export"); } else { MessageError("Data type " + block.Name + " is not consistent. Please compile", "Export"); } } } catch (Exception ex) { MessageError(ex.Message, "Exception"); } } }
//Exports a regular block private static void ExportRegularBlock(PlcSoftware plcSoftware) { PlcBlock plcBlock = plcSoftware.BlockGroup.Blocks.Find("TestFC1"); plcBlock.Export(new FileInfo(string.Format(@"D:\Samples\{0}.xml", plcBlock.Name)), ExportOptions.WithDefaults); }
private void btn_copy_Click(object sender, EventArgs e) { List <PlcSoftware> plcs = new List <PlcSoftware>(); foreach (TreeNode node in new TreeNodeDepthFirstEnumerator(toTreeView.Nodes)) { PlcSoftware sw = node.Tag as PlcSoftware; if (node.Checked && sw != null) { plcs.Add(sw); } } TaskDialog tasks = new TaskDialog(); List <PlcType> plc_types = new List <PlcType>(); List <PlcBlock> plc_blocks = new List <PlcBlock>(); List <Siemens.Engineering.Hmi.Screen.Screen> screens = new List <Siemens.Engineering.Hmi.Screen.Screen> (); List <TagTable> tag_tables = new List <TagTable>(); foreach (TreeNode node in new TreeNodeDepthFirstEnumerator(fromTreeView.Nodes)) { if (node.Checked) { PlcBlock block = node.Tag as PlcBlock; if (block != null) { plc_blocks.Add(block); } PlcType type = node.Tag as PlcType; if (type != null) { plc_types.Add(type); } Siemens.Engineering.Hmi.Screen.Screen screen = node.Tag as Siemens.Engineering.Hmi.Screen.Screen; if (screen != null) { screens.Add(screen); } TagTable tag_table = node.Tag as TagTable; if (tag_table != null) { tag_tables.Add(tag_table); } } } foreach (PlcBlock block in plc_blocks) { foreach (PlcSoftware plc in plcs) { tasks.AddTask(new CopyPlcBlockTask(portal, block, plc, check_overwrite.Checked)); } } foreach (PlcType type in plc_types) { foreach (PlcSoftware plc in plcs) { tasks.AddTask(new CopyPlcTypeTask(portal, type, plc, check_overwrite.Checked)); } } tasks.ShowDialog(this); }
// add a fc for testing private void btnImport_Click(object sender, EventArgs e) { if (software == null) { return; } if (treeView1.SelectedNode != null) { var sel = treeView1.SelectedNode.Tag; if (sel is PlcBlockGroup) { try { PlcBlockGroup group = (PlcBlockGroup)sel; string fPath = Application.StartupPath + "\\TestFC1.xml"; //string fPath = Application.StartupPath + "\\StepData.xml"; FileInfo f = new FileInfo(fPath); // now load the xml document XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(fPath); // get version of the file XmlNode bkm = xmlDoc.SelectSingleNode("//Document//Engineering"); string version = bkm.Attributes["version"].Value; // check the correct type //XmlNode dataType = xmlDoc.SelectSingleNode("//Document//SW.Types.PlcStruct"); //if (dataType != null) { // get the name of the data type XmlNode nameDefination = xmlDoc.SelectSingleNode("//Document//SW.Blocks.GlobalDB//AttributeList//Name"); if (nameDefination == null) { nameDefination = xmlDoc.SelectSingleNode("//Document//SW.Blocks.FC//AttributeList//Name"); } string name = nameDefination.InnerText; // check if the data type exists PlcBlock t = group.Blocks.Find(name); if (t == null) { // import the file group.Blocks.Import(f, ImportOptions.None); IterateThroughDevices(project); } else { // overwrite? DialogResult res = MessageBox.Show("Data block " + name + " exists already. Overwrite ?", "Overwrite", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); if (res == DialogResult.OK) { // overwrite plc block group.Blocks.Import(f, ImportOptions.Override); IterateThroughDevices(project); } } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("No group selected!", "No group", MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { MessageBox.Show("Nothing is selected!", "Nothing selected", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
private void button2_Click(object sender, EventArgs e) { // nothing selected if (treeView1.SelectedNode == null) { return; } // only blocks and structs if (!(treeView1.SelectedNode.Tag is PlcBlock) && !(treeView1.SelectedNode.Tag is PlcStruct)) { return; } FolderBrowserDialog folderDialog = new FolderBrowserDialog(); folderDialog.Description = "Select export path"; folderDialog.SelectedPath = Application.StartupPath + "\\Export"; DialogResult res = folderDialog.ShowDialog(); // ok now save if (res == DialogResult.OK) { try { // for plcBlocks like OB,FB,FC if (treeView1.SelectedNode.Tag is PlcBlock) { PlcBlock block = (PlcBlock)treeView1.SelectedNode.Tag; string fPath = Application.StartupPath + "\\Export\\plcBlock_" + block.ProgrammingLanguage.ToString() + "_" + block.Name + ".xml"; fPath = getNextFileName(fPath); FileInfo f = new FileInfo(fPath); block.Export(f, ExportOptions.None); MessageBox.Show("File " + f + " has beed exported", "Export", MessageBoxButtons.OK, MessageBoxIcon.Information); } // fpr data types if (treeView1.SelectedNode.Tag is PlcStruct) { PlcStruct block = (PlcStruct)treeView1.SelectedNode.Tag; string fPath = Application.StartupPath + "\\Export\\plcBlock_" + block.Name + ".xml"; fPath = getNextFileName(fPath); FileInfo f = new FileInfo(fPath); block.Export(f, ExportOptions.None); MessageBox.Show("File " + f + " has beed exported", "Export", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }