@@ -121,13 +121,13 @@ def __init__(self, db_file=None, private_appdata=False, **kwargs):
121
121
122
122
if BUILDING_GALLERY :
123
123
if len (INSTANCES ) != 0 :
124
- self . _app = INSTANCES [0 ]
125
- self . _app . new ( )
126
- self . _version = self . _app . version
127
- self . _disposed = True
124
+ instance : App = INSTANCES [0 ]
125
+ instance . _share ( self )
126
+ if db_file != None :
127
+ self . open ( db_file )
128
128
return
129
129
if len (INSTANCES ) > 0 :
130
- raise Exception ("Cannot have more than one embedded mechanical instance" )
130
+ raise Exception ("Cannot have more than one embedded mechanical instance! " )
131
131
version = kwargs .get ("version" )
132
132
self ._version = initializer .initialize (version )
133
133
configuration = kwargs .get ("config" , _get_default_addin_configuration ())
@@ -288,6 +288,37 @@ def version(self):
288
288
"""Returns the version of the app."""
289
289
return self ._version
290
290
291
+ def _share (self , other ) -> None :
292
+ """Shares the state of self with other.
293
+
294
+ Other is another instance of App.
295
+ This is used when the BUILDING_GALLERY flag is on.
296
+ In that mode, multiple instance of App are used, but
297
+ they all point to the same underlying application
298
+ object. Because of that, special care needs to be
299
+ taken to properly share the state. Other will be
300
+ a "weak reference", which doesn't own anything."""
301
+
302
+ # the other app is not expecting to have a project
303
+ # already loaded
304
+ self ._app .new ()
305
+
306
+ # set up the type hint (typing.Self is python3.11+)
307
+ other : App = other
308
+
309
+ # copy `self` state to other.
310
+ other ._app = self ._app
311
+ other ._version = self ._version
312
+ other ._poster = self ._poster
313
+ other ._updated_scopes = self ._updated_scopes
314
+
315
+ # all events will be handled by the original App instance
316
+ other ._subscribed = False
317
+
318
+ # finally, set the other disposed flag to be true
319
+ # so that the shutdown sequence isn't duplicated
320
+ other ._disposed = True
321
+
291
322
def _subscribe (self ):
292
323
try :
293
324
# This will throw an error when using pythonnet because
0 commit comments