6 Replies Latest reply on Sep 5, 2017 11:51 AM by Jacob Corder

    Replace Component Macro

    Wojciech Plachta

      I am trying to create a macro to replace a component in an assembly with the option of letting the user choose which part will replace it. I have started with the basic macro offered on SolidWorks but that requires you to include the file destination within the macro. So essentially it would only work for replacing a component with one and the same part. I am just looking to include the script to have the user choose which file will be used to replace the current component. I have attached the macro I copied from SolidWorks.

        • Re: Replace Component Macro
          Nilesh Patel

          Why do you want to create a macro when the functionality is already available in SolidWorks?

          • Re: Replace Component Macro
            Jacob Corder

            first remove the silly line from solidworks

             

            Const sFileName             As String = "filename"

            replace with

            Dim sFileName                As String

             

            having this prompt for a browser window in VBA is going to be hard. i cannot find a way to do that.

            perhaps a VSTA .VBProj will allow you to use the System.Windows.Forms.OpenFileDialog

             

            browsing for the file is going to be the hard part using a .swp macro

            • Re: Replace Component Macro
              Deepak Gupta

              Try these codes

               

              Option Explicit

                  Dim swApp                   As SldWorks.SldWorks

                  Dim swModel                 As SldWorks.ModelDoc2

                  Dim swAssy                  As SldWorks.AssemblyDoc

                  Dim swSelMgr                As SldWorks.SelectionMgr

                  Dim swSelComp               As SldWorks.Component2

                  Dim bRet                    As Boolean

                  Dim Filter                  As String

                  Dim fileConfig              As String

                  Dim fileDispName            As String

                  Dim fileOptions             As Long

                  Dim sFileName               As String

               

               

              Sub main()

                 

                  Set swApp = Application.SldWorks

                  Set swModel = swApp.ActiveDoc

                 

                  Filter = "SOLIDWORKS Files (*.sldprt; *.sldasm)|*.sldprt;*.sldasm|"

                  sFileName = swApp.GetOpenFileName("File to Attach", "", Filter, fileOptions, fileConfig, fileDispName)

               

                  Set swAssy = swModel

                  Set swSelComp = swAssy.SelectionManager.GetSelectedObjectsComponent(1)

                  bRet = swAssy.ReplaceComponents(sFileName, "", True, True)

               

              End Sub