4 Replies Latest reply on Aug 22, 2018 12:31 PM by Peter McKnight

    How to swAssy.AddComponent5 at user specified location (more similar to drag and drop)?

    Peter McKnight

      I have created a custom explorer form to organize our company's parts categorically and have a macro to add the selected component to the active assembly.  However the best that I can currently do is to throw the part at the assembly origin which quite often is buried in the machine.  Is there any way to insert the component either via user location selection or at the current cursor location?  Thanks for the help.

        • Re: How to swAssy.AddComponent5 at user specified location (more similar to drag and drop)?
          Mr Omkar Deshpande

          I am not sure but swCommands_InsertComponents will help, i think

          • Re: How to swAssy.AddComponent5 at user specified location (more similar to drag and drop)?
            Danniel Sims

            Peter,

             

            I ran into the same issue with a macro I made to use out of excel.  I used Getbox to get the exterior corner of the active assembly's bounding box.  Each time a part is added, the box increases, so the next added part is staggered outward from the center of the model.

             

            The following is straight code from a userform in Excel, but it should give you the basic idea.

            ---------------------------------------------------------------------------------------------------------------------

            Dim swApp As Object

            Dim Part As Object

            Dim SelMgr As Object

            Dim boolstatus As Boolean

            Dim longstatus As Long, longwarnings As Long

            Dim Feature As Object

            Dim ModelDoc As Object

            Dim Splitter, sldType

            Dim swModel As Object

            Dim swAssy As Object

            Dim vBox As Variant

            Dim X_max As Double

            Dim Y_max As Double

            Dim Z_max As Double

            Dim Pathname

              

            Splitter = Split(ufBOMSeek.lbSeek.Value, "\", , vbTextCompare)

             

            If UCase(Right(Splitter(UBound(Splitter)), 6)) = "SLDASM" Then

                sldType = 2

            Else

                sldType = 1

            End If

             

            Set swApp = CreateObject("SldWorks.Application.25")

            Set Part = swApp.ActiveDoc

            Set swModel = swApp.ActiveDoc

            Set swAssy = swModel

             

            Pathname = swModel.GetPathName

             

            On Error Resume Next

            vBox = swAssy.GetBox(0)

            X_max = vBox(0)

            Y_max = vBox(4)

            Z_max = vBox(5)

             

            If Not Part.GetType = 2 Then

                MsgBox "Active file is not an assembly."

                Exit Sub

            Else

                If Pathname = ufBOMSeek.lbSeek.Value Then

                    MsgBox "You cannot add an assembly to itself."

                    Exit Sub

                End If

            End If

             

            Set SelMgr = Part.SelectionManager

            Set ModelDoc = swApp.OpenDoc(ufBOMSeek.lbSeek.Value, sldType)

             

            Part.ClearSelection2 True

            'Part.AddComponent ufBOMSeek.lbSeek.value, X_max, Y_max, Z_max

            Part.AddComponent4 ufBOMSeek.lbSeek.Value, "", X_max, Y_max, Z_max

            swApp.Closedoc Splitter(UBound(Splitter))

            boolstatus = Part.EditRebuild3

            boolstatus = Part.SetUserPreferenceToggle(198, True)

            Part.ViewZoomtofit2

            Set Part = Nothing

            Set swApp = Nothing

             

            ---------------------------------------------------------------------------------------------------------------------

             

            Hope it helps