01-10-2018 10:23 AM - edited 01-10-2018 10:41 AM
I've written a script for a two channel resultant calculation. I've used the DataPortal Selection to prompt the user to select two channels, then it goes into the calculation. The script works fine, but I'd like to build in some error checking. Here's what I'd like to check for:
1) Did the user only make two selections?
2) Are both selections Channels? (And not Groups)
3) Do both selections have the same unit?
4) What is the group index of the selected channels? (I need this so I can insure the calculation is done on the proper channels.)
Seems like the Portal.Selection properties aren't as accessible, or maybe I'm not able to get the right syntax. Below is the full script without any of the desired error checking.
If I can get #1 and #4 then 2 & 3 will be easy, just can't figure out how to get those two.
Dim ChnA, ChnB, Resultant, GroupNo Call MsgBoxDisp("Please select two channels in the Data Portal") '-- Set Channels ChnA = Portal.Structure.Selection(1).Name ChnB = Portal.Structure.Selection(2).Name '-- Calculate Resultant Call ChnMul("[1]/"&ChnA,"[1]/"&ChnA,"[1]/ChnAMultiplied") Call ChnMul("[1]/"&ChnB,"[1]/"&ChnB,"[1]/ChnBMultiplied") Call ChnAdd("[1]/ChnAMultiplied","[1]/ChnBMultiplied","[1]/Added") Call ChnCalculate("Ch(""[1]/Resultant"")=Sqr(Ch(""[1]/Added""))") '-- Cleanup Data.Root.ChannelGroups(1).Channels("Resultant").Properties("unit_string").Value = Data.Root.ChannelGroups(1).Channels(ChnA).Properties("unit_string").Value Data.Root.ChannelGroups(1).Channels("Resultant").Properties("description").Value = ChnA&" & "&ChnB&" Resultant" Data.Root.ChannelGroups(1).Channels("Resultant").Properties("Name").Value = ChnA&", "&ChnB&" Resultant" Call Data.Root.ChannelGroups(1).Channels.Remove("ChnAMultiplied") Call Data.Root.ChannelGroups(1).Channels.Remove("ChnBMultiplied") Call Data.Root.ChannelGroups(1).Channels.Remove("Added")
Solved! Go to Solution.
01-11-2018 06:30 AM
The number of selections you can count by Portal.Structure.Selection.Count (and compare it the allowed number)
To check whether selection is channel or not you could use the CNo function. If the result is 0 (and there is no group with the name of a channel) the selection is not a channel.
CNo(Portal.Structure.Selection.Item(1).Name)
If you made sure that selection is a channel you can get the unit that way: Data.GetChannel(Portal.Structure.Selection.Item(1).Name).UnitSymbol
01-11-2018 02:51 PM
Thanks for the response Muller, I was able to make some of those changes, and that gets me almost all the way there. Still need to identify the group number of the selected channels. The user may have multiple groups loaded into the portal, so I don't want to always assume group 1. The other way to do it would be to make the group of the selected channels the default group, but the Portal object doesn't seem to have that option either.
Updated code:
Dim ChnA, ChnB, Resultant Call MsgBoxDisp("Please select two channels in the Data Portal") If Portal.Structure.Selection.Count = 2 Then '-- Set Channels ChnA = Portal.Structure.Selection(1).Name ChnB = Portal.Structure.Selection(2).Name '-- Extra Error Checking If Data.Root.ChannelGroups(1).Channels(ChnA).Properties("unit_string").Value = Data.Root.ChannelGroups(1).Channels(ChnB).Properties("unit_string").Value Then '-- Calculate Resultant Call ChnMul("[1]/"&ChnA,"[1]/"&ChnA,"[1]/ChnAMultiplied") Call ChnMul("[1]/"&ChnB,"[1]/"&ChnB,"[1]/ChnBMultiplied") Call ChnAdd("[1]/ChnAMultiplied","[1]/ChnBMultiplied","[1]/Added") Call ChnCalculate("Ch(""[1]/Resultant"")=Sqr(Ch(""[1]/Added""))") '-- Cleanup Data.Root.ChannelGroups(1).Channels("Resultant").Properties("unit_string").Value = Data.Root.ChannelGroups(1).Channels(ChnA).Properties("unit_string").Value Data.Root.ChannelGroups(1).Channels("Resultant").Properties("description").Value = ChnA&" & "&ChnB&" Resultant" Data.Root.ChannelGroups(1).Channels("Resultant").Properties("Name").Value = ChnA&", "&ChnB&" Resultant" Call Data.Root.ChannelGroups(1).Channels.Remove("ChnAMultiplied") Call Data.Root.ChannelGroups(1).Channels.Remove("ChnBMultiplied") Call Data.Root.ChannelGroups(1).Channels.Remove("Added") Else Call MsgBoxDisp("Channel units do not match") End If Else Call MsgBoxDisp("Please only select two channels") End If
01-12-2018 01:48 PM
Hi TamerDTS,
I looked a bit for how to make a specific group the default and get to this help document. In addition to that I also found this other forum post where they discuss about finding a group index by its name.
I hope you can find this helpful!
01-12-2018 04:29 PM
Thanks for the tips. Unfortunately these won't work for my application. I'm trying to set the default group based off the channel selections the user made, or get the group index based off those selections. There doesn't seem to be a way to do this from the Data Portal selections.
01-13-2018 08:16 AM
You can get the group of a selected channel in that way: Data.GetChannel(Portal.Structure.Selection.Item(1).Name).ChannelGroup.Name (and for sure there are other ways too).
And to activate a group you just have to call this: Data.Root.ChannelGroups(Groupname).Activate.
01-15-2018 01:42 AM
Hi TamerDTS,
I suggest working with the current Data-API and Portal-API functionality and not using outdated function like CNo etc. In DIAdem 2017 you can use the example “Obsolete commands and variables” to locate those in your scripts.
Please find below my suggestion to your request. I hope this helps.
dim oPortalSelection, oResultA, oResultB, oResultAdd, oCurrGroupChns dim oChnA, oChnB, oResultant Call MsgBoxDisp("Please select two channels in the Data Portal") set oPortalSelection = Portal.Structure.Selection if oPortalSelection.Count = 2 Then ' check whether channels are selected if oPortalSelection(1).IsKindOf(eDataChannel) and oPortalSelection(2).IsKindOf(eDataChannel) then ' check whether channels are numeric if (oPortalSelection(1).DataType = DataTypeChnFloat64) and (oPortalSelection(2).DataType = DataTypeChnFloat64) then '-- Set Channels set oChnA = oPortalSelection(1) set oChnB = oPortalSelection(2) '-- Extra Error Checking If oChnA.UnitSymbol = oChnB.UnitSymbol Then set oCurrGroupChns = Data.Root.ActiveChannelGroup.Channels '-- Calculate Resultant set oResultA = ChnMul(oChnA, oChnA, "/ChnAMultiplied") set oResultB = ChnMul(oChnB, oChnB, "/ChnBMultiplied") set oResultAdd = ChnAdd(oResultA(1), oResultB(1), "/Added") ' prepare formula set oResultant = oCurrGroupChns.Add("Resultent", DataTypeChnFloat64) Dim sFormula, aSymbol(2), aValues(2) sFormula = "Res = Sqr(A)" aSymbol(1) = "Res" aSymbol(2) = "A" set aValues(1) = oResultant set aValues(2) = oResultAdd Call Calculate (sFormula, aSymbol, aValues) '-- Cleanup oResultant.UnitSymbol = oChnA.UnitSymbol oResultant.Properties("description").Value = oChnA.Name &" - " & oChnB.Name & " Resultant" oResultant.Name = oChnA.Name &"_" & oChnB.Name & "_Resultant" call oCurrGroupChns.Remove(oResultA(1).Name) call oCurrGroupChns.Remove(oResultB(1).Name) call oCurrGroupChns.Remove(oResultAdd(1).Name) Else Call MsgBoxDisp("Channel units do not match") End If Else Call MsgBoxDisp("Data type missmatch") End If Else Call MsgBoxDisp("Please only select channels") End If Else Call MsgBoxDisp("Please select two channels") End If
Greetings
Walter
01-15-2018 11:20 AM
Thanks Walter. This script is almost perfect. We just need to switch the active group right before the calculation. Let me see if I can add that line in.
01-15-2018 11:45 AM
Here's the test case that doesn't seem to work correctly:
Two groups in portal with different names, but all the channels have the same names. Group 2 is the Active group before running the script. The user selects two channels from Group 1. The script does calculate the correct channel, however the result channel ends up in the second group because that is the default group.
01-16-2018 12:30 AM
OK, in that case it seems to make sense to check also whether both channels are selected in the same group. Please try this.
Option Explicit 'Erzwingt die explizite Deklaration aller Variablen in einem Script. dim oPortalSelection, oResultA, oResultB, oResultAdd, oCurrGroupChns dim oChnA, oChnB, oResultant Call MsgBoxDisp("Please select two channels in the Data Portal") set oPortalSelection = Portal.Structure.Selection if oPortalSelection.Count = 2 Then ' check whether channels are selected if oPortalSelection(1).IsKindOf(eDataChannel) and oPortalSelection(2).IsKindOf(eDataChannel) then ' check whether channels are numeric if (oPortalSelection(1).DataType = DataTypeChnFloat64) and (oPortalSelection(2).DataType = DataTypeChnFloat64) then ' check whether channels selected in the same group if oPortalSelection(1).ChannelGroup.Name = oPortalSelection(2).ChannelGroup.Name then ' set channel group als default call oPortalSelection(1).ChannelGroup.Activate '-- Set Channels set oChnA = oPortalSelection(1) set oChnB = oPortalSelection(2) '-- Extra Error Checking If oChnA.UnitSymbol = oChnB.UnitSymbol Then set oCurrGroupChns = Data.Root.ActiveChannelGroup.Channels '-- Calculate Resultant set oResultA = ChnMul(oChnA, oChnA, "/ChnAMultiplied") set oResultB = ChnMul(oChnB, oChnB, "/ChnBMultiplied") set oResultAdd = ChnAdd(oResultA(1), oResultB(1), "/Added") ' prepare formula set oResultant = oCurrGroupChns.Add("Resultent", DataTypeChnFloat64) Dim sFormula, aSymbol(2), aValues(2) sFormula = "Res = Sqr(A)" aSymbol(1) = "Res" aSymbol(2) = "A" set aValues(1) = oResultant set aValues(2) = oResultAdd Call Calculate (sFormula, aSymbol, aValues) '-- Cleanup oResultant.UnitSymbol = oChnA.UnitSymbol oResultant.Properties("description").Value = oChnA.Name &" - " & oChnB.Name & " Resultant" oResultant.Name = oChnA.Name &"_" & oChnB.Name & "_Resultant" call oCurrGroupChns.Remove(oResultA(1).Name) call oCurrGroupChns.Remove(oResultB(1).Name) call oCurrGroupChns.Remove(oResultAdd(1).Name) Else Call MsgBoxDisp("Channel units do not match") End If else Call MsgBoxDisp("Channels needs to be selected from the same channel group") end if Else Call MsgBoxDisp("Data type missmatch") End If Else Call MsgBoxDisp("Please only select channels") End If Else Call MsgBoxDisp("Please select two channels") End If
Greetings
Walter