How to enumerate through the child's of a tree item
Requirements
To enumerate through the child’s of a tree item in a configuration, an instance of the TwinCAT System Manager must be created and a configuration has to be opened. The LookupTreeItem method of the ITcSysManager interface returns a ITcSmTreeItem interface pointer implemented by the tree item referenced by its pathname.
This interface contains two ways of child enumeration. The first one is the _NewEnum property that is automatically used by the VB ForEach statement and enumerates over all childs of a tree item. This includes for example the process image child and the input and output childs of an I/O device. The second way is to use the ChildCount and Child(i) > property that allows to enumerate over the "main" child (e.g. only over the I/O boxes of an I/O device).
Procedure
The ProgId "TCatSysManager.TcSysManager" can be used to create an instance of the System Manager that implements the ITcSysManager interface. This interface has a LookupTreeItem method that returns a ITcSmTreeItem pointer to a specific tree item given by its pathname. To enumerate through the all childs of a tree item "TIID^Device 1 (FC310x)^Box 1 (BK3100)" the following vbscript code can be used:
Sample (vbscript):
dim tsm
dim item
dim str
dim itemChild
set tsm = CreateObject("TCatSysManager.TcSysManager")
call tsm.OpenConfiguration("c:\twincat\myConfig.wsm")
set item = tsm.LookupTreeItem("TIID^Device 1(FC310x)")
For Each Child In item
Set itemChild = Child
str = itemChild.Name
Next Child
To enumerate only through the main childs of a tree item "TIID^Device 1 (FC310x)^Box 1 (BK3100)" the following vbscript code can be used:
dim tsm
dim item
dim str
set tsm = CreateObject("TCatSysManager.TcSysManager")
call tsm.OpenConfiguration("c:\twincat\myConfig.wsm")
set item = tsm.LookupTreeItem("TIID^Device 1(FC310x)")
For i = 1 To item.ChildCount
str = item.Child(i).Name
Next
See Also
How to... section, ITcSysManager, LookupTreeItem, ITcSmTreeItem, _NewEnum, ChildCount, Child(i)
Built on Tuesday, Mai 01, 2001