Cancel
Showing results for 
Search instead for 
Did you mean: 

[UPDATE] Creating multiple parts in a Buffer on Initilisation

Hi All,

 

Hopefully a simple quick problem to solve!

 

I'm adding a function to my simulation that allows the user to input how many parts are created in certain buffers on initilisation of the simulation. I know you can use the 'create' code to create MUs in your desired location, and I've worked out you can just duplicate the code to create more than one part.

 

I was wondering if there was a simpler way to create more than one part in a buffer instead of repeating the code? This would make it easier to write the code that will allow the user to input their quantity of choice.

 

Thanks for any help!

 

Kind Regards,

 

Nathan

5 REPLIES

Re: Creating multiple parts in a Buffer on Initilisation

Valued Contributor
Valued Contributor

Hi Nathan,

 

You can create multiple parts in a buffer by using following SimTalk code.

 

is

i: integer;

do

for i:= 1 to 100 loop

.MUs.entity.create (Buffer);

next

end;

 

 

 

Thanks & Regards,

Ibrahim.

Re: Creating multiple parts in a Buffer on Initilisation

Thank you Ibrahim, that works perfectly!

 

Regards,

 

Nathan

Re: Creating multiple parts in a Buffer on Initilisation

Further to my original problem...

 

I am trying to set up the simulation so the user simply types in the number of the quanity of parts that are created in the buffer into a tablefile before the simulation is ran.

 

I will then have a button that is pressed to apply the changes. Obviously I would like to keep the code behind this button as simple as possible and make it so if I alter the original table in anyway that the code will still work.

 

I have tried the following, however it does not seem to work:

 

is
 i : integer;
 j : object;
 k : object;
 n : integer;
do
 
 j := BufferCap[3,i]; -- (Path to Buffer)
 k := BufferCap[4,i]; -- (Path to MU)
 n := BufferCap[2,i]; -- (Numerical value of no. of parts to be created in buffer)
  
 for i:=1 to BufferCap.YDim loop
  for i:=1 to n loop
  .k.create(j);
  next;
 next;
end;

 

I am still a beginner with SimTalk so are there any obvious issues I am missing? I have included the test simulation I'm working on for reference.

 

Thanks for any help,

 

Nathan

 

Re: Creating multiple parts in a Buffer on Initilisation

Gears Phenom Gears Phenom
Gears Phenom

Hello.

 

Their is a few things that will prevent your code that you posted from running how you like, first of all is that i is referenced in 2 for loops, the for loop will change the value of i every time it looks so if you have 2 loops inside eachover both depending on the same value then it will have unpredictable results.

 

Secondly, their is nothing to check the capacity of the buffers, so if the user presses the button multiple times then it will keep adding in more parts to the buffers until they are full. This can be fixed by comparing the number of parts in the buffer to your target number of parts as you fill it.

 

For every empty line in your table the program will get a VOID value inside both j and k, but you cant run methods on voids so this will cause the code to crash when this happens.

is
	j : object;
	k : object;
	n : integer;
do
	for local row := 1 to BufferCap.YDim loop
		j := BufferCap[3,row]; -- (Path to Buffer)
		k := BufferCap[4,row]; -- (Path to MU)
		n := BufferCap[2,row]; -- (Numerical value of no. of parts to be created in buffer)
		if j /= VOID and k /= VOID then
			while j.NumMuParts < n loop
				k.create(j);
			end;
		end;
	next;
end;

This code should loop through each row of the table using the value row to hold the row number.

 

Will then get the values j, k and n like before for that row.

 

It will check if the values for j and k are not VOID, if so then it can carry on and fill the buffer, if not then it dont have either a buffer or a part (or both) so cant continue.

 

Will loop through adding a part to the buffer while the number of parts within that buffer is less than the target number of parts for that buffer.

 

 

Only issue now from your model is that if the button to set the capacity is not pressed before you set parts within the buffer then it will prevent the code from running, this can be solved by adding in the following line at the top of the method (after the 'do') for the button to make it run the code from the other button.

 

Button1.OnClicked;

JamesM

Re: Creating multiple parts in a Buffer on Initilisation

This works perfectly, thank you so much JamesM.

 

Like I said I'm still learning this software and SimTalk from scratch so help like this makes me learn more all the time.

 

Much appreciated!

 

Nathan