Site icon BLUE BYTE SYSTEMS INC.

SOLIDWORKS Macro to Insert Part into New Part Document (Derived Parts)

Overview

This utility is intended for SOLIDWORKS engineers who are looking to easily create a derived part from an existing design. This SOLIDWORKS macro to insert part into new part document automates this process of opening a new part document and inserting the currently active part as an external reference.. Most importantly, it also allows you to transfer the material properties of the original part to the new part, so you can ensure the material specification is not lost.

This provides a highly effective workflow in which derived designs can be created, simplified versions of large assemblies can be produced, or configurations can be generated that remain linked to the material specifications specified in the original part file.

System Requirements

Pre-requisites

Results

Steps to Implement the Macro

  1. Open the Part Document: Ensure that the original SOLIDWORKS part document you intend to replicate or derive from is the currently active window in SOLIDWORKS.
  2. Load and Execute the Macro:

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).

' ******************************************************************************
' Insert Part into a New Part
' Description: This macro inserts the currently active SolidWorks part into a new part,
'              copies the material properties, and adjusts the view.
' ******************************************************************************
Option Explicit

' Global variables for SolidWorks application and document objects
Dim swApp As SldWorks.SldWorks                    ' SolidWorks application object
Dim Part, Parent As Object                        ' Objects for new part and parent part
Dim boolstatus As Boolean                         ' Boolean status for operations
Dim longstatus As Long, longwarnings As Long      ' Status and warnings for operations
Dim Parent_FullPathName As String                 ' Full path of the parent part
Dim EmptyStr As String                            ' Placeholder for empty strings
Dim myFeature As Object                           ' Feature object for inserting the part
Dim sMatName As String                            ' Material name of the parent part
Dim sMatDB As String                              ' Material database path

Sub main()
    ' Initialize SolidWorks application
    Set swApp = Application.SldWorks
    Set Parent = swApp.ActiveDoc                  ' Get the currently active document

    ' Precondition: Exit quietly if the active document is not a part
    If (Parent.GetType <> swDocPART) Then Exit Sub

    ' Get the full path name of the parent part
    Parent_FullPathName = Parent.GetPathName

    ' Get the short name (title) of the parent part and remove the file extension if present
    Dim ParentName As String
    ParentName = Parent.GetTitle
    If InStr(1, ParentName, ".") Then
        ParentName = Left$(ParentName, InStr(1, ParentName, ".") - 1)
    End If

    ' Get the material name and database path of the parent part
    sMatName = Parent.GetMaterialPropertyName2("", sMatDB)

    ' Create a new part
    Set Part = swApp.NewPart

    ' Insert the parent part into the new part
    Set myFeature = Part.InsertPart2(Parent_FullPathName, 1)
    Part.ClearSelection2 True                    ' Clear any selections in the new part

    ' Set the view to isometric and fit the window
    Part.ShowNamedView2 "*Isometric", 7
    Part.ViewZoomtofit2

    ' Copy material from the parent part to the new part
    Part.SetMaterialPropertyName "SOLIDWORKS Materials.sldmat", sMatName

    ' Clean up references to objects
    Set Parent = Nothing
    Set Part = Nothing
End Sub

Macro

You can download the macro from here.

Need an expert’s help customizing the macro?

Contact us to customize this SOLIDWORKS macro to insert part into new part document based on your needs (e.g., applying a feature template automatically, file naming conventions, or any PDM/PLM integrations). 

Exit mobile version