10-05-2021 03:46 AM
Hi,
I have a list of many ChannelGroups and I want to keep only those containing specific Channels. The problem is that the Channels are not always in the same place, thus I have to find them first and then define to keep only the ChannelGroups containing these channels.
Can anyone help with a macro model?
Thank you in advance
razvim
10-07-2021 07:39 AM
Try this script:
Dim oGrp, oChn, iGrp
'Create an array with a list of the channel names to keep
ReDim arrChnsToKeep(2)
arrChnsToKeep(0) = "Time"
arrChnsToKeep(1) = "Speed"
arrChnsToKeep(2) = "RPM"
'Create sample channel groups and channels
Call Data.Root.Clear()
Portal.Visible = True
Set oGrp = Data.Root.ChannelGroups.Add("Group1")
Set oChn = oGrp.Channels.Add("Time",DataTypeChnFloat64)
Set oChn = oGrp.Channels.Add("DateTime",DataTypeChnDate)
Set oChn = oGrp.Channels.Add("Speed",DataTypeChnFloat64)
Set oChn = oGrp.Channels.Add("Torque",DataTypeChnFloat64)
Set oChn = oGrp.Channels.Add("RPM",DataTypeChnFloat64)
Set oChn = oGrp.Channels.Add("OilTempF",DataTypeChnFloat64)
'Create channel group with missing channel "RPM"
Set oGrp = Data.Root.ChannelGroups.Add("Group2")
Set oChn = oGrp.Channels.Add("Time",DataTypeChnFloat64)
Set oChn = oGrp.Channels.Add("DateTime",DataTypeChnDate)
Set oChn = oGrp.Channels.Add("Speed",DataTypeChnFloat64)
Set oChn = oGrp.Channels.Add("Torque",DataTypeChnFloat64)
Set oChn = oGrp.Channels.Add("OilTempF",DataTypeChnFloat64)
'
Set oGrp = Data.Root.ChannelGroups.Add("Group3")
Set oChn = oGrp.Channels.Add("Time",DataTypeChnFloat64)
Set oChn = oGrp.Channels.Add("DateTime",DataTypeChnDate)
Set oChn = oGrp.Channels.Add("Speed",DataTypeChnFloat64)
Set oChn = oGrp.Channels.Add("Torque",DataTypeChnFloat64)
Set oChn = oGrp.Channels.Add("RPM",DataTypeChnFloat64)
Set oChn = oGrp.Channels.Add("OilTempF",DataTypeChnFloat64)
Call Portal.Refresh()
'Remove channel groups that don't have the channels in arrChnsToKeep
For iGrp = Data.Root.ChannelGroups.Count To 1 Step -1
If Not bChnGrpHasReqdChns(Data.Root.ChannelGroups(iGrp), arrChnsToKeep) Then
Call LogFileWrite("Removing channel group '" & Data.Root.ChannelGroups(iGrp).Name & "'")
Call Data.Root.ChannelGroups.Remove(iGrp)
End If
Next
Call Portal.Refresh()
Function bChnGrpHasReqdChns(ByVal oGrp, ByVal arrChnsReqd)
If Not IsObject(oGrp) Then Call Err.Raise(65535,,"ERROR: Argument oGrp is not an object")
If Not IsArray(arrChnsReqd) Then Call Err.Raise(65535,,"ERROR: Argument arrChnsReqd is not an array")
bChnGrpHasReqdChns = True
Dim bAllChnsFound, i
For i = 0 To uBound(arrChnsReqd)
If Not oGrp.Channels.Exists(arrChnsReqd(i)) Then
'Call LogFileWrite("Chn Grp '" & oGrp.Name & "' is missing required channel '" & arrChnsReqd(i) & "'")
bChnGrpHasReqdChns = False
Exit For
End If
Next
End Function 'bChnGrpHasReqdChns()