US$0.00
0

SolidWorks Macro for Automated Cylinder Array Generation

Overview

This SolidWorks macro for automated cylinder array generation fully automates the creation of a linear array of cylinders in a SolidWorks part document. To gather user-defined parameters, the macro uses a graphical user interface (GUI), which includes radius, height, and extrusion depth, and then programmatically generates the cylindrical bodies. 

SolidWorks Macro for Automated Cylinder Array Generation 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

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

Prerequisites

  • SolidWorks must be running, and a part document must be active.
  • The macro requires user input to define parameters for the cylinder, including quantity, radius, height, and depth.

Results

  • The macro performs the following tasks:
  • The specified number of cylindrical features is created with the defined parameters.
  • All cylinders are placed in a linear array. Spacing of all cylinders is calculated based on the total cylinder radius and the array count, so they are placed properly.

How To Set Up & Implement The Macro?

Follow these simple steps in order to set up and run the macro:

1. Create the UserForm

  • Open the VBA editor in SolidWorks by pressing (Alt + F11).
  • In the Project Explorer pane, right-click on the macro project (e.g., Macro1), then Insert > UserForm.
  • Change the new form name from UserForm to UserForm1.
  • Design the form with the following items:
    โ—‹     Labels: Add labels for Tube O.D., Wall Thickness, Length, and Depth.
    โ—‹ Text Boxes: Place text boxes next to the labels to enter the data. For example, use Txt1 for Tube O.D., Txt2 for Wall Thickness, Txt3 for Length, and Txt4 for Depth.
    โ—‹     Button: Add a command button labeled Create Tube and set its name to Pcmd.
  • If you want, you can add an image. This would simply be a picture to visually represent the form based on the Tube’s geometry.

2. Add the VBA code

  • The main VBA macro code should be copied and pasted into a new module.
  • The code specific to the UserForm should be copied and pasted into the code-behind UserForm1.

3. Save and Run the Macro

  • Save the macro file in .swp format (CreateCylinders.swp, for example).
  • In SolidWorks, go to Tools > Macro > Run, select the macro file created in the previous step.

4. Input Parameters

When the macro runs, the UserForm1 GUI will appear. Enter the required parameters for your cylinders and click the Create Tube button to generate the model.

VBA Macro Code

' 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
Dim Part As Object
Dim SelMgr As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim Feature As Object
Dim ad As String

Sub main()
    UserForm1.Show
End Sub

VBA UserForm Code

Option Explicit
Dim swApp As SldWorks.SldWorks
Dim Part As SldWorks.ModelDoc2
Dim SelMgr As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim Feature As Object
Dim ad As String
Dim L As Double
Dim W As Double
Dim R As Double
Dim H As Double
Dim No As Integer
Dim i As Integer
Dim D As Double
Dim flag As Boolean

Private Sub Pcmd_Click()
    Set swApp = Application.SldWorks
    ad = swApp.GetUserPreferenceStringValue(swDefaultTemplatePart)
    swApp.SetUserPreferenceToggle swInputDimValOnCreate, False
    
    Set Part = swApp.NewDocument(ad, 0, 0#, 0#)
    Set Part = swApp.ActiveDoc

    No = Txt1.Value
    R = Txt2.Value / 1000
    H = Txt3.Value / 1000
    L = (10 * (No + 1) + (No / 2) * (4 * 1000 * R + 10 * (No - 1))) / 1000
    W = (20 + 2 * 1000 * R + (No - 1) * 10) / 1000
    D = Txt4.Value / 1000

    ' Create base sketch and extrusion
    boolstatus = Part.Extension.SelectByID2("Top Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
    Part.SketchManager.InsertSketch True
    Part.ClearSelection2 True
    Part.SketchRectangle 0, 0, 0, L, W, 0, 1
    Part.ClearSelection2 True

    ' Set sketch dimensions
    boolstatus = Part.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 0.04195712890625, 0, 0.0004216796875, False, 0, Nothing, 0)
    Dim Annotation As Object
    Set Annotation = Part.AddDimension2(0.0411138, 0, 0.0156021)
    Part.ClearSelection2 True
    Part.Parameter("D1@Sketch1").SystemValue = L
    boolstatus = Part.Extension.SelectByID2("Line2", "SKETCHSEGMENT", -0.00021083984375, 0, -0.0303609375, False, 0, Nothing, 0)
    Set Annotation = Part.AddDimension2(-0.0132829, 0, -0.0303609)
    Part.ClearSelection2 True
    boolstatus = Part.Extension.SelectByID2("D1@Sketch1@Part1.SLDPRT", "DIMENSION", 0, 0, 0, False, 0, Nothing, 0)
    Part.Parameter("D2@Sketch1").SystemValue = W
    Part.ClearSelection2 True

    Part.SketchManager.InsertSketch True
    Part.ShowNamedView2 "*Trimetric", 8
    Part.ClearSelection2 True

    boolstatus = Part.Extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
    Part.FeatureManager.FeatureExtrusion2 True, False, False, 0, 0, D, 0.01, False, False, False, False, 0.01745329251994, 0.01745329251994, False, False, False, False, 1, 1, 1, 0, 0, False
    Part.SelectionManager.EnableContourSelection = 0

    ' Create cylinders based on input parameters
    For i = 1 To No
        boolstatus = Part.Extension.SelectByID2("", "FACE", L, D, -W / 2, False, 0, Nothing, 0)
        Part.SketchManager.InsertSketch True
        Part.ClearSelection2 True
        Part.CreateCircleByRadius2 (10 * i + (i / 2) * (4 * 1000 * R + 10 * (i - 1)) - R * 1000 - (i - 1) * 5) / 1000, W / 2, 0, (1000 * R + (i - 1) * 5) / 1000
        Part.ClearSelection2 True

        Part.SketchManager.InsertSketch True
        Part.FeatureManager.FeatureExtrusion True, False, False, 0, 0, (1000 * H + 10 * (i - 1)) / 1000, 0.01, False, False, False, False, 0.01745329251994, 0.01745329251994, False, False, False, False, 1, 1, 1
        Part.SelectionManager.EnableContourSelection = 0
    Next

    Part.ViewZoomtofit2
End Sub

Macro

You can download the macro from here.

Customization & Improvements

While this macro is extremely useful, engineering workflows often require specific automation solutions. If you have a requirement that needs you to modify this mapping to encompass other geometries, combine it into another design process, or develop a new SolidWorks add-in altogether, we can assist! The SolidWorks macro for automated cylinder array generation is a good example of how targeted automation can simplify repetitive modelling tasks.

At Blue Byte System Inc., we focus on developing customized macros and add-ins based on a minimal definition to suit specific business workflow needs; ultimately increasing the productivity of the engineer and/or organization.

Why wait then? Contact us to discuss how we can assist you in achieving your peak productivity with our custom solutions.

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
    ×