Create object using custom code

Create object using custom code

anjunittur123
Not applicable
3,253 Views
13 Replies
Message 1 of 14

Create object using custom code

anjunittur123
Not applicable

[ FlexSim 20.0.10 ]

In my model, I want to generate 25 items on floor storage type rack using code (no process flow).

Unless I connect source to floor storage, it is not generating any item. After I connect source and use the createcopy(), it is generating 150 items for the one time looping in the source inter-arrival code.

Also I want to assign label to each item through code. The labels should be assigned during the generation of the items.

Can you please help? @Felix Möhlmann

Create_object_using code.fsm



0 Likes
Accepted solutions (1)
3,254 Views
13 Replies
Replies (13)
Message 2 of 14

FelixMoehlmann
Mentor
Mentor
Accepted solution

Where did you put the code before adding the source?

You always need some event to trigger the execution of the code. The 'On Run Start' model trigger might be a good option for this.

1658483747340.png

Instead of creating the item directly in the storage, you might want to first create them in the model and then move them into the storage ('moveobject(object, destination)'). Otherwise the placement logic won't work correctly.

The 'createcopy()' command returns a reference to the created object (which you already assign to the 'item' variable). As such you can assign labels with the normal syntax (item.labelName = value).

if(Model.time == 0) // Only on first start
{
    // Get references to reuse them in the loop
    Object destination = Model.find("FloorStorage1");
    treenode flowItem = Model.find("/Tools/FlowItemBin/Box/1");
   
    for(int i = 1; i <= 25; i++)
    {
       Object item = createcopy(flowItem, model());
       item.creationRank = i;
       moveobject(item, destination);
    }
}

Because you put the code in the inter-arrival time but don't return any value (the time value that is expected from the code), the value is assumed to be zero and the inter-arrival time triggers again immediately. This repeats until the storage is full.

create-object-using-code-fm.fsm

0 Likes
Message 3 of 14

anjunittur123
Not applicable

Thank you @Felix Möhlmann , If i want to assign label in the code to each box?

Also I want to generate boxes on top of pallets, how to do this purely with code OnRunStart?

0 Likes
Message 4 of 14

FelixMoehlmann
Mentor
Mentor

"If i want to assign label in the code to each box?"

That is what the code in my answer does. Could you specify what should be different to the way the way that code works?

To generate the boxes on pallets, create a pallet. For that just adjust the path in the find() command to refer to the pallet flow item instead. Then move the box into the pallet with 'moveobject(object, destination)'.

0 Likes
Message 5 of 14

anjunittur123
Not applicable
Yes , I am able to generate boxes on pallets using the commands. Thank you

"To assign label"

I want to assign particular part number to each part. The creationrank assigns unique numbers to all the parts. Similarly I want to assign part name as "Part1", "Part2"...

How to do that?

0 Likes
Message 6 of 14

FelixMoehlmann
Mentor
Mentor

The for-loop works by incrementing an index variable 'i'. You can use that variable to determine which value should be assigned to the label. This could be the number itself, like above.

Or the data could be stored in a table and the index is used as the rows number.

item.labelName = Table(...)[column];

To use the number as part of a text, such as 'Part1', you first have to convert it into a string variable.

item.labelName = "Part" + string.fromNum(i);

The name of an item can be changed similar to labels, through the 'name' property.

item.name = "...";
0 Likes
Message 7 of 14

anjunittur123
Not applicable
Thank you @Felix Möhlmann it was helpful
0 Likes
Message 8 of 14

anjunittur123
Not applicable

Hi @Felix Möhlmann , I am storing part destination data in global table, what is the command to reference it using code?

I am using Model.find() as of now to refer each buffer. I have 10 buffers to send parts. In that case, how can I read the row and column from global table and use it to find the destination of that part?

0 Likes
Message 9 of 14

FelixMoehlmann
Mentor
Mentor
Table(tableName)[row][column]

If you first need to find the correct row by looking up a value in a different column, you can use

Table(tableName).getRowByKey(keyValue, keyColumn)

The key value could be a label value or the name, etc.

So together it would be something like this:

int row = Table(tableName).getRowByKey(keyValue, keyColumn);
string destName = Table(tableName)[row]['destColumn'];
Object destination = Model.find(destName);

All in one command

Model.find(Table(...)[destColumn])
0 Likes
Message 10 of 14

anjunittur123
Not applicable

I have written the code OnRunStart

1658743479525.png

But I am getting this error.

Image.png

What keyvalue should be used?

0 Likes
Message 11 of 14

FelixMoehlmann
Mentor
Mentor

You declare (or 'create') the 'item' variable inside the for-loop. It is therefor only valid inside that 'scope'. A scope is defined by the {}-parentheses. The entire if-statement is also a scope. You can use variables in a scope that is nested within (like how you can use 'destination' inside the for-loop, but not the other way around).

1658743843390.png

Because 'item' is not defined at that point, 'KitName' can't be read from it (one of the errors) and 'getRowByKey()' returns 0, which is not a valid row number (first error).

What is your goal here? If you want to assign 'destination2' to the item, then that should also happen within the for-loop. Iin this case you also already know the row through the index 'i'.

0 Likes
Message 12 of 14

anjunittur123
Not applicable

I agree with you @Felix Möhlmann

My goal here is to refer the destination from global table to be sent to buffer. In this example model below, I want to send the item to 2 buffers referencing from the global table "destination" column.

Can you tell me what mistake I am doing here?

create-object-using-code.fsm

0 Likes
Message 13 of 14

FelixMoehlmann
Mentor
Mentor

You correctly determine the destination object but you don't do anything with that information. The code just ends after the 'destination2' variable is declared.

If you need the reference to the destination object later, for example in a process flow, you should write it to a label on the item.

If you know that the dest. object is connected to the current object via an a-connection, you can also directly assign the corresponding port number with 'ipopno()' and use that in the 'Send to Port' field.

You can of course also combine these: Assign the object reference to a label and then use 'ipopno' in Send to Port.

create-object-using-code(1).fsm

0 Likes
Message 14 of 14

anjunittur123
Not applicable
Thank you for the help and for solving the queries.
0 Likes