9 Replies Latest reply on Feb 23, 2016 9:33 AM by Christian Estep

    Using VB.NET to insert Solidworks Part

    Christian Estep

      Hey Everyone, I have a question regarding the code used in VB.NET to insert a part into Solidworks. I've been able to setup the basic dialog boxes in Visual Studio that are triggered when someone clicks the Launch button on the task pane in the Solidworks main program. But actually getting Solidworks to insert a part has got me stumped so far. Any help on this would be awesome. Thanks again.

        • Re: Using VB.NET to insert Solidworks Part
          Rolando Garza

          Have you tried the macro recorder?

           

          If you're looking to open a file, here is a link that might interest you:

          open a part file using a button in a userform in VB.Net

            • Re: Using VB.NET to insert Solidworks Part
              Viktor Bovzdarenko

              please look at this macro: 2013 SOLIDWORKS API Help - Add Component and Mate Example (VBA)

              the component is inserted using : Set swcomponent = swAssy.AddComponent5(strCompModelname, swAddComponentConfigOptions_CurrentSelectedConfig, "", False, "", -1, -1, -1)

              note that  part should be in SW memory before calling AddComponent5

                • Re: Using VB.NET to insert Solidworks Part
                  Christian Estep

                  Thanks guys for your help on this issue. I wanted to make mention that I'm just trying to insert a new part into an already existing Solidworks part. That's kind of the goal of creating this VB.NET add-in so that this process becomes automated. I'm still having issues with getting Solidworks to insert a new part. I'm a newbie at programming so any help would be greatly appreciated.

                   

                  This is what I have so far in terms of my code. I created a dialog box for SW part insertion.

                  I have the code below linked to my "Okay" button in my VB.NET project. Can anyone tell me what exactly I'm missing to make this work?

                   

                  Private Sub Button1_Click(ByVal sender As System.Object,

                      ByVal e As System.EventArgs) Handles Button1.Click

                          'process the data from the User From with solidworks

                          Module1.Process(SwApp, TextBox1.Text)

                          Dim swcomponent As SolidWorks.Interop.sldworks.Component2

                   

                          swcomponent = SwApp.AddComponent5(swAddComponentConfigOptions_e.swAddComponentConfigOptions_CurrentSelectedConfig, "", False, "", -1, -1, -1)

                   

                          Me.DialogResult = System.Windows.Forms.DialogResult.OK

                          Me.Close()

                      End Sub

                  DialogBox1.bmp

                    • Re: Using VB.NET to insert Solidworks Part
                      Rolando Garza

                      Isn't that method for adding parts to assemblies?

                      have you looked into InsertPart2?

                      what error are you getting?

                       

                      side notes:

                      Also, have you considered using the MenuStrip control in place of those buttons?

                      It'll free up real estate on the form and it'll allow you to add more methods of your own if you decide to expand your applications.

                      (i think it looks more professional and predictable to the user)

                      Do you need that cancel button?

                      Sorry, not trying to nitpick.

                      It's been my experience that you do not give users the ability to cancel something that must run.

                      If all the button does is close the application, then delete it because you already have that little "X" up to the right.

                      (i usually disable that little "x" because i like to e in control of how my code exits)

                        • Re: Using VB.NET to insert Solidworks Part
                          Christian Estep

                          Hey Garza,

                           

                          Thanks for replying to my thread. You're right, there are a lot of things in terms of the overall layout and look of the dialog box that could use updating. I'd like to get around to fixing those issues but at the moment I'm more concerned with the overall functionality of this add-in. In other words, I would just like this thing to finally run, lol. Does anyone know the VB.NET code in order to Insert a New Part into an open Model template - not an Assembly - in SolidWorks? And where exactly would this code be placed in a VB.NET project? I am currently using Visual Studio Community 2015 Edition - with an installed VB.NET Solidworks Standard template. I've tried searching webpage after webpage, online looking for more detailed information on how to Insert a new part into an existing Model, using VB.NET. Unfortunately, I have found little to nothing to help me figure this out. I'd be eternally grateful for anyone's expertise and knowledge on this subject.

                            • Re: Using VB.NET to insert Solidworks Part
                              Rolando Garza

                              I've never done what you are asking.

                              What do you mean by "Model template"?

                              I think you mean a Part file that another Part file will be inserted into.

                              That would mean you are trying to replicate the Insert>Part command.

                              Kinda like this:

                              Dim swApp As Object

                              Dim Part As Object

                              Dim myFeature As Object

                               

                              Sub main()

                                   Set swApp = Application.SldWorks

                                   Set Part = swApp.ActiveDoc

                                   Set myFeature = Part.InsertPart2("C:\AwesomeWorkGoesHere\FM8432.SLDPRT", 383)

                               

                                   Part.ClearSelection2 True

                              End Sub

                               

                              Are you after something like that?

                                • Re: Using VB.NET to insert Solidworks Part
                                  Christian Estep

                                  Hey Garza,

                                   

                                  I haven't forgotten about you and thanks for your reply to this thread. I really appreciate your help with my VB.NET project. Unfortunately, at the moment, this particular project has been put on the back-burner. Will be returning to this issue soon.

                                  • Re: Using VB.NET to insert Solidworks Part
                                    Christian Estep

                                    Okay All, I'm back on trying to Insert a Part into an Open Part Model in Solidworks. I've been able to click on the "Insert New Part Into Existing Configuration" and an Open Dialog Box will appear but when I go to Select a .Sldprt file to open. Nothing appears in my SolidWorks template no new part...

                                     

                                    MySWAddinScreenshot1.png

                                     

                                    Active SolidWorks Window With Addin.png

                                    Does anyone know if I'm missing something in my code? Confused as to what the problem could be? Would be very appreciative for any help I can get on this issue. Thanks again.

                            • Re: Using VB.NET to insert Solidworks Part
                              Christian Estep

                              Any ideas as to what needs to be fixed with this VB.NET Addin? I could really use some expert advice at the moment..