Ever felt the need to add a center of mass point to your SOLIDWORKS part, but don’t know how to? You’re in the right place. In this guide, you’ll learn how to add center of mass to a part in SolidWorks.
Introduction to the Center of Mass (COM)
The center of Mass (COM) is a fundamental characteristic that is the mean location of the mass of a body. It is important to understand COM in order to study a part’s stability, balance, and inertia. Whether you are designing a very sophisticated assembly for aerospace or a simple bracket for a consumer product, the COM helps you to predict how the part will behave under different forces and conditions.
Although SOLIDWORKS will calculate the COM for you automatically, wouldn’t it be convenient to have a permanent visual reference point within your part file? That is where a simple VBA macro comes in.
This article will guide you through a SOLIDWORKS macro that adds a Center of Mass point for you automatically. It’s gonna be easy to refer to in drawings, sketches, and assemblies alike.
Why Use a Macro for This?
SOLIDWORKS includes calculators for the Center of Mass, but they require several clicks and do not create a persistent feature in the tree. That can be annoying if you are going to need to refer to the COM frequently.
A macro removes that step. With one click, you can:
- Save time: No more searching menus for the COM.
- Improve the workflow: The COM point is included as a feature and becomes a permanent addition to your design.
- Increase collaboration: The COM point is exposed to anyone who opens the part file, so everyone is on the same page.
The VBA Macro: Your Toolkit
The following VBA macro is a quick fix to create a COM point. We will make sure that you have all the requirements set up before you write the code.
System Requirements
- SOLIDWORKS Version: SOLIDWORKS 2017 and later.
- Operating System: Windows 7, 8, 10, or later.
- VBA Environment: SOLIDWORKS includes a built-in VBA environment. You can get to it by going to Tools > Macro > New or Edit.
- Other Libraries: None. This macro uses only standard SOLIDWORKS API references, so you will not need to install other files.
Pre-requisites
For the macro to function correctly, ensure your part file is in the following state:
- Active Document: You must execute the macro on an active part document (.sldprt).
- Material Assignment: The part must contain a valid material assigned. This is necessary for SOLIDWORKS to be able to calculate the mass and, therefore, the COM correctly.
- Geometry: The space cannot be empty. It must contain a minimum of one solid or surface.
How to Use the Macro?
To use the Macro, you don’t really have to be an expert programmer:
- Open SOLIDWORKS: Open SOLIDWORKS and open the part you want to utilize.
- Access the VBA Editor: Go to Tools > Macro > New. Give your macro a name (e.g., Add_COM_Point.swp) and save it. This will activate the VBA editor.
- Paste the Code: Simply copy the code block above and paste it into the new module.
- Save and Run: Save the macro (Ctrl + S) and close the VBA editor. Now go to Tools > Macro > Run, and select your macro. Alternatively, you might want to assign a keyboard shortcut or pin it on a toolbar for quick use.
The VBA Code
'The code provided is for educational purposes only and should be used at your own risk.
'Blue Byte Systems Inc. assumes no responsibility for any issues or damages that may arise from using or modifying this code.
'For more information, visit [Blue Byte Systems Inc.](https://bluebyte.biz).
Dim swApp As SldWorks.SldWorks
Dim swModelDoc As SldWorks.ModelDoc2
Dim swCenterMass As SldWorks.Feature
Dim swCenterMassReferencePoint As SldWorks.Feature
Option Explicit
Sub main()
Set swApp = Application.SldWorks
Set swModelDoc = swApp.ActiveDoc
Set swCenterMass = swModelDoc.FeatureManager.InsertCenterOfMass
Set swCenterMassReferencePoint = swModelDoc.FeatureManager.InsertCenterOfMassReferencePoint
End Sub
Macro
You can download the macro from here.
Customizing and Expanding the Macro
This is a good place to begin a macro, but with the SOLIDWORKS API, there are an incredible number of possibilities. You can use this macro and modify it to:
- Create a custom property: Create a custom property automatically on the part with the coordinates of the COM.
- Integrate with other tools: Use the COM point to open a new sketch or reference geometry.
- Batch process files: Modify the macro to run on a group of multiple part files in a folder, a very handy feature for large projects.
Conclusion
With this simple VBA macro, you can automate your SOLIDWORKS process and provide your part designs with a professional touch. Center of Mass is an important property, and having a permanent visual reference to it can save your time, improve the accuracy, and make your design decisions simpler. Start with this code, experiment with the SOLIDWORKS API, and identify areas where you can further automate your design process. hopefully you have understood how you can add center of mass to a part in SolidWorks within a few minutes. Thanks for reading!

