13 Replies Latest reply on Jul 29, 2013 7:57 AM by Evan Dlugopolski

    How to make a macro to add parts into an assembly?

    Evan Dlugopolski

      Hello all,


      I have been struggling to figure out a way to make a macro that will add my current part doc to an open assembly.


      All i want to do is run the macro and have the current part added into the open assembly.  After that (not with a macro) I want to close the part doc, create a new part, and then run the macro again for the second part to be added to the same open assembly and repeat.  I dont need anything to happen in the assembly like mates or anything.  I just simply need a single part doc to be added to an open assembly and then be able to repeat this process over and over.


      Please if anyone can help with the code or point me toward some information that could help me it would be greatly appreciated.


      Thank You



        • Re: How to make a macro to add parts into an assembly?
          Peter Ham

          If you search the API help for 'AddComponent5' you should get a help topic showing you the method to use, and an example in various languages.


          Alternatively record a macro of inserting a component, and edit it where required.

          • Re: How to make a macro to add parts into an assembly?
            Keith Hooks

            I'm thinking it would work like this:


            1. Get a list of open documents
            2. Identify the assembly
            3. Identify the part
            4. Add the part to the assembly
            5. Close the part


            It might become problematic if you have more than one assembly and/or part open at a time.  If you agree that those are the appropriate steps, then I believe the coding is pretty straight-forward.  The snippet below is the bare bones is missing any obvious error-checking that needs to be performed. 


                Public swApp As SldWorks



                Public Sub main()



                    Dim openDocs As Object

                    Dim swPartDoc As PartDoc

                    Dim swAssemblyDoc As AssemblyDoc

                    Dim currentModel As ModelDoc2

                    Dim partFileName As String

                    Dim index As Integer



                    openDocs = swApp.GetDocuments()



                    For Each currentModel In openDocs

                        If (currentModel.GetType = swDocumentTypes_e.swDocASSEMBLY) Then

                            swAssemblyDoc = currentModel

                        End If

                        If (currentModel.GetType = swDocumentTypes_e.swDocPART) Then

                            swPartDoc = currentModel

                            partFileName = swPartDoc.GetPathName()

                        End If




                    swAssemblyDoc.AddComponent4(partFileName, "Default", 0.0, 0.0, 0.0)




                End Sub