4 Replies Latest reply on Mar 27, 2018 5:19 AM by Murali B,B.

    Assembly from part/assemby

    Patrice Marlier

      Hi all,

       

      I am a beginner in the API

      I would like to create an assembly from part of which I know the way.

      I tried to use the macro recorder to look at the code but it does not work.

      Do you have a basis code for this type of macro

       

      It would be great if you can help me

       

      Thank you

      Dim swApp As Object

       

      Dim Part As Object

      Dim boolstatus As Boolean

      Dim longstatus As Long, longwarnings As Long

       

      Sub main()

       

      Set swApp = Application.SldWorks

       

      Set Part = swApp.OpenDoc6("C:\Plans\ASM1.SLDASM", 2, 0, "", longstatus, longwarnings)

      1. swApp.ActivateDoc2 "ASM11", False, longstatus

      Set Part = swApp.ActiveDoc

      'Set Part = swApp.ActiveDoc

      Dim myModelView As Object

      Set myModelView = Part.ActiveView

      1. myModelView.FrameLeft = 0
      2. myModelView.FrameTop = 0

      Set myModelView = Part.ActiveView

      1. myModelView.FrameState = swWindowState_e.swWindowMaximized

      Set myModelView = Part.ActiveView

      1. myModelView.FrameState = swWindowState_e.swWindowMaximized

      boolstatus = Part.AddComponent("C:\Plans\PART1.SLDASM", 0, 0, 0)

      boolstatus = Part.AddComponent("C:\Plans\PART2.SLDASM", 100, 100, 100)

      End Sub

        • Re: Assembly from part/assemby
          Fatih Mehmet Ozcan

          Creates a new assembly and put your active part file into it

           

          Dim swApp As SldWorks.SldWorks
          Sub main()

           

          Set swApp = Application.SldWorks

           

          Dim model As ModelDoc2
          Set model = swApp.ActiveDoc
          If model Is Nothing Then Exit Sub

           

          'MsgBox model.GetTitle

           

          Dim swcomponent As SldWorks.Component2
          Dim swasm As SldWorks.AssemblyDoc
          Dim longstatus As Long
          Dim status As Long

           

          assytemplate = swApp.GetUserPreferenceStringValue(swDefaultTemplateAssembly)
          activefile = model.GetPathName

           

          'swApp.ActivateDoc2 activefile, False, longstatus
          Set model = swApp.NewDocument(assytemplate, 0, 0, 0)
          Set swasm = model
          Set swcomponent = swasm.AddComponent5(activefile, swAddComponentConfigOptions_CurrentSelectedConfig, "", False, "", 0, 0, 0)
          model.ViewZoomtofit2

           

          End Sub

          • Re: Assembly from part/assemby
            Patrice Marlier

            Hello,

            I looked at your code Fatih and I keep it for an application whose assembly does not exist, here is what I just wrote

             

            Dim swApp As Object

             

            Dim Part As Object

            Dim boolstatus As Boolean

            Dim longstatus As Long, longwarnings As Long

             

            Sub main()

             

            Set swApp = Application.SldWorks

             

            Set Part = swApp.OpenDoc6("C:\Plans\PART1.SLDASM", 2, 0, "", longstatus, longwarnings)

            Set Part = swApp.OpenDoc6("C:\Plans\PART2.SLDASM", 2, 0, "", longstatus, longwarnings)

             

            Set Part = swApp.OpenDoc6("C:\Plans\ASM1.SLDASM", 2, 0, "", longstatus, longwarnings)

             

            swApp.ActivateDoc2 "ASM1", False, longstatus

             

            boolstatus = Part.AddComponent("C:\Plans\PART1.SLDASM", 0, 0, 0)

            boolstatus = Part.AddComponent("C:\Plans\PART2.SLDASM", 0, 0, 0)

             

            End Sub

            • Re: Assembly from part/assemby
              Murali B,B.

              Open a new assembly.

              open the Part/Assembly in solidworks before adding it into the new assembly.