18 Replies Latest reply on Nov 27, 2018 8:11 AM by Eric Jacobson

    Open New Solidworks Instance

    Eric Jacobson

      Hey, All;

       

      I have this bit of code I've written for a windows form that opens a drawing when a barcode is scanned.

       

      My issue is that the drawing opens in the primary solidworks instance (I.E: if there are 3 instances running, it opens in the first opened instance).

       

      My colleagues would like this to open a new solidworks instance and I'm not entirely certain how to do this. I would think it involves the process.start line, but I have no idea how to resolve it... I feel like it's something simple and I'm just too new to this to get it.

       

      Anyone have any ideas?

       

       

       

       

       

          Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click

              Try

                  Dim barcode As String = tb_barcode.Text

                  Dim filename As String = tb_barcode.Text

                  'Add File Extension to end of path

                  Dim ext As String = ".SLDDRW"

                  'Split job number from detail number in barcode textbox

                  barcode = Split(tb_barcode.Text, ".")(0)

                  filename = Split(tb_barcode.Text, ".")(1)

                  'File path with jobnumber/detail number variables

                  System.Diagnostics.Process.Start("G:\Fixtures\" & barcode & "\Details\" & barcode & " DET " & filename & ext)

              Catch

                  MessageBox.Show("File Not Found")

              End Try

          End Sub

      End Class

          • Re: Open New Solidworks Instance
            Eric Jacobson

            I feel like this is the right answer, but that's still way over my head

              • Re: Open New Solidworks Instance
                Taus Moller

                I know the feeling

                 

                The code comes in two distinct parts. The first part deals with spawning a new solidworks instance. This is fairly straight forward, That is this part:

                SolidWorks = System.Diagnostics.Process.Start(SolidWorksPath());

                 

                This should spawn a new Solidworks instance. . In case you don't know, the SldWorks you interact with when writing macros is what is known as a COM object, which is a method for windows processes to interact with each other . Each open of solidworks has its own instance of this. Now, the hard part is to get the COM handle from the newly opened process. That is what the entire second part of the code deals with.

                 

                The good news is that if all you need to do is open a document there is no need for it. You can simply supply the document you want to open in solidworks as a command line argument Something like this should do the trick

                 

                System.Diagnostics.Process.Start("'C:\Program files\Solidworks Corp\Solidwork\SLDWORKS.exe' 'G:\Fixtures\" & barcode & "\Details\" & barcode & " DET " & filename & ext + "'")
                
                  • Re: Open New Solidworks Instance
                    Eric Jacobson

                    Thanks so much for the help. I'm really trying to understand the basics and when things are broken down like this it all really starts to make some sense haha

                     

                    Now, for some reason I'm getting an error saying the system cannot find the file specified. When debugging in Visual Studio, I see that my 'job' and 'detail' variables are correct and the file path reads properly and the file name are also correct:

                     

                    So the bar code would read as: 1000.10

                     

                    The path/filename comes in as:

                    G:\Fixtures\1000\Details\1000 DET 10.SLDDRW

                     

                    Which should make things go I think? Any insight as to why there is not?

                     

                    Again, I really appreciate the help!

                     

                     

                     

                     

                    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click

                            'Try

                                Dim barcode As String = tb_barcode.Text

                                Dim filename As String = tb_barcode.Text

                                'Add File Extension to end of path

                                Dim ext As String = ".SLDDRW"

                                Dim original As String = tb_barcode.Text

                                Dim cut_at As String = "."

                                Dim stringSeparators() As String = {cut_at}

                                Dim split = original.Split(stringSeparators, 2, StringSplitOptions.RemoveEmptyEntries)

                                Dim job = split(0)

                                Dim detail = split(1)

                                Dim files As String = job & " DET " & detail & ext

                     

                                'File path with job/detail number variables

                                System.Diagnostics.Process.Start("'C:\Program files\Solidworks Corp\Solidwork\SLDWORKS.exe' 'G:\Fixtures\" & job & "\Details\" & files + "'") 

                           ' Catch

                               ' MessageBox.Show("File Not Found")

                           ' End Try

                        End Sub

                      • Re: Open New Solidworks Instance
                        Taus Moller

                        If you were in c# my guess would be that the  "\" is a special character. I don't know how VB deals with that though. Try to stick the command into a variable and verify it comes out like you expect. So something like this:

                         

                        Dim command = "'C:\Program files\Solidworks Corp\Solidwork\SLDWORKS.exe' 'G:\Fixtures\" & job & "\Details\" & files + "'"

                         

                        and verify that in visual studio that command is what you expect it to be.

                         

                        If it looks right. try opening up powershell and paste the command and see what it tells you

                          • Re: Open New Solidworks Instance
                            Eric Jacobson

                            So i'm up and running again after working backwards a bit.

                             

                            I have a line, that when not commented out, will open a print... just not in a new instance. I also have the line that you've mentioned, calling the SLD.EXE before my variable file path, but doesn't open anything even thought my file path is correct while debugging.

                             

                            I've tried goofing with that line a bit (changing the " " and adding a , and whatnot) and I haven't been able to get it to work.

                             

                            I feel like I'm missing something simple but it's just beyond me haha

                             

                             

                             

                            Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click

                                   Try

                                       Dim barcode As String = tb_barcode.Text

                                       Dim filename As String = tb_barcode.Text

                                       'Add File Extension to end of path

                                       Dim ext As String = ".SLDDRW"

                                       'Split job number from detail number in barcode textbox

                                       barcode = Split(tb_barcode.Text, ".")(0)

                                       filename = Split(tb_barcode.Text, ".")(1)

                                       'File path with jobnumber/detail number variables

                             

                            '- This line will open a print

                                       'System.Diagnostics.Process.Start("G:\Fixtures\" & barcode & "\Details\" & barcode & " DET " & filename & ext)

                            '- This line will NOT open a print... SLD.EXE file path is correct, variable file path is also correct...

                                       System.Diagnostics.Process.Start("'C:\Program files\Solidworks Corp\Solidwork\SLDWORKS.exe' 'G:\Fixtures\" & barcode & "\Details\" & barcode & " DET " & filename & ext + "'")

                             

                                   Catch

                                       MessageBox.Show("File Not Found")

                                   End Try

                               End Sub

                              • Re: Open New Solidworks Instance
                                Taus Moller

                                As i said, verify that the command comes out as expected and test it out in the powershell.

                                 

                                if you do:

                                Dim command = "'C:\Program files\Solidworks Corp\Solidwork\SLDWORKS.exe' 'G:\Fixtures\" & barcode & "\Details\" & barcode & " DET " & filename & ext + "'"
                                

                                 

                                What is stored in the command variable? Open up powershell and test it out. What does it tell you?

                                 

                                Also, remove your "Try" block while debugging. You are superessing the real error and returning "file not found" instead. Thus you don't even know what the error is. Removing it would probably give you a better idea about your problem.

                    • Re: Open New Solidworks Instance
                      Keith Rice

                      Opening a drawing directly can be unreliable; I have SOLIDWORKS crash a lot, for example, when I try to open a drawing by double clicking on it (and SOLIDWORKS isn't already open). I would recommend creating a new instance of SOLIDWORKS using Process.Start(), getting the pointer, and then using ISldWorks.OpenDoc6 to open the drawing. Consequently, you are headed down the right path by using Process.Start().

                       

                      If you're interested in a robust solution, let me note that we sell a .NET developer library called CADSharpTools which includes a function called RunSolidWorks. This does quite a bit:

                       

                      • Returns the ISldWorks pointer
                      • Let you specify the version you want to open (including the ability to automatically use the newest version installed on your computer)
                      • Suppress dialogs (in the event that you're opening multiple instances of same version)
                      • Specify a timeout (so the user isn't waiting forever if SWX fails to load)
                      • Require that the main window be created before proceeding with further code execution

                       

                      Even if you decide to pursue your own route I hope this at least lets you know what is possible should you try to implement it yourself. Best of luck.

                       

                      Keith

                      SOLIDWORKS API Training and Services

                      • Re: Open New Solidworks Instance
                        Artem Taturevych

                        Eric,

                         

                        The reason why it is failing for you is you have your file path with a space. In this case you need to put your argument in "". Please see the code below which takes care of it and should work for you:

                         

                        C#

                        static void Main(string[] args)
                        {
                            OpenFileInNewSwInstance(@"C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\SLDWORKS.exe",
                                @"C:\My Files\Part1.SLDPRT");
                        }
                        
                        static void OpenFileInNewSwInstance(string swAppPath, string filePath)
                        {
                            ProcessStartInfo prcStartInfo = new ProcessStartInfo(swAppPath, 
                                string.Format("\"{0}\"", filePath));
                        
                        
                            Process.Start(prcStartInfo);
                        }
                        

                        VB.NET

                        Module Module1

                         

                        Sub Main()

                            OpenFileInNewSwInstance("C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\SLDWORKS.exe", "C:\My Files\Part1.SLDPRT")

                        End Sub

                         

                        Private Sub OpenFileInNewSwInstance(ByVal swAppPath As String, ByVal filePath As String)

                            Dim prcStartInfo As ProcessStartInfo = New ProcessStartInfo(swAppPath, String.Format("""{0}""", filePath))

                            prcStartInfo.UseShellExecute = False

                            Process.Start(prcStartInfo)

                        End Sub

                         

                        End Module

                          • Re: Open New Solidworks Instance
                            Taus Moller

                            Note he is using VB though. Would it still be an issue? It seems it handles strings differently. At least from the looks of it, it seems "\" is not and escape character.

                              • Re: Open New Solidworks Instance
                                Artem Taturevych

                                Updated that with VB.NET example.

                                  • Re: Open New Solidworks Instance
                                    Eric Jacobson

                                    It works!

                                     

                                    Artem, it looks as if you were spot on with the spaces diagnosis!

                                     

                                    This is where my code stands right now. I'll modify this when I figure out how to turn off that pesky 'journal file could not be created' popup thingy.

                                     

                                    I really appreciate everyone's input and guidance here. I've learned a lot from what started as a little pet project.... who knew spaces would be my undoing?!

                                     

                                     

                                    Private Sub OpenFileInNewSwInstance(ByVal swAppPath As String, ByVal filePath As String)

                                     

                                           Dim prcStartInfo As ProcessStartInfo = New ProcessStartInfo(swAppPath, String.Format("""{0}""", filePath))

                                     

                                           prcStartInfo.UseShellExecute = False

                                     

                                           Process.Start(prcStartInfo)

                                     

                                       End Sub

                                     

                                       Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click

                                           Try

                                           Dim barcode As String = tb_barcode.Text

                                               Dim filename As String = tb_barcode.Text

                                               'Add File Extension to end of path

                                               Dim ext As String = ".SLDDRW"

                                               Dim prog As String

                                               Dim pth As String

                                               'Split job number from detail number in barcode textbox

                                               barcode = Split(tb_barcode.Text, ".")(0)

                                               filename = Split(tb_barcode.Text, ".")(1)

                                               prog = barcode & " DET " & filename & ext

                                               pth = "G:\Fixtures\" & barcode & "\Details\" & prog

                                               OpenFileInNewSwInstance("C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\SLDWORKS.exe", "G:\Fixtures\" & barcode & "\Details\" & barcode & " DET " & filename & ext)

                                         

                                           Catch

                                           MessageBox.Show("File Not Found")

                                           ' End Try

                                       End Sub