Hide Table of Contents

Get Differences Between Parts Example (VBA)

This examples shows how to get the differences between two parts.




' Problem:

'       When a part is designed for injection molding, many small

'       design details must be added for the injection molding

'       process. Typically, these are draft angles, thickening

'       of walls, additional ribs, and so on. Quite often, without

'       specialized knowledge and experience, these are added

'       to the model, by mold maker, after the part has been

'       designed by a designer.


'       It is often desirable to find out what changes

'       have been made to the original model. This is usually

'       done to determine the change in the amount of material for

'       the revised part.


'       This sample code uses several geometry- and

'       topology-related APIs to find the differences and

'       reverse difference between two parts.


' Preconditions:

'       (1) Two parts are open.

'       (2) Both parts are single body parts.

'       (3) Both parts only contain solid bodies.


' Postconditions:

'       Depending on the differences between parts, one or two new parts

'       showing the differences are created.


' Notes:

'       (1) This example is not intended to be a replacement for SOLIDWORKS

'           Utilities comparison utility. This code illustrates

'           the use of several geometric and topological APIs.

'       (2) The new parts can contain one or more solid bodies.

'       (3) This code is intended to run on parts that are similar.




Option Explicit


Sub CreateDiffPart _

( _

    swApp As SldWorks.SldWorks, _

    vBodyResArr As Variant _


    Dim vBodyRes                    As Variant

    Dim swBodyRes                   As SldWorks.body2

    Dim swPartRes                   As SldWorks.PartDoc

    Dim swFeatRes                   As SldWorks.feature


    If IsEmpty(vBodyResArr) Then Exit Sub

    Set swPartRes = swApp.NewPart

    For Each vBodyRes In vBodyResArr

        Set swBodyRes = vBodyRes

        Set swFeatRes = swPartRes.CreateFeatureFromBody3(swBodyRes, False, _

                                swCreateFeatureBodyCheck + swCreateFeatureBodySimplify): Debug.Assert Not swFeatRes Is Nothing


 End Sub


Sub main()

    Const sPartName1                As String = "d:\samples\block.sldprt"

    Const sPartName2                As String = "d:\samples\blockfillet.sldprt"

    Dim swApp                       As SldWorks.SldWorks


    Dim swModel1                    As SldWorks.ModelDoc2

    Dim swPart1                     As SldWorks.PartDoc

    Dim swBody1                     As SldWorks.body2

    Dim swBody1Copy                 As SldWorks.body2

    Dim swBody1Copy2                As SldWorks.body2


    Dim swModel2                    As SldWorks.ModelDoc2

    Dim swPart2                     As SldWorks.PartDoc

    Dim swBody2                     As SldWorks.body2

    Dim swBody2Copy                 As SldWorks.body2

    Dim swBody2Copy2                As SldWorks.body2


    Dim vswBody1                    As Variant

    Dim vswBody2                    As Variant

    Dim vBodyResArr1                As Variant

    Dim vBodyResArr2                As Variant


    Dim nRetval                     As Long

    Dim bRet                        As Boolean


    Set swApp = Application.SldWorks

    Set swModel1 = swApp.GetOpenDocumentByName(sPartName1)

    Set swModel2 = swApp.GetOpenDocumentByName(sPartName2)

    Set swPart1 = swModel1

    Set swPart2 = swModel2


    ' Get all of the visible bodies in each part

    vswBody1 = swPart1.GetBodies2(swSolidBody, True)

    vswBody2 = swPart2.GetBodies2(swSolidBody, True)

    Set swBody1 = vswBody1(0)

    Set swBody2 = vswBody2(0)

    Set swBody1Copy = swBody1.Copy

    Set swBody2Copy = swBody2.Copy

    Set swBody1Copy2 = swBody1.Copy

    Set swBody2Copy2 = swBody2.Copy


    vBodyResArr1 = swBody1Copy2.Operations2(SWBODYCUT, swBody2Copy2, nRetval): Debug.Assert swBodyOperationNoError = nRetval

    vBodyResArr2 = swBody2Copy.Operations2(SWBODYCUT, swBody1Copy, nRetval): Debug.Assert swBodyOperationNoError = nRetval

    If IsEmpty(vBodyResArr1) And IsEmpty(vBodyResArr2) Then Exit Sub


    Debug.Print "Part1          = " & swModel1.GetPathName

    Debug.Print "Part2          = " & swModel2.GetPathName


    If Not IsEmpty(vBodyResArr1) Then

        Debug.Print "  Diffs(1-2)   = " & UBound(vBodyResArr1) + 1

    End If

    If Not IsEmpty(vBodyResArr2) Then

        Debug.Print "  Diffs(2-1)   = " & UBound(vBodyResArr2) + 1

    End If


    CreateDiffPart swApp, vBodyResArr1

    CreateDiffPart swApp, vBodyResArr2

End Sub


Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

Subject:   Feedback on Help Topics
Page:   Get Differences Between Parts Example (VBA)
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:


We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again

Web Help Content Version: API Help (English only) 2015 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.