US$0.00
0

SolidWorks Macro for Creating a Center of Gravity (CoG) Point

This step-by-step tutorial provides an exhaustive tutorial on how to utilize a SolidWorks Macro for creating a center of gravity (CoG) Point in placing a 3D sketch point automatically at the Center of Gravity (CoG) of an assembly or a part. It presents the code of the macro, step-by-step guidelines, and tips on troubleshooting to help you precisely locate the center of mass required for balancing, structural analysis, and simulation.

SolidWorks Macro for Creating a Center of Gravity (CoG) Point and An illustration of a person working at a computer, with hands on a keyboard and mouse. The computer screen displays a 3D model of a mechanical part with a hole, viewed in a design software interface showing tools like "Pains," "Origine," "Extrusion," and "Add Step" on the left sidebar. An orange gear icon with a small robot face overlays the screen, symbolizing automation or AI assistance in the design process.

System Requirements

In order to use this macro effectively, you will need the following:

  • SolidWorks Version: SolidWorks 2014 or newer
  • Operating System: Windows 7 or later

Prerequisites

  • The active document must be a part or assembly file.
  • The part or assembly must have valid geometry to calculate the center of gravity.
  • Ensure that the part or assembly is open and active before running the macro.

Macro Code

The macro is written in Visual Basic for Applications (VBA). You can cut and paste the code itself directly into a new macro file in SolidWorks or download the .swp file from the following link.

' Disclaimer:
' The code provided 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).

Option Explicit

Dim swApp As Object                 ' SolidWorks application object
Dim Part As Object                  ' Active document object (part or assembly)
Dim boolstatus As Boolean           ' Boolean status variable
Dim longstatus As Long              ' Long status variable for capturing operation results
Dim Annotation As Object            ' Annotation object for any annotations added (not used here)
Dim Gtol As Object                  ' Geometric tolerance object (not used here)
Dim DatumTag As Object              ' Datum tag object (not used here)
Dim FeatureData As Object           ' Feature data object for manipulating feature details (not used here)
Dim Feature As Object               ' Feature object for creating/manipulating features (not used here)
Dim Component As Object             ' Component object for assemblies (not used here)

' Main subroutine to create the Center of Gravity point in a 3D sketch
Sub main()
    Dim mp As Variant                ' Array to hold the mass properties (center of gravity coordinates)
    Dim PlaneObj As Object           ' Plane object (not used here)
    Dim PlaneName As String          ' Name of the plane (not used here)
    Dim SketchObj As Object          ' Sketch object for creating the 3D sketch (not used here)
    Dim Version As String            ' SolidWorks version (not used here)

    ' Error handling block to capture unexpected issues
    On Error GoTo errhandlr

    ' Initialize SolidWorks application
    Set swApp = Application.SldWorks
    

    ' Check if SolidWorks application is available
    If swApp Is Nothing Then
        MsgBox "SolidWorks application not found. Please ensure SolidWorks is installed and running.", vbCritical, "SolidWorks Not Found"
        Exit Sub
    End If

    ' Get the currently active document
    Set Part = swApp.ActiveDoc

    ' Check if there is an active document open in SolidWorks
    If Part Is Nothing Then
        MsgBox "No active document found. Please open a part or assembly and try again.", vbCritical, "No Active Document"
        Exit Sub
    End If

    ' Check if the active document is a drawing (GetType = 3 corresponds to drawing)
    If Part.GetType = 3 Then
        MsgBox "This macro only works on parts or assemblies. Please open a part or assembly and try again.", vbCritical, "Invalid Document Type"
        Exit Sub
    End If

    ' Enable adding objects directly to the database without showing in the UI
    Part.SetAddToDB True

    ' Get the mass properties of the active part or assembly
    ' mp array holds center of mass coordinates (mp(0) = X, mp(1) = Y, mp(2) = Z)
    mp = Part.GetMassProperties

    ' Check if mass properties are valid (in case the part has no geometry)
    If Not IsArray(mp) Or UBound(mp) < 2 Then
        MsgBox "No geometry found in the part or assembly. Cannot calculate center of gravity.", vbCritical, "Invalid Geometry"
        Exit Sub
    End If

    ' Insert a new 3D sketch
    Part.Insert3DSketch

    ' Create a point at the center of gravity coordinates
    Part.CreatePoint2 mp(0), mp(1), mp(2)

    ' Exit the sketch mode
    Part.InsertSketch

    ' Rename the newly created feature to "CenterOfGravity" in the feature tree
    Part.FeatureByPositionReverse(0).Name = "CenterOfGravity"

    ' Successfully exit the subroutine
    Exit Sub

' Error handling block
errhandlr:
    MsgBox "An error occurred. No valid geometry found to process.", vbCritical, "Error"
    Exit Sub

End Sub

You can download the Macro with a click here.

Need to Modify The Macro to Specific Requirements?

Or perhaps you would like to use it with other workflows? Contact us anytime. We at Blue Byte System Inc. provide custom macro development services that are tailored to address your individual requirements. Call us and maximize your work productivity with our powerful solutions today!

Author

Amen Jlili

Amen Jlili is the founder and technical director of Blue Byte Systems Inc., a software company in Vancouver, Canada, specializing in automating SOLIDWORKS and PDM. With over a decade of experience, he has authored several courses and open-source frameworks related to the SOLIDWORKS API. His leadership ensures that Blue Byte Systems prioritizes customer satisfaction and delivers high-quality software and CAD design solutions.
0
    0
    Your Cart
    Your cart is emptyReturn to Shop
    ×