10 Replies Latest reply on Feb 12, 2015 7:03 AM by Deepak Gupta

    Normal To & Saved Views - Macro

    Nick Hayman

      I would like to form a few simple tasks into one macro.

       

      To set the situation: A part is open and a face is selected > Run the macro...

       

      The macro: With the face selected as previously mentioned. i want the part orientated uisng' Normal To' command. Then i want that view saved as 'REF'. The i want the part to close.

       

      The idea being that the user opens the part selects a face and then the macro saves a view normal to that face and the part closes. The user can then open another part and repeat.

       

      I've tried this using the record macro command but no luck.

       

      Any help appreciated!

       

      Many Thanks in advance.

       

      Nick

        • Re: Normal To & Saved Views - Macro
          Santosh Pawar

          Hi Nick,

          Try this,

           

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc
             
              swApp.RunCommand swCommands_NormalTo, ""
             
              swModel.NameView "REF"
             
              swModel.Save

            • Re: Normal To & Saved Views - Macro
              Nick Hayman

              Dim swApp As Object
              Sub main()

              Set swApp = Application.SldWorks
                  Set swModel = swApp.ActiveDoc
                 
                  swApp.RunCommand swCommands_NormalTo, ""
                 
                  swModel.NameView "REF"
                 
                  swModel.Save
                 
                  swApp.CloseDoc ""

              Set swApp = Application.SldWorks
              End Sub

               

              What do I need to add to the code for the macro to then use the open command? Doesn't need to select a new part just finish with the open window opened.

               

              Hope that makes sense?

                • Re: Normal To & Saved Views - Macro
                  Deepak Gupta

                  Add these on the top with other declarations

                  Dim Filter                      As String

                  Dim fileName                    As String

                  Dim fileConfig                  As String

                  Dim fileDispName                As String

                  Dim fileOptions                 As Long

                   

                  Add these line before the End sub

                  Filter = "SolidWorks Files (*.sldprt; *.sldasm; *.slddrw)|*.sldprt;*.sldasm;*.slddrw|Filter name (*.fil)|*.fil|All Files (*.*)|*.*|"

                  fileName = swApp.GetOpenFileName("File to Attach", "", Filter, fileOptions, fileConfig, fileDispName)

                    • Re: Normal To & Saved Views - Macro
                      Nick Hayman

                      Thanks Deepak,

                       

                      This seems to set the window file as filter. I deleted some of your code to show just SW files. See below:

                       

                      Filter = "SolidWorks Files (*.sldprt; *.sldasm; *.slddrw)|*.sldprt;*.sldasm;*.slddrw|Filter name (*.fil)|*.fil|All Files (*.*)|*.*|"

                       

                      However, now when I double click on a part I want to open from that window it doesn't seem to work.

                       

                      Not sure how to modify this so that once the macro has run I can open a part from the window opened by the macro.

                        • Re: Normal To & Saved Views - Macro
                          Santosh Pawar

                          Hi Nick,

                           

                          What Deepak has suggested should work, once you get filename you can use OpenDoc6 to open the file and proceed with it. However if you know that you have to process multiple files then why don't you just run the code in a loop on a folder which contains your part files.

                          Re: Run macro on all *.SLDLFP files in a directory, including subdirectories.

                          Here instead of *.SLDFLP use *.SLDPRT

                           

                          I hope that makes sense.

                           

                          OOPS!!! sorry, forgot you have to select the face

                            • Re: Normal To & Saved Views - Macro
                              Nick Hayman

                              A code loop would be good.

                              I also forgot this and got excited about speeding up the process! But unfortunately it needs manual face selection to work.

                               

                              Anyway... here is the code in my macro (should be the same as the above):

                               

                              Dim swApp As Object
                              Dim Filter                      As String
                              Dim fileName                    As String
                              Dim fileConfig                  As String
                              Dim fileDispName                As String
                              Dim fileOptions                 As Long

                              Sub main()

                              Set swApp = Application.SldWorks
                                  Set swModel = swApp.ActiveDoc
                                 
                                  swApp.RunCommand swCommands_NormalTo, ""
                                 
                                  swModel.NameView "REF"
                                 
                                  swModel.Save
                                 
                                  swApp.CloseDoc ""

                              Set swApp = Application.SldWorks

                              Filter = "SolidWorks Files (*.sldprt; *.sldasm; *.slddrw)|*.sldprt;*.sldasm;*.slddrw|Filter name (*.fil)|*.fil|All Files (*.*)|*.*|"

                              fileName = swApp.GetOpenFileName("File to Attach", "", Filter, fileOptions, fileConfig, fileDispName)

                              End Sub

                               

                               

                              This is what I now get once the macro has finished:

                               

                              aaa.JPG

                               

                              I want it to show all SW parts. However even when I change the above image to SW only or All files and try to open the next file it fails.

                               

                              Have I copied the code incorrectly and missed something?

                               

                              Many Thanks

                              Nick

                                • Re: Normal To & Saved Views - Macro
                                  Santosh Pawar

                                  Here's the working code for you,

                                   

                                  Dim swApp As SldWorks.SldWorks
                                  Dim Filter                      As String
                                  Dim fileName                    As String
                                  Dim fileConfig                  As String
                                  Dim fileDispName                As String
                                  Dim fileOptions                 As Long
                                  Sub main()
                                  Set swApp = Application.SldWorks
                                  Filter = "SolidWorks Files (*.sldprt; *.sldasm; *.slddrw)|*.sldprt;*.sldasm;*.slddrw"
                                  fileName = swApp.GetOpenFileName("File to Attach", "", Filter, fileOptions, fileConfig, fileDispName)

                                      Set swModel = swApp.OpenDoc6(fileName, swDocPART, 1, "", 0, 0)
                                      Stop 'Select the Face in UI
                                     
                                      swApp.RunCommand swCommands_NormalTo, ""
                                     
                                      swModel.NameView "REF"
                                     
                                      swModel.Save
                                     
                                      swApp.CloseDoc ""
                                  End Sub