Solidworks C# Addin - Sending a string to a macro

How to Send a String to a Macro in Solidworks C# Addin 🛠️
Are you struggling to send strings to your macros in Solidworks C# Addin? 😰 Don't worry, we have some easy solutions for you! 💡
Here's a common problem faced by Solidworks developers:
"I'm currently working on a new Solidworks task-pane and I want to include user input via text boxes in my macros. However, I cannot find a way to write a scratch file or import these strings into my macros. Is there another way to send these strings to my macros? My addin is written in C# and I'm using the Solidworks RunMacro2 method."
Now, let's dive into the solutions! 🏊♀️
Solution 1: Use a Shared File 💾
One way to send strings to your macros is by using a shared file. Here's how you can do it:
Create a text file that will serve as the shared file. For example,
keyStoneAddinScratch.txt.Write your strings to this file from your task-pane code:
StreamWriter writeText = new StreamWriter("C:\\keyStoneAddinScratch.txt"); foreach (var textBox in this.Controls.OfType<TextBox>()) { writeText.WriteLine(textBox.Text); } writeText.Close();Call your macro using the
RunMacro2method and pass the file path as an argument:iSwApp.RunMacro2(macroPath + "Efficiency Interface.swp", "hitButton", procName, 0, out runMacroError);In your macro code, read the strings from the shared file:
Dim fileContent As String fileContent = ReadFromFile("C:\\keyStoneAddinScratch.txt")You can then process the strings as needed.
Solution 2: Use Custom Properties 📋
Another option is to use custom properties to send your strings to the macros. Here's how:
Set the custom properties in your task-pane code:
boolstatus = custPropMgr.Set("CustomPropertyName", textBox.Text);Access the custom properties in your macro code:
Dim customProperty As String customProperty = ReadCustomProperty("CustomPropertyName")You can then use the
customPropertystring in your macro.
And there you have it! Two easy solutions to send strings to your macros in Solidworks C# Addin. Feel free to try them out and see which one works best for your specific requirements. 🚀
If you found this guide helpful, don't forget to share it with your fellow Solidworks developers! Let's spread the knowledge and make everyone's lives easier. 😄
Have any other Solidworks-related questions or need further assistance? Don't hesitate to leave a comment below! We're always here to help. 👇
Take Your Tech Career to the Next Level
Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.



