Cancel
Showing results for 
Search instead for 
Did you mean: 

using an index with for cycle

Dear community,

 

i have to load multiple machine and i've written this code

If CNC_Charging=1 
	for var a := 1 to root.CNC.CNC_Buffer_IN_1.capacity 
		@.move(root.Tab_Reparti[1,2])
	next
	CNC_Charging:=CNC_Charging+1
end
If CNC_Charging=2 
	for var b := 1 to root.CNC.CNC_Buffer_IN_2.capacity 
		@.move(root.Tab_Reparti[2,2])
	next
	CNC_Charging:=CNC_Charging+1
end
If CNC_Charging=3 
	for var c := 1 to root.CNC.CNC_Buffer_IN_3.capacity 
		@.move(root.Tab_Reparti[3,2])
	next
	CNC_Charging:=CNC_Charging+1
end
If CNC_Charging=4 
	for var d := 1 to root.CNC.CNC_Buffer_IN_4.capacity 
		@.move(root.Tab_Reparti[4,2])
	next
	CNC_Charging:=CNC_Charging+1
end
If CNC_Charging=5 
	for var e := 1 to root.CNC.CNC_Buffer_IN_5.capacity 
		@.move(root.Tab_Reparti[5,2])
	next
	CNC_Charging:=CNC_Charging+1
end
If CNC_Charging=6 
	for var f := 1 to root.CNC.CNC_Buffer_IN_6.capacity 
		@.move(root.Tab_Reparti[6,2])
	next
	CNC_Charging:=CNC_Charging+1
end
If CNC_Charging=7 
	for var g := 1 to root.CNC.CNC_Buffer_IN_7.capacity 
		@.move(root.Tab_Reparti[7,2])
	next
	CNC_Charging:=CNC_Charging+1
end
If CNC_Charging=8 
	for var h := 1 to root.CNC.CNC_Buffer_IN_8.capacity 
		@.move(root.Tab_Reparti[8,2])
	next
end

 It's working but my question is: there is a way to make a more compact code with an index like the one below?

for a :=1 to root.CNC.CNC_Buffer_IN_(i).capacity  
	@.move(root.CNC.CNC_(i).pred)
next

where is the index of the number of machines?

Thank you.

1 REPLY

Re: using an index with for cycle

Siemens Valued Contributor Siemens Valued Contributor
Siemens Valued Contributor

Hi francescopoli!

 

I guess you can use an index to solve the loading of the machines, for example:

 

var MachineBuffer, Machine : object

 

MachineBuffer := str_to_obj("root.CNC.CNC_Buffer_IN_"+num_to_str(CNC_Charging)) -- addressing the machine buffer according to CNC_Charging input

 

Machine := str_to_obj("root.CNC.CNC_IN_"+num_to_str(CNC_Charging)) --addressing the machine according to CNC_Charging input

 

for local i := 1 to MachineBuffer .Capacity

@.move(Machine)

next

 

Hope that it helps!

 

Regards Johan