question

hermione12 avatar image
0 Likes"
hermione12 asked hermione12 commented

Create object using custom code

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



FlexSim 20.0.10
createcopyinter-arrival
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

Felix Möhlmann avatar image
0 Likes"
Felix Möhlmann answered hermione12 commented

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).

  1. if(Model.time == 0) // Only on first start
  2. {
  3.     // Get references to reuse them in the loop
  4.     Object destination = Model.find("FloorStorage1");
  5.     treenode flowItem = Model.find("/Tools/FlowItemBin/Box/1");
  6.    
  7.     for(int i = 1; i <= 25; i++)
  8.     {
  9.        Object item = createcopy(flowItem, model());
  10.        item.creationRank = i;
  11.        moveobject(item, destination);
  12.     }
  13. }

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


· 12
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.