1 Reply Latest reply on Aug 30, 2018 7:38 PM by Todd Bennett

    Component Reference Property - code help

    Todd Bennett

      I have some code I "borrowed" and was trying to make work for my situation.

       

      From an assembly, I need to select one or more parts and change their COMPONENT REFERENCE to be whatever value I need.

      Anyone want to fix this or give me something better?

       

      Thanks,

      Todd

       

      [code]

      Sub main()

       

          Dim myModelView As Object

          Dim Part As ModelDoc2

          Dim boolstatus As Boolean

          Dim longstatus As Long, longwarnings As Long

          Dim swEntity As Entity

          Dim objectCount As Integer

          Dim swComponent As Component2

          Dim refRoot As String

          Dim refNumber As Integer

         

          Set swApp = Application.SldWorks

          Set Part = swApp.ActiveDoc

         

          Set myModelView = Part.ActiveView

         

          Dim swSelectionMgr As SelectionMgr

          Set swSelectionMgr = Part.SelectionManager

        

          objectCount = swSelectionMgr.GetSelectedObjectCount

       

          refRoot = InputBox("Reference Number")

       

          For i = 1 To objectCount

              Set swEntity = swSelectionMgr.GetSelectedObject6(i, -1)

              Set swComponent = swEntity.GetComponent

              swComponent.ComponentReference = refRoot

          Next

         

          Part.ForceRebuild3 (True)

       

      End Sub

      [/code]

        • Re: Component Reference Property - code help
          Todd Bennett

          OK, this seems to work.  It should do a rebuild at the end, but I'll figure that out later.

           

           

          [code]

          Sub main()

           

              Dim swApp As SldWorks.SldWorks

              Dim swModel As SldWorks.ModelDoc2

              Dim swSelMgr As SldWorks.SelectionMgr

              Dim swSelComp As SldWorks.Component2

              Dim i As Long

              Dim CurSelCount As Long

             

              Dim refRoot As String

             

              Dim myModelView As Object

              Dim Part As ModelDoc2

              Dim boolstatus As Boolean

              Dim longstatus As Long, longwarnings As Long

             

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

             

              If swModel.GetType <> swDocASSEMBLY Then Exit Sub 'Only works in assemblies

             

              Set swSelMgr = swModel.SelectionManager

              CurSelCount = swSelMgr.GetSelectedObjectCount

           

              refRoot = InputBox("Reference Number")

           

              For i = CurSelCount To 1 Step -1

                  Set swSelComp = swSelMgr.GetSelectedObjectsComponent(i)

                  swSelMgr.DeSelect2 i, -1

                  swSelComp.ComponentReference = refRoot

              Next

             

          End Sub

          [/code]