Usage of some expression functions in the script editor of components in vision

Good day,
I have tried to write the scripts in Script Editor that contain next functions:

  1. concat("part_path_A", "part_path_B")
  2. toStr(2)

while "concat" and "toInt' has worked perfectly in expression functions, I had been disappointed that I can't use them in script editors.

This force me to write super longue code (almost 100 lines) manually by listing all possible combinations inbetween "part_path_A", "part_path_B" and also there is some number inbetween them. I shell convert this number (I can take it from memory tag or pass it as parameter to graphic object) to string before combine

Is there any way to do it (such combination of strings) by usage of other existing functions, that do works well in Script Editor of graphic component in Vision?
Thank you very much in advance,

Welcome to the forum.

If you are writing a Python / Jython script then use the '+' operator for concatenation.
result = "part_path_A" + "part_path_B"

1 Like

Scripting in Ignition, as @Transistor mentioned, is Jython, which uses Python 2.7 syntax and has (most of) the Python 2.7 standard library available.

I'm certain that there is a better way, if you post your script, I am sure that we can help you out.

1 Like

Good day, Transistor, PGriffith, Irose,
Thank your for your replies.
It has actually helped.
I didn't know that
result = "part_path_A" + "part_path_B"
would actually work.

However, if I can't use functions that do transform numbers to string, it will help me just a little bit.

I have tried "toStr", that works in expressions.
Am I wrong with syntaxis? Is there another function?

I need to simplify this to avoid having hundreds of lines. I have different combinations of numbers after M and after Tab, that comes as parameters.


if event.source.selected == 1:
MKey = system.tag.readBlocking('[Bande]MKey')[0].value	
	if (MKey==2):
		path0 = "[Bande]M2/Tab1/SomeX_LB0"
		path1 = "[Bande]M2/Tab1/SomeX_LB1"
		path2 = "[Bande]M2/Tab1/SomeX_LB2"
		path3 = "[Bande]M2/Tab1/SomeX_LB3"
		path4 = "[Bande]M2/Tab1/SomeX_LB4"
		pathR = "[Bande]M2/Tab1/SomeX_LB5"
	if (MKey==3):
		path0 = "[Bande]M3/Tab1/SomeX_LB0"
		path1 = "[Bande]M3/Tab1/SomeX_LB1"
		path2 = "[Bande]M3/Tab1/SomeX_LB2"
		path3 = "[Bande]M3/Tab1/SomeX_LB3"
		path4 = "[Bande]M3/Tab1/SomeX_LB4"
		pathR = "[Bande]M3/Tab1/SomeX_LB5"
	if (MKey==4):
		path0 = "[Bande]M4/Tab1/SomeX_LB0"
		path1 = "[Bande]M4/Tab1/SomeX_LB1"
		path2 = "[Bande]M4/Tab1/SomeX_LB2"
		path3 = "[Bande]M4/Tab1/SomeX_LB3"
		path4 = "[Bande]M4/Tab1/SomeX_LB4"
		pathR = "[Bande]M4/Tab1/SomeX_LB5"
	if (MKey==5):
		path0 = "[Bande]M5/Tab1/SomeX_LB0"
		path1 = "[Bande]M5/Tab1/SomeX_LB1"
		path2 = "[Bande]M5/Tab1/SomeX_LB2"
		path3 = "[Bande]M5/Tab1/SomeX_LB3"
		path4 = "[Bande]M5/Tab1/SomeX_LB4"
		pathR = "[Bande]M5/Tab1/SomeX_LB5"
	if (MKey==6):
		path0 = "[Bande]M6/Tab1/SomeX_LB0"
		path1 = "[Bande]M6/Tab1/SomeX_LB1"
		path2 = "[Bande]M6/Tab1/SomeX_LB2"
		path3 = "[Bande]M6/Tab1/SomeX_LB3"
		path4 = "[Bande]M6/Tab1/SomeX_LB4"
		pathR = "[Bande]M6/Tab1/SomeX_LB5"
	if (MKey==7):
		path0 = "[Bande]M7/Tab1/SomeX_LB0"
		path1 = "[Bande]M7/Tab1/SomeX_LB1"
		path2 = "[Bande]M7/Tab1/SomeX_LB2"
		path3 = "[Bande]M7/Tab1/SomeX_LB3"
		path4 = "[Bande]M7/Tab1/SomeX_LB4"
		pathR = "[Bande]M7/Tab1/SomeX_LB5"
	if (MKey==8):
		path0 = "[Bande]M8/Tab1/SomeX_LB0"
		path1 = "[Bande]M8/Tab1/SomeX_LB1"
		path2 = "[Bande]M8/Tab1/SomeX_LB2"
		path3 = "[Bande]M8/Tab1/SomeX_LB3"
		path4 = "[Bande]M8/Tab1/SomeX_LB4"
		pathR = "[Bande]M8/Tab1/SomeX_LB5"
	if (MKey==9):

***** (to be continued)

What you've written there could be replaced exactly with:

if event.source.selected == 1:
	MKey = system.tag.readBlocking('[Bande]MKey')[0].value	
	path0, path1, path2, path3, path4, pathR = [
		"[Bande]M{}/Tab1/SomeX_LB{}".format(MKey, i)
		for i in xrange(6)
	]

But there's probably even better opportunities for optimization if you can post your full code.
Citations:

https://pyformat.info/

3 Likes

It is resolved.
I have found that I should just use function "str", and I works.

I had been confused a lot because expression functions that we have to use while writing expressions are not the same as somebody should use for Script writing in graphic objects.

Thanks a lot.
I has helped.

Scripting is a full programming language based on Python 2.7.

Expression Language is more like an Excel formula. You can't write a program with it - just an expression that returns a value. It can't write to another object.

Thank you very much PGriffith,
Small question for optimisation.
Last one.
If you need to do next - to read multiple variables like ....

V1 = system.tag.readBlocking(path1)[0].value
V2 = system.tag.readBlocking(path2)[0].value
V3 = system.tag.readBlocking(path3)[0].value
V4 = system.tag.readBlocking(path4)[0].value
V5 = system.tag.readBlocking(path5)[0].value

You have already made a full list of path (array of path), that you are interested in

path_all = [path1, path2, path3, ...]

How you will write a cycle to read all variables from those paths?

The system.tag.readBlocking function takes, as its first parameter, a list of tag paths. So you can just add all of the tag paths into a list and pass that in. The returned Qualified Values will be a parallel array in the order that you supplied the tag paths.

system.tag.readBlocking | Ignition User Manual

That is why even when reading one tag value you first need to index the return value like you did.

Just don't break the paths out with unpacking like I did in my example (to keep consistency with your script).

So, instead of:

	path0, path1, path2, path3, path4, pathR = [
		"[Bande]M{}/Tab1/SomeX_LB{}".format(MKey, i)
		for i in xrange(5)
	]

Do something like this:

paths = [
	"[Bande]M{}/Tab1/SomeX_LB{}".format(MKey, i)
	for i in xrange(6)
]
values = system.tag.readBlocking(paths)
# optionally, unpack to separate variables:
v1, v2, v3, v4, v5 = [qv.value for qv in values]

Note the comment from @rperretta about the return value from readBlocking - it's a list of qualified value objects, so if you just care about the value attribute you need to extra it.

3 Likes

This becomes:

values = [qv.value for value in system.tag.readBlocking(['path1','path2','path3','path4','path5'])]

and will give you a List with all of the values in the same order you provided the paths.

3 Likes

Wouldn't this need to be xrange(6) For LB0 ... LB5?

2 Likes