8 Replies Latest reply on May 13, 2015 12:51 PM by Deepak Gupta

    instance a file (that is already in session) into an assembly

    Rolando Garza

      I'm trying to instance a part into my assembly using the InsertComponent command because i would like to be able to drag the part from the list on the left into the assembly.

      I can't get this to work for a file that is already loaded in session.

      If the file is already in the assembly, my code will not open the file again in is own window - which , apparently, what is required by the InsertComponent tool so that it is listed in the box on the left. Below is some sample code. Can someone please set me straight here.


      Private Sub main()
      Dim InsertComp As ModelDoc2
      Dim FileName As String
      Dim fileerror As Long
      Dim filewarning As Long
      Set swApp = Application.SldWorks
      Set swModel = swApp.ActiveDoc
      FileName = "C:\test\test.sldprt"
      If swModel Is Nothing Then
          RetVal = MsgBox("No document open.  Please open an assembly and try again.", vbOKOnly + vbMsgBoxSetForeground)
          Exit Sub
      End If
      If Not swModel.GetType = swDocASSEMBLY Then
          MsgBox ("Active window is not an assembly.")
          Exit Sub
      End If
      swAssy = swModel.GetTitle
      Set InsertComp = swApp.OpenDoc6(FileName, swDocPART, swOpenDocOptions_Silent, "", fileerror, filewarning)
      'Active the target assembly
      swApp.ActivateDoc (swAssy)
      'Initiate the "Insert Component" command
      bret = swApp.RunCommand(swCommands_InsertComponents, Empty)
      'Close the selected document
      swApp.CloseDoc (FileName)
      End Sub