Solidworks C# Addin - Sending a string to a macro

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for 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:

  1. Create a text file that will serve as the shared file. For example, keyStoneAddinScratch.txt.

  2. 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();
  3. Call your macro using the RunMacro2 method and pass the file path as an argument:

    iSwApp.RunMacro2(macroPath + "Efficiency Interface.swp", "hitButton", procName, 0, out runMacroError);
  4. 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:

  1. Set the custom properties in your task-pane code:

    boolstatus = custPropMgr.Set("CustomPropertyName", textBox.Text);
  2. Access the custom properties in your macro code:

    Dim customProperty As String customProperty = ReadCustomProperty("CustomPropertyName")

    You can then use the customProperty string 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.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my