The extension attribute does not persist when reopening the Mechanical window
I am setting an extension attribute through
ExtAPI.ExtensionManager.CurrentExtension.Attributes["MyAttribute"] = myValue
However, the attribute does not persist when reopening the Mechanical window.
How can I save the attribute?
Answers
-
Check out this link
By default the change of an attribute happens only in the context of that current application. It does not push it to the "extension level" which operates across applications (i.e. multiple mechanical sessions). You can use the API calls to determine when to explicitly push the local application attribute values to the extension context and/or pull down the latest that may have been modified form another application.
Example:
ExtAPI.ExtensionMgr.CurrentExtension.SetAttributeValueWithSync("MyData", 2.)In addition when saving attributes to a custom ACT object (not the extension itself), you can use the Object.NotifyChange() to denote that current attribute values should be saved to the extension globally. I don't believe the changes will be persistent on close/reopen unless you do this NotifyChange on the object.
0 -
@Mike.Thompson said:
Check out this linkBy default the change of an attribute happens only in the context of that current application. It does not push it to the "extension level" which operates across applications (i.e. multiple mechanical sessions). You can use the API calls to determine when to explicitly push the local application attribute values to the extension context and/or pull down the latest that may have been modified form another application.
Example:
ExtAPI.ExtensionMgr.CurrentExtension.SetAttributeValueWithSync("MyData", 2.)In addition when saving attributes to a custom ACT object (not the extension itself), you can use the Object.NotifyChange() to denote that current attribute values should be saved to the extension globally. I don't believe the changes will be persistent on close/reopen unless you do this NotifyChange on the object.
Thank you for your response. Here is my alternative solution (before I learned that attributes can be saved).
import json import os import traceback def get_extension_name(): return ExtAPI.ExtensionManager.Extensions[0].Name def create_json_filepath(var_name): working_dir = ExtAPI.DataModel.Project.Model.Analyses[0].WorkingDir extension_name = get_extension_name() var_json_file = os.path.join(working_dir, "{}_ExtensionName={}.json".format(var_name, extension_name)) return var_json_file def write_json_data(file_path, data): with open(file_path, "w") as json_file: json.dump(data, json_file) def read_json_data(file_path): with open(file_path, "r") as json_file: json_data = json.load(json_file) return json_data def set_variable(name, value): var_json_file = create_json_filepath(name) try: write_json_data(var_json_file, value) except: err_str = ''.join(traceback.format_exception(*sys.exc_info())) ExtAPI.Log.WriteError("{}".format(err_str)) def get_variable(name): var_json_file = create_json_filepath(name) if not os.path.isfile(var_json_file): return try: json_data = read_json_data(var_json_file) return json_data except: err_str = ''.join(traceback.format_exception(*sys.exc_info())) ExtAPI.Log.WriteError("{}".format(err_str))
0