Problems with GlobalVar during runtime

All topics related to questions in Pedestrian Dynamics
Post Reply
fabianmirz
Posts: 6
Joined: Wednesday 06 March, 2019 - 15:22

Problems with GlobalVar during runtime

Post by fabianmirz » Thursday 07 March, 2019 - 13:41

Hi,

i am working on a model where agents are send to a waiting location and when a pre-definded number of agents are located at the waiting location, some will be sent back to an entry_exit location.
Based on a GlobalVar [globalAgentsFP], the Agent Generator should sent a different number of agents to the waiting location.
Actually, the initialization of the variable seems to have some problems during run time. I init the GlobalVar in a Entry_Exit Location at the Activity Time Field, this is not optimal but i wanted to prevent the GlobalVar being initialised multiple times or affected by a trigger.
So does anyone have a suggestion where to define a GlobalVar optimally?
A sample of the model i am working at is attached.

Thank you in advance and best regards
Fabian
Attachments
globalVar.mod
(127.43 KiB) Downloaded 30 times

User avatar
marlies
Posts: 300
Joined: Monday 17 January, 2011 - 09:28

Re: Problems with GlobalVar during runtime

Post by marlies » Thursday 07 March, 2019 - 13:47

Hi Fabian,

Global variables are best defined in the following location:

Menu > Modeling > User Functions.

You will see a field that says "initialization code". You can use that field to declare global variables. The code will be executed only once on initialiazation and not during the run.

Hope this helps!

Kind regards, Marlies

fabianmirz
Posts: 6
Joined: Wednesday 06 March, 2019 - 15:22

Re: Problems with GlobalVar during runtime

Post by fabianmirz » Thursday 07 March, 2019 - 15:35

Hi Marlies,

many thanks for your help, this solved my problem.

best regards
Fabian

fabianmirz
Posts: 6
Joined: Wednesday 06 March, 2019 - 15:22

Re: Problems with GlobalVar during runtime

Post by fabianmirz » Saturday 22 June, 2019 - 13:23

Hello,

I tried to initialize the globalVar with a value which is extracted from an excel sheet.
I found some helpful code here, but something is wrong when I try to access an excel file - the globalVar seems not to be initialized after running this code during the start of PD. What is the best way to access an excel sheet here to extract some cell values?

Many thanks for your help in advance.
best regards

Fabian


do(
var([strExcelFile], vbString),
var([excelOLE], vbOle),

excelOLE := 0,

strExcelFile := [C:\users\user\desktop\sheet1.xlsx],

{Check if file is existing, otherwise give an error}
if(
Not(FileExists(strExcelFile)),
do(
Msg(Concat([File: ], strExcelFile, [, does not yet exist!], 3)),
return
),

{**Open existing**}
do(
{** Note that it is necessary for the Ole connection that all "\" in the path are replaced with a double one "\\"! **}
strExcelFile := StringReplace(strExcelFile, [\], [\\], SR_REPLACE_ALL),

excelOLE := CreateOLE([Excel.application]),
Ole(excelOLE, [visible], [0]),
Ole(excelOLE, Concat([WorkBooks.Open(], sbo, strExcelFile, sbc, [)]))
)
),

{**Get data**}
GlobalVar([seatedEG], ExcelRef(1,3)),
{** Check value seatedEG **}
Msg(seatedEG),

{**Close and save the workbook**}
Ole(excelOLE, [ActiveWorkbook.Save]),
Ole(excelOLE, [ActiveWorkbook.Saved], True),
Ole(excelOLE, [ActiveWorkbook.Close]),
Ole(excelOLE, [Application.Quit]),
excelOLE := 0
)

fabianmirz
Posts: 6
Joined: Wednesday 06 March, 2019 - 15:22

Re: Problems with GlobalVar during runtime

Post by fabianmirz » Saturday 22 June, 2019 - 18:18

I solved it, if anyone has similar problems:

do(
var([strExcelFile], vbString),
Dim([excelOLE], vbOle),
var([valNrScenarios], vbValue),

excelOLE := 0,

strExcelFile := [C:\users\s4famirz\desktop\fopra\sheet1.xlsx],

{Check if file is existing, otherwise give an error}
if(
Not(FileExists(strExcelFile)),
do(
Msg(Concat([File: ], strExcelFile, [, does not yet exist!], 3)),
return
),

{**Open existing**}
do(
{** Note that it is necessary for the Ole connection that all "\" in the path are replaced with a double one "\\"! **}
strExcelFile := StringReplace(strExcelFile, [\], [\\], SR_REPLACE_ALL),

excelOLE := CreateOLE([Excel.application]),
Ole(excelOLE, [visible], [0]),
Ole(excelOLE, Concat([WorkBooks.Open(], sbo, strExcelFile, sbc, [)]))
)
),
{Activate sheet}
Ole(excelOLE, Concat([Worksheets(],Sbo,[Sheet1],Sbc,[).Activate])
),

{** Init globalVar with data from excelOLE**}
GlobalVar([seatedEG], vbValue),
seatedEG := Ole(excelOLE, Concat([Cells(],String(1),[,],String(3),[).Value])
),

{**Close and save the workbook**}
Ole(excelOLE, [ActiveWorkbook.Save]),
Ole(excelOLE, [ActiveWorkbook.Saved], True),
Ole(excelOLE, [ActiveWorkbook.Close]),
Ole(excelOLE, [Application.Quit]),
excelOLE := 0
)

User avatar
Ignasi Echaniz
Posts: 16
Joined: Monday 28 May, 2018 - 10:44

Re: Problems with GlobalVar during runtime

Post by Ignasi Echaniz » Monday 24 June, 2019 - 09:32

Hi Fabian,

Thanks a lot for sharing your solution with the community. Nice that you are using Pedestrian Dynamics complex scripting functions!

Indeed it is a good practice to activate your excel sheet and access the data through OLE function.

For others users, a good practice tips when retrieving and sending information to/from Excel from Pedestrian Dynamics are:
- Make sure to set the right directory of your excel file in your computer
- Activate the right sheet
- Be aware the type of cell your are importing (value or string)

Kind regards,

Ignasi Echaniz

Post Reply