question

Nathan S6 avatar image
0 Likes"
Nathan S6 asked Matthew Gillespie answered

How to create objects through FlexScript and then assign them to a group?

I have a global table that contains a series of pieces of equipment, quantity, and x/y positions. The desire is to simply run a snippet of custom code at the beginning of the process flow that will then iterate through each resource, and then create an object in the model to represent the equipment, while also adding them to their associated group. I have been attempting to use the drawobject function to no avail.

Below is my code block so far:

for (int i = 1; i <= totalEquipment; i++){
int j = 0;
int numResource = Table("Equipment")[i]["Quantity"];
int xPos = Table("Equipment")[i]["xPos"];
int yPos = Table("Equipment")[i]["yPos"];
while (j < numResource){
//CREATE OBJECT HERE

//ASSIGN OBJECT TO GROUP
}
}

FlexSim 20.0.2
flexscriptgroupsobject creationassigncreate objects
5 |100000

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

tannerp avatar image
3 Likes"
tannerp answered

Hi @Nathan S6,

Here's a bit of code that will do what you're looking for:

for (int i = 1; i < 20; i++)	{
	Object newProcessor = createinstance(library().find("?Processor"), model());
	newProcessor.location.x = i;
	Group("Group1").addMember(newProcessor);
}

You didn't provide a model, so I am attaching an example with the code in the script box. You can run it and see that it works, then customize it to your needs.

new-objects-add-to-group.fsm


5 |100000

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

Matthew Gillespie avatar image
2 Likes"
Matthew Gillespie answered
Table table = Table("Equipment");
Group group = Group("Equipment");
int totalEquipment = table.numRows;

for (int i = 1; i <= totalEquipment; i++) {		
	int numResource = [i]["Quantity"];
	double xPos = table[i]["xPos"];
	double yPos = table[i]["yPos"];
	for (int j = 1; j <= numResource; j++) {
		//CREATE OBJECT HERE
		Object newObject = createinstance(library().find("?Processor"), model());
		newObject.setLocation(Vec3(xPos, yPos, 0), Vec3(0.5, 0.5, 0));

		//ASSIGN OBJECT TO GROUP
		group.addMember(newObject);
	}
}
5 |100000

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