question

George C2 avatar image
0 Likes"
George C2 asked George C2 commented

Code: How to check if an object exists in the model?

Goal: So I would like to check if a specific object with a specific name exists in the current model.

Q: How would I write an IF statement within a process flow custom code to query such a thing?

Current Code:

if(model().find("Operator1").first){
    //Do something
}else{
    //Do something else
}

> Do I need to make the "model().find..." part == #, NULL, or something else?

FlexSim 19.1.0
codemodelobjectexist
5 |100000

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

Jacob Gillespie avatar image
2 Likes"
Jacob Gillespie answered George C2 commented

Your current code should work fine.

There are only certain situations when you need to use objectexists(). Use it whenever you are using a non dot-syntax command which returns a node. For example:

if(objectexists(node("Operator1", model()))){
    //Do something
}else{
    //Do something else
}

--

If you are asking how to first check if Operator1 exists you can do this:

if(Model.find("Operator1") && Model.find("Operator1").first){
    //Do something
}else{
    //Do something else
}

Or this:

if(Model.find("Operator1")?.first){
    //Do something
}else{
    //Do something else
}
· 1
5 |100000

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

George C2 avatar image George C2 commented ·

Thanks for the help!

0 Likes 0 ·
Aaron C avatar image
0 Likes"
Aaron C answered

Try:
if(objectexists(Model.find("NameOfObject")))
{
// Do Something
}

5 |100000

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

José Antonio MD avatar image
0 Likes"
José Antonio MD answered

Hello @George C2,

FlexSim has a specific function for that:

objectexists()

Its value will be 1 if it exists in your model and 0 if not..

I hope I've been helpful.

5 |100000

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

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

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