Reputation: 101
As part of some workflow optimizations, I'd like to map a large number of Solidworks functions to hotkeys. The program itself has the capability to assign functions to hotkeys, but it's only a small fraction of the whole command set. The rest can be assigned to hotkeys by a workaround where a Macro is created that runs that function and assigning that Macro to a hotkey.
Macros are created in Solidworks one of two ways:
Within Solidworks a series of commands can be recorded and then they are saved as a .swb file. Running that Macro repeats the recorded command(s). (This method is buggy and really hit or miss)
A Macro can be created directly by using Microsoft Visual Basic for Applications which Solidworks uses as a Macro editor. The resulting file is again a .swb file and running it executes the Solidworks functions that it calls. This method is the easiest way to create multiple hotkeys because all I need to do is edit one line of the code and I can execute any function that I want.
Here is what that looks like:
Dim swApp As Object
Dim Part As Object
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
swApp.RunCommand swCommands_Leader_Add_Bent, ""
End Sub
The above Example Macro, in VB, adds a jog point to the selected leader line. The line below is the one responsible for executing the Solidworks function.
swApp.RunCommand swCommands_Leader_Add_Bent, ""
For example, changing swCommands_Leader_Add_Bent
to swCommands_Select_Midpoint
now makes the code select the midpoint of a line or edge.
Using that method I can create Macros that can run any of Solidworks' three thousand or so commands. The issue is, the process is a little tedious, and doing it for the dozens or hundreds of commands would be time-consuming. I have the list of all SolidWorks functions so ideally, I'd like to feed that list to some python code, the code edit the swApp.RunCommand
line with a function from the list, and that code output .swb files that are Macros that are ready to be assigned to hotkeys.
Some other comments:
I'm not exactly sure what kind of file a .swb file is so forgive me if my terminology is off. I know they are not a text file as opening it up in a text editor results in nonsense. They are edited with Microsoft Visual Basic for Applications 7.1. Solidworks calls them SW VBA Macros.
I am not looking to Run the macros or the VBA code itself from python. I have already gotten that working and it too slow for my needs. I want to use python to generate the VBA files.
I have searched around and have not been able to find a method that lets python edit, write to, or generate VBA files.
The solution does not have to be in python, but that is the language that I am most familiar with. I am open to other languages or methods.
In conclusion, I am looking to automate the creation of a large number of SW VBA Macros (*.swb), can y'all provide any guidance or suggestions?
Upvotes: 0
Views: 465
Reputation:
If I have understood your question correctly, the following should work (not tested!!);
You could create a function to substitute any command name into the code that you provided, like this;
def macro_code_text(command_name):
return f"""
Dim swApp As Object
Dim Part As Object
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
swApp.RunCommand {command_name}, ""
End Sub
"""
..And then you could create a list containing all your command names;
command_names = ['swCommands_Leader_Add_Bent', 'swCommands_Select_Midpoint']
..And then iterate over the list, saving the resulting texts to files with the '.swb' extension;
generic_file_name = 'my_solidworks_macro_'
for i, command_name in enumerate(command_names):
with open(generic_file_name + str(i) + '.swb', 'w+') as file:
file.write(macro_code_text(command_name))
..This should produce a number of files called 'my_solidworks_macro_0', 'my_solidworks_macro_1' etc. each with a different command name from the list you provided.
Upvotes: 0