8 Replies Latest reply on May 24, 2017 4:57 PM by Peter Brinkhuis

    MACRO TO CHECK INTERSECTION BETWEEN TWO COMPONENTS IN ASSEMBLY

    Korbi Anis

      Hello brothers , I wanto to know how can I check the intersection between to components in assembly after add mates . thanks in advance

        • Re: MACRO TO CHECK INTERSECTION BETWEEN TWO COMPONENTS IN ASSEMBLY
          Dennis Dohogne

          If you mean Interference then just select the parts and go to Tools -> Evaluate (near the bottom) -> Interference Detection.

          • Re: MACRO TO CHECK INTERSECTION BETWEEN TWO COMPONENTS IN ASSEMBLY
            Christian Chu

            Use SW build-in Interference Detection - see below image

             

             

            • Re: MACRO TO CHECK INTERSECTION BETWEEN TWO COMPONENTS IN ASSEMBLY
              Korbi Anis

              thank you very much for you answers. I tried it and it works fine . are there a macro to check interference between parts in assembly ? I want to automate this !!

              • Re: MACRO TO CHECK INTERSECTION BETWEEN TWO COMPONENTS IN ASSEMBLY
                Korbi Anis

                I tried to write a macro to check if there are an interference between two components in assembly but at last I cannot display the result if there are interference or not !

                 

                Option Explicit

                Dim swApp As SldWorks.SldWorks

                Sub main()

                    Dim swModel As SldWorks.ModelDoc2

                    Dim swPart1 As SldWorks.PartDoc

                    Dim swPart2 As SldWorks.PartDoc

                    Dim swComponent1 As SldWorks.Component2

                    Dim swComponent2 As SldWorks.Component2

                    Dim bValue As Boolean

                    Dim swBody1 As SldWorks.Body2

                    Dim swBody2 As SldWorks.Body2

                    Dim swBodyCopy1 As SldWorks.Body2

                    Dim swBodyCopy2 As SldWorks.Body2

                    Dim vBodies As Variant

                    Dim swModeler As SldWorks.Modeler

                    Dim vFaces1 As Variant

                    Dim vFaces2 As Variant

                    Dim vFace As Variant

                    Dim swFace As SldWorks.Face2

                    Dim vEdges As Variant

                    Dim vEdge As Variant

                    Dim swEdge As SldWorks.Edge

                    Dim swVertex1 As SldWorks.Vertex

                    Dim swVertex2 As SldWorks.Vertex

                    Dim vPoint1 As Variant

                    Dim vPoint2 As Variant

                    Dim swTransform1 As SldWorks.MathTransform

                    Dim swTransform2 As SldWorks.MathTransform

                    Dim swInverseTransform As SldWorks.MathTransform

                    Dim swRelativeTransform As SldWorks.MathTransform

                    Dim swMathPoint As SldWorks.MathPoint

                    Dim swMathPointTransformed As SldWorks.MathPoint

                    Dim swMathUtility As SldWorks.MathUtility

                    Dim lIdx As Long

                    Dim lNumEdges As Long

                    Dim lCoord As Long

                    Dim lEpsilon As Double

                    lEpsilon = 0.000000001

                    ' Connect to SOLIDWORKS

                    Set swApp = Application.SldWorks

                    ' Get modeler

                    Set swModeler = swApp.GetModeler

                    ' Get Math utility

                    Set swMathUtility = swApp.GetMathUtility

                    ' Get active document

                    Set swModel = swApp.ActiveDoc

                    ' Select first component

                  

                    Set swComponent1 = swModel.SelectionManager.GetSelectedObject6(1, -1)

                    ' Select second component

                    Set swComponent2 = swModel.SelectionManager.GetSelectedObject6(1, -1)

                    ' Get transform for each component

                    Set swTransform1 = swComponent1.Transform

                    Set swTransform2 = swComponent2.Transform

                    ' Get document for each component

                    Set swPart1 = swComponent1.GetModelDoc

                    Set swPart2 = swComponent2.GetModelDoc

                    ' Get bodies for first component

                    vBodies = swComponent1.GetBodies2(swBodyType_e.swSolidBody)

                    Set swBody1 = vBodies(0)

                    ' Make a copy for boolean operations

                    Set swBodyCopy1 = swBody1.Copy

                    ' Get bodies for second component

                    vBodies = swComponent2.GetBodies2(swBodyType_e.swSolidBody)

                    Set swBody2 = vBodies(0)

                    ' Make a copy for boolean operations

                    Set swBodyCopy2 = swBody2.Copy

                    ' Move bodies to their correct position

                    ' Apply transform

                    bValue = swBodyCopy1.ApplyTransform(swTransform1)

                    bValue = swBodyCopy2.ApplyTransform(swTransform2)

                    swModel.EditRebuild3

                    bValue = swModeler.CheckInterference(swBodyCopy1, swBodyCopy2, True, vFaces1, vFaces2, vBodies)

                   

                    End Sub

                  • Re: MACRO TO CHECK INTERSECTION BETWEEN TWO COMPONENTS IN ASSEMBLY
                    Peter Brinkhuis

                    First of all, what's with all the variable declarations? You are only using 14 variables. You can even bring that down by using arrays so you don't have to create two variables every time. Just do something like Dim swmodels(1) as ModelDoc2 to create an array with maximum index of 1. Since they start at zero, you have two items in your array.

                     

                    Are you setting both component 1 and 2 as the same selected component?

                     

                    To print some text when you are writing and debugging a macro, you can use Debug.Print("print this text"). You can combine it with other string variables Debug.Print("print this text" & stringVar1) or doubles (convert it to a string first via Cstr(doubleVar1)). In production, you could use the message box msgbox("print this text"). You can pass some more parameters to set the title of the message box, the types of buttons etc.