How do I communicate with the Project Page from within Mechanical?
Using Workbench Mechanical scripting how can I get information from the project page? How can I make changes to the project page?
Best Answer
-
The answer is to utilize the wbjn module, whose source code can be found in the ACT installation directory (e.g. C:\Program Files\ANSYS Inc\v232\Addins\ACT\libraries\Mechanical). In theory, you can run project page commands from Mechanical like this:
import wbjn WB_cmds = '''My Workbench commands here''' wbjn.ExecuteCommand(ExtAPI,WB_cmds)
However, the above snippet is pretty useless so far, because:
A ) It does not allow us to return any information to Mechanical
B ) If the Workbench commands modified project in any way, Mechanical and/or Workbench would crash. This is due to a threading issue.To retrieve information from the Project Page and return it to Mechanical, we use the returnValue function. Here is a common example:
import wbjn WB_cmds = ''' my_directory = GetUserFilesDirectory() returnValue(my_directory) ''' UserDirectory = wbjn.ExecuteCommand(ExtAPI,WB_cmds)
If we want to make modifications to the Project Page, we need to interact with the Project Page on a separate thread from Mechanical. Here is an example to change the name of a system in the Project Page from within Mechanical:
import System from System.Threading import * def RunWBJN(cmds): def Internal_RunWBJN(): import wbjn wbjn.ExecuteCommand(ExtAPI,cmds) thread = System.Threading.Thread(System.Threading.ThreadStart(Internal_RunWBJN)) thread.Start() project_cmds = ''' system1 = GetAllSystems()[0] system1.DisplayText = "my name" ''' RunWBJN(project_cmds)
Here are some other examples:
https://discuss.ansys.com/discussion/2477/in-wb-mechanical-scripting-how-can-i-change-the-transformation-angles-of-an-external-data-file
https://discuss.ansys.com/discussion/2476/in-workbench-mechanical-scripting-how-can-i-get-the-location-of-an-external-data-file/p1?new=11