MACRO LIBRARY

Merge temporary bodies

Free public access. No account or subscription required.

Files

Merge temporary bodies

Code

'Demonstrates the results of non-intersecting bodies using IBody2::Operations2
    
'Preconditions: Part with two non-intersecting bodies is open and they are both selected _
    in the FM tree
'Results: see immediate window
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swBody1 As SldWorks.Body2
Dim swBody2 As SldWorks.Body2
Dim vBodies As Variant
Dim lngErrors As Long
Dim i As Integer
    
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    Set swBody1 = swSelMgr.GetSelectedObject6(1, -1)
    'swModel.HideSolidBody
    Set swBody1 = swBody1.Copy
    Set swBody2 = swSelMgr.GetSelectedObject6(2, -1)
    'swModel.HideSolidBody
    Set swBody2 = swBody2.Copy
    
    Dim vResults As Variant
    vResults = swBody1.GetIntersectionEdges(swBody2)
    If IsEmpty(vResults) Then Debug.Print "hi"
    Stop
    vBodies = swBody1.Operations2(SWBODYINTERSECT, swBody2, lngErrors)
    If IsEmpty(vBodies) Then
        Debug.Print "Number of result bodies: 0"
    Else
        Debug.Print "Number of result bodies: " & UBound(vBodies) + 1
        Dim swFinalBody As SldWorks.Body2
        Set swFinalBody = vBodies(0)
        swFinalBody.Display3 swModel, RGB(0, 255, 0), 0
        Dim swPart As SldWorks.PartDoc
        Set swPart = swModel
        swPart.CreateFeatureFromBody3 swFinalBody, False, 0
    End If
    Debug.Print "Error code: " & lngErrors
    
    Set swFinalBody = Nothing
    Set swBody1 = Nothing
    Set swBody2 = Nothing
End Sub
    
'Notes:
'For two non-intersecting bodies...
    'Add: No errors, main body is the result
    'Cut: No errors, main body is the result
    'Intersect: No errors, no result bodies
    
'Operations2 makes the main and tool bodies invalid immediately afterward (API Help)