5 Replies Latest reply on Dec 20, 2012 1:27 PM by Aaron Larson

    Initiate the "Insert Component" property manager

    Aaron Larson

      Is there a way to initiate the "Insert Component" property mangager without completing the component insertion?  Below is my reason:


      We have a design library which has many folders and subfolders essentially with a goal of having 1 part per folder so that the users can logically navigate to find what they need.  This is really cumbersome and because the parts are checked into our PLM system.  So, updating a series of parts from the sytem is cumbersome because each part has to be checked out into its own folder to avoid issues. 


      I have a database apart from the PLM that contains some basic information about the parts in the library.  I have a nice little user form in a macro that searches through this database to locate a part.  What I'd like to be able to do is insert a part from the form. The AddComponent functions don't get what I need because I want the smart mate functionality of the library components.  So I'd like to be able to pull the part in the "Insert Component" property manager and fire that so the user can "drag & drop" the components into their assembly. 


      BTW we're on 2009 and soon to upgrade to 2011.

        • Re: Initiate the "Insert Component" property manager
          Keith Rice



          You can activate nearly any command in SolidWorks using ISldWorks::RunCommand. I recommend you check out this blog post for details on how to use this API call.


          This code worked for me in 2013 and I am guessing it will work in 2009:




          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2


          Sub main()

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

              swApp.RunCommand swCommands_InsertComponents, Empty

          End Sub





          Video Tutorials for the SolidWorks API

            • Re: Initiate the "Insert Component" property manager
              Aaron Larson

              Thank you so much - great blog post.  I too have been writing macros for far too long to have not known about that call.  This isn't as elegant as replicating the drag and drop from the design library but it gets the job done efficiently.

              • Re: Initiate the "Insert Component" property manager
                Aaron Larson

                Here's one more tag along for this.  I'm using the DocumentInvisible property to load the library file into memory so it is available for the "Insert Component" command. 


                The only problem is... when I load it "invisibly" it doesn't seem to appear in the list of components.



                Private Sub cmdInsertPart_Click()


                Dim InsertComp As ModelDoc2

                Dim FileName As String

                Dim fileerror As Long

                Dim filewarning As Long


                Set swApp = Application.SldWorks

                Set swModel = swApp.ActiveDoc


                'See if any document is open

                If swModel Is Nothing Then

                    RetVal = MsgBox("No document open.  Please open an assembly and try again.", vbOKOnly + vbMsgBoxSetForeground)

                    Exit Sub

                End If


                'Make sure an assembly is open

                If Not swModel.GetType = swDocASSEMBLY Then

                    MsgBox ("Active window is not an assembly.")

                    Exit Sub

                End If



                'Make sure a result is selected

                If Me.lstResults.ListIndex = -1 Then

                    MsgBox ("Nothing selected.  Make a selection from the list and try again.")

                    Exit Sub

                End If


                swAssy = swModel.GetTitle

                FileName = FileLocation & ItemArray(Me.lstResults.ListIndex, 0) & ".SLDPRT"


                'Set Document visibility to Invisible so part is accessible from Insert Component PropMgr

                'swApp.DocumentVisible False, swDocumentTypes_e.swDocPART


                'Open the selected part

                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 library document

                swApp.CloseDoc (FileName)


                'Reset document visibility

                'swApp.DocumentVisible True, swDocumentTypes_e.swDocPART


                End Sub




                Message was edited by: Aaron Larson I should mention that the reason I'm interested in loading the library component only to memory and not the screen is that we deal with very large and unruly assemblies and I don't want the user to have to deal with waiting for Solidworks to toggle screens just to insert the component.