Hi Dominik,
thanks for your patience. The problem in the implementation, especially the crash during the execution of the shutdown command or also "Close all windows" results in a recursive trigger of the event, since all models are closed simulantously, triggering a retrievel of the related models and triggering another round. This results in an endless loop of closing.
Please find below a proposal on how to solve that issue. The idea is
a) to stop the event from being triggered in case related models are found using a global variable that is toggled
b) do not perform the CLOSE AdoScript within the event but only store a list of related, to-be closed model IDs
c) trigger an update action within the set of models
d) close the related, open and to-be-closed models in the UpdateActions event and reset the global variables.
Some code snipplets can be found below - let me know what you think on that approach!
1# init the needed global Variable
2ON_EVENT "AppInitialized" {
3 # Check wheter the Window Discard event should be triggered or not
4 SETG bPerformCloseEvent:1
5 # Global Variable to list all open and related models to the one to be closed and close if anything in there plus clean up
6 SETG lRelatedOpenModels:""
7 # other things in the eve
8 ....
9}
Modify the event "BeforeDiscardModelWindow" to only run in case the global variable bPerformCloseEvent is 1
1ON_EVENT "BeforeDiscardModelWindow" {
2 # only trigger the event if set accordingly in parent event, no recursion should be done
3 IF (bPerformCloseEvent) {
4 EVENT_LOG msgType:"EVENT_LOG" message: ("BeforeDiscardModelWindow")
5 EXECUTE file: (g_path_asc + "before_discard_model_window.asc") scope:same
6 }
7}
Within the script "before_discard_model_window.asc", collect the related models, check if they are open and create a list, set the list in the global variable lRelatedOpenModels. In case the list is not empty after the search, trigger an UpdateAction by activating a model
1# search for models related to the one open
2SET lRelatedModels: ("")
3FIND_MODEL_ID (activeModelId) modelType: (modelNameObjDecomp) resultModelId:modelIdObjDecomp
4SET lRelatedModels: (tokunion(lRelatedModels, STR modelIdObjDecomp))
5FIND_MODEL_ID (activeModelId) modelType: (modelNameIAS) resultModelId:modelIdIAS
6SET lRelatedModels: (tokunion(lRelatedModels, STR modelIdIAS))
7FIND_MODEL_ID (activeModelId) modelType: (modelNameTransDecomp) resultModelId:modelIdTransDecomp
8SET lRelatedModels: (tokunion(lRelatedModels, STR modelIdTransDecomp))
9FIND_MODEL_ID (activeModelId) modelType: (modelNameVES) resultModelId:modelIdVES
10SET lRelatedModels: (tokunion(lRelatedModels, STR modelIdVES))
11
12SET lRelatedOpenModels: ("")
13FOR sRelatedModel in: (lRelatedModels) {
14 CC "Modeling" IS_OPENED modelid: (VAL sRelatedModel)
15 IF (isopened = 1) {
16 SET lRelatedOpenModels: (tokunion(lRelatedOpenModels, sRelatedModel))
17 }
18}
19
20IF (tokcnt (lRelatedOpenModels) > 0 ) {
21 SETG bPerformCloseEvent:0
22 # remember open models, that need to be closed and do so in UpdateActions when modelid != -1
23 SETG lRelatedOpenModels: (lRelatedOpenModels)
24 # need to trigger ONE updateActions event
25 CC "Modeling" GET_ACT_MODEL
26 # find a model but not the current active one, since activating the active one does not trigger an event
27 SET possibleActivates: (tokdiff (lRelatedOpenModels, STR modelid))
28 CC "Modeling" ACTIVATE_MODEL (VAL token(possibleActivates, 1))
29}
Perform the close in the UpdateAction and reset the global variables
1ON_EVENT "UpdateActions" {
2 # only consider this event if the modelid != -1, this means that a model is open during the update, this should exclude already the close all and shutdown crashes
3 IF (modelid != -1) {
4 # check for the the lRelatedOpenModels global variable, this would mean that a close action was detected, and the necessary shutdown should happen
5 IF (tokcnt (lRelatedOpenModels) > 0) {
6 EVENT_LOG msgType:"EVENT_LOG" message: ("UpdateActions for modelid: " + STR modelid + " count " + STR (tokcnt (lRelatedOpenModels)))
7 # make sure to only run the shutdown once!
8 FOR sRelatedOpenModel in :(lRelatedOpenModels) {
9 CC "Modeling" SET_MODIFIED modelid: (VAL sRelatedOpenModel)
10 CC "Modeling" SAVE modelid: (VAL sRelatedOpenModel)
11 CC "Modeling" CLOSE modelid: (VAL sRelatedOpenModel) quiet
12 }
13 # clean up lRelatedOpenModels
14 SETG lRelatedOpenModels:""
15 # turn on event listing again
16 SETG bPerformCloseEvent:1
17 }
18 }
19}