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

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

nathan_s6
Not applicable
273 Views
2 Replies
Message 1 of 3

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

nathan_s6
Not applicable

[ FlexSim 20.0.2 ]

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")["Quantity"];
int xPos = Table("Equipment")["xPos"];
int yPos = Table("Equipment")["yPos"];
while (j < numResource){
//CREATE OBJECT HERE

//ASSIGN OBJECT TO GROUP
}
}

0 Likes
Accepted solutions (1)
274 Views
2 Replies
Replies (2)
Message 2 of 3

tanner_p
Not applicable
Accepted solution

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

Message 3 of 3

Matthew_Gillespie
Autodesk
Autodesk
Table table = Table("Equipment");
Group group = Group("Equipment");
int totalEquipment = table.numRows;

for (int i = 1; i <= totalEquipment; i++) {		
	int numResource = ["Quantity"];
	double xPos = table["xPos"];
	double yPos = table["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);
	}
}


Matthew Gillespie
FlexSim Software Developer