Blog 8.7.2012 No Comments

ISldWorks::RunCommand is probably the most powerful call in the entire SolidWorks API. Using this single, easy-to-use API call you can execute over 3,000 individual SolidWorks commands. Need to copy and paste an object? ISldWorks::RunCommand. Need to bring up a feature’s PropertyManager page? ISldWorks::RunCommand. Need to hide the FeatureManager tree? ISldWorks::RunCommand. That’s right—many actions that seemingly have no corresponding API call can be fired using this incredibly versatile method.

But you know what’s amazing? Most API programmers don’t even know it exists! I was one of those programmers for much longer than I would like to admit. Let’s prevent that from being your case as well.

The Basics

First of all, you may have noticed in the API Help a command very similar to ISldWorks::RunCommand called IModelDocExtension::RunCommand. The only difference between the two is that the former can also simulate mouse clicks using the members of swMouse_e, whereas the latter cannot. Keep things simple by always using ISldWorks::RunCommand, which requires these two arguments:

CommandID – This is where you specify the desired SolidWorks command as defined in the swCommands_e enumeration, which contains the list of the 3000+ commands I mentioned earlier.

NewTitle – This is an arbitrary title that you pick that will appear in the title of PropertyManager pages, should you use ISldWorks::RunCommand to open such a page. If you have no need to specify a title, you can set it to Empty.

Finally, the return value is a True or False depending on whether the command successfully runs.

Example 1: Hiding the FeatureManager tree

I see this one in the API forums occasionally. Within SolidWorks, you can click the little tab with three arrows on it, and this will hide or show the FeatureManager tree. Search through the API all you want and you will never find an API call that shows or hides the FeatureManager tree. Yet if we go to the swCommands_e listing and search for the keyword “tree” then we will eventually come across swCommands_Hideshow_Brwser_Tree.

So what will this look like in our code? Try this out:

Dim swApp As SldWorks.SldWorks
    Sub main()
    Set swApp = Application.SldWorks
    Debug.Print swApp.RunCommand(swCommands_Hideshow_Brwser_Tree, Empty)
End Sub

The most difficult part, as you can tell, is actually finding the correct command in swCommands_e. My advice is to search for simple, obvious words like “tree” rather than “FeatureManager”. Nevertheless you may have to test out different commands before you find the right one.

Example 2: Displaying a feature’s PropertyManager page

Whereas most automation macros are not concerned with the user interface, other macros may want to involve the user at some point along the way. In that case it may be useful to bring up the appropriate feature PMP or dialog box. In this case, we want to display the extruded boss/base PMP. Notice that the PMP title is changed to “test”:

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    swApp.RunCommand swCommands_Fillet, "Test"
End Sub

If you want to see this example taken to the next level, check out our free macro “Run individual SolidWorks commands” which uses ISldWorks::GetRunningCommandInfo to return the information about the open PMP.

Example 3: Move a BOM to a different sheet

Ever wanted to move a BOM using the API? Good luck trying to do with IDrawingDoc or any other drawing-related interface. Instead you need to replicate with RunCommand what you would do manually, which is cut and paste the BOM. To use this example, open up a new drawing with two sheets, one of which is named Sheet2. Select the BOM and run this code.

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swDraw = swModel
    swApp.RunCommand swCommands_Cut, Empty
    swDraw.ActivateSheet "Sheet2"
    swApp.RunCommand swCommands_Paste, Empty
End Sub

You might notice that the BOM is being pasted at the location of your cursor. If you’re a premium member, check out the version of this macro that programmatically selects the BOM so that the user need not pre-select it and also positions the BOM at a precise location on the drawing.

That concludes our look at what I call the “Swiss Army knife of the SolidWorks API”. Please share your comments and questions below.

Commanding,
Keith

Want to keep up with new CADSharp.com content? Sign up for our newsletter.


Questions and Comments

You must be logged in to post a comment.