February 09, 2015

SolidWorks API "Hello World"

Well, this lesson appears to be one of the inevitable cliché in learning programming. It never gets old. So name it whatever you like. Unfortunately, we're not going to be learning a new programming language, or it will seem like. Coding a macro to automate your SolidWorks tasks does not require you to learn a special language for that purpose because there isn't one. 

What the guys at SolidWorks did was to put together an API (Application Programming Interface) which is a set of bundled functions and procedures (Methods is the word here) of the things you can accomplish SolidWorks in a programtical way. If you don't know what an API, go read about it here.

In short, an API functions like a process. The mention of process invoke three different things:

-Inputs: The stuff you input into the process
-Outputs: The expect set of results delivered by the process
-The added value: The API itself. It takes the inputs and does some stuff to and spits out a set of results.

With the API, you can harvest the power of SolidWorks. The Solidworks API gives you four programming languages of choice (I like to think of them as three):

-VBA (The little of brother of Visual Basic Dot Net)

So basically our HelloWorld tutorial is going to command the SolidWorks application to send a message to the user with the text "Hello World". We'll make it more exciting by setting the text to "Hello World, this is my first SolidWorks macro".

For the purpose of this tutorial, we're going to use VBA which stands for Visual Basic For Applications and I think you've guessed it by now. Application (Dropping the S here) in our case is going to be SolidWorks.

Kicking thing off, we'll launch the SolidWorks application.

  • Click on your Windows Start Button and in the search bar (If you're using Windows 7), type "SolidWorks". The first item in the search result should the SolidWorks application. 
  • Launch it by clicking on it
  • Once started, go to Tools > Macros > New... 
  • SolidWorks will prompt you a Save As dialog. Now, save our macro to a directory of your preference. Make sure that the file extension is .SWP. To do that, choose the file extension from the listbox below that of the file name of your macro. Your choice should be something like "SW VBA Macros". 

    Here's a screenshot to help you:
  • Once saved, the VBA editor will launch.
  • If you've done everything correctly, you'll be prompted a window like the following:
  • Let's go through that code for minute:
    Dim swApp As Object
    Sub main()
    Set swApp = Application.SldWorks
    End Sub
    So bascially Dim is like saying to VBA to declare a variable. For this code above, the name of the variable is SwApp short for SolidWorksApplication. Be aware that variable names cannot start with a numeric or have spaces in them.
    Declaring variables is something that gets old with time when you're programming a SolidWorks macro, however, it is a necessary evil, otherwise the VBA will not know what application we're using.
    And then, there is this "As" thing which I presume you'll guess it for the type of the variable.
    By default SolidWorks will declare the SwApp as an Object but in our case we're going to change that to SldWorks.SldWorks which is the SolidWorks object for the entire SolidWorks Application according the SolidWorks API documentation.
    By doing that, we've declared the Solidworks application variable. That means, we can now access and use the SolidWorks application. We can't do much with just the Solidworks application but it gets the job done for the Hello World tutorial.
    The next line is sub main()
    Basically, the sub stands for subroutine which is a procedure if you will. Think of a procedure as a block of instructions with a name. Subroutines and functions serve for the purpose of modular programming. They make your code reusable. So, I think we can be clear that we've declared a subroutine called "main".
    Now, the next line is: Set SwApp = Application.SldWorks
    Set is used whenever you want to reference a variable. With Set, we're telling the VBA that our variable SwApp is referenced to the SolidWorks Application using "Application.SldWorks".
    With that done, we can get to do the Hello World.
    We're going now to add the follow line to the code:
    Swapp.SendMsgToUser("Hello World, this is my first macro")
    What we did was that we basically told SolidWorks to use the method SendMsgToUser2 to send the message to the user with the string "HelloWorld, this is my first macro".
    The last line is end which tells the VBA that our main procedure has ended.
    Now to wrap everything up, your code should look like the following:
    Dim SwApp as SldWorks.SldWorks
    Sub main()
    Set SwApp = Application.SldWorks
    SwApp.SendMsgToUser("Hello World, this is my first macro")
  • With that, we're pretty much done with the programming part. Now to the execution! Click on the little green play button in the VBA editor or press F5. The end result should be something like:
    There you go my friend! Hooray! Hello World in a message box.

    If you have any questions, do not hesitate to ask!

No comments:

Post a Comment