michaelthompson Posted February 6, 2014 Share Posted February 6, 2014 We have a need to perform conditional assignments within scripts that are piped to gpascii, but we can't find any conditional operator or function which works within the on-line command language. For example, we would like to do: conversionFactor=if(metricUnits,millimetersPerBaseUnit,inchesPerBaseUnit); or equally effective using a conditional operator: conversionFactor=metricUnits ? millimetersPerBaseUnit : inchesPerBaseUnit; Neither of these are supported (either in the command-line or script language). We thought we had a solution by using the cpx command: cpx if(metricUnits) conversionFactor=millimetersPerBaseUnit else conversionFactor=inchesPerBaseUnit but that doesn't work because the cpx command is asynchronous and we have no way from the command line to wait for it to complete before proceeding with other assignments which use the conversionFactor we wish to assign. We are looking for other ideas, suggestions, or consideration given to adding either a conditional operator (?:) or if() function to the expression evaluation. Link to comment Share on other sites More sharing options...
michaelthompson Posted February 6, 2014 Author Share Posted February 6, 2014 Incidentally, there are other options that are kind of ugly: // assume metricUnits is a 1 or 0 value // when metricUnits is a 1, conversionFactor = 1*millimeters+(1-1)*inches = millimeters // when metricUnits is a 0, conversionFactor = 0*millimeters+(1-0)*inches = inches conversionFactor=metricUnits*millimeters+(1-metricUnits)*inches One could also use an array where the value of metricUnits is the index into the array. Neither approach is as readable as a conditional operator or conditional function. Even the above could be more friendly if the following were allowed: conversionFactor=metricUnits*millimeters+(!metricUnits)*inches Link to comment Share on other sites More sharing options...
curtwilson Posted February 6, 2014 Share Posted February 6, 2014 There are a couple of options that should be satisfactory. If you use the "cx" (one-line, one-shot PLC program) construct as opposed to the "cpx" (one-line, one-shot motion program) construct that you tried, it will execute immediately and complete before the next command line in the thread is processed. In a straight on-line command, to work with a Boolean value, you could use the "^" exclusive-OR operator to get the effect that you want: conversionFactor = metricUnits*millimeters + (metricUnits^1)*inches Link to comment Share on other sites More sharing options...
michaelthompson Posted February 6, 2014 Author Share Posted February 6, 2014 Thanks Curt. We will give those a try. We had considered the cx command, but couldn't tell from the documentation whether it would be any different than the cpx command regarding its execution synchonization. It was getting late in our testing last night and I decided to reach out for help today instead of endlessly searching. I appreciate the fast response. It's still worth considering either a ?: and/or if() function evaluation in your future plans. Also, logical operations don't seem to be supported in the command-line parser. They would be handy on occasion. Link to comment Share on other sites More sharing options...
Recommended Posts