MACRO LIBRARY

Insert a new part in a sub-assembly

Free public access. No account or subscription required.

Files

Insert a new part in a sub-assembly

Code

Module: mMain

'Demonstrates how to insert a new part in a sub-assembly.
    
'Preconditions: Assembly is open and face is selected.
'Result: A simple sketch is added to the assembly that owns the selected
'   face's component.
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Const NEW_PART_PATH As String = "C:\test.sldprt"
    
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim swSelMgr As SldWorks.SelectionMgr
Dim swFace As SldWorks.Face2
Dim swComp As SldWorks.Component2
Dim swParentComp As SldWorks.Component2
    
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    If swModel Is Nothing Then Exit Sub
    If swModel.GetType <> swDocASSEMBLY Then Exit Sub
    
    Set swAssy = swModel
    Set swSelMgr = swModel.SelectionManager
    
    If swSelMgr.GetSelectedObjectType3(1, -1) <> swSelFACES Or swSelMgr.GetSelectedObjectCount2(-1) <> 1 Then
        MsgBox "Please select a single face."
        Exit Sub
    End If
    
    Set swFace = swSelMgr.GetSelectedObject6(1, -1)
    Set swComp = swSelMgr.GetSelectedObjectsComponent4(1, -1)
    Set swParentComp = swComp.GetParent
    
    If swParentComp Is Nothing Then
        swComp.Select False
    Else
        swParentComp.Select False
    End If
    
    swAssy.EditAssembly
    swFace.Select False
    swAssy.InsertNewPart2 NEW_PART_PATH, swFace
    swModel.SketchManager.InsertSketch True
    swModel.SketchManager.CreateCircleByRadius 0, 0, 0, 0.01
    swAssy.EditAssembly
End Sub