SOLIDWORKS API: Easy Macros - Tree Display Keyboard Shortcut

SOLIDWORKS API: Easy Macros – Tree Display Keyboard Shortcut

Table of Contents

Easy Macro Introduction

There is a huge amount of capability available to the user from the
SOLIDWORKS APIs. Basically, most everything we have available in the graphic
user interface (GUI) is also available through the API interface.

But there are some road blocks that prevent most of us from dipping our toes
into the API waters. First, it’s programming. If you haven’t done much
programming, it can feel like a daunting task to get started. Second, it’s not
just programming – it uses Microsoft’s Component Object Model (COM),
which can seem even more daunting.

Rather than trying to spend a lot of time up front trying to understand
everything about programming and COM before writing anything, the intent of
this article is to jump head first into the shallow pool by showing the steps
to write a simple macro using Visual Basic for Applications (VBA). We have
some excellent earlier blog articles that go into more detail about the
concepts being used. Feel free to jump to those
SOLIDWORKS API articles
for reference.

So, let’s get started…

Tree Display Settings

SOLIDWORKS - Easy Macros | Tree Display Settings
This is a simple macro that is a convenience, rather than trying to automate
a repetitive task.Looking at an assembly document you have probably noticed,
and then ignored, the string of text off to the right of component names in
the assembly feature tree, right? By default, this is showing the
configuration name and display state name of the components. Seeing this
information is occasionally useful, but much of the time, it’s just
cluttering the screen and taking up valuable virtual real estate.
SOLIDWORKS Easy Macros | SOLIDWORKS API
The commands to turn this ON and OFF are pretty well buried… and they take
some fairly precise mousing to hit. Also, these commands are not available
in the ‘Customize’ command for assigning a keyboard shortcut. Let’s create
an easy macro and assign a keyboard shortcut to it for quick access.

Create a New Macro

This is a perfect case for using a macro assigned to a keyboard shortcut. It’s
actually a very simple macro to record or write. There are separate commands
for the configuration name and display state name. Let’s do the display state
name for this example, and write this one from scratch, rather than recording
it. It makes cleaner code.

To create a new, blank macro, just follow these steps:

  1. Open an existing assembly document with some components in
    the tree
  2. Select the Tools/Macro/New… command. A Browse dialog box
    will open asking you to give the macro a name and save it to some location.
    Call it ‘DispStateTreeDisplay.swp’ or whatever you like. You will want to
    save this in a convenient folder somewhere on your local C: drive. I have a
    ‘MACROS’ folder under ‘C:\SWX_COMMON’
  3. Make sure the ‘Save as type:’ drop down menu is set to ‘SW VBA Macros (*.swp)

When you hit the ‘Save’ button, the VBA Editor pops up – don’t panic. There
are only a few lines of code to write. Here’s what the screen should look
like:

Easy Macro | VBA Display State

Just type in the following lines (or better yet, copy and paste them from
here):

‘ **********************************************************

‘ * Show or Hide Display State name in Feature tree

‘ * Same as right-clicking the top of the feature tree and

‘ * selecting ‘Tree Display/Show Display State Names’

‘ * Works like a toggle. Run macro to turn it on or off.

‘ ***********************************************************

Option Explicit

Dim swApp As SldWorks.SldWorks

Dim Part As SldWorks.ModelDoc2

Dim swFeatMgr As SldWorks.FeatureManager

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

Set swFeatMgr = Part.FeatureManager

If (swFeatMgr.ShowDisplayStateNames) Then

swFeatMgr.ShowDisplayStateNames = False

Else

swFeatMgr.ShowDisplayStateNames = True

End If

End Sub

The green text is code that already existed in the new macro. You can leave
that text there and add the new, or delete out the existing text and replace
it with the text above.

Explanation of the Code

Comments

Any time you see a single quote mark, ‘ , anything that comes after that mark
on that line is a comment, meaning that it does not get run as an instruction
– it’s ignored by the code. It’s there for us humans.

DIM and SET statements

Generally, the DIM statement is for telling the computer to set aside some
memory to be used by a variable or ‘object’. It stands for ‘Declare in
Memory’. The ‘SET’ statement puts the data for the object allocated by the DIM
statement in that memory location (see the blog article
SolidWorks API Building Blocks – Part 2 for more information on this). In this case, those DIM and SET commands are
used for COM objects. In the case of our macro, here’s what that means: it’s
like telling the computer where to find the command that you want to use,
which, in this case, is the command to show or hide the display state in the
feature tree.

Rather than put all of the commands and data available with an application in
one big bucket, they set up a hierarchy – like a folder structure – to help
organize them. If you look at our blog article
SolidWorks API Building Blocks – Part 3, it is basically describing this folder-like structure of where SolidWorks
has logically organized commands. This is a simplification, but for this
simple macro, these DIM and SET statements are just giving us access to the
command that we need. You can use the API help file to figure out which
‘bucket’ a command resides in to use in your macro.

Showing and Hiding the Display State

The actual turning on and off of the display state from the feature tree is
done in the IF and ELSE part of the code. Anything that is a simple ON or OFF
is specified as either TRUE (ON) or FALSE (OFF). So the IF statement is just
asking if it is TRUE (ON) – and if it is, then make it FALSE. Otherwise, it
must already be FALSE, so make it TRUE. Pretty simple. The only tricky parts
are finding the command in the Help file, and the fact that we begin each
command with the name of the object that has the command. In this case
‘swFeatMgr.’

Keyboard Shortcuts

Now we’re ready to set up our keyboard shortcut. Make sure you have identified
your MACRO folder in ‘System Options/File Locations/Macros’.

  1. Select the ‘Tools/Customize’ command and then select the ‘Keyboard’ tab of
    the Customize dialog box.
  2. Select ‘Macros’ from the ‘Category’ drop down list. There should be a ‘New
    Macro Button’ row in the table.
  3. Click on the browse button (the three dots) and browse out to one of your
    macros you just saved.
  4. Now, click into the ‘Shortcut(s)’ column and type an unused key or key
    combination to your liking. I have ‘u’ for Display States.

Voila! Turn the display of this information on and off with just the press of
a key on the keyboard. You can create another one to show or hide the
configuration name, too. Same macro, just use
‘ShowComponentConfigurationNames’ instead of ‘ShowDisplayStateNames’.

Note: The state of this command is saved as part of
your assembly templates. So if you don’t want this ‘on’ by default when you
create a new assembly document, turn it off in your assembly templates!

If you’re interested in learning even more about SOLIDWORKS API, you might
also want to check out our online course –
SOLIDWORKS API Programming.

Hawk Ridge Systems Engineering Team

Hawk Ridge Systems Engineering Team

It often takes a team to solve a problem – and sometimes it takes a team to write about it. The Hawk Ridge Systems Engineering Team is comprised of our Product Managers, Applications Engineers, and Support Engineers. They've collaborated on this article to bring you the most accurate information about the solutions you use for design and manufacturing.
0 0 votes
Article Rating
Subscribe
Notify of
guest

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Sunil
Sunil
2 years ago

Thank you sir. I need a macro of parts+assembly which can run on any assembly. Please sugges the appropriate path. I need to solve this issue as soon as possible.

windows 10 sound crackling
windows 10 sound crackling
2 years ago

The API of the SolidWorks is having the different work which is having the best one to find out the execution for the particular part which can be working perfectly to found out the perfection.

solidworks cad dual monitors

Free Upgrades on SOLIDWORKS