Destroy all agv in model before starting simulation

Destroy all agv in model before starting simulation

sakamoto_ryosuke
Collaborator Collaborator
4 Views
6 Replies
Message 1 of 7

Destroy all agv in model before starting simulation

sakamoto_ryosuke
Collaborator
Collaborator

[ FlexSim 20.1.3 ]

Hello, I want to destroy all the agv in the model before I start the simulation. So, at OnModelReset, I wrote a code such as below referring to the reference, but can not seem to get it right. What am I missing?

forobjecttreeunder(model(
{
     Object a;
     if(classobject(a) == library().find("?TaskExecuter"))
     {
         a.destory();
     }
}

Thank you in advance

0 Likes
Accepted solutions (1)
5 Views
6 Replies
Replies (6)
Message 2 of 7

patrick_zweekhorst
Advocate
Advocate
Accepted solution

Hi @ryosuke.s,

You are defining a variable called a, but this variable already exists in the forobjecttreeunder command. So you are overwriting the variable a. You could do something like Object object = a; But it is not directly needed. Your code should be something like this:

forobjecttreeunder(model( ) )
{   
     if( isclasstype( a, "TaskExecuter" ) == 1 && a.up.up.name != "FlowItemBin" )
     {
         print( a );
         Object object = a;
        object.destroy();
     }
}

Note the extra check in the if statement, where I check if the object is in the flowItemBin. Otherwise the code will not work correctly.

Good luck

Message 3 of 7

sakamoto_ryosuke
Collaborator
Collaborator

Hi @patrick.zweekhorst, Thank you for your support. I've tried your code, but when I click reset, only one agv is destroyed at a time. So, if I want to delete 4 agvs, I have to click reset button 4 times. I was thinking that forobjecttreeunder will call all objects in the model and destroy. Is my understanding incorrect?

destroyTE.fsm

0 Likes
Message 4 of 7

patrick_zweekhorst
Advocate
Advocate

Destroying things in the for loops seems to be stopping the for loop. You can try the following code:

Array teArray = [];
forobjecttreeunder(model( ) )
{   
     if( isclasstype( a, "TaskExecuter" ) == 1 && a.up.up.name != "FlowItemBin" )
     {
        teArray.push( a );
     }
}

for( int i = 1; i <= teArray.length; i++ )
{
    Object te = teArray[ i ];
    te.destroy();
}
Message 5 of 7

sakamoto_ryosuke
Collaborator
Collaborator

Hi @patrick.zweekhorst, thank you for your reply. However this code destroys not just the agv but operator and asrs vehicle as well....

0 Likes
Message 6 of 7

patrick_zweekhorst
Advocate
Advocate

Hi @ryosuke.s,

I assumed by your example code that all task executers you had in the model were AGVs. If that is not the case you will need to check if the te is actually an AGV. This can be done with the following code:

Array teArray = [];
forobjecttreeunder(model( ) )
{   
     if( isclasstype( a, "TaskExecuter" ) == 1 && a.up.up.name != "FlowItemBin" )
     {
         Object te = a;
         if( ownerobject( te.find(">variables/navigator/1").value ).name == "AGVNetwork" )
         {
             teArray.push( te );
         }      
     }
}

for( int i = 1; i <= teArray.length; i++ )
{
    Object te = teArray[ i ];
    te.destroy();
}


By the way. Are you creating the objects during the simulations run? If that is the case you could also just set the flag destroy on reset like this:

  switch_destroyonreset(newObject, 1)

Good luck

Message 7 of 7

sakamoto_ryosuke
Collaborator
Collaborator

@patrick.zweekhorst, the switch_destroyonreset worked fine, thank you. The other code gave me an error and didn't work.

0 Likes