/// <summary> /// Get Da_Items or Leafs, according to the browse options /// </summary> /// <param name="browseOptions"></param> /// <param name="addrSpaceElem"></param> /// <param name="node"></param> /// <param name="session"></param> private void GetTagNode(DaAddressSpaceElementBrowseOptions browseOptions, DaAddressSpaceElement addrSpaceElem, TreeNode node) { DaGetPropertiesOptions propertyGetOptions = new DaGetPropertiesOptions(); propertyGetOptions.WhatPropertyData = EnumPropertyData.ALL; DaAddressSpaceElement[] addressSpaceElements = null; if (ResultCode.SUCCEEDED(addrSpaceElem.Browse(browseOptions, out addressSpaceElements, executionOptions))) { for (int i = 0; i < addressSpaceElements.Length; i++) { TreeNode newNode = new TreeNode(addressSpaceElements[i].Name, TAG_INDEX, TAG_INDEX); newNode.Name = addressSpaceElements[i].Name; newNode.Tag = addressSpaceElements[i]; node.Nodes.Add(newNode); //get properties AddProperties(addressSpaceElements[i], propertyGetOptions, newNode); } AddProperties(addrSpaceElem, propertyGetOptions, node); } }
public OpcForm(OutProc anOutProc) { InitializeComponent(); try { m_outProc = anOutProc; m_opcClient = m_outProc.OpcClient; m_session = m_opcClient.GetSession(); m_executionOptions = m_opcClient.GetExecutionOptions(); //create the browse tree root element TreeNode treeRoot = new TreeNode(m_session.Url, 0, 0); DaAddressSpaceElement rootDaAddressSpaceElement = new DaAddressSpaceElement(EnumAddressSpaceElementType.BRANCH, String.Empty, string.Empty, string.Empty, 0, null); rootDaAddressSpaceElement.Session = m_session; treeRoot.Tag = rootDaAddressSpaceElement; itemsTreeView.Nodes.Add(treeRoot); DaAddressSpaceElement[] addressSpaceElements = null; #region Browse branches //--------------------- DaAddressSpaceElementBrowseOptions browseOptions = new DaAddressSpaceElementBrowseOptions(); browseOptions.ElementTypeFilter = EnumAddressSpaceElementType.BRANCH; //get the root's children if (ResultCode.SUCCEEDED(rootDaAddressSpaceElement.Browse( browseOptions, out addressSpaceElements, m_executionOptions))) { if (addressSpaceElements != null) { for (int i = 0; i < addressSpaceElements.Length; i++) { TreeNode node = new TreeNode(addressSpaceElements[i].Name, 1, 1); node.Tag = addressSpaceElements[i]; treeRoot.Nodes.Add(node); } //end for } } //end if //- #endregion addressSpaceElements = null; #region Browse Leaves //------------------- browseOptions.ElementTypeFilter = EnumAddressSpaceElementType.LEAF; //get the root's children if (ResultCode.SUCCEEDED(rootDaAddressSpaceElement.Browse( browseOptions, out addressSpaceElements, m_executionOptions))) { if (addressSpaceElements != null) { for (int i = 0; i < addressSpaceElements.Length; i++) { TreeNode node = new TreeNode(addressSpaceElements[i].Name, 2, 2); node.Tag = addressSpaceElements[i]; treeRoot.Nodes.Add(node); } //end for } // end if } //end if //- #endregion #region GetDaProperties //--------------------- DaProperty[] daProperties = null; m_propertyGetOptions.WhatPropertyData = EnumPropertyData.NONE; if (ResultCode.SUCCEEDED(rootDaAddressSpaceElement.GetDaProperties( m_propertyGetOptions, out daProperties, m_executionOptions))) { if (daProperties != null) { for (int i = 0; i < daProperties.Length; i++) { TreeNode node = new TreeNode(daProperties[i].Name, 3, 3); //add all properties except OPC-specific properties if (daProperties[i].Id >= 100) { treeRoot.Nodes.Add(node); } } //end for } } // end if //- #endregion } catch (Exception exc) { MessageBox.Show(exc.ToString()); } // end try...catch } // end constructor
} // end Main private void TreeViewBeforeExpand(object sender, System.Windows.Forms.TreeViewCancelEventArgs e) { try { itemsTreeView.Cursor = System.Windows.Forms.Cursors.WaitCursor; foreach (TreeNode node in e.Node.Nodes) { m_propertyGetOptions.WhatPropertyData = EnumPropertyData.NONE; DaAddressSpaceElement[] addressSpaceElements = null; node.Nodes.Clear(); if (node.Tag is DaAddressSpaceElement) { DaAddressSpaceElement element = node.Tag as DaAddressSpaceElement; DaAddressSpaceElementBrowseOptions browseOptions = new DaAddressSpaceElementBrowseOptions(); #region Branches //----------- browseOptions.ElementTypeFilter = EnumAddressSpaceElementType.BRANCH; if (ResultCode.SUCCEEDED(element.Browse( browseOptions, out addressSpaceElements, m_executionOptions))) { if (addressSpaceElements != null) { for (int i = 0; i < addressSpaceElements.Length; i++) { TreeNode newNode = new TreeNode(addressSpaceElements[i].Name, 1, 1); newNode.Tag = addressSpaceElements[i]; node.Nodes.Add(newNode); } //end for } } //end if //- #endregion addressSpaceElements = null; #region Leaves //----------- browseOptions.ElementTypeFilter = EnumAddressSpaceElementType.LEAF; if (ResultCode.SUCCEEDED(element.Browse( browseOptions, out addressSpaceElements, m_executionOptions))) { if (addressSpaceElements != null) { for (int i = 0; i < addressSpaceElements.Length; i++) { TreeNode newNode = new TreeNode(addressSpaceElements[i].Name, 2, 2); newNode.Tag = addressSpaceElements[i]; node.Nodes.Add(newNode); } //end for } } //end if //- #endregion DaProperty[] daProperties = null; #region Properties //---------------- if (ResultCode.SUCCEEDED(element.GetDaProperties( m_propertyGetOptions, out daProperties, m_executionOptions))) { if (daProperties != null) { for (int i = 0; i < daProperties.Length; i++) { //add all properties except OPC-specific properties if (daProperties[i].Id >= 100) { TreeNode newNode = new TreeNode(daProperties[i].Description, 3, 3); node.Nodes.Add(newNode); } } //end for } } // end if //- #endregion } // end if node.Collapse(); } // end if itemsTreeView.Cursor = System.Windows.Forms.Cursors.Default; } catch (Exception exc) { MessageBox.Show(exc.ToString()); } } // end treeview_beforeexpand
/// <summary> /// Get Da BrowseData /// </summary> private void GetDaData(TreeNode node, TreeNode rootNode, object daSessionObject) { if (node.ImageIndex == TAG_INDEX) { return; } DaSession session = daSessionObject as DaSession; if (session == null) { return; } if (daRootSpaceElem == null) { return; } DaAddressSpaceElement[] addressSpaceElements = null; //get branches DaAddressSpaceElementBrowseOptions browseOptions = new DaAddressSpaceElementBrowseOptions(); browseOptions.RetrieveItemId = true; DaAddressSpaceElement currentAddSpaceElem = null; if (node.Tag == null) { return; } if (node.Tag is DaSession) { currentAddSpaceElem = daRootSpaceElem; } if (node.Tag is DaAddressSpaceElement) { currentAddSpaceElem = node.Tag as DaAddressSpaceElement; } if (currentAddSpaceElem != null) { browseOptions.ElementTypeFilter = EnumAddressSpaceElementType.BRANCH; if (ResultCode.SUCCEEDED(currentAddSpaceElem.Browse(browseOptions, out addressSpaceElements, executionOptions))) { node.Nodes.Clear(); for (int i = 0; i < addressSpaceElements.Length; i++) { TreeNode newNode = new TreeNode(addressSpaceElements[i].Name, NODE_INDEX, NODE_INDEX); newNode.Name = addressSpaceElements[i].Name; newNode.Tag = addressSpaceElements[i]; node.Nodes.Add(newNode); newNode.Nodes.Add(new TreeNode("")); } } //get the items //browseOptions.ElementTypeFilter = EnumAddressSpaceElementType.DA_ITEM; //GetTagNode(browseOptions, currentAddSpaceElem, node); //get leaves browseOptions.ElementTypeFilter = EnumAddressSpaceElementType.LEAF; GetTagNode(browseOptions, currentAddSpaceElem, node); } }